-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathprocessor.php
232 lines (216 loc) · 10.2 KB
/
processor.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
<?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/>.
/**
* Offers functionality to trigger, process and finish lifecycle processes.
*
* @package tool_lifecycle
* @copyright 2017 Tobias Reischmann WWU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_lifecycle;
use tool_lifecycle\local\entity\trigger_subplugin;
use tool_lifecycle\event\process_triggered;
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\lib_manager;
use tool_lifecycle\local\manager\workflow_manager;
use tool_lifecycle\local\manager\delayed_courses_manager;
use tool_lifecycle\local\response\step_interactive_response;
use tool_lifecycle\local\response\step_response;
use tool_lifecycle\local\response\trigger_response;
/**
* Offers functionality to trigger, process and finish lifecycle processes.
*
* @package tool_lifecycle
* @copyright 2017 Tobias Reischmann WWU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class processor {
/**
* Processes the trigger plugins for all relevant courses.
*/
public function call_trigger() {
$activeworkflows = workflow_manager::get_active_automatic_workflows();
$exclude = array();
foreach ($activeworkflows as $workflow) {
$countcourses = 0;
$counttriggered = 0;
$countexcluded = 0;
mtrace('Calling triggers for workflow "' . $workflow->title . '"');
$triggers = trigger_manager::get_triggers_for_workflow($workflow->id);
$delayedcourses = delayed_courses_manager::get_delayed_courses_for_workflow($workflow->id);
$recordset = $this->get_course_recordset($triggers, array_merge($exclude, $delayedcourses));
while ($recordset->valid()) {
$course = $recordset->current();
$countcourses++;
foreach ($triggers as $trigger) {
$lib = lib_manager::get_automatic_trigger_lib($trigger->subpluginname);
$response = $lib->check_course($course, $trigger->id);
if ($response == trigger_response::next()) {
$recordset->next();
continue 2;
}
if ($response == trigger_response::exclude()) {
array_push($exclude, $course->id);
$countexcluded++;
$recordset->next();
continue 2;
}
if ($response == trigger_response::trigger()) {
continue;
}
}
// If all trigger instances agree, that they want to trigger a process, we do so.
$process = process_manager::create_process($course->id, $workflow->id);
process_triggered::event_from_process($process)->trigger();
$counttriggered++;
$recordset->next();
}
mtrace(" $countcourses courses processed.");
mtrace(" $counttriggered courses triggered.");
mtrace(" $countexcluded courses excluded.");
}
}
/**
* Calls the process_course() method of each step submodule currently responsible for a given course.
*/
public function process_courses() {
foreach (process_manager::get_processes() as $process) {
$workflow = workflow_manager::get_workflow($process->workflowid);
while (true) {
try {
$course = get_course($process->courseid);
} catch (\dml_missing_record_exception $e) {
// Course no longer exists!
break;
}
if ($process->stepindex == 0) {
if (!process_manager::proceed_process($process)) {
// Happens for a workflow with no step.
delayed_courses_manager::set_course_delayed_for_workflow($course->id, false, $workflow);
break;
}
}
$step = step_manager::get_step_instance_by_workflow_index($process->workflowid, $process->stepindex);
$lib = lib_manager::get_step_lib($step->subpluginname);
mtrace("Processing '$step->subpluginname' step on course $course->id...");
try {
if ($process->waiting) {
$result = $lib->process_waiting_course($process->id, $step->id, $course);
} else {
$result = $lib->process_course($process->id, $step->id, $course);
}
} catch (\Exception $e) {
process_manager::insert_process_error($process, $e);
break;
}
if ($result == step_response::waiting()) {
process_manager::set_process_waiting($process);
break;
} else if ($result == step_response::proceed()) {
if (!process_manager::proceed_process($process)) {
delayed_courses_manager::set_course_delayed_for_workflow($course->id, false, $workflow);
break;
}
} else if ($result == step_response::rollback()) {
delayed_courses_manager::set_course_delayed_for_workflow($course->id, true, $workflow);
process_manager::rollback_process($process);
break;
} else {
throw new \moodle_exception('Return code \''. var_dump($result) . '\' is not allowed!');
}
}
}
}
/**
* In case we are in an interactive environment because the user is lead through the interactive interfaces
* of multiple steps, this function cares for a redirection and processing through these steps until we reach a
* no longer interactive state of the workflow.
*
* @param int $processid Id of the process
* @return boolean if true, interaction finished.
* If false, the current step is still processing and cares for displaying the view.
* @throws \coding_exception
* @throws \dml_exception
*/
public function process_course_interactive($processid) {
$process = process_manager::get_process_by_id($processid);
$step = step_manager::get_step_instance_by_workflow_index($process->workflowid, $process->stepindex + 1);
// If there is no next step, then proceed, which will delete/finish the process.
if (!$step) {
delayed_courses_manager::set_course_delayed_for_workflow($process->courseid, false, $process->workflowid);
process_manager::proceed_process($process);
return true;
}
if ($interactionlib = lib_manager::get_step_interactionlib($step->subpluginname)) {
// Actually proceed to the next step.
process_manager::proceed_process($process);
$response = $interactionlib->handle_interaction($process, $step);
switch ($response) {
case step_interactive_response::still_processing():
return false;
break;
case step_interactive_response::no_action():
break;
case step_interactive_response::proceed():
// In case of proceed, call recursively.
return $this->process_course_interactive($processid);
break;
case step_interactive_response::rollback():
delayed_courses_manager::set_course_delayed_for_workflow($process->courseid, true, $process->workflowid);
process_manager::rollback_process($process);
break;
}
return true;
}
return true;
}
/**
* Returns a record set with all relevant courses.
* Relevant means that there is currently no lifecycle process running for this course.
* @param trigger[] $triggers List of triggers, which will be asked for additional where requirements.
* @param int[] $exclude List of course id, which should be excluded from execution.
* @return \moodle_recordset with relevant courses.
* @throws \coding_exception
* @throws \dml_exception
*/
public function get_course_recordset($triggers, $exclude) {
global $DB;
$where = 'true';
$whereparams = array();
foreach ($triggers as $trigger) {
$lib = lib_manager::get_automatic_trigger_lib($trigger->subpluginname);
list($sql, $params) = $lib->get_course_recordset_where($trigger->id);
if (!empty($sql)) {
$where .= ' AND ' . $sql;
$whereparams = array_merge($whereparams, $params);
}
}
if (!empty($exclude)) {
list($insql, $inparams) = $DB->get_in_or_equal($exclude, SQL_PARAMS_NAMED);
$where .= " AND NOT {course}.id {$insql}";
$whereparams = array_merge($whereparams, $inparams);
}
$sql = 'SELECT {course}.* from {course} '.
'left join {tool_lifecycle_process} '.
'ON {course}.id = {tool_lifecycle_process}.courseid '.
'LEFT JOIN {tool_lifecycle_proc_error} pe ON {course}.id = pe.courseid ' .
'WHERE {tool_lifecycle_process}.courseid is null AND ' .
'pe.courseid IS NULL AND '. $where;
return $DB->get_recordset_sql($sql, $whereparams);
}
}