Merge branch 'MDL-26401-master' of git://github.com/lameze/moodle

This commit is contained in:
Andrew Nicols 2020-08-19 08:50:37 +08:00
commit d8d05f04d9
4 changed files with 50 additions and 28 deletions

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();

View File

@ -27,6 +27,7 @@ if (!defined('MOODLE_INTERNAL')) {
}
require_once($CFG->libdir.'/formslib.php');
require_once($CFG->libdir . '/csvlib.class.php');
/**
* Groups import form class
@ -56,6 +57,19 @@ class groups_import_form extends moodleform {
$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);

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>

View File

@ -43,6 +43,7 @@ $string['creategrouping'] = 'Create grouping';
$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['editgroupsettings'] = 'Edit group settings';
$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.