diff --git a/classes/actions.php b/classes/actions.php index 92e94bd..9c751ce 100644 --- a/classes/actions.php +++ b/classes/actions.php @@ -153,7 +153,7 @@ public static function duplicate(array $modules, $sectionnumber = false): void { try { $duplicatedmod = duplicate_module($modinfo->get_course(), $modinfo->get_cm($cmid)); } catch (\Exception $e) { - $errors[] = 'cmid:' . $cmid . '(' . $e->getMessage() . ')'; + $errors[$cmid] = 'cmid:' . $cmid . '(' . $e->getMessage() . ')'; continue; } $cms[$cmid] = $duplicatedmod->id; @@ -175,14 +175,26 @@ public static function duplicate(array $modules, $sectionnumber = false): void { // Move each module to the end of their section. moveto_module($duplicatedmod, $section); } - $event = \block_massaction\event\massaction_duplicated::create([ + $event = \block_massaction\event\course_modules_duplicated::create([ 'context' => \context_course::instance($courseid), 'other' => [ 'cms' => $cms, - 'errors' => $errors, + 'failed' => array_keys($errors), ], ]); $event->trigger(); + if ($errors) { + foreach ($errors as $cmid => $error) { + $event = \block_massaction\event\course_modules_duplicated_failed::create([ + 'context' => \context_course::instance($courseid), + 'other' => [ + 'cmid' => $cmid, + 'error' => $error, + ], + ]); + $event->trigger(); + } + } } /** @@ -278,8 +290,16 @@ public static function duplicate_to_course(array $modules, int $targetcourseid, // Let order of mods in a section be mod1, mod2, mod3, mod4, mod5. If we duplicate mod2, mod4, the order afterwards will be // mod1, mod2, mod3, mod4, mod5, mod2(dup), mod4(dup). $duplicatedmods = []; + $cms = []; + $errors = []; foreach ($idsincourseorder as $cmid) { - $duplicatedmod = massactionutils::duplicate_cm_to_course($targetmodinfo->get_course(), $sourcemodinfo->get_cm($cmid)); + try { + $duplicatedmod = massactionutils::duplicate_cm_to_course($targetmodinfo->get_course(), $sourcemodinfo->get_cm($cmid)); + } catch (\Exception $e) { + $errors[$cmid] = 'cmid:' . $cmid . '(' . $e->getMessage() . ')'; + continue; + } + $cms[$cmid] = $duplicatedmod; $duplicatedmods[] = $duplicatedmod; } @@ -292,6 +312,26 @@ public static function duplicate_to_course(array $modules, int $targetcourseid, moveto_module($targetmodinfo->get_cm($modid), $targetsection); } } + $event = \block_massaction\event\course_modules_duplicated::create([ + 'context' => \context_course::instance($targetcourseid), + 'other' => [ + 'cms' => $cms, + 'failed' => array_keys($errors), + ], + ]); + $event->trigger(); + if ($errors) { + foreach ($errors as $cmid => $error) { + $event = \block_massaction\event\course_modules_duplicated_failed::create([ + 'context' => \context_course::instance($targetcourseid), + 'other' => [ + 'cmid' => $cmid, + 'error' => $error, + ], + ]); + $event->trigger(); + } + } } /** diff --git a/classes/event/massaction_duplicated.php b/classes/event/course_modules_duplicated.php similarity index 68% rename from classes/event/massaction_duplicated.php rename to classes/event/course_modules_duplicated.php index f81d4ad..4f33bbb 100644 --- a/classes/event/massaction_duplicated.php +++ b/classes/event/course_modules_duplicated.php @@ -19,7 +19,7 @@ use core\event\base; /** - * The massaction_duplicated event class. + * The course_modules_duplicated event class. * * @package block_massaction * @category event @@ -27,7 +27,7 @@ * @copyright 2023 Catalyst IT * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class massaction_duplicated extends base { +class course_modules_duplicated extends base { /** * Initialise required event data properties. @@ -43,17 +43,27 @@ protected function init() { * @return string */ public static function get_name(): string { - return get_string('event:massaction_duplicated', 'block_massaction'); + return get_string('event:course_modules_duplicated', 'block_massaction'); } + /** + * Return localised event description. + * + * @return string + */ public function get_description(): string { $cms = []; + $failed = []; foreach ($this->other['cms'] as $srccm => $dstcm) { $cms[] = 'cmid from \'' . $srccm . '\' to \'' . $dstcm . '\''; } - $errormsg = !empty($this->other['errors']) ? " with error '" . implode("','", $this->other['errors']) : "'"; - return "Mass action duplicate has been completed. " . implode(", ", $cms) - . $errormsg; + foreach ($this->other['failed'] as $cmid) { + $failed[] = 'cmid \'' . $cmid . '\''; + } + return 'Course modules duplicate has been completed. ' + . 'Summary: ' . count($cms) . ' Completed, ' . count($failed) . ' Failed.' + . ($cms ? ' Completed ' . implode(", ", $cms) . '.' : '') + . ($failed ? ' Failed ' . implode(", ", $failed) . '.' : ''); } /** @@ -67,5 +77,8 @@ protected function validate_data() { if (!isset($this->other['cms']) || !is_array($this->other['cms'])) { throw new \coding_exception('The \'cms\' value must be array and set in other.'); } + if (!isset($this->other['failed']) || !is_array($this->other['failed'])) { + throw new \coding_exception('The \'failed\' value must be array and set in other.'); + } } } diff --git a/classes/event/course_modules_duplicated_failed.php b/classes/event/course_modules_duplicated_failed.php new file mode 100644 index 0000000..691f5bf --- /dev/null +++ b/classes/event/course_modules_duplicated_failed.php @@ -0,0 +1,75 @@ +. + +namespace block_massaction\event; + +use core\event\base; + +/** + * The course_modules_duplicated_failed event class. + * + * @package block_massaction + * @category event + * @author Tomo Tsuyuki + * @copyright 2023 Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class course_modules_duplicated_failed extends base { + + /** + * Initialise required event data properties. + */ + protected function init() { + $this->data['crud'] = 'c'; + $this->data['edulevel'] = self::LEVEL_OTHER; + } + + /** + * Return localised event name. + * + * @return string + */ + public static function get_name(): string { + return get_string('event:course_modules_duplicated_failed', 'block_massaction'); + } + + /** + * Return localised event description. + * + * @return string + */ + public function get_description(): string { + return 'Course modules duplicate failed. ' + . 'cmid: ' . $this->other['cmid'] + . 'error:' . $this->other['error']; + } + + /** + * Validates the custom data. + * + * @throws \coding_exception if missing required data. + */ + protected function validate_data() { + parent::validate_data(); + + if (!isset($this->other['cmid'])) { + throw new \coding_exception('The \'cmid\' value must be set in other.'); + } + if (!isset($this->other['error'])) { + throw new \coding_exception('The \'error\' value must be set in other.'); + } + } +} diff --git a/lang/en/block_massaction.php b/lang/en/block_massaction.php index f5766c3..10a799c 100644 --- a/lang/en/block_massaction.php +++ b/lang/en/block_massaction.php @@ -65,7 +65,8 @@ $string['deletecheckconfirm'] = 'Are you sure you want to delete the following module(s)?'; $string['duplicatemaxactivities'] = 'Maximum amount of course modules to duplicate'; $string['duplicatemaxactivities_description'] = 'Maximum amount of course modules which can be duplicated at the same time without running the process as background task. If set to "0" all duplication operations will be run as background task.'; -$string['event:massaction_duplicated'] = 'Mass action duplicated'; +$string['event:course_modules_duplicated'] = 'Course modules duplicated'; +$string['event:course_modules_duplicated_failed'] = 'Course modules duplicated failed'; $string['invalidaction'] = 'Unknown action: {$a}'; $string['invalidmoduleid'] = 'Invalid module ID: {$a}'; $string['invalidcoursemodule'] = 'Invalid course module'; diff --git a/tests/massaction_test.php b/tests/massaction_test.php index 42d3002..2c6dce9 100644 --- a/tests/massaction_test.php +++ b/tests/massaction_test.php @@ -504,11 +504,18 @@ public function test_mass_duplicate_modules(): void { */ public function test_mass_duplicate_modules_failing_in_the_middle(): void { global $DB; - // Delete one module record to fail to duplicate for the module. + // Delete 3 module records to fail to duplicate for the module. $modinfo = get_fast_modinfo($this->course->id); $assigncms = $modinfo->get_instances_of('assign'); - $assigncm = reset($assigncms); - $DB->execute('DELETE FROM {assign} WHERE id = ' . $assigncm->instance); + $numberoferror = 3; + $counter = 0; + foreach ($assigncms as $assigncm) { + $DB->execute('DELETE FROM {assign} WHERE id = ' . $assigncm->instance); + $counter++; + if ($numberoferror == $counter) { + break; + } + } $coursemodules = $this->get_test_course_modules(); // Prepare redirect Events. @@ -523,28 +530,29 @@ public function test_mass_duplicate_modules_failing_in_the_middle(): void { $sink->close(); $created = 0; $duplicated = 0; + $failed = 0; foreach ($events as $event) { $class = get_class($event); switch ($class) { case \core\event\course_module_created::class: $created++; break; - case \block_massaction\event\massaction_duplicated::class: + case \block_massaction\event\course_modules_duplicated::class: $duplicated++; - $eventduplicated = $event; + break; + case \block_massaction\event\course_modules_duplicated_failed::class: + $failed++; break; } } // Modules are duplicated except one deleted module. - $this->assertEquals(count($coursemodules) * 2 - 1, count($newcoursemodules)); - $this->assertEquals(count($coursemodules) - 1, $created); + $this->assertEquals(count($coursemodules) * 2 - $numberoferror, count($newcoursemodules)); + $this->assertEquals(count($coursemodules) - $numberoferror, $created); + // 1 duplicate event (Summary). $this->assertEquals(1, $duplicated); - $data = $eventduplicated->get_data(); - $errors = $data['other']['errors']; - $this->assertCount(1, $errors); - $error = reset($errors); - $this->assertStringStartsWith('cmid:' . $assigncm->id, $error); + // 3 failed events. + $this->assertEquals($numberoferror, $failed); } /** @@ -561,6 +569,7 @@ public function test_mass_duplicate_modules_failing_in_the_middle(): void { * @throws restore_controller_exception */ public function test_mass_duplicate_modules_to_course(): void { + global $DB; $sourcecourseid = $this->course->id; $sourcecoursemodinfo = get_fast_modinfo($sourcecourseid); // The teacher in the source course should have the necessary capability to backup modules. @@ -636,6 +645,31 @@ public function test_mass_duplicate_modules_to_course(): void { $this->assertEquals($targetcoursemodinfo->get_cm($targetcoursemodinfo->get_sections()[4][$i])->name, $sourcecoursemodinfo->get_cm($selectedmoduleids[$i])->name); } + + // Test if some of the activities are broken, but still complete the job. + $targetcourseid = $this->setup_target_course_for_duplicating(); + $assigncms = $sourcecoursemodinfo->get_instances_of('assign'); + $numberoferror = 3; + $counter = 0; + foreach ($assigncms as $assigncm) { + $DB->execute('DELETE FROM {assign} WHERE id = ' . $assigncm->instance); + $counter++; + if ($numberoferror == $counter) { + break; + } + } + $coursemodules = $this->get_test_course_modules(); + // Prepare redirect Events. + $sink = $this->redirectEvents(); + block_massaction\actions::duplicate_to_course($coursemodules, $targetcourseid, $targetsectionnum); + $events = $sink->get_events(); + $sink->close(); + $targetcoursemodinfo = get_fast_modinfo($targetcourseid); + $this->assertCount(count($coursemodules) - $numberoferror, $targetcoursemodinfo->get_cms()); + $failedevents = array_filter($events, function($event) { + return ($event instanceof \block_massaction\event\course_modules_duplicated_failed); + }); + $this->assertCount($numberoferror, $failedevents); } /**