-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathworkflowoverview.php
497 lines (470 loc) · 21.4 KB
/
workflowoverview.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?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/>.
/**
* Displays a workflow in a nice visual form.
*
* @package tool_lifecycle
* @copyright 2025 Thomas Niedermaier University Münster
* @copyright 2021 Nina Herrmann and Justus Dieckmann, WWU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../../config.php');
require_once($CFG->libdir . '/adminlib.php');
require_login();
define('PAGESIZE', 20);
use core\task\manager;
use tool_lifecycle\action;
use tool_lifecycle\local\manager\delayed_courses_manager;
use tool_lifecycle\local\manager\lib_manager;
use tool_lifecycle\local\manager\process_manager;
use tool_lifecycle\local\manager\step_manager;
use tool_lifecycle\local\manager\trigger_manager;
use tool_lifecycle\local\manager\workflow_manager;
use tool_lifecycle\local\response\trigger_response;
use tool_lifecycle\local\table\courses_in_step_table;
use tool_lifecycle\local\table\triggered_courses_table;
use tool_lifecycle\processor;
use tool_lifecycle\settings_type;
use tool_lifecycle\tabs;
use tool_lifecycle\urls;
require_login();
$workflowid = required_param('wf', PARAM_INT);
$stepid = optional_param('step', null, PARAM_INT);
$triggerid = optional_param('trigger', null, PARAM_INT);
$triggered = optional_param('triggered', null, PARAM_INT);
$excluded = optional_param('excluded', null, PARAM_INT);
$delayed = optional_param('delayed', null, PARAM_INT);
$used = optional_param('used', null, PARAM_INT);
$search = optional_param('search', null, PARAM_RAW);
$showdetails = optional_param('showdetails', 0, PARAM_INT);
if ($showdetails == 0) {
if (isset($SESSION->showdetails)) {
if ($SESSION->showdetails == $workflowid) {
$showdetails = $workflowid;
}
}
} else if ($showdetails == -1) {
$SESSION->showdetails = $showdetails = 0;
} else {
$SESSION->showdetails = $workflowid;
}
$workflow = workflow_manager::get_workflow($workflowid);
$iseditable = workflow_manager::is_editable($workflow->id);
$isactive = workflow_manager::is_active($workflow->id);
$isdeactivated = workflow_manager::is_deactivated($workflow->id);
$params = ['wf' => $workflow->id];
if ($stepid) {
$params['step'] = $stepid;
} else if ($triggerid) {
$params['trigger'] = $triggerid;
} else if ($triggered) {
$params['triggered'] = $triggered;
} else if ($delayed) {
$params['delayed'] = $delayed;
} else if ($excluded) {
$params['excluded'] = $excluded;
}
$syscontext = context_system::instance();
$PAGE->set_url(new \moodle_url(urls::WORKFLOW_DETAILS, $params));
$PAGE->set_context($syscontext);
$PAGE->set_title($workflow->title);
// Link to open the popup with the course list.
$popuplink = new moodle_url(urls::WORKFLOW_DETAILS, ['wf' => $workflow->id]);
// Link for loading the page with no popupwindow.
$nosteplink = new moodle_url(urls::WORKFLOW_DETAILS, ['wf' => $workflowid]);
// Link for changing the extended details view mode.
$showdetailslink = new moodle_url(urls::WORKFLOW_DETAILS, $params);
$action = optional_param('action', null, PARAM_TEXT);
if ($action) {
if ($action == 'deletedelay') {
$cid = required_param('cid', PARAM_INT);
$DB->delete_records('tool_lifecycle_delayed_workf', ['courseid' => $cid, 'workflowid' => $workflow->id]);
} else {
step_manager::handle_action($action, optional_param('actionstep', null, PARAM_INT), $workflow->id);
trigger_manager::handle_action($action, optional_param('actiontrigger', null, PARAM_INT), $workflow->id);
$processid = optional_param('processid', null, PARAM_INT);
if ($processid) {
$process = process_manager::get_process_by_id($processid);
if ($action === 'rollback') {
process_manager::rollback_process($process);
delayed_courses_manager::set_course_delayed_for_workflow($process->courseid, true, $workflow);
} else if ($action === 'proceed') {
process_manager::proceed_process($process);
delayed_courses_manager::set_course_delayed_for_workflow($process->courseid, false, $workflow);
} else {
throw new coding_exception('processid was specified but action was neither "rollback" nor "proceed"!');
}
}
redirect($PAGE->url);
}
}
$PAGE->set_pagetype('admin-setting-' . 'tool_lifecycle');
$PAGE->set_pagelayout('admin');
$renderer = $PAGE->get_renderer('tool_lifecycle');
$heading = get_string('pluginname', 'tool_lifecycle')." / ".
get_string('workflowoverview', 'tool_lifecycle').": ". $workflow->title;
echo $renderer->header($heading);
$activelink = false;
$deactivatedlink = false;
$draftlink = false;
if ($isactive) { // Active workflow.
$id = 'activeworkflows';
$activelink = true;
$classdetails = "bg-primary text-white";
} else {
if ($isdeactivated) { // Deactivated workflow.
$id = 'deactivatedworkflows';
$deactivatedlink = true;
$classdetails = "bg-dark text-white";
} else { // Draft.
$id = 'workflowdrafts';
$draftlink = true;
$classdetails = "bg-light";
}
}
$tabrow = tabs::get_tabrow($activelink, $deactivatedlink, $draftlink);
$renderer->tabs($tabrow, $id);
$steps = step_manager::get_step_instances($workflow->id);
$triggers = trigger_manager::get_triggers_for_workflow($workflow->id);
$str = [
'edit' => get_string('edit'),
'delete' => get_string('delete'),
'move_up' => get_string('move_up', 'tool_lifecycle'),
'move_down' => get_string('move_down', 'tool_lifecycle'),
];
$nextrun = 0;
$coursestriggered = [];
$displaytotaltriggered = false;
if ($showdetails) {
/*
On moodle instances with many courses the following call can be fatal, because each trigger
check function will be called for every single course of the instance to determine how many
courses will be triggered by the workflow/the specific trigger. This count is only being
used to show the admin how many courses will be triggered, it has no functional aspect.
*/
$amounts = (new processor())->get_count_of_courses_to_trigger_for_workflow($workflow);
$coursestriggered = $amounts['all']->coursestriggered;
$nextrun = $amounts['all']->nextrun;
$displaytotaltriggered = !empty($triggers);
}
$task = manager::get_scheduled_task('tool_lifecycle\task\lifecycle_task');
$lastrun = $task->get_last_run_time();
$nextrunt = $task->get_next_run_time();
if (!$task->is_component_enabled() && !$task->get_run_if_component_disabled()) {
$nextrunt = get_string('plugindisabled', 'tool_task');
} else if ($task->get_disabled()) {
$nextrunt = get_string('taskdisabled', 'tool_task');
} else if ($nextrunt < time()) {
$nextrunt = get_string('asap', 'tool_task');
}
if (is_int($nextrunt) && $nextrun ?? false) { // Task nextrun and trigger nextrun are valid times: take the minimum.
$nextrun = userdate(min($nextrunt, $nextrun), get_string('strftimedatetimeshort', 'langconfig'));
} else if (!is_int($nextrunt) && $nextrun ?? false) { // Only trigger nextrun is valid time.
$nextrun = userdate($nextrun, get_string('strftimedatetimeshort', 'langconfig'));
} else if (is_int($nextrunt)) { // Only task next run is valid time.
$nextrun = userdate($nextrunt, get_string('strftimedatetimeshort', 'langconfig'));
} else { // There is no valid next run time. Print the task message.
$nextrun = $nextrunt;
}
$displaytriggers = [];
$displaytimetriggers = [];
$displaysteps = [];
$nomanualtriggerinvolved = true;
foreach ($triggers as $trigger) {
// The array from the DB Function uses ids as keys.
// Mustache cannot handle arrays which have other keys therefore a new array is build.
// FUTURE: Nice to have Icon for each subplugin.
$trigger = (object)(array) $trigger; // Cast to normal object to be able to set dynamic properties.
$actionmenu = new action_menu([
new action_menu_link_secondary(
new moodle_url(urls::EDIT_ELEMENT, ['type' => settings_type::TRIGGER, 'elementid' => $trigger->id]),
new pix_icon('i/edit', $str['edit']), $str['edit']),
]);
if ($iseditable) {
$actionmenu->add(new action_menu_link_secondary(
new moodle_url($PAGE->url,
['action' => action::TRIGGER_INSTANCE_DELETE, 'sesskey' => sesskey(), 'actiontrigger' => $trigger->id]),
new pix_icon('t/delete', $str['delete']), $str['delete'])
);
}
$trigger->actionmenu = $OUTPUT->render($actionmenu);
$response = null;
$lib = lib_manager::get_trigger_lib($trigger->subpluginname);
if ($trigger->automatic = !$lib->is_manual_trigger()) {
$response = $lib->check_course(null, null);
}
$nomanualtriggerinvolved &= $trigger->automatic;
if ($showdetails) {
if ($trigger->automatic) {
$sqlresult = trigger_manager::get_trigger_sqlresult($trigger);
if ($sqlresult == "false") {
$trigger->classfires = "border-danger";
$trigger->additionalinfo = $amounts[$trigger->sortindex]->additionalinfo ?? "-";
} else {
if ($response != trigger_response::triggertime()) {
if ($amounts[$trigger->sortindex]->triggered) {
$trigger->classfires = "border-success";
} else if ($amounts[$trigger->sortindex]->excluded) {
$trigger->classfires = "border-danger";
}
$trigger->excludedcourses = $amounts[$trigger->sortindex]->excluded;
$trigger->triggeredcourses = $amounts[$trigger->sortindex]->triggered;
}
}
}
$displaytotaltriggered &= $trigger->automatic;
}
if ($response == trigger_response::triggertime()) {
$displaytimetriggers[] = $trigger;
if (isset($amounts[$trigger->sortindex]->lastrun)) {
$lastrun = $amounts[$trigger->sortindex]->lastrun;
}
} else {
$displaytriggers[] = $trigger;
}
}
foreach ($steps as $step) {
$step = (object)(array) $step; // Cast to normal object to be able to set dynamic properties.
$ncourses = $DB->count_records('tool_lifecycle_process',
['stepindex' => $step->sortindex, 'workflowid' => $workflowid]);
$step->numberofcourses = $ncourses;
if ($step->id == $stepid) {
$step->selected = true;
}
$actionmenu = new action_menu([
new action_menu_link_secondary(
new moodle_url(urls::EDIT_ELEMENT, ['type' => settings_type::STEP, 'elementid' => $step->id]),
new pix_icon('i/edit', $str['edit']), $str['edit']),
]);
if ($iseditable) {
$actionmenu->add(new action_menu_link_secondary(
new moodle_url($PAGE->url,
['action' => action::STEP_INSTANCE_DELETE, 'sesskey' => sesskey(), 'actionstep' => $step->id]),
new pix_icon('t/delete', $str['delete']), $str['delete'])
);
if ($step->sortindex > 1) {
$actionmenu->add(new action_menu_link_secondary(
new moodle_url($PAGE->url,
['action' => action::UP_STEP, 'sesskey' => sesskey(), 'actionstep' => $step->id]),
new pix_icon('t/up', $str['move_up']), $str['move_up'])
);
}
if ($step->sortindex < count($steps)) {
$actionmenu->add(new action_menu_link_secondary(
new moodle_url($PAGE->url,
['action' => action::DOWN_STEP, 'sesskey' => sesskey(), 'actionstep' => $step->id]),
new pix_icon('t/down', $str['move_down']), $str['move_down'])
);
}
}
$step->actionmenu = $OUTPUT->render($actionmenu);
$displaysteps[] = $step;
}
// Popup courses list.
$out = null;
$ncourses = 0;
$courseids = [];
$hiddenfieldssearch = [];
$hiddenfieldssearch[] = ['name' => 'wf', 'value' => $workflowid];
$hiddenfieldssearch[] = ['name' => 'showdetails', 'value' => $showdetails];
if ($stepid) { // Display courses table with courses of this step.
$step = step_manager::get_step_instance($stepid);
$ncourses = $DB->count_records('tool_lifecycle_process',
['stepindex' => $step->sortindex, 'workflowid' => $workflowid]);
$table = new courses_in_step_table($step,
optional_param('courseid', null, PARAM_INT), $ncourses, $search);
ob_start();
$table->out(PAGESIZE, false);
$out = ob_get_contents();
ob_end_clean();
$hiddenfieldssearch[] = ['name' => 'step', 'value' => $stepid];
} else if ($triggerid) { // Display courses table with triggered courses of this trigger.
$trigger = trigger_manager::get_instance($triggerid);
if ($courseids = (new processor())->get_triggercourses($trigger, $workflow)) {
$table = new triggered_courses_table($courseids, 'triggered', $trigger->instancename,
null, null, $search);
ob_start();
$table->out(PAGESIZE, false);
$out = ob_get_contents();
ob_end_clean();
$hiddenfieldssearch[] = ['name' => 'trigger', 'value' => $triggerid];
}
} else if ($triggered) { // Display courses table with triggered courses of this workflow.
$table = new triggered_courses_table($coursestriggered, 'triggeredworkflow', null,
$workflow->title, null, $search);
ob_start();
$table->out(PAGESIZE, false);
$out = ob_get_contents();
ob_end_clean();
$hiddenfieldssearch[] = ['name' => 'triggered', 'value' => $triggered];
} else if ($excluded) { // Display courses table with excluded courses of this trigger.
$trigger = trigger_manager::get_instance($excluded);
if ($courseids = (new processor())->get_triggercourses($trigger, $workflow)) {
$table = new triggered_courses_table($courseids, 'exclude', $trigger->instancename, null, null, $search);
ob_start();
$table->out(PAGESIZE, false);
$out = ob_get_contents();
ob_end_clean();
$hiddenfieldssearch[] = ['name' => 'excluded', 'value' => $excluded];
}
} else if ($delayed) { // Display courses table with courses delayed for this workflow.
if ($courseids = (new processor())->get_courses_delayed_for_workflow($workflowid)) {
$table = new triggered_courses_table( $courseids, 'delayed',
null, $workflow->title, $workflowid, $search);
ob_start();
$table->out(PAGESIZE, false);
$out = ob_get_contents();
ob_end_clean();
$hiddenfieldssearch[] = ['name' => 'delayed', 'value' => $delayed];
}
} else if ($used) { // Display courses triggered by this workflow but involved in other processes already.
if ($courseids = $amounts['all']->used ?? null) {
$table = new triggered_courses_table( $courseids, 'used',
null, $workflow->title, $workflowid, $search);
ob_start();
$table->out(PAGESIZE, false);
$out = ob_get_contents();
ob_end_clean();
$hiddenfieldssearch[] = ['name' => 'used', 'value' => $used];
}
}
// Search box for courses list.
$searchhtml = '';
if ((intval($courseids) + intval($ncourses)) > PAGESIZE ) {
$searchhtml = $renderer->render_from_template('tool_lifecycle/search_input', [
'action' => (new moodle_url(urls::WORKFLOW_DETAILS))->out(false),
'uniqid' => 'tool_lifecycle-search-courses',
'inputname' => 'search',
'extraclasses' => 'ml-3 mt-3',
'inform' => false,
'searchstring' => get_string('searchcourses', 'tool_lifecycle'),
'query' => $search,
'hiddenfields' => $hiddenfieldssearch,
]);
}
$data = [
'editsettingslink' => (new moodle_url(urls::EDIT_WORKFLOW, ['wf' => $workflow->id]))->out(false),
'title' => $workflow->title,
'rollbackdelay' => format_time($workflow->rollbackdelay),
'finishdelay' => format_time($workflow->finishdelay),
'delayglobally' => $workflow->delayforallworkflows,
'trigger' => $displaytriggers,
'timetrigger' => $displaytimetriggers,
'counttriggers' => count($displaytriggers),
'counttimetriggers' => count($displaytimetriggers),
'showcoursecounts' => $showdetails,
'steps' => $displaysteps,
'popuplink' => $popuplink,
'nosteplink' => $nosteplink,
'table' => $out,
'workflowid' => $workflowid,
'search' => $searchhtml,
'classdetails' => $classdetails,
'includedelayedcourses' => $workflow->includedelayedcourses,
'includesitecourse' => $workflow->includesitecourse,
'showdetails' => $showdetails,
'showdetailslink' => $showdetailslink,
'showdetailsicon' => $showdetails == 0,
'isactive' => $isactive || $isdeactivated,
'nextrun' => $nextrun,
'lastrun' => userdate($lastrun, get_string('strftimedatetimeshort', 'langconfig')),
'nomanualtriggerinvolved' => $nomanualtriggerinvolved,
];
if ($showdetails) {
// The triggers total box.
$data['displaytotaltriggered'] = $displaytotaltriggered;
$triggered = $amounts['all']->triggered ?? 0;
$triggeredhtml = $triggered > 0 ? html_writer::span($triggered, 'text-success font-weight-bold') : 0;
$data['coursestriggered'] = $triggeredhtml;
$data['coursestriggeredcount'] = $triggered;
if ($triggered) {
// Count delayed total, displayed in mustache only if there are any.
$delayed = count($amounts['all']->delayedcourses); // Matters only if delayedcourses are not included in workflow.
$delayedlink = new moodle_url($popuplink, ['delayed' => $workflowid]);
$delayedhtml = $delayed > 0 ? html_writer::link($delayedlink, $delayed,
['class' => 'text-warning btn btn-outline-warning']) : 0;
$data['coursesdelayed'] = $delayedhtml;
// Count in other processes used courses total, displayed in mustache only if there are any.
$used = count($amounts['all']->used) ?? 0;
$usedlink = new moodle_url($popuplink, ['used' => "1"]);
$usedhtml = $used > 0 ? html_writer::link($usedlink, $used,
['class' => 'btn btn-outline-secondary']) : 0;
$data['coursesused'] = $usedhtml;
}
}
$addtriggerselect = "";
$addstepselect = "";
$activate = "";
$newworkflow = false;
// Box to add triggers or steps to workflow by use of select fields.
if (workflow_manager::is_editable($workflow->id)) {
// Add trigger select field.
$triggertypes = trigger_manager::get_chooseable_trigger_types();
$selectabletriggers = [];
foreach ($triggertypes as $triggertype => $triggername) {
if ($triggers) {
foreach ($triggers as $workflowtrigger) {
if ($triggertype == $workflowtrigger->subpluginname) {
continue 2;
}
}
} else {
// After workflow creation only provide course selection (and manual) triggers for the adding a trigger selection field.
$lib = lib_manager::get_trigger_lib($triggertype);
if (!$lib->is_manual_trigger()) {
if ($lib->check_course(null, null) == trigger_response::triggertime()) {
continue;
}
}
$newworkflow = true;
}
$selectabletriggers[$triggertype] = $triggername;
}
$addtriggerselect = $OUTPUT->single_select(new \moodle_url(urls::EDIT_ELEMENT,
['type' => settings_type::TRIGGER, 'wf' => $workflow->id]),
'subplugin', $selectabletriggers, '', ['' => get_string('add_new_trigger_instance', 'tool_lifecycle')],
null, ['id' => 'tool_lifecycle-choose-trigger']);
// Add step selection field.
if (!$newworkflow) { // At first select a course selection trigger, than you can select the first step.
if ($steptypes = step_manager::get_step_types()) {
$addstepselect = $OUTPUT->single_select(new \moodle_url(urls::EDIT_ELEMENT,
['type' => settings_type::STEP, 'wf' => $workflow->id]),
'subplugin', $steptypes, '', ['' => get_string('add_new_step_instance', 'tool_lifecycle')],
null, ['id' => 'tool_lifecycle-choose-step']);
}
if ($id == 'workflowdrafts') {
if ($triggers && $steps) { // At least one trigger and one step is necessary to activate the draft workflow.
$activate = $OUTPUT->single_button(new \moodle_url(urls::ACTIVE_WORKFLOWS,
[
'action' => action::WORKFLOW_ACTIVATE,
'sesskey' => sesskey(),
'workflowid' => $workflow->id,
'backtooverview' => '1',
]),
get_string('activateworkflow', 'tool_lifecycle'));
} else {
$activate = get_string('invalid_workflow', 'tool_lifecycle').
$OUTPUT->pix_icon('i/circleinfo', get_string('invalid_workflow_details', 'tool_lifecycle'));
}
}
}
$data['addtriggerselect'] = $addtriggerselect;
$data['addstepselect'] = $addstepselect;
$data['activate'] = $activate;
$data['newworkflow'] = $newworkflow;
}
echo $OUTPUT->render_from_template('tool_lifecycle/workflowoverview', $data);
echo $renderer->footer();