diff --git a/classes/actions.php b/classes/actions.php index 8429000..fd986e2 100644 --- a/classes/actions.php +++ b/classes/actions.php @@ -147,6 +147,9 @@ public static function duplicate(array $modules, $sectionnumber = false): void { // sorted by their id: // 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). + + $cms = []; + $errors = []; $duplicatedmods = []; $targetformat = course_get_format($courseid); $sectionsrestricted = massactionutils::get_restricted_sections($courseid, $targetformat->get_format()); @@ -156,7 +159,22 @@ public static function duplicate(array $modules, $sectionnumber = false): void { if (in_array($cm->sectionnum, $sectionsrestricted)) { throw new moodle_exception('sectionrestricted', 'block_massaction'); } - $duplicatedmod = duplicate_module($modinfo->get_course(), $modinfo->get_cm($cmid)); + + try { + $duplicatedmod = duplicate_module($modinfo->get_course(), $modinfo->get_cm($cmid)); + } catch (\Exception $e) { + $errors[$cmid] = 'cmid:' . $cmid . '(' . $e->getMessage() . ')'; + $event = \block_massaction\event\course_modules_duplicated_failed::create([ + 'context' => \context_course::instance($courseid), + 'other' => [ + 'cmid' => $cmid, + 'error' => $errors[$cmid], + ], + ]); + $event->trigger(); + continue; + } + $cms[$cmid] = $duplicatedmod->id; $duplicatedmods[] = $duplicatedmod; } @@ -175,6 +193,14 @@ 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\course_modules_duplicated::create([ + 'context' => \context_course::instance($courseid), + 'other' => [ + 'cms' => $cms, + 'failed' => array_keys($errors), + ], + ]); + $event->trigger(); } /** @@ -270,6 +296,8 @@ 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 = []; $sourceformat = course_get_format($sourcecourseid); $sourcesectionsrestricted = massactionutils::get_restricted_sections($sourcecourseid, $sourceformat->get_format()); foreach ($idsincourseorder as $cmid) { @@ -278,7 +306,23 @@ public static function duplicate_to_course(array $modules, int $targetcourseid, if (in_array($sourcecm->sectionnum, $sourcesectionsrestricted)) { throw new moodle_exception('sectionrestricted', 'block_massaction'); } - $duplicatedmod = massactionutils::duplicate_cm_to_course($targetmodinfo->get_course(), $sourcecm); + + try { + $duplicatedmod = massactionutils::duplicate_cm_to_course($targetmodinfo->get_course(), + $sourcemodinfo->get_cm($cmid)); + } catch (\Exception $e) { + $errors[$cmid] = 'cmid:' . $cmid . '(' . $e->getMessage() . ')'; + $event = \block_massaction\event\course_modules_duplicated_failed::create([ + 'context' => \context_course::instance($sourcecourseid), + 'other' => [ + 'cmid' => $cmid, + 'error' => $errors[$cmid], + ], + ]); + $event->trigger(); + continue; + } + $cms[$cmid] = $duplicatedmod; $duplicatedmods[] = $duplicatedmod; } @@ -291,6 +335,14 @@ 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($sourcecourseid), + 'other' => [ + 'cms' => $cms, + 'failed' => array_keys($errors), + ], + ]); + $event->trigger(); } /** diff --git a/classes/event/course_modules_duplicated.php b/classes/event/course_modules_duplicated.php new file mode 100644 index 0000000..c8ac260 --- /dev/null +++ b/classes/event/course_modules_duplicated.php @@ -0,0 +1,82 @@ +. + +namespace block_massaction\event; + +use core\event\base; + +/** + * The course_modules_duplicated event class. + * + * @package block_massaction + * @category event + * @author Tomo Tsuyuki + * @copyright 2023 Catalyst IT + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class course_modules_duplicated 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', '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 . '\''; + } + 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) . '.' : ''); + } + + /** + * Validates the custom data. + */ + protected function validate_data() { + parent::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..a874071 --- /dev/null +++ b/classes/event/course_modules_duplicated_failed.php @@ -0,0 +1,73 @@ +. + +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. + */ + 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/classes/massactionutils.php b/classes/massactionutils.php index 9b17bd5..e1cb0ed 100644 --- a/classes/massactionutils.php +++ b/classes/massactionutils.php @@ -159,6 +159,9 @@ public static function duplicate_cm_to_course(object $course, object $cm): int { } } } + if (empty($newcmid)) { + throw new \moodle_exception('Could not duplicate course module from id ' . $cm->id); + } return $newcmid; } diff --git a/lang/en/block_massaction.php b/lang/en/block_massaction.php index c8e5445..284b234 100644 --- a/lang/en/block_massaction.php +++ b/lang/en/block_massaction.php @@ -76,6 +76,8 @@ $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['enablebulkediting'] = 'Enable bulk editing'; +$string['event:course_modules_duplicated'] = 'Course modules duplicated'; +$string['event:course_modules_duplicated_failed'] = 'Course modules failed to duplicate'; $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 6dba37e..dfc6726 100644 --- a/tests/massaction_test.php +++ b/tests/massaction_test.php @@ -492,6 +492,65 @@ public function test_mass_duplicate_modules(): void { $modinfo->get_cm($selectedmoduleids[3])->name . ' (copy)'); } + /** + * Tests duplicating multiple modules something error in the middle of the process. + * + * @covers \block_massaction\actions::duplicate + * @return void + */ + public function test_mass_duplicate_modules_failing_in_the_middle(): void { + global $DB; + // Delete 3 module records to fail to duplicate for the module. + $modinfo = get_fast_modinfo($this->course->id); + $assigncms = $modinfo->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($coursemodules); + + $newcoursemodules = $this->get_test_course_modules(); + + // Check error message. + $events = $sink->get_events(); + $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\course_modules_duplicated::class: + $duplicated++; + break; + case \block_massaction\event\course_modules_duplicated_failed::class: + $failed++; + break; + } + } + + // Modules are duplicated except one deleted module. + $this->assertEquals(count($coursemodules) * 2 - $numberoferror, count($newcoursemodules)); + $this->assertEquals(count($coursemodules) - $numberoferror, $created); + // 1 duplicate event (Summary). + $this->assertEquals(1, $duplicated); + // 3 failed events. + $this->assertEquals($numberoferror, $failed); + } + /** * Tests the duplicating of multiple modules to a different course. * @@ -506,6 +565,7 @@ public function test_mass_duplicate_modules(): 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. @@ -581,6 +641,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); } /**