From 3660bb9502ff5f5e221ad8d0fc723101af72834f Mon Sep 17 00:00:00 2001 From: Philipp Memmel Date: Sun, 20 Feb 2022 10:48:43 +0100 Subject: [PATCH 1/3] Add user dependent availability to actions for interactive steps --- .../table/interaction_attention_table.php | 14 +++++++++++++- lang/de/tool_lifecycle.php | 1 + lang/en/tool_lifecycle.php | 1 + step/interactionlib.php | 19 +++++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/classes/local/table/interaction_attention_table.php b/classes/local/table/interaction_attention_table.php index 7b7b43c4..9bd92652 100644 --- a/classes/local/table/interaction_attention_table.php +++ b/classes/local/table/interaction_attention_table.php @@ -109,12 +109,24 @@ public function init() { * @throws \invalid_parameter_exception */ public function col_tools($row) { + global $USER; $output = ''; $step = step_manager::get_step_instance($row->stepinstanceid); $tools = interaction_manager::get_action_tools($step->subpluginname, $row->processid); + $toolsstrings = array_map(fn($action) => $action['action'], $tools); + + $availabletoolsstrings = lib_manager::get_step_interactionlib($step->subpluginname)-> + get_available_actions_for_user($toolsstrings, $USER->id, $row->courseid); + foreach ($tools as $tool) { - $output .= $this->format_icon_link($tool['action'], $row->processid, $step->id, $tool['alt']); + // Only show actions which are valid actions and which are available to the current user. + if (in_array($tool['action'], $toolsstrings) && in_array($tool['action'], $availabletoolsstrings)) { + $output .= $this->format_icon_link($tool['action'], $row->processid, $step->id, $tool['alt']); + } + } + if (empty($output)) { + $output = get_string('noactionsavailable', 'tool_lifecycle'); } return $output; } diff --git a/lang/de/tool_lifecycle.php b/lang/de/tool_lifecycle.php index b3f2bcaa..ae389b7c 100644 --- a/lang/de/tool_lifecycle.php +++ b/lang/de/tool_lifecycle.php @@ -162,6 +162,7 @@ $string['coursename'] = 'Kursname'; $string['lastaction'] = 'Letzte Aktion am'; +$string['noactionsavailable'] = 'Sie können keine Aktion ausführen. Warten Sie, bis ein anderer Benutzer dies tut.'; $string['workflow_started'] = 'Workflow gestartet.'; $string['workflow_is_running'] = 'Workflow läuft.'; diff --git a/lang/en/tool_lifecycle.php b/lang/en/tool_lifecycle.php index eca85b7d..357a7214 100644 --- a/lang/en/tool_lifecycle.php +++ b/lang/en/tool_lifecycle.php @@ -167,6 +167,7 @@ $string['coursename'] = 'Course name'; $string['lastaction'] = 'Last action on'; +$string['noactionsavailable'] = 'You cannot execute any action but have to wait for another user to handle this course.'; $string['anonymous_user'] = 'Anonymous User'; $string['workflow_started'] = 'Workflow started.'; diff --git a/step/interactionlib.php b/step/interactionlib.php index a4909a52..2870b344 100644 --- a/step/interactionlib.php +++ b/step/interactionlib.php @@ -105,4 +105,23 @@ abstract public function handle_interaction($process, $step, $action = 'default' public function get_due_date($processid, $stepid) { return null; } + + /** + * Method to limit the actions to specific users. Returns all actions by default so if not overwritten all actions are available + * to any user. You can overwrite this method to filter the $actionstrings array according to your needs to limit the actions + * shown to the given user. + * + * Care: When overwriting this method you want to make sure that at least one user can use at least one action or have a timeout + * in your step to avoid any deadlocked workflows. + * + * @param array $actionstrings array of action strings. These should match the strings with the key 'action' + * in {@see get_action_tools} return array. + * @param int $userid the id of the user for whom the returned actions should be visible + * @param int $courseid the id of the currently handled course + * @return array the (possibly) filtered $actions array containing the strings of the actions which should be available to the + * given user + */ + public function get_available_actions_for_user(array $actionstrings, int $userid, int $courseid) : array { + return $actionstrings; + } } From 69af78bd87675d2961256644a4c81c8476f24931 Mon Sep 17 00:00:00 2001 From: Philipp Memmel Date: Sun, 20 Feb 2022 23:02:13 +0100 Subject: [PATCH 2/3] Add corresponding method to interaction_manager --- classes/local/manager/interaction_manager.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/classes/local/manager/interaction_manager.php b/classes/local/manager/interaction_manager.php index 87ecfff8..cccf1e28 100644 --- a/classes/local/manager/interaction_manager.php +++ b/classes/local/manager/interaction_manager.php @@ -155,6 +155,27 @@ public static function get_action_tools($subpluginname, $processid) { return $interactionlib->get_action_tools($process); } + /** + * Returns an array of interaction tools strings which are in fact displayed to the given user on the view.php. + * + * @param string $subpluginname name of the step + * @param int $processid id of the process the action strings are requested for + * @param int $userid id of the user for whom the actions are visible or not + * @return array of action strings visible to the given usertools + * @throws \coding_exception + * @throws \dml_exception + * @throws \invalid_parameter_exception + */ + public static function get_available_actions_for_user(string $subpluginname, int $processid, int $userid) : array { + $interactionlib = lib_manager::get_step_interactionlib($subpluginname); + $process = process_manager::get_process_by_id($processid); + if (!$process) { + throw new \invalid_parameter_exception(get_string('noprocessfound', 'tool_lifecycle')); + } + $actionstrings = array_map(fn($action) => $action['action'], $interactionlib->get_action_tools($process)); + return $interactionlib->get_available_actions_for_user($actionstrings, $userid, $process->courseid); + } + /** * Returns the status message for the given process. * @param int $processid id of the process the status message is requested for From a6fed3f25706024dd0d9ac8923225266ea94f990 Mon Sep 17 00:00:00 2001 From: Philipp Memmel Date: Mon, 21 Mar 2022 09:13:39 +0100 Subject: [PATCH 3/3] Return empty array if no interactionlib is available --- classes/local/manager/interaction_manager.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/classes/local/manager/interaction_manager.php b/classes/local/manager/interaction_manager.php index cccf1e28..da3fb2ea 100644 --- a/classes/local/manager/interaction_manager.php +++ b/classes/local/manager/interaction_manager.php @@ -167,11 +167,14 @@ public static function get_action_tools($subpluginname, $processid) { * @throws \invalid_parameter_exception */ public static function get_available_actions_for_user(string $subpluginname, int $processid, int $userid) : array { - $interactionlib = lib_manager::get_step_interactionlib($subpluginname); $process = process_manager::get_process_by_id($processid); if (!$process) { throw new \invalid_parameter_exception(get_string('noprocessfound', 'tool_lifecycle')); } + $interactionlib = lib_manager::get_step_interactionlib($subpluginname); + if (!$interactionlib) { + return []; + } $actionstrings = array_map(fn($action) => $action['action'], $interactionlib->get_action_tools($process)); return $interactionlib->get_available_actions_for_user($actionstrings, $userid, $process->courseid); }