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/group/import_form.php b/group/import_form.php index e877b5f7bbf08..ce13446d3ecb5 100644 --- a/group/import_form.php +++ b/group/import_form.php @@ -27,6 +27,7 @@ } require_once($CFG->libdir.'/formslib.php'); +require_once($CFG->libdir . '/csvlib.class.php'); /** * Groups import form class @@ -56,6 +57,19 @@ function definition() { $mform->addElement('hidden', 'id'); $mform->setType('id', PARAM_INT); + $choices = csv_import_reader::get_delimiter_list(); + $mform->addElement('select', 'delimiter_name', get_string('csvdelimiter', 'group'), $choices); + if (array_key_exists('cfg', $choices)) { + $mform->setDefault('delimiter_name', 'cfg'); + } else if (get_string('listsep', 'langconfig') == ';') { + $mform->setDefault('delimiter_name', 'semicolon'); + } else { + $mform->setDefault('delimiter_name', 'comma'); + } + + $choices = core_text::get_encodings(); + $mform->addElement('select', 'encoding', get_string('encoding', 'group'), $choices); + $mform->setDefault('encoding', 'UTF-8'); $this->add_action_buttons(true, get_string('importgroups', 'core_group')); $this->set_data($data); diff --git a/lang/en/error.php b/lang/en/error.php index afa81bacb6e24..6a728796b47cf 100644 --- a/lang/en/error.php +++ b/lang/en/error.php @@ -195,10 +195,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.

diff --git a/lang/en/group.php b/lang/en/group.php index 9f32f85f89049..ab1ce3eb3dd4c 100644 --- a/lang/en/group.php +++ b/lang/en/group.php @@ -43,6 +43,7 @@ $string['creategroupinselectedgrouping'] = 'Create group in grouping'; $string['createingrouping'] = 'Grouping of auto-created groups'; $string['createorphangroup'] = 'Create orphan group'; +$string['csvdelimiter'] = 'CSV delimiter'; $string['databaseupgradegroups'] = 'Groups version is now {$a}'; $string['defaultgrouping'] = 'Default grouping'; $string['defaultgroupingname'] = 'Grouping'; @@ -59,6 +60,7 @@ $string['editusersgroupsa'] = 'Edit groups for "{$a}"'; $string['enablemessaging'] = 'Group messaging'; $string['enablemessaging_help'] = 'If enabled, group members can send messages to the others in their group via the messaging drawer.'; +$string['encoding'] = 'Encoding'; $string['enrolmentkey'] = 'Enrolment key'; $string['enrolmentkey_help'] = 'An enrolment key enables access to the course to be restricted to only those who know the key. If a group enrolment key is specified, then not only will entering that key let the user into the course, but it will also automatically make them a member of this group.