Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions classes/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,9 @@ public static function duplicate_to_course(array $modules, int $targetcourseid,
// 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).
$duplicatedmods = [];
$cms = [];
$errors = [];
$sourcecms = [];
$duplicatedmods = [];
$filtersectionshook = new filter_sections_same_course($sourcecourseid, array_keys($sourcemodinfo->get_section_info_all()));
\core\di::get(\core\hook\manager::class)->dispatch($filtersectionshook);
$srcfilteredsections = $filtersectionshook->get_sectionnums();
Expand All @@ -363,44 +363,44 @@ public static function duplicate_to_course(array $modules, int $targetcourseid,
if (!in_array($sourcecm->sectionnum, $srcfilteredsections)) {
throw new moodle_exception('sectionrestricted', 'block_massaction', '', $sourcecm->sectionnum);
}
$sourcecms[] = $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;
try {
$duplicatedmods = massactionutils::duplicate_cms_to_course(
$targetmodinfo->get_course(),
$sourcecms
);
} catch (\Exception $e) {
$event = \block_massaction\event\course_modules_duplicated_failed::create(
[
'context' => \context_course::instance($sourcecourseid),
'other' => [
'cmid' => implode(',', array_map(function ($cm) {
return $cm->id;
}, $sourcecms)),
'error' => $e->getMessage(),
],
]
);
$event->trigger();
$errors[] = $e->getMessage();
}

// We need to reload new course structure.
$targetmodinfo = get_fast_modinfo($targetcourseid);
$targetsection = $targetmodinfo->get_section_info($sectionnum);
if ($sectionnum != -1) {
// A target section has been specified, so we have to move the course modules.
foreach ($duplicatedmods as $modid) {
foreach (array_values($duplicatedmods) as $modid) {
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),
'cms' => $modules,
'failed' => $errors,
],
]);
$event->trigger();
Expand Down
53 changes: 53 additions & 0 deletions classes/massaction_backup_controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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;

use backup;
use backup_controller;

/**
* A specialized controller for creating backups of multiple activities.
*
* @package block_massaction
* @copyright 2026 ISB Bayern
* @author Stefan Hanauska <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class massaction_backup_controller extends backup_controller {
/**
* Builds a backup controller for multiple activities.
*
* @param integer $userid The user ID for whom the backup is being created.
* @param array $cmids An array of course module IDs to be included in the backup.
*/
public function __construct(int $userid, array $cmids) {
if (empty($cmids)) {
throw new \invalid_argument_exception('At least one cmid must be provided');
}
parent::__construct(
backup::TYPE_1ACTIVITY,
array_values($cmids)[0],
backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO,
backup::MODE_IMPORT,
$userid
);
$this->plan = new massaction_backup_plan($this);
$this->plan = massaction_backup_plan_builder::build_multiple_activities_plan($this, $cmids);
$this->plan->set_built();
}
}
38 changes: 38 additions & 0 deletions classes/massaction_backup_plan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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;

use backup_plan;

/**
* A specialized backup plan that provides a method to set the built flag to true.
*
* @package block_massaction
* @copyright 2026 ISB Bayern
* @author Stefan Hanauska <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class massaction_backup_plan extends backup_plan {
/**
* Set built flag to true, so that the plan can be executed. This is needed as the plan is built in a separate builder class.
*
* @return void
*/
public function set_built() {
$this->built = true;
}
}
57 changes: 57 additions & 0 deletions classes/massaction_backup_plan_builder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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;

use backup_controller;
use backup_plan_builder;
use backup_root_task;
use backup_final_task;

/**
* A specialized backup plan builder that provides a method to build a plan for creating a backup from multiple activities.
*
* @package block_massaction
* @copyright 2026 ISB Bayern
* @author Stefan Hanauska <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class massaction_backup_plan_builder extends backup_plan_builder {
/**
* Builds a plan that creates a backup from multiple activities.
*
* @param backup_controller $controller The backup controller for which the plan is being built.
* @param array $cmids An array of course module IDs to be included in the backup.
* @return massaction_backup_plan
*/
public static function build_multiple_activities_plan(backup_controller $controller, array $cmids): massaction_backup_plan {
if (empty($cmids)) {
throw new \invalid_argument_exception('At least one cmid must be provided');
}

$plan = $controller->get_plan();

$plan->add_task(new backup_root_task('root_task'));

foreach ($cmids as $cmid) {
self::build_activity_plan($controller, $cmid);
}

$plan->add_task(new backup_final_task('final_task'));

return $plan;
}
}
55 changes: 55 additions & 0 deletions classes/massaction_restore_controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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;

use backup;
use restore_controller;

/**
* Class massaction_restore_controller
*
* @package block_massaction
* @copyright 2026 ISB Bayern
* @author Stefan Hanauska <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class massaction_restore_controller extends restore_controller {
/**
* Constructs a massaction_restore_controller instance.
*
* @param string $tempdir The temporary directory where the backup file is located.
* @param integer $courseid The ID of the course into which the backup will be restored.
* @param integer $userid The user ID for whom the restore is being performed.
*/
public function __construct(string $tempdir, int $courseid, int $userid) {
parent::__construct(
$tempdir,
$courseid,
backup::INTERACTIVE_NO,
backup::MODE_IMPORT,
$userid,
backup::TARGET_CURRENT_ADDING
);
$cmids = array_keys($this->get_info()->activities);
$this->progress = new \core\progress\none();
$this->progress->start_progress('Constructing restore_controller');
$this->plan = new massaction_restore_plan($this);
$this->plan = massaction_restore_plan_builder::build_multiple_activities_plan($this, $cmids);
$this->plan->set_built();
$this->progress->end_progress();
}
}
38 changes: 38 additions & 0 deletions classes/massaction_restore_plan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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;

use restore_plan;

/**
* A specialized restore plan that provides a method to set the built flag to true.
*
* @package block_massaction
* @copyright 2026 ISB Bayern
* @author Stefan Hanauska <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class massaction_restore_plan extends restore_plan {
/**
* Set built flag to true, so that the plan can be executed. This is needed as the plan is built in a separate builder class.
*
* @return void
*/
public function set_built() {
$this->built = true;
}
}
59 changes: 59 additions & 0 deletions classes/massaction_restore_plan_builder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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;

use restore_plan_builder;
use restore_root_task;
use restore_final_task;
/**
* A specialized restore plan builder that provides a method to build a plan for restoring multiple activities.
*
* @package block_massaction
* @copyright 2026 ISB Bayern
* @author Stefan Hanauska <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class massaction_restore_plan_builder extends restore_plan_builder {
/**
* Builds a plan that restores a backup of multiple activities.
*
* @param massaction_restore_controller $controller The restore controller for which the plan is being built.
* @param array $cmids An array of course module IDs that should be restored.
* @return massaction_restore_plan
*/
public static function build_multiple_activities_plan(massaction_restore_controller $controller, array $cmids): massaction_restore_plan {
if (empty($cmids)) {
throw new \invalid_argument_exception('At least one cmid must be provided');
}

$plan = $controller->get_plan();

$plan->add_task(new restore_root_task('root_task'));
$controller->get_progress()->progress();

foreach ($cmids as $cmid) {
self::build_activity_plan($controller, $cmid);
}

$plan->add_task(new restore_final_task('final_task'));
$controller->get_progress()->progress();

$plan->set_built();

return $plan;
}
}
Loading
Loading