Skip to content

Commit

Permalink
Issue #81 No failure during duplicate and write error into event
Browse files Browse the repository at this point in the history
  • Loading branch information
TomoTsuyuki committed Oct 13, 2023
1 parent a7a94aa commit af294bc
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 2 deletions.
18 changes: 17 additions & 1 deletion classes/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,16 @@ 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 . '(' . $e->getMessage() . ')';
continue;
}
$cms[$cmid] = $duplicatedmod->id;
$duplicatedmods[] = $duplicatedmod;
}

Expand All @@ -167,6 +175,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\massaction_duplicated::create([
'context' => \context_course::instance($courseid),
'other' => [
'cms' => $cms,
'errors' => $errors,
],
]);
$event->trigger();
}

/**
Expand Down
71 changes: 71 additions & 0 deletions classes/event/massaction_duplicated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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 massaction_duplicated event class.
*
* @package block_massaction
* @category event
* @author Tomo Tsuyuki <tomotsuyuki@catalyst-au.net>
* @copyright 2023 Catalyst IT
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class massaction_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:massaction_duplicated', 'block_massaction');
}

public function get_description(): string {
$cms = [];
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;
}

/**
* Validates the custom data.
*
* @throws \coding_exception if missing required 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.');
}
}
}
1 change: 1 addition & 0 deletions lang/en/block_massaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
$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['invalidaction'] = 'Unknown action: {$a}';
$string['invalidmoduleid'] = 'Invalid module ID: {$a}';
$string['invalidcoursemodule'] = 'Invalid course module';
Expand Down
55 changes: 55 additions & 0 deletions tests/massaction_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,61 @@ 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
* @throws require_login_exception
* @throws restore_controller_exception
* @throws dml_exception
* @throws moodle_exception
*/
public function test_mass_duplicate_modules_failing_in_the_middle(): void {
global $DB;
// Delete one module record 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);
$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;
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:
$duplicated++;
$eventduplicated = $event;
break;
}
}

// Modules are duplicated except one deleted module.
$this->assertEquals(count($coursemodules) * 2 - 1, count($newcoursemodules));
$this->assertEquals(count($coursemodules) - 1, $created);
$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);
}

/**
* Tests the duplicating of multiple modules to a different course.
*
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

defined('MOODLE_INTERNAL') || die;

$plugin->version = 2023041700;
$plugin->version = 2023041701;
$plugin->requires = 2022041900;
$plugin->component = 'block_massaction';
$plugin->maturity = MATURITY_STABLE;
Expand Down

0 comments on commit af294bc

Please sign in to comment.