Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add user dependent availability to actions for interactive steps #143

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
24 changes: 24 additions & 0 deletions classes/local/manager/interaction_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,30 @@ 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 {
$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);
}

/**
* Returns the status message for the given process.
* @param int $processid id of the process the status message is requested for
Expand Down
14 changes: 13 additions & 1 deletion classes/local/table/interaction_attention_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions lang/de/tool_lifecycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
Expand Down
1 change: 1 addition & 0 deletions lang/en/tool_lifecycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
Expand Down
19 changes: 19 additions & 0 deletions step/interactionlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}