mirror of
https://github.com/moodle/moodle.git
synced 2025-01-19 06:18:28 +01:00
Allow use of alternate delimiters in csv files
This commit is contained in:
parent
eb3daafe3f
commit
811cf8919d
@ -30,6 +30,17 @@
|
||||
$struploadusers = get_string("uploadusers");
|
||||
$straddnewuser = get_string("importuser");
|
||||
|
||||
$csv_encode = '/\&\#44/';
|
||||
if (isset($CFG->CSV_DELIMITER)) {
|
||||
$csv_delimiter = "\" . $CFG->CSV_DELIMITER;
|
||||
$csv_delimiter2 = $CFG->CSV_DELIMITER;
|
||||
if (isset($CFG->CSV_ENCODE)) {
|
||||
$csv_encode = '/\&\#' . $CFG->CSV_ENCODE . '/';
|
||||
}
|
||||
} else {
|
||||
$csv_delimiter = "\,";
|
||||
$csv_delimiter2 = ",";
|
||||
}
|
||||
|
||||
/// Print the header
|
||||
|
||||
@ -88,7 +99,7 @@
|
||||
"group5" =>1);
|
||||
|
||||
// --- get header (field names) ---
|
||||
$header = split("\,", fgets($fp,1024));
|
||||
$header = split($csv_delimiter, fgets($fp,1024));
|
||||
// check for valid field names
|
||||
foreach ($header as $i => $h) {
|
||||
$h = trim($h); $header[$i] = $h; // remove whitespace
|
||||
@ -111,11 +122,12 @@
|
||||
foreach ($optionalDefaults as $key => $value) {
|
||||
$user->$key = addslashes($adminuser->$key);
|
||||
}
|
||||
//Note: commas within a field should be encoded as ,
|
||||
$line = split("\,", fgets($fp,1024));
|
||||
//Note: commas within a field should be encoded as , (for comma separated csv files)
|
||||
//Note: semicolon within a field should be encoded as ; (for semicolon separated csv files)
|
||||
$line = split($csv_delimiter, fgets($fp,1024));
|
||||
foreach ($line as $key => $value) {
|
||||
//decode encoded commas
|
||||
$record[$header[$key]] = preg_replace('/\&\#44/',',',trim($value));
|
||||
$record[$header[$key]] = preg_replace($csv_encode,$csv_delimiter2,trim($value));
|
||||
}
|
||||
if ($record[$header[0]]) {
|
||||
// add a new user to the database
|
||||
|
Loading…
x
Reference in New Issue
Block a user