mirror of
https://github.com/moodle/moodle.git
synced 2025-01-19 06:18:28 +01:00
43 lines
909 B
PHP
43 lines
909 B
PHP
|
<?php //$Id$
|
||
|
|
||
|
function get_records_csv($file, $tablename) {
|
||
|
global $CFG, $db;
|
||
|
|
||
|
if (!$metacolumns = $db->MetaColumns($CFG->prefix . $table)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if(!($handle = fopen($file, 'r'))) {
|
||
|
error('get_records_csv failed to open '.$file);
|
||
|
}
|
||
|
|
||
|
$fieldnames = fgetcsv($handle, 4096);
|
||
|
if(empty($fieldnames)) {
|
||
|
fclose($handle);
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
$columns = array();
|
||
|
|
||
|
foreach($metacolumns as $metacolumn) {
|
||
|
$ord = array_search($metacolumn->name, $fieldnames);
|
||
|
if(is_int($ord)) {
|
||
|
$columns[$metacolumn->name] = $ord;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$rows = array();
|
||
|
|
||
|
while (($data = fgetcsv($handle, 4096)) !== false) {
|
||
|
$item = new stdClass;
|
||
|
foreach($columns as $name => $ord) {
|
||
|
$item->$name = $data[$ord];
|
||
|
}
|
||
|
$rows[] = $item;
|
||
|
}
|
||
|
|
||
|
fclose($handle);
|
||
|
return $rows;
|
||
|
}
|
||
|
|
||
|
?>
|