Skip to content

Commit

Permalink
MDL-26401 group: change import to use csv_import_reader class
Browse files Browse the repository at this point in the history
  • Loading branch information
lameze committed Aug 12, 2020
1 parent 4cdcf3a commit d4d744d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
56 changes: 30 additions & 26 deletions group/import.php
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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 &#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 {
Expand Down Expand Up @@ -232,6 +235,7 @@
}
}

$csvimport->close();
echo $OUTPUT->single_button($returnurl, get_string('continue'), 'get');
echo $OUTPUT->footer();
die;
Expand All @@ -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();
2 changes: 2 additions & 0 deletions lang/en/error.php
Expand Up @@ -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'] = '<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>
Expand Down

0 comments on commit d4d744d

Please sign in to comment.