Skip to content

Commit

Permalink
Issue Syxton#81 Add fail event and support duplicate to another course
Browse files Browse the repository at this point in the history
  • Loading branch information
TomoTsuyuki committed Oct 24, 2023
1 parent 83c1382 commit 75612d4
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 22 deletions.
48 changes: 44 additions & 4 deletions classes/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}
}

/**
Expand Down Expand Up @@ -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;
}

Expand All @@ -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();
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
use core\event\base;

/**
* The massaction_duplicated event class.
* 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 massaction_duplicated extends base {
class course_modules_duplicated extends base {

/**
* Initialise required event data properties.
Expand All @@ -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) . '.' : '');
}

/**
Expand All @@ -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.');
}
}
}
75 changes: 75 additions & 0 deletions classes/event/course_modules_duplicated_failed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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.
*
* @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.');
}
}
}
2 changes: 2 additions & 0 deletions lang/en/block_massaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
$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: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';
Expand Down
58 changes: 46 additions & 12 deletions tests/massaction_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}

/**
Expand All @@ -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.
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 75612d4

Please sign in to comment.