From 5dcd7e28677602597fcd4d2808550a78fe5edb7d Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Mon, 1 Jul 2013 13:55:22 +0800 Subject: [PATCH] MDL-13114 tool_uploadcourse: Output tracker in place --- admin/tool/uploadcourse/classes/processor.php | 36 ++++++++++++++++--- admin/tool/uploadcourse/classes/tracker.php | 9 ++++- .../lang/en/tool_uploadcourse.php | 11 +++--- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/admin/tool/uploadcourse/classes/processor.php b/admin/tool/uploadcourse/classes/processor.php index 211e7d8e03e..e29f6228eff 100644 --- a/admin/tool/uploadcourse/classes/processor.php +++ b/admin/tool/uploadcourse/classes/processor.php @@ -173,32 +173,54 @@ class tool_uploadcourse_processor { /** * Execute the process. * + * @param object $tracker the output tracker to use. * @return void */ - public function execute() { + public function execute($tracker = null) { if ($this->processstarted) { throw new coding_exception('Process has already been started'); } $this->processstarted = true; - $tracker = new tool_uploadcourse_tracker(tool_uploadcourse_tracker::OUTPUT_PLAIN); + if (empty($tracker)) { + $tracker = new tool_uploadcourse_tracker(tool_uploadcourse_tracker::NO_OUTPUT); + } $tracker->start(); + $total = 0; + $created = 0; + $updated = 0; + $deleted = 0; + $errors = 0; + // Loop over the CSV lines. while ($line = $this->cir->next()) { $this->linenb++; + $total++; $data = $this->parse_line($line); $course = $this->get_course($data); if ($course->prepare()) { $course->proceed(); - $tracker->output($this->linenb, true, $course->get_statuses(), $data); + + $status = $course->get_statuses(); + if (array_key_exists('coursecreated', $status)) { + $created++; + } else if (array_key_exists('courseupdated', $status)) { + $updated++; + } else if (array_key_exists('coursedeleted', $status)) { + $deleted++; + } + + $tracker->output($this->linenb, true, $status, $data); } else { + $errors++; $tracker->output($this->linenb, false, $course->get_errors(), $data); } } $tracker->finish(); + $tracker->results($total, $created, $updated, $deleted, $errors); $this->remove_restore_content(); } @@ -295,14 +317,18 @@ class tool_uploadcourse_processor { * This only returns passed data, along with the errors. * * @param integer $rows number of rows to preview. + * @param object $tracker the output tracker to use. * @return array of preview data. */ - public function preview($rows = 10) { + public function preview($rows = 10, $tracker = null) { if ($this->processstarted) { throw new coding_exception('Process has already been started'); } $this->processstarted = true; - $tracker = new tool_uploadcourse_tracker(tool_uploadcourse_tracker::OUTPUT_PLAIN); + + if (empty($tracker)) { + $tracker = new tool_uploadcourse_tracker(tool_uploadcourse_tracker::NO_OUTPUT); + } $tracker->start(); // Loop over the CSV lines. diff --git a/admin/tool/uploadcourse/classes/tracker.php b/admin/tool/uploadcourse/classes/tracker.php index 48e2eda8f3d..2dc6be593a3 100644 --- a/admin/tool/uploadcourse/classes/tracker.php +++ b/admin/tool/uploadcourse/classes/tracker.php @@ -121,8 +121,15 @@ class tool_uploadcourse_tracker { ); if ($this->outputmode == self::OUTPUT_PLAIN) { + foreach ($message as $msg) { + $this->buffer->output($msg); + } } else if ($this->outputmode == self::OUTPUT_HTML) { - echo html_writer::end_tag('table'); + $buffer = new progress_trace_buffer(new html_list_progress_trace()); + foreach ($message as $msg) { + $buffer->output($msg); + } + $buffer->finished(); } } diff --git a/admin/tool/uploadcourse/lang/en/tool_uploadcourse.php b/admin/tool/uploadcourse/lang/en/tool_uploadcourse.php index 9396aa06f22..27326d59f68 100644 --- a/admin/tool/uploadcourse/lang/en/tool_uploadcourse.php +++ b/admin/tool/uploadcourse/lang/en/tool_uploadcourse.php @@ -47,6 +47,11 @@ $string['courserenamingnotallowed'] = 'Course renaming is not allowed'; $string['coursereset'] = 'Course reset'; $string['courseresetnotallowed'] = 'Course reset now allowed'; $string['courserestored'] = 'Course restored'; +$string['coursestotal'] = 'Courses total: {$a}'; +$string['coursescreated'] = 'Courses created: {$a}'; +$string['coursesupdated'] = 'Courses updated: {$a}'; +$string['coursesdeleted'] = 'Courses deleted: {$a}'; +$string['courseserrors'] = 'Courses errors: {$a}'; $string['courseshortnameincremented'] = 'Course shortname incremented {$a->from} -> {$a->to}'; $string['courseshortnamegenerated'] = 'Course shortname generated: {$a}'; $string['coursetorestorefromdoesnotexist'] = 'The course to restore from does not exist'; @@ -57,6 +62,7 @@ $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['idnumberalreadyinuse'] = 'ID number already used by a course'; $string['invalidbackupfile'] = 'Invalid backup file'; $string['invalidcourseformat'] = 'Invalid course format'; $string['invalidroles'] = 'Invalid role names: {$a}'; @@ -102,15 +108,12 @@ $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['courseupdated'] = 'Course updated'; $string['courseuptodate'] = 'Course up-to-date'; +$string['courseupdated'] = 'Course updated'; $string['coursedeleted'] = 'Course deleted'; $string['courserenamed'] = 'Course renamed'; -$string['coursescreated'] = 'Courses created'; -$string['coursesdeleted'] = 'Courses deleted'; $string['coursesrenamed'] = 'Courses renamed'; $string['coursesskipped'] = 'Courses skipped'; -$string['coursesupdated'] = 'Courses updated'; $string['coursenotadded'] = 'Course not added - already exists'; $string['coursenotaddederror'] = 'Course not added - error'; $string['coursenotdeletederror'] = 'Course not deleted - error';