From 6ac8499bace47c1e0d11648ca056b5c230ceeafe Mon Sep 17 00:00:00 2001 From: Philipp Memmel Date: Mon, 21 Mar 2022 10:51:48 +0100 Subject: [PATCH] Added event for hooks after database operations have been done --- classes/event/process_processed.php | 146 ++++++++++++++++++++++ classes/local/manager/process_manager.php | 12 ++ lang/de/tool_lifecycle.php | 1 + lang/en/tool_lifecycle.php | 1 + 4 files changed, 160 insertions(+) create mode 100644 classes/event/process_processed.php diff --git a/classes/event/process_processed.php b/classes/event/process_processed.php new file mode 100644 index 00000000..993d01e7 --- /dev/null +++ b/classes/event/process_processed.php @@ -0,0 +1,146 @@ +. + +/** + * The process_processed event. + * + * @package tool_lifecycle + * @copyright 2022 ISB Bayern + * @author Philipp Memmel + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace tool_lifecycle\event; + +use coding_exception; +use core\event\base; +use dml_exception; +use moodle_url; + +/** + * The process_processed event class. + * + * In contrast to the events {@link process_proceeded} and {@link process_rollback} this event is being fired after the process + * has been fully handled. So there is a chance the process has already been removed when this event is being received. + * + * @property-read array $other { + * Extra information about event. + * + * - int processid: the id of the process. + * - int workflowid: the id of the workflow. + * - int courseid: the id of the course. + * }; + * + * @package tool_lifecycle + * @copyright 2022 ISB Bayern + * @author Philipp Memmel + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class process_processed extends base { + + /** + * Creates an event with a process id. + * + * @param int $processid the id of the process + * @param int $workflowid the id of the workflow of the process with $processid + * @param int $courseid the id of the course the process is handling + * @return base + * @throws coding_exception + * @throws dml_exception + */ + public static function event_from_process($processid, $workflowid, $courseid): base { + $data = array( + 'context' => \context_system::instance(), + 'other' => array( + 'processid' => $processid, + 'workflowid' => $workflowid, + 'courseid' => $courseid + ) + ); + return self::create($data); + } + + /** + * Init method. + * + * @return void + */ + protected function init() { + $this->data['crud'] = 'u'; + $this->data['edulevel'] = self::LEVEL_OTHER; + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + $processid = $this->other['processid']; + $workflowid = $this->other['workflowid']; + $courseid = $this->other['courseid']; + + return "The process with id '$processid' and the corresponding workflow with id '$workflowid' " + . "for the course with the id '$courseid' has been changed."; + } + + /** + * Return localised event name. + * + * @return string + * @throws coding_exception + */ + public static function get_name() { + return get_string('process_processed_event', 'tool_lifecycle'); + } + + /** + * Returns relevant URL. + * + * @return moodle_url + */ + public function get_url() { + return new moodle_url('/admin/tool/lifecycle/view.php'); + } + + /** + * Custom validation. + * + * @throws coding_exception + */ + protected function validate_data() { + parent::validate_data(); + + if (!isset($this->other['processid'])) { + throw new coding_exception('The \'processid\' value must be set'); + } + + if (!isset($this->other['workflowid'])) { + throw new coding_exception('The \'workflowid\' value must be set'); + } + + if (!isset($this->other['courseid'])) { + throw new coding_exception('The \'courseid\' value must be set'); + } + } + + /** + * Implementation of get_other_mapping. + */ + public static function get_other_mapping() { + // No backup and restore. + return false; + } +} diff --git a/classes/local/manager/process_manager.php b/classes/local/manager/process_manager.php index 0eba63e9..b0bee834 100644 --- a/classes/local/manager/process_manager.php +++ b/classes/local/manager/process_manager.php @@ -25,6 +25,7 @@ use core\event\course_deleted; use Exception; +use tool_lifecycle\event\process_processed; use tool_lifecycle\local\entity\process; use tool_lifecycle\event\process_proceeded; use tool_lifecycle\event\process_rollback; @@ -154,9 +155,15 @@ public static function proceed_process(&$process) { $process->waiting = false; $process->timestepchanged = time(); $DB->update_record('tool_lifecycle_process', $process); + process_processed::event_from_process($process->id, $process->workflowid, $process->courseid)->trigger(); return true; } else { + // Backup information before we remove the process, so we can trigger the process_processed event after the deletion. + $processid = $process->id; + $workflowid = $process->workflowid; + $courseid = $process->courseid; self::remove_process($process); + process_processed::event_from_process($processid, $workflowid, $courseid)->trigger(); return false; } } @@ -191,7 +198,12 @@ public static function rollback_process($process) { } $lib->rollback_course($process->id, $step->id, $course); } + // Backup information before we remove the process, so we can trigger the process_proceeded event after the deletion. + $processid = $process->id; + $workflowid = $process->workflowid; + $courseid = $process->courseid; self::remove_process($process); + process_processed::event_from_process($processid, $workflowid, $courseid)->trigger(); } /** diff --git a/lang/de/tool_lifecycle.php b/lang/de/tool_lifecycle.php index b3f2bcaa..3fd9600c 100644 --- a/lang/de/tool_lifecycle.php +++ b/lang/de/tool_lifecycle.php @@ -174,6 +174,7 @@ $string['process_triggered_event'] = 'Ein Prozess wurde ausgelöst'; $string['process_proceeded_event'] = 'Ein Prozess wurde fortgeführt'; +$string['process_processed_event'] = 'Ein Prozess wurde behandelt'; $string['process_rollback_event'] = 'Ein Prozess wurde zurückgesetzt'; $string['courseid'] = 'Kurs-ID'; diff --git a/lang/en/tool_lifecycle.php b/lang/en/tool_lifecycle.php index eca85b7d..29a6dcda 100644 --- a/lang/en/tool_lifecycle.php +++ b/lang/en/tool_lifecycle.php @@ -181,6 +181,7 @@ // Events. $string['process_triggered_event'] = 'A process has been triggered'; $string['process_proceeded_event'] = 'A process has been proceeded'; +$string['process_processed_event'] = 'A process has been treated'; $string['process_rollback_event'] = 'A process has been rolled back'; // Privacy API.