MDL-26401 group: change import to use csv_import_reader class

This commit is contained in:
Simey Lameze
2020-08-12 16:35:56 +08:00
parent b21f182106
commit f307dc23d7
2 changed files with 34 additions and 28 deletions
+32 -28
View File
@@ -49,30 +49,37 @@ $PAGE->set_pagelayout('admin');
$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;
if (isset($CFG->CSV_ENCODE)) {
$csv_encode = '/\&\#' . $CFG->CSV_ENCODE . '/';
}
} else {
$csv_delimiter = ",";
}
$text = $mform_post->get_file_content('userfile');
$text = preg_replace('!\r\n?!',"\n",$text);
$text = $importform->get_file_content('userfile');
$text = preg_replace('!\r\n?!', "\n", $text);
$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);
}
$csvimport->init();
unset($text);
// make arrays of valid fields for error checking
@@ -88,12 +95,12 @@ if ($mform_post->is_cancelled()) {
);
// --- 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 @@ if ($mform_post->is_cancelled()) {
// 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 &#44 (for comma separated csv files)
//Note: semicolon within a field should be encoded as &#59 (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 @@ if ($mform_post->is_cancelled()) {
}
}
$csvimport->close();
echo $OUTPUT->single_button($returnurl, get_string('continue'), 'get');
echo $OUTPUT->footer();
die;
@@ -240,5 +244,5 @@ if ($mform_post->is_cancelled()) {
/// Print the form
echo $OUTPUT->header();
echo $OUTPUT->heading_with_help($strimportgroups, 'importgroups', 'core_group');
$mform_post ->display();
$importform->display();
echo $OUTPUT->footer();
+2
View File
@@ -195,10 +195,12 @@ $string['coursemisconf'] = 'Course is misconfigured';
$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'] = '<b>Invalid CSV file:</b> First line must include "Header Fields" and the file must be type of <br />"Expanded Fields/Comma Separated"<br />or<br /> "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'] = '<p>Error: Database connection failed</p>
<p>It is possible that the database is overloaded or otherwise not running properly.</p>