-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlocallib.php
413 lines (365 loc) · 14.9 KB
/
locallib.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
<?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/>.
/**
* Workflow block library code. This file defines some miscellaneous things, and
* then includes all the workflow classes.
*
* @package block_workflow
* @copyright 2011 Lancaster University Network Services Limited
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
require_once($CFG->dirroot . '/blocks/workflow/classes/exceptions.php');
require_once($CFG->dirroot . '/blocks/workflow/classes/workflow.php');
require_once($CFG->dirroot . '/blocks/workflow/classes/step.php');
require_once($CFG->dirroot . '/blocks/workflow/classes/step_state.php');
require_once($CFG->dirroot . '/blocks/workflow/classes/email.php');
require_once($CFG->dirroot . '/blocks/workflow/classes/todo.php');
require_once($CFG->dirroot . '/blocks/workflow/classes/command.php');
require_once($CFG->dirroot . '/blocks/workflow/classes/command_assignrole.php');
require_once($CFG->dirroot . '/blocks/workflow/classes/command_email.php');
require_once($CFG->dirroot . '/blocks/workflow/classes/command_override.php');
require_once($CFG->dirroot . '/blocks/workflow/classes/command_setactivitysetting.php');
require_once($CFG->dirroot . '/blocks/workflow/classes/command_setactivityvisibility.php');
require_once($CFG->dirroot . '/blocks/workflow/classes/command_setcoursevisibility.php');
require_once($CFG->dirroot . '/blocks/workflow/classes/command_setactivitylinkedsetting.php');
/** @var string An active state for a step_state. */
define('BLOCK_WORKFLOW_STATE_ACTIVE', 'active');
/** @var string A completed state for a step_state. */
define('BLOCK_WORKFLOW_STATE_COMPLETED', 'completed');
/** @var string An aborted state for a step_state. */
define('BLOCK_WORKFLOW_STATE_ABORTED', 'aborted');
/** @var int The enabled state for a workflow. */
define('BLOCK_WORKFLOW_ENABLED', 0);
/** @var int The obsolste state for a workflow. */
define('BLOCK_WORKFLOW_OBSOLETE', 1);
/** @var int The maximum comment length to be disapled in block. */
define('BLOCK_WORKFLOW_MAX_COMMENT_LENGTH', 200);
/**
* Return an list of all of the workflows ordered by obsolete status, then appliesto, and finally
* the shortname
*
* @package block
* @subpackage workflow
* @copyright 2011 Lancaster University Network Services Limited
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @return array Containing a list of the all workflows
*/
function block_workflow_load_workflows() {
global $DB;
return $DB->get_records('block_workflow_workflows', null, 'obsolete ASC, appliesto ASC, shortname ASC');
}
/**
* Return the list of modules that workflows may apply to (appliesto)
*
* The list contains course as the first item, plus every installed plugin
* as returned by {@link get_plugin_list}.
*
* @package block
* @subpackage workflow
* @copyright 2011 Lancaster University Network Services Limited
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @return array Associative array to fill an appliesto select
*/
function block_workflow_appliesto_list() {
// Applies to should contain courses ...
$return = array('course' => get_string('course'));
// ... and any installed modules.
$mods = get_plugin_list('mod');
foreach ($mods as $name => $path) {
$return[$name] = get_string('pluginname', 'mod_' . $name);
}
return $return;
}
/**
* Return the formatted language string for the specified $appliesto
*
* @package block
* @subpackage workflow
* @copyright 2011 Lancaster University Network Services Limited
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @param string $appliesto The language key
* @return string The formatted version for the $appliesto
*/
function block_workflow_appliesto($appliesto) {
if ($appliesto == 'course') {
return get_string($appliesto);
}
return get_string('pluginname', 'mod_' . $appliesto);
}
/**
* Returns a list of the roles available at the specified contextlevel
*
* @package block
* @subpackage workflow
* @copyright 2011 Lancaster University Network Services Limited
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @param string $contextlevel The contextlevel
* @return mixed The database results, or null if no result was found
*/
function block_workflow_contextlevel_roles($contextlevel) {
global $DB;
$sql = "SELECT DISTINCT roles.*
FROM {role_context_levels} cl
INNER JOIN {role} roles ON roles.id = cl.roleid
WHERE cl.contextlevel = ?
ORDER BY roles.sortorder ASC
";
return role_fix_names($DB->get_records_sql($sql, array($contextlevel)));
}
/**
* Return an array of the default editor options to use for the standard moodle html editor
*
* @package block
* @subpackage workflow
* @copyright 2011 Lancaster University Network Services Limited
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @return array Containing a list of default properties
*/
function block_workflow_editor_options() {
$options = array();
// Disallow files.
$options['maxfiles'] = 0;
$options['autosave'] = false;
return $options;
}
/**
* Return a human-readable string to describe the editor format
*
* @package block
* @subpackage workflow
* @copyright 2011 Lancaster University Network Services Limited
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @param int The editor format
* @return string The human-readable string format
*/
function block_workflow_editor_format($type) {
switch ($type) {
case FORMAT_HTML:
return get_string('format_html', 'block_workflow');
case FORMAT_PLAIN:
return get_string('format_plain', 'block_workflow');
default:
return get_string('format_unknown', 'block_workflow');
}
}
/**
* Coverts human-readable string to editor format, used in importing
*
* @package block
* @subpackage workflow
* @copyright 2011 Lancaster University Network Services Limited
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @param string The human-readable string format
* @return int The editor format
*/
function block_workflow_convert_editor_format($format) {
$knownformats = array(
get_string('format_html', 'block_workflow') => FORMAT_HTML,
get_string('format_plain', 'block_workflow') => FORMAT_PLAIN,
);
if (isset($knownformats[$format])) {
return $knownformats[$format];
} else {
throw new block_workflow_exception(get_string('invalidformat', 'block_workflow', $format));
}
}
/**
* Check whether the current user can make changes to the specified state
*
* That is to say, that ths current user has either the workflow:dostep
* permission, or is listed in the step roles for the specified state step
*
* @param object $state The step_state object
* @return boolean Whether or not the user has permission
*/
function block_workflow_can_make_changes($state) {
global $USER;
static $canmakechanges = array();
$context = $state->context();
if (isset($canmakechanges[$context->id][$state->id])) {
return $canmakechanges[$context->id][$state->id];
} else {
$canmakechanges[$context->id][$state->id] = false;
}
if (has_capability('block/workflow:dostep', $context)) {
$canmakechanges[$context->id][$state->id] = true;
return $canmakechanges[$context->id][$state->id];
}
foreach ($state->step()->roles() as $role) {
if (user_has_role_assignment($USER->id, $role->id, $context->id)) {
$canmakechanges[$context->id][$state->id] = true;
return $canmakechanges[$context->id][$state->id];
}
}
return $canmakechanges[$context->id][$state->id];
}
/**
* Return all active steps which are set for 'extranotify' or 'autofinish'
*
* @param array $stepoptions An array of params to check whther we are looking for 'extranotify' or 'autofinish'
* @return array
*/
function block_workflow_get_active_steps_with_fields_not_null($stepoptions) {
global $DB;
list($offsettype, $offset, $textarea) = $stepoptions;
$where = $textarea ? ' AND step.onextranotifyscript IS NOT NULL ' : '';
$sql = "SELECT state.id AS stateid, state.stepid, state.state,
wf.id AS workflowid, wf.appliesto,
step.name AS stepname, step.$offsettype, step.$offset,
c.id AS courseid, c.shortname AS courseshortname,
cm.instance AS moduleid
FROM {block_workflow_step_states} state
LEFT JOIN {block_workflow_steps} step ON step.id = state.stepid
LEFT JOIN {block_workflow_workflows} wf ON wf.id = step.workflowid
LEFT JOIN {context} ctx ON ctx.id = state.contextid
LEFT JOIN {course} c ON c.id = ctx.instanceid
LEFT JOIN {course_modules} cm ON cm.id = ctx.instanceid AND wf.appliesto <> 'course'
WHERE step.$offsettype IS NOT NULL $where
AND state.state = :state
AND (ctx.contextlevel = :coursecotext OR ctx.contextlevel = :modulecontext)
ORDER BY state.id ASC";
$options = array('state' => BLOCK_WORKFLOW_STATE_ACTIVE,
'coursecotext' => CONTEXT_COURSE,
'modulecontext' => CONTEXT_MODULE);
return $DB->get_records_sql($sql, $options);
}
/**
* Return the timestamp for 'extranotify' or 'autofinish'
* @param string $courseshortname
* @param int $courseid
* @param int $moduleid
* @param string $offsettype
* @param int $offset
* @return int
*/
function block_workflow_get_offset_time($courseshortname, $courseid, $moduleid, $offsettype, $offset) {
global $DB;
list($dbtable, $dbfield) = explode(';', $offsettype);
if ($dbtable === 'vl_v_crs_version_pres') {
$table = \local_oudataload\util::table('vl_v_crs_version_pres');
$date = $DB->get_field_sql("
SELECT MIN($dbfield)
FROM $table
WHERE vle_course_short_name = ?
", array($courseshortname));
$timestamp = 0;
if ($date) {
$timestamp = strtotime($date);
}
} else if ($dbtable === 'course') {
$timestamp = $DB->get_field('course', $dbfield, array('id' => $courseid));
} else {
$timestamp = $DB->get_field($dbtable, $dbfield, array('id' => $moduleid));
}
if ($timestamp) {
return $timestamp + $offset;
} else {
return null;
}
}
/**
* Send email notifications.
* @return void
*/
function block_workflow_send_extra_notification() {
$options = array('extranotify', 'extranotifyoffset', 'onextranotifyscript');
$activesteps = block_workflow_get_active_steps_with_fields_not_null($options);
if (!$activesteps) {
return;
}
$now = time();
foreach ($activesteps as $key => $activestep) {
try {
$notificationtime = block_workflow_get_offset_time($activestep->courseshortname,
$activestep->courseid, $activestep->moduleid, $activestep->extranotify, $activestep->extranotifyoffset);
// Is is the time to notify?
if ($notificationtime && $notificationtime < $now) {
$state = new block_workflow_step_state($activestep->stateid);
if ($state->step()->onextranotifyscript) {
$state->step()->process_script($state, $state->step()->onextranotifyscript);
// Trigger an event for the extra notification.
$event = \block_workflow\event\step_extra_notification_processed::create_from_step_state($state);
$event->trigger();
}
// Cron setup user.
cron_setup_user();
}
} catch (Exception $e) {
block_workflow_report_scheduled_task_error('send extra notifications', $e, $activestep);
}
}
}
/**
* Finish the step automatically.
* @return void
*/
function block_workflow_autofinish_steps() {
$options = array('autofinish', 'autofinishoffset', null);
$activesteps = block_workflow_get_active_steps_with_fields_not_null($options);
if (!$activesteps) {
return;
}
$now = time();
foreach ($activesteps as $key => $activestep) {
try {
$autofinishtime = block_workflow_get_offset_time($activestep->courseshortname,
$activestep->courseid, $activestep->moduleid, $activestep->autofinish, $activestep->autofinishoffset);
// Is is the time to finish the step automatically?
if ($autofinishtime < $now) {
// Add a comment and finish the step automatically.
$newcomment = get_string('finishstepautomatically', 'block_workflow',
date('H:i:s') . ' on ' . date('jS \of F Y'));
$state = new block_workflow_step_state($activestep->stateid);
$state->finish_step($newcomment, FORMAT_HTML);
// Cron setup user.
cron_setup_user();
}
} catch (Exception $e) {
block_workflow_report_scheduled_task_error(
'automatic step finisher', $e, $activestep);
}
}
}
/**
* Helper function used by both {@link block_workflow_autofinish_steps()}
* and {@link block_workflow_send_extra_notification()}.
*
* @param string $taskname which task - used in the debug output.
* @param Exception $e the exception.
* @param $activestep if this relates to a particular step state.
* Only the $activestep->stateid field is used (currently).
*/
function block_workflow_report_scheduled_task_error($taskname, Exception $e, $activestep = null) {
$message = "Workflow task $taskname failed: " . $e->getMessage() . " at " . date('H:i:s');
if ($activestep) {
$message .= ' while processing step state ' . $activestep->stateid;
}
mtrace($message);
if (!empty($e->debuginfo)) {
mtrace("Debug info:");
mtrace($e->debuginfo);
}
mtrace("Backtrace:");
mtrace(format_backtrace($e->getTrace(), true));
}