From f1ef4a614e80419fa48e15697be8b622ba4cf65a Mon Sep 17 00:00:00 2001 From: Kata <35190838+olivabigyo@users.noreply.github.com> Date: Thu, 14 Mar 2024 12:12:29 +0100 Subject: [PATCH] Reorder the remaining active workflows when disabling a workflow (#201) This fixes #128. --- classes/local/manager/workflow_manager.php | 26 +++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/classes/local/manager/workflow_manager.php b/classes/local/manager/workflow_manager.php index 25c09b82..87521b71 100644 --- a/classes/local/manager/workflow_manager.php +++ b/classes/local/manager/workflow_manager.php @@ -82,12 +82,36 @@ public static function remove($workflowid, $hard = false) { * @throws \dml_transaction_exception */ public static function disable($workflowid) { + global $DB; + $transaction = $DB->start_delegated_transaction(); $workflow = self::get_workflow($workflowid); if ($workflow && self::is_disableable($workflowid)) { $workflow->timeactive = null; + self::remove_from_sortindex($workflow); $workflow->sortindex = null; $workflow->timedeactive = time(); - self::insert_or_update($workflow); + $DB->update_record('tool_lifecycle_workflow', $workflow); + } + $transaction->allow_commit(); + } + + /** + * Removes a workflow from the sortindex. + * + * @param workflow $toberemoved + * @throws \dml_exception + * @throws \dml_transaction_exception + */ + public static function remove_from_sortindex($toberemoved) { + global $DB; + if (isset($toberemoved->sortindex)) { + $workflows = self::get_active_automatic_workflows(); + foreach ($workflows as $workflow) { + if ($workflow->sortindex > $toberemoved->sortindex) { + $workflow->sortindex--; + $DB->update_record('tool_lifecycle_workflow', $workflow); + } + } } }