MDL-13114 tool_uploadcourse: Introducing output tracker

This commit is contained in:
Frederic Massart
2013-07-15 10:02:54 +08:00
parent 8383903fcc
commit 5b2f7718f0
4 changed files with 233 additions and 6 deletions
+2 -1
View File
@@ -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;
}
}
+13 -2
View File
@@ -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;
+213
View File
@@ -0,0 +1,213 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* 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');
}
}
}
@@ -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';