Skip to content

Commit

Permalink
Issue #81 No failure during duplicate and write error into event (#90)
Browse files Browse the repository at this point in the history
* Update ci.yml

* Issue #81 No failure during duplicate and write error into event

* Issue #81 Add fail event and support duplicate to another course

* Issue #81 Refactor error event trigger
  • Loading branch information
TomoTsuyuki authored Jan 11, 2024
1 parent beda7a2 commit 692919f
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 2 deletions.
53 changes: 51 additions & 2 deletions classes/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,24 @@ 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 = [];
foreach ($idsincourseorder as $cmid) {
$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;
}

Expand All @@ -167,6 +183,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();
}

/**
Expand Down Expand Up @@ -262,8 +286,25 @@ 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() . ')';
$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;
}

Expand All @@ -276,6 +317,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();
}

/**
Expand Down
82 changes: 82 additions & 0 deletions classes/event/course_modules_duplicated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?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/>.

namespace block_massaction\event;

use core\event\base;

/**
* The course_modules_duplicated event class.
*
* @package block_massaction
* @category event
* @author Tomo Tsuyuki <[email protected]>
* @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.');
}
}
}
73 changes: 73 additions & 0 deletions classes/event/course_modules_duplicated_failed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?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/>.

namespace block_massaction\event;

use core\event\base;

/**
* The course_modules_duplicated_failed event class.
*
* @package block_massaction
* @category event
* @author Tomo Tsuyuki <[email protected]>
* @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.');
}
}
}
2 changes: 2 additions & 0 deletions lang/en/block_massaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
85 changes: 85 additions & 0 deletions tests/massaction_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 692919f

Please sign in to comment.