diff --git a/classes/local/manager/process_manager.php b/classes/local/manager/process_manager.php index 48268d6a9972c94f78470490517231757fcb7186..f8b28c9fc9b7a16eced8bc6eb1e5f9efe940b451 100644 --- a/classes/local/manager/process_manager.php +++ b/classes/local/manager/process_manager.php @@ -23,6 +23,7 @@ */ namespace tool_lifecycle\local\manager; +use core\event\course_deleted; use tool_lifecycle\local\entity\process; use tool_lifecycle\event\process_proceeded; use tool_lifecycle\event\process_rollback; @@ -220,4 +221,28 @@ class process_manager { return null; } } + + /** + * Callback for the course deletion observer. + * @param course_deleted $event The course deletion event. + * @throws \dml_exception + */ + public static function course_deletion_observed($event) { + $process = self::get_process_by_course_id($event->get_data()['courseid']); + if ($process) { + self::abort_process($process); + } + } + + /** + * Aborts a running process. + * @param process $process The process to abort. + * @throws \dml_exception + */ + public static function abort_process($process) { + $step = step_manager::get_step_instance_by_workflow_index($process->workflowid, $process->stepindex); + $steplib = lib_manager::get_step_lib($step->subpluginname); + $steplib->abort_course($process); + self::remove_process($process); + } } diff --git a/db/events.php b/db/events.php new file mode 100644 index 0000000000000000000000000000000000000000..fa9c644903c868f40393d70064ea801ff1f59a26 --- /dev/null +++ b/db/events.php @@ -0,0 +1,32 @@ +<?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/>. + +/** + * Event observer. + * + * @package tool_lifecycle + * @copyright 2020 Justus Dieckmann WWU + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$observers = array( + array( + 'eventname' => 'core\event\course_deleted', + 'callback' => 'tool_lifecycle\local\manager\process_manager::course_deletion_observed', + ) +); diff --git a/db/upgrade.php b/db/upgrade.php index 1ee58e39902355f4db55431fcd2fa385058af038..6de05fb90af41c4494cc3a400754a72719dc4175 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -433,5 +433,18 @@ function xmldb_tool_lifecycle_upgrade($oldversion) { upgrade_plugin_savepoint(true, 2019082300, 'tool', 'lifecycle'); } + if ($oldversion < 2020091800) { + $sql = "SELECT p.* FROM {tool_lifecycle_process} p " . + "LEFT JOIN {course} c ON p.courseid = c.id " . + "WHERE c.id IS NULL"; + $processes = $DB->get_records_sql($sql); + foreach ($processes as $procrecord) { + $process = \tool_lifecycle\local\entity\process::from_record($procrecord); + \tool_lifecycle\local\manager\process_manager::abort_process($process); + } + + upgrade_plugin_savepoint(true, 2020091800, 'tool', 'lifecycle'); + } + return true; } \ No newline at end of file diff --git a/step/lib.php b/step/lib.php index 03018d932e729e91023fd483b5d5c21582b5de25..f544e9ea7ea761cfb61bf452fa7b300fd229b283 100644 --- a/step/lib.php +++ b/step/lib.php @@ -24,6 +24,7 @@ */ namespace tool_lifecycle\step; +use tool_lifecycle\local\entity\process; use tool_lifecycle\local\manager\step_manager; use tool_lifecycle\local\response\step_response; @@ -123,6 +124,14 @@ abstract class libbase { public function extend_add_instance_form_definition_after_data($mform, $settings) { } + /** + * This method can be overridden. It is called when a course and the + * corresponding process get deleted. + * @param process $process the process that was aborted. + */ + public function abort_course($process) { + } + } /**