From d4d744d5448b1afa8d600d7f155981939b0a98da Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Thu, 2 Apr 2020 10:27:04 +0800 Subject: [PATCH] MDL-26401 group: change import to use csv_import_reader class --- group/import.php | 56 +++++++++++++++++++++++++---------------------- lang/en/error.php | 2 ++ 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/group/import.php b/group/import.php index 2f563a21c3a60..58f405bd0cd0a 100644 --- a/group/import.php +++ b/group/import.php @@ -49,30 +49,37 @@ $returnurl = new moodle_url('/group/index.php', array('id'=>$id)); -$mform_post = new groups_import_form(null, array('id'=>$id)); +$importform = new groups_import_form(null, ['id' => $id]); // If a file has been uploaded, then process it -if ($mform_post->is_cancelled()) { +if ($importform->is_cancelled()) { redirect($returnurl); -} else if ($mform_post->get_data()) { +} else if ($formdata = $importform->get_data()) { echo $OUTPUT->header(); - $csv_encode = '/\&\#44/'; - if (isset($CFG->CSV_DELIMITER)) { - $csv_delimiter = $CFG->CSV_DELIMITER; + $text = $importform->get_file_content('userfile'); + $text = preg_replace('!\r\n?!', "\n", $text); - if (isset($CFG->CSV_ENCODE)) { - $csv_encode = '/\&\#' . $CFG->CSV_ENCODE . '/'; - } - } else { - $csv_delimiter = ","; + $rawlines = explode("\n", $text); + + require_once($CFG->libdir . '/csvlib.class.php'); + $importid = csv_import_reader::get_new_iid('groupimport'); + $csvimport = new csv_import_reader($importid, 'groupimport'); + $delimiter = $formdata->delimiter_name; + $encoding = $formdata->encoding; + $readcount = $csvimport->load_csv_content($text, $encoding, $delimiter); + + if ($readcount === false) { + print_error('csvfileerror', 'error', $PAGE->url, $csvimport->get_error()); + } else if ($readcount == 0) { + print_error('csvemptyfile', 'error', $PAGE->url, $csvimport->get_error()); + } else if ($readcount == 1) { + print_error('csvnodata', 'error', $PAGE->url); } - $text = $mform_post->get_file_content('userfile'); - $text = preg_replace('!\r\n?!',"\n",$text); + $csvimport->init(); - $rawlines = explode("\n", $text); unset($text); // make arrays of valid fields for error checking @@ -88,12 +95,12 @@ ); // --- get header (field names) --- - $header = explode($csv_delimiter, array_shift($rawlines)); + $header = explode($csvimport::get_delimiter($delimiter), array_shift($rawlines)); // check for valid field names foreach ($header as $i => $h) { $h = trim($h); $header[$i] = $h; // remove whitespace if (!(isset($required[$h]) or isset($optionalDefaults[$h]) or isset($optional[$h]))) { - print_error('invalidfieldname', 'error', 'import.php?id='.$id, $h); + print_error('invalidfieldname', 'error', $PAGE->url, $h); } if (isset($required[$h])) { $required[$h] = 2; @@ -102,32 +109,28 @@ // check for required fields foreach ($required as $key => $value) { if ($value < 2) { - print_error('fieldrequired', 'error', 'import.php?id='.$id, $key); + print_error('fieldrequired', 'error', $PAGE->url, $key); } } $linenum = 2; // since header is line 1 - foreach ($rawlines as $rawline) { + while ($line = $csvimport->next()) { $newgroup = new stdClass();//to make Martin happy foreach ($optionalDefaults as $key => $value) { $newgroup->$key = current_language(); //defaults to current language } - //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 = explode($csv_delimiter, $rawline); foreach ($line as $key => $value) { - //decode encoded commas - $record[$header[$key]] = preg_replace($csv_encode, $csv_delimiter, trim($value)); + $record[$header[$key]] = trim($value); } - if (trim($rawline) !== '') { + if (trim(implode($line)) !== '') { // add a new group to the database // add fields to object $user foreach ($record as $name => $value) { // check for required values if (isset($required[$name]) and !$value) { - print_error('missingfield', 'error', 'import.php?id='.$id, $name); + print_error('missingfield', 'error', $PAGE->url, $name); } else if ($name == "groupname") { $newgroup->name = $value; } else { @@ -232,6 +235,7 @@ } } + $csvimport->close(); echo $OUTPUT->single_button($returnurl, get_string('continue'), 'get'); echo $OUTPUT->footer(); die; @@ -240,5 +244,5 @@ /// Print the form echo $OUTPUT->header(); echo $OUTPUT->heading_with_help($strimportgroups, 'importgroups', 'core_group'); -$mform_post ->display(); +$importform->display(); echo $OUTPUT->footer(); diff --git a/lang/en/error.php b/lang/en/error.php index df935094c9944..882d717ff0f5c 100644 --- a/lang/en/error.php +++ b/lang/en/error.php @@ -194,10 +194,12 @@ $string['courserequestdisabled'] = 'Sorry, but course requests have been disabled by the administrator.'; $string['csvcolumnduplicates'] = 'Duplicate columns detected'; $string['csvemptyfile'] = 'The CSV file is empty'; +$string['csvfileerror'] = 'There is something wrong with the format of the CSV file. Please check the number of headings and columns match, and that the delimiter and file encoding are correct: {$a}'; $string['csvfewcolumns'] = 'Not enough columns, please verify the delimiter setting'; $string['csvinvalidcols'] = 'Invalid CSV file: First line must include "Header Fields" and the file must be type of
"Expanded Fields/Comma Separated"
or
"Expanded Fields with CAVV Result Code/Comma Separated"'; $string['csvinvalidcolsnum'] = 'Invalid CSV file - each line must include 49 or 70 fields'; $string['csvloaderror'] = 'An error occurred while loading the CSV file: {$a}'; +$string['csvnodata'] = 'Invalid CSV file - The CSV file has headers but does not contain any data.'; $string['csvweirdcolumns'] = 'Invalid CSV file format - number of columns is not constant!'; $string['dbconnectionfailed'] = '

Error: Database connection failed

It is possible that the database is overloaded or otherwise not running properly.