4343use tool_lifecycle \local \response \step_response ;
4444use 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 }
0 commit comments