diff --git a/admin/tool/uploadcourse/classes/course.php b/admin/tool/uploadcourse/classes/course.php index c2720bde365..d441123e05e 100644 --- a/admin/tool/uploadcourse/classes/course.php +++ b/admin/tool/uploadcourse/classes/course.php @@ -511,7 +511,8 @@ class tool_uploadcourse_course { } $exists = true; } - $this->status('courseshortnamegenerated', new lang_string('courseshortnamegenerated', 'tool_uploadcourse')); + $this->status('courseshortnamegenerated', new lang_string('courseshortnamegenerated', 'tool_uploadcourse', + $newshortname)); $this->shortname = $newshortname; } } diff --git a/admin/tool/uploadcourse/classes/processor.php b/admin/tool/uploadcourse/classes/processor.php index 80c1e8057d6..211e7d8e03e 100644 --- a/admin/tool/uploadcourse/classes/processor.php +++ b/admin/tool/uploadcourse/classes/processor.php @@ -181,6 +181,9 @@ class tool_uploadcourse_processor { } $this->processstarted = true; + $tracker = new tool_uploadcourse_tracker(tool_uploadcourse_tracker::OUTPUT_PLAIN); + $tracker->start(); + // Loop over the CSV lines. while ($line = $this->cir->next()) { $this->linenb++; @@ -189,11 +192,14 @@ class tool_uploadcourse_processor { $course = $this->get_course($data); if ($course->prepare()) { $course->proceed(); + $tracker->output($this->linenb, true, $course->get_statuses(), $data); } else { - $this->log_error($course->get_errors()); + $tracker->output($this->linenb, false, $course->get_errors(), $data); } } + $tracker->finish(); + $this->remove_restore_content(); } @@ -296,6 +302,8 @@ class tool_uploadcourse_processor { throw new coding_exception('Process has already been started'); } $this->processstarted = true; + $tracker = new tool_uploadcourse_tracker(tool_uploadcourse_tracker::OUTPUT_PLAIN); + $tracker->start(); // Loop over the CSV lines. $preview = array(); @@ -305,12 +313,15 @@ class tool_uploadcourse_processor { $course = $this->get_course($data); $result = $course->prepare(); if (!$result) { - $this->log_error($course->get_errors()); + $tracker->output($this->linenb, $result, $course->get_errors(), $data); + } else { + $tracker->output($this->linenb, $result, $course->get_statuses(), $data); } $row = $data; $preview[$this->linenb] = $row; } + $tracker->finish(); $this->remove_restore_content(); return $preview; diff --git a/admin/tool/uploadcourse/classes/tracker.php b/admin/tool/uploadcourse/classes/tracker.php new file mode 100644 index 00000000000..48e2eda8f3d --- /dev/null +++ b/admin/tool/uploadcourse/classes/tracker.php @@ -0,0 +1,213 @@ +. + +/** + * Output tracker. + * + * @package tool_uploadcourse + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Class output tracker. + * + * @package tool_uploadcourse + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class tool_uploadcourse_tracker { + + /** + * Constant to output nothing. + */ + const NO_OUTPUT = 0; + + /** + * Constant to output HTML. + */ + const OUTPUT_HTML = 1; + + /** + * Constant to output plain text. + */ + const OUTPUT_PLAIN = 2; + + /** + * @var array columns to display. + */ + protected $columns = array('line', 'result', 'id', 'shortname', 'fullname', 'idnumber', 'status'); + + /** + * @var int row number. + */ + protected $rownb = 0; + + /** + * @var int chosen output mode. + */ + protected $outputmode; + + /** + * @var object output buffer. + */ + protected $buffer; + + /** + * Constructor. + * + * @param int $outputmode desired output mode. + */ + public function __construct($outputmode = self::NO_OUTPUT) { + $this->outputmode = $outputmode; + if ($this->outputmode == self::OUTPUT_PLAIN) { + $this->buffer = new progress_trace_buffer(new text_progress_trace()); + } + } + + /** + * Finish the output. + * + * @return void + */ + public function finish() { + if ($this->outputmode == self::NO_OUTPUT) { + return; + } + + if ($this->outputmode == self::OUTPUT_HTML) { + echo html_writer::end_tag('table'); + } + } + + /** + * Output the results. + * + * @param int $total total courses. + * @param int $created count of courses created. + * @param int $updated count of courses updated. + * @param int $deleted count of courses deleted. + * @param int $errors count of errors. + * @return void + */ + public function results($total, $created, $updated, $deleted, $errors) { + global $OUTPUT; + + if ($this->outputmode == self::NO_OUTPUT) { + return; + } + + $message = array( + get_string('coursestotal', 'tool_uploadcourse', $total), + get_string('coursescreated', 'tool_uploadcourse', $created), + get_string('coursesupdated', 'tool_uploadcourse', $updated), + get_string('coursesdeleted', 'tool_uploadcourse', $deleted), + get_string('courseserrors', 'tool_uploadcourse', $errors) + ); + + if ($this->outputmode == self::OUTPUT_PLAIN) { + } else if ($this->outputmode == self::OUTPUT_HTML) { + echo html_writer::end_tag('table'); + } + } + + /** + * Output one more line. + * + * @param int $line line number. + * @param bool $outcome success or not? + * @param array $status array of statuses. + * @param array $data extra data to display. + * @return void + */ + public function output($line, $outcome, $status, $data) { + global $OUTPUT; + if ($this->outputmode == self::NO_OUTPUT) { + return; + } + + if ($this->outputmode == self::OUTPUT_PLAIN) { + $message = array( + $line, + $outcome ? 'OK' : 'NOK', + isset($data['id']) ? $data['id'] : '', + isset($data['shortname']) ? $data['shortname'] : '', + isset($data['fullname']) ? $data['fullname'] : '', + isset($data['idnumber']) ? $data['idnumber'] : '' + ); + $this->buffer->output(implode("\t", $message)); + if (!empty($status)) { + foreach ($status as $st) { + $this->buffer->output($st, 1); + } + } + } else if ($this->outputmode == self::OUTPUT_HTML) { + $ci = 0; + $this->rownb++; + if (is_array($status)) { + $status = implode(html_writer::empty_tag('br'), $status); + } + if ($outcome) { + $outcome = $OUTPUT->pix_icon('i/valid', ''); + } else { + $outcome = $OUTPUT->pix_icon('i/invalid', ''); + } + echo html_writer::start_tag('tr', array('class' => 'r' . $this->rownb)); + echo html_writer::tag('td', $line, array('class' => 'c' . $ci++)); + echo html_writer::tag('td', $outcome, array('class' => 'c' . $ci++)); + echo html_writer::tag('td', $status, array('class' => 'c' . $ci++)); + echo html_writer::tag('td', isset($data['id']) ? $data['id'] : '', array('class' => 'c' . $ci++)); + echo html_writer::tag('td', isset($data['shortname']) ? $data['shortname'] : '', array('class' => 'c' . $ci++)); + echo html_writer::tag('td', isset($data['fullname']) ? $data['fullname'] : '', array('class' => 'c' . $ci++)); + echo html_writer::tag('td', isset($data['idnumber']) ? $data['idnumber'] : '', array('class' => 'c' . $ci++)); + echo html_writer::end_tag('tr'); + } + } + + /** + * Start the output. + * + * @return void + */ + public function start() { + if ($this->outputmode == self::NO_OUTPUT) { + return; + } + + if ($this->outputmode == self::OUTPUT_PLAIN) { + $columns = array_flip($this->columns); + unset($columns['status']); + $columns = array_flip($columns); + $this->buffer->output(implode("\t", $columns)); + } else if ($this->outputmode == self::OUTPUT_HTML) { + $ci = 0; + echo html_writer::start_tag('table', array('class' => 'generaltable boxaligncenter flexible-wrap', + 'summary' => get_string('uploadcoursesresult', 'tool_uploadcourse'))); + echo html_writer::start_tag('tr', array('class' => 'heading r' . $this->rownb)); + echo html_writer::tag('th', get_string('csvline', 'tool_uploadcourse'), array('class' => 'c' . $ci++, 'scope' => 'col')); + echo html_writer::tag('th', get_string('outcome', 'tool_uploadcourse'), array('class' => 'c' . $ci++, 'scope' => 'col')); + echo html_writer::tag('th', get_string('status'), array('class' => 'c' . $ci++, 'scope' => 'col')); + echo html_writer::tag('th', get_string('id', 'tool_uploadcourse'), array('class' => 'c' . $ci++, 'scope' => 'col')); + echo html_writer::tag('th', get_string('shortname'), array('class' => 'c' . $ci++, 'scope' => 'col')); + echo html_writer::tag('th', get_string('fullname'), array('class' => 'c' . $ci++, 'scope' => 'col')); + echo html_writer::tag('th', get_string('idnumber'), array('class' => 'c' . $ci++, 'scope' => 'col')); + echo html_writer::end_tag('tr'); + } + } + +} diff --git a/admin/tool/uploadcourse/lang/en/tool_uploadcourse.php b/admin/tool/uploadcourse/lang/en/tool_uploadcourse.php index 058df85cb47..9396aa06f22 100644 --- a/admin/tool/uploadcourse/lang/en/tool_uploadcourse.php +++ b/admin/tool/uploadcourse/lang/en/tool_uploadcourse.php @@ -48,19 +48,23 @@ $string['coursereset'] = 'Course reset'; $string['courseresetnotallowed'] = 'Course reset now allowed'; $string['courserestored'] = 'Course restored'; $string['courseshortnameincremented'] = 'Course shortname incremented {$a->from} -> {$a->to}'; -$string['courseshortnamegenerated'] = 'Course shortname generated'; +$string['courseshortnamegenerated'] = 'Course shortname generated: {$a}'; $string['coursetorestorefromdoesnotexist'] = 'The course to restore from does not exist'; $string['courseupdated'] = 'Course updated'; +$string['csvline'] = 'Line'; $string['errorwhilerestoringcourse'] = 'Error while restoring the course'; $string['errorwhiledeletingcourse'] = 'Error while deleting the course'; $string['generatedshortnameinvalid'] = 'The generated shortname is invalid'; $string['generatedshortnamealreadyinuse'] = 'The generated shortname is already in use'; +$string['id'] = 'ID'; $string['invalidbackupfile'] = 'Invalid backup file'; $string['invalidcourseformat'] = 'Invalid course format'; $string['invalidroles'] = 'Invalid role names: {$a}'; $string['missingmandatoryfields'] = 'Missing value for mandatory fields: {$a}'; $string['missingshortnamenotemplate'] = 'Missing shortname and shortname template not set'; +$string['outcome'] = 'Outcome'; $string['updatemodedoessettonothing'] = 'Update mode does not allow anything to be updated'; +$string['uploadcoursesresult'] = 'Upload courses results'; $string['unknownimportmode'] = 'Unknown import mode'; $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 (don\t use comma-quoted as Moodle does not support it): {$a}'; @@ -98,7 +102,6 @@ $string['uploadcourses_help'] = 'Courses may be uploaded (and optionally enrolle * The first record contains a list of fieldnames defining the format of the rest of the file * Required fieldnames are coursename, password, firstname, lastname, email'; $string['uploadcoursespreview'] = 'Upload courses preview'; -$string['uploadcoursesresult'] = 'Upload courses results'; $string['courseupdated'] = 'Course updated'; $string['courseuptodate'] = 'Course up-to-date'; $string['coursedeleted'] = 'Course deleted'; @@ -129,7 +132,6 @@ $string['ccbulk'] = 'Select for bulk operations'; $string['ccbulkall'] = 'All courses'; $string['ccbulknew'] = 'New courses'; $string['ccbulkupdated'] = 'Updated courses'; -$string['cccsvline'] = 'CSV line'; $string['cclegacy1role'] = '(Original Student) typeN=1'; $string['cclegacy2role'] = '(Original Teacher) typeN=2'; $string['cclegacy3role'] = '(Original Non-editing teacher) typeN=3';