Skip to content

Commit a8fd530

Browse files
committed
implement checks for plugin dependencies
1 parent 8401478 commit a8fd530

File tree

5 files changed

+112
-73
lines changed

5 files changed

+112
-73
lines changed

classes/processor.php

Lines changed: 71 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
use tool_lifecycle\local\response\step_response;
4444
use tool_lifecycle\local\response\trigger_response;
4545

46+
require_once(__DIR__ . '/../locallib.php');
47+
4648
/**
4749
* Offers functionality to trigger, process and finish lifecycle processes.
4850
*
@@ -56,24 +58,23 @@ class processor {
5658
* Processes the trigger plugins for all relevant courses.
5759
*/
5860
public function call_trigger() {
59-
global $FULLSCRIPT, $CFG, $USER;
61+
global $FULLSCRIPT, $USER;
6062

61-
$run = str_contains($FULLSCRIPT, 'run.php'); // Called by run-command of workflowoverview?
62-
// Debug mode if admin setting debug is active and function is not called within a behat test.
63-
$debug = $run && $CFG->debugdeveloper && !defined('BEHAT_SITE_RUNNING');
63+
$eol = "\n";
64+
// HTML end of line if called by run-command of workflowoverview.
65+
if ($run = str_contains($FULLSCRIPT, 'run.php')) {
66+
$eol = "<br>";
67+
}
68+
$automatictest = (defined('PHPUNIT_TEST') && PHPUNIT_TEST) ||
69+
defined('BEHAT_SITE_RUNNING');
6470

6571
// Only active workflows that are not manual workflows.
6672
$activeworkflows = workflow_manager::get_active_automatic_workflows();
6773

6874
// Print debug message if this is not a behat test.
69-
if (!defined('BEHAT_SITE_RUNNING')) {
70-
if ($run) {
71-
echo \html_writer::div(get_string ('active_workflows_header_title', 'tool_lifecycle').
72-
": ".count($activeworkflows));
73-
} else {
74-
mtrace(get_string ('active_workflows_header_title', 'tool_lifecycle').
75-
": ".count($activeworkflows));
76-
}
75+
if (!$automatictest) {
76+
mtrace(get_string ('active_automatic_workflows_heading', 'tool_lifecycle').
77+
": ".count($activeworkflows), $eol);
7778
}
7879

7980
// Walk through the active workflows.
@@ -84,20 +85,43 @@ public function call_trigger() {
8485
$exclude = [];
8586

8687
// Print debug message if this is not a behat test.
87-
if (!defined('BEHAT_SITE_RUNNING')) {
88-
if ($run) {
89-
echo \html_writer::div('Calling triggers for workflow "' . $workflow->title . '" '.
90-
userdate(time(), get_string('strftimedatetimeaccurate'),
91-
core_date::get_user_timezone($USER)));
92-
} else {
93-
mtrace('Calling triggers for workflow "' . $workflow->title . '" '.
94-
userdate(time(), get_string('strftimedatetimeaccurate'),
95-
core_date::get_user_timezone($USER)));
96-
}
88+
if (!$automatictest) {
89+
mtrace('Calling triggers for workflow "' . $workflow->title . '" '.
90+
userdate(time(), get_string('strftimedatetimeaccurate'), core_date::get_user_timezone($USER)),
91+
$eol);
9792
}
9893

9994
// Get workflow triggers and settings.
10095
$triggers = trigger_manager::get_triggers_for_workflow($workflow->id);
96+
foreach ($triggers as $trigger) {
97+
// Check if plugin dependencies are met.
98+
if ($trigger->subpluginname == 'customfieldsemester') {
99+
if (lifecycle_is_plugin_installed('semester', 'customfield') === false) {
100+
if ($run) {
101+
mtrace(get_string('workflownotvalid', 'tool_lifecycle')." ".
102+
get_string('customfieldsemesternotinstalled',
103+
'tool_lifecycle', "customfieldsemester"), $eol);
104+
}
105+
continue 2;
106+
}
107+
} else if ($trigger->subpluginname == 'semindependent') {
108+
$nosemester =
109+
settings_manager::get_settings($trigger->id, settings_type::TRIGGER)['nosemester'] ?? false;
110+
if ($nosemester) {
111+
if (lifecycle_is_plugin_installed('semester', 'customfield') === false) {
112+
if (lifecycle_is_plugin_installed('semester', 'customfield') === false) {
113+
if ($run) {
114+
mtrace(get_string('workflownotvalid', 'tool_lifecycle')." ".
115+
get_string('customfieldsemesternotinstalled',
116+
'tool_lifecycle', "semindependent"), $eol);
117+
}
118+
continue 2;
119+
}
120+
}
121+
}
122+
}
123+
}
124+
101125
if (!$workflow->includesitecourse) {
102126
$exclude[] = 1;
103127
}
@@ -116,17 +140,17 @@ public function call_trigger() {
116140
$lib = lib_manager::get_automatic_trigger_lib($trigger->subpluginname);
117141
$response = $lib->check_course($course, $trigger->id);
118142
if ($response == trigger_response::next()) {
119-
if ($debug) {
120-
echo \html_writer::div("Course next: $course->id");
143+
if ($run) {
144+
mtrace("Course next: $course->id", $eol);
121145
}
122146
$recordset->next();
123147
continue 2;
124148
}
125149
if ($response == trigger_response::exclude()) {
126150
array_push($exclude, $course->id);
127151
$countexcluded++;
128-
if ($debug) {
129-
echo \html_writer::div("Course exclude: $course->id");
152+
if ($run) {
153+
mtrace("Course exclude: $course->id", $eol);
130154
}
131155
$recordset->next();
132156
continue 2;
@@ -136,22 +160,16 @@ public function call_trigger() {
136160
$process = process_manager::create_process($course->id, $workflow->id);
137161
process_triggered::event_from_process($process)->trigger();
138162
$counttriggered++;
139-
if ($debug) {
140-
echo \html_writer::div("Course triggered: $course->id");
163+
if ($run) {
164+
mtrace("Course triggered: $course->id", $eol);
141165
}
142166
$recordset->next();
143167
}
144168
// Final debug messages if this is not a behat test.
145-
if (!defined('BEHAT_SITE_RUNNING')) {
146-
if ($run) {
147-
echo \html_writer::div(" $countcourses courses processed.");
148-
echo \html_writer::div(" $counttriggered courses triggered.");
149-
echo \html_writer::div(" $countexcluded courses excluded.");
150-
} else {
151-
mtrace(" $countcourses courses processed.");
152-
mtrace(" $counttriggered courses triggered.");
153-
mtrace(" $countexcluded courses excluded.");
154-
}
169+
if (!$automatictest) {
170+
mtrace(" $countcourses courses processed.", $eol);
171+
mtrace(" $counttriggered courses triggered.", $eol);
172+
mtrace(" $countexcluded courses excluded.", $eol);
155173
}
156174
}
157175
}
@@ -160,19 +178,18 @@ public function call_trigger() {
160178
* Calls the process_course() method of each step submodule currently responsible for a given course.
161179
*/
162180
public function process_courses() {
163-
global $FULLSCRIPT, $CFG;
181+
global $FULLSCRIPT;
164182

165-
$run = str_contains($FULLSCRIPT, 'run.php');
183+
$eol = "\n";
184+
// HTML end of line if called by run-command of workflowoverview.
185+
if ($run = str_contains($FULLSCRIPT, 'run.php')) {
186+
$eol = "<br>";
187+
}
166188
$automatictest = (defined('PHPUNIT_TEST') && PHPUNIT_TEST) ||
167189
defined('BEHAT_SITE_RUNNING');
168-
$debug = $run && $CFG->debugdeveloper && !$automatictest;
169190

170191
if (!$automatictest) {
171-
if ($run) {
172-
echo \html_writer::div(get_string ('lifecycle_task', 'tool_lifecycle'));
173-
} else {
174-
mtrace(get_string ('lifecycle_task', 'tool_lifecycle'));
175-
}
192+
mtrace(get_string ('lifecycle_task', 'tool_lifecycle'), $eol);
176193
}
177194
$coursesprocessed = 0;
178195
$coursesprocesserrors = 0;
@@ -207,25 +224,25 @@ public function process_courses() {
207224
}
208225
if ($result == step_response::waiting()) {
209226
process_manager::set_process_waiting($process);
210-
if ($debug) {
211-
echo \html_writer::div("Course processed: $course->id - Result: Waiting");
227+
if ($run) {
228+
mtrace("Course processed: $course->id - Result: Waiting", $eol);
212229
}
213230
break;
214231
} else if ($result == step_response::proceed()) {
215232
if (!process_manager::proceed_process($process)) {
216233
delayed_courses_manager::set_course_delayed_for_workflow($course->id,
217234
false, $process->workflowid);
218235
}
219-
if ($debug) {
220-
echo \html_writer::div("Course processed: $course->id - Result: Proceed");
236+
if ($run) {
237+
mtrace("Course processed: $course->id - Result: Proceed", $eol);
221238
}
222239
break;
223240
} else if ($result == step_response::rollback()) {
224241
delayed_courses_manager::set_course_delayed_for_workflow($course->id,
225242
true, $process->workflowid);
226243
process_manager::rollback_process($process);
227-
if ($debug) {
228-
echo \html_writer::div("Course processed: $course->id - Result: Rollback");
244+
if ($run) {
245+
mtrace("Course processed: $course->id - Result: Rollback", $eol);
229246
}
230247
break;
231248
} else {
@@ -234,13 +251,8 @@ public function process_courses() {
234251
}
235252
}
236253
if (!$automatictest) {
237-
if ($run) {
238-
echo \html_writer::div(" $coursesprocessed courses processed.");
239-
echo \html_writer::div(" $coursesprocesserrors ".get_string('errors', 'search').".");
240-
} else {
241-
mtrace(" $coursesprocessed courses processed.");
242-
mtrace(" $coursesprocesserrors ".get_string('errors', 'search').".");
243-
}
254+
mtrace(" $coursesprocessed courses processed.", $eol);
255+
mtrace(" $coursesprocesserrors ".get_string('errors', 'search').".", $eol);
244256
}
245257

246258
}

lang/de/tool_lifecycle.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@
106106
$string['coursestriggeredworkflow'] = 'Getriggerte Kurse für diesen Workflow \'{$a->title}\'';
107107
$string['coursesused'] = 'Kurse bereits in der Verarbeitung eines anderen Workflows';
108108
$string['create_copy'] = 'Kopie erstellen';
109-
$string['create_step'] = 'Step erstellen';
109+
$string['create_step'] = 'Schritt erstellen';
110110
$string['create_trigger'] = 'Trigger erstellen';
111111
$string['create_workflow_from_existing'] = 'Kopie von bestehendem Workflow erstellen';
112-
$string['date'] = 'Fällligkeitsdatum';
112+
$string['customfieldsemesternotinstalled'] = 'Der Trigger {$a} benötigt das Plugin customfield_semester, das nicht installiert ist.';
113+
$string['date'] = 'Fälligkeitsdatum';
113114
$string['deactivated'] = 'Deaktiviert';
114115
$string['deactivated_workflows_header'] = 'Inaktive';
115116
$string['deactivated_workflows_header_title'] = 'Liste der deaktivierten Workflows';
@@ -325,6 +326,7 @@
325326
$string['workflow_trigger'] = 'Trigger für den Workflow';
326327
$string['workflow_was_not_imported'] = 'Der Workflow wurde nicht importiert!';
327328
$string['workflownotfound'] = 'Es konnte kein Workflow mit der ID {$a} gefunden werden!';
329+
$string['workflownotvalid'] = 'Dieser workflow ist nicht gültig.';
328330
$string['workflowoverview'] = 'Workflow ansehen';
329331
$string['workflowoverview_list_header'] = 'Details zu Workflows';
330332
$string['workflowsettings'] = 'Workflow Einstellungen';

lang/en/tool_lifecycle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
$string['create_step'] = 'Create step';
111111
$string['create_trigger'] = 'Create trigger';
112112
$string['create_workflow_from_existing'] = 'Copy new workflow from existing';
113+
$string['customfieldsemesternotinstalled'] = 'The trigger {$a} depends on the plugin customfield_semester, which is not installed.';
113114
$string['date'] = 'Due date';
114115
$string['deactivated'] = 'Deactivated';
115116
$string['deactivated_workflows_header'] = 'Deactivated';
@@ -327,6 +328,7 @@
327328
$string['workflow_trigger'] = 'Trigger for the workflow';
328329
$string['workflow_was_not_imported'] = 'The workflow was not imported!';
329330
$string['workflownotfound'] = 'Workflow with id {$a} could not be found';
331+
$string['workflownotvalid'] = 'This workflow is not valid.';
330332
$string['workflowoverview'] = 'View workflow';
331333
$string['workflowoverview_list_header'] = 'Details of Workflows';
332334
$string['workflowsettings'] = 'Workflow settings';

subplugins.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,11 @@
6363
if ($plugindescription) {
6464
echo html_writer::start_div().$plugindescription;
6565
if ($trigger == 'customfieldsemester') {
66-
if (lifecycle_is_plugin_installed('semester', 'customfield') === false) {
66+
if (!lifecycle_is_plugin_installed('semester', 'customfield') === false) {
6767
echo \html_writer::span(
68-
get_string('customfieldsemesternotinstalled', 'lifecycletrigger_customfieldsemester'),
69-
'text-danger');
68+
get_string('customfieldsemesternotinstalled', 'tool_lifecycle', "customfieldsemester"),
69+
'text-danger ml-1');
7070
}
71-
7271
}
7372
echo html_writer::end_div();
7473
}

workflowoverview.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
require_once(__DIR__ . '/../../../config.php');
2727
require_once($CFG->libdir . '/adminlib.php');
28+
require_once(__DIR__ . '/locallib.php');
2829

2930
require_admin();
3031

@@ -67,6 +68,7 @@
6768
$showdetails = optional_param('showdetails', 0, PARAM_INT);
6869
$showsql = optional_param('showsql', 0, PARAM_INT);
6970
$showtablesql = optional_param('showtablesql', 0, PARAM_INT);
71+
7072
$debugtriggersql = "";
7173
$debugtablesql = "";
7274
if ($showdetails == 0) {
@@ -194,6 +196,29 @@
194196

195197
$steps = step_manager::get_step_instances($workflow->id);
196198
$triggers = trigger_manager::get_triggers_for_workflow($workflow->id);
199+
foreach ($triggers as $trigger) {
200+
// Check if plugin dependencies are met.
201+
if ($trigger->subpluginname == 'customfieldsemester') {
202+
if (lifecycle_is_plugin_installed('semester', 'customfield') === false) {
203+
echo $OUTPUT->notification(get_string('workflownotvalid', 'tool_lifecycle')." ".
204+
get_string('customfieldsemesternotinstalled',
205+
'tool_lifecycle', "customfieldsemester"), 'error');
206+
echo $renderer->footer();
207+
die();
208+
}
209+
} else if ($trigger->subpluginname == 'semindependent') {
210+
$nosemester = settings_manager::get_settings($trigger->id, settings_type::TRIGGER)['nosemester'] ?? false;
211+
if ($nosemester) {
212+
if (lifecycle_is_plugin_installed('semester', 'customfield') === false) {
213+
echo $OUTPUT->notification(get_string('workflownotvalid', 'tool_lifecycle')." ".
214+
get_string('customfieldsemesternotinstalled',
215+
'tool_lifecycle', "semindependent"), 'error');
216+
echo $renderer->footer();
217+
die();
218+
}
219+
}
220+
}
221+
}
197222

198223
$str = [
199224
'edit' => get_string('edit'),
@@ -210,8 +235,8 @@
210235
if ($showdetails) {
211236
/*
212237
* Preview of what courses would be triggered if the course selection ran now.
213-
* For each trigger the amount of the select statement without the courses already in this process will be count.
214-
* The amount of courses already in the process is shown as well.
238+
* For each trigger, the amount of the select statement without the courses already in this process will be counted.
239+
* The number of courses already in the process is shown as well.
215240
*/
216241
$amounts = (new processor())->get_count_of_courses_to_trigger_for_workflow($workflow);
217242
$coursestriggered = $amounts['all']->coursestriggered ?? 0;
@@ -255,9 +280,8 @@
255280
$displaytimetriggers = [];
256281
foreach ($triggers as $trigger) {
257282
// The array from the DB Function uses ids as keys.
258-
// Mustache cannot handle arrays which have other keys therefore a new array is build.
259-
// FUTURE: Nice to have Icon for each subplugin.
260-
$trigger = (object)(array) $trigger; // Cast to normal object to be able to set dynamic properties.
283+
// Mustache cannot handle arrays which have other keys. Therefore, a new array is build.
284+
$trigger = (object)(array) $trigger; // Cast to a normal object to be able to set dynamic properties.
261285
$editlink = new moodle_url(urls::EDIT_ELEMENT, ['type' => settings_type::TRIGGER, 'elementid' => $trigger->id]);
262286
$trigger->editlink = $editlink->out();
263287
$actionmenu = new action_menu([
@@ -403,14 +427,14 @@
403427
ob_end_clean();
404428
$hiddenfieldssearch[] = ['name' => 'trigger', 'value' => $triggerid];
405429
$tablecoursesamount = $amounts[$trigger->sortindex]->triggered;
406-
echo \html_writer::div(\html_writer::link(new \moodle_url(urls::WORKFLOW_DETAILS,
430+
$out .= \html_writer::div(\html_writer::link(new \moodle_url(urls::WORKFLOW_DETAILS,
407431
[
408432
"wf" => $workflowid,
409-
"trigger" => $triggerid,
433+
"showsql" => "1",
410434
"showtablesql" => "1",
411435
"showdetails" => "1",
412436
]),
413-
"XXYYZZ&nbsp;&nbsp;&nbsp;", ["class" => "text-muted fs-6 text-decoration-none"]));
437+
"&nbsp;&nbsp;&nbsp;", ["class" => "text-muted fs-6 text-decoration-none"]));
414438
}
415439
} else if ($excluded) { // Display courses table with excluded courses of this trigger.
416440
$trigger = trigger_manager::get_instance($excluded);

0 commit comments

Comments
 (0)