-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlib.php
160 lines (139 loc) · 5.7 KB
/
lib.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
<?php
/**
* Run scheduled tasks according to a cron spec. Based on
* http://docs.moodle.org/en/Development:Scheduled_Tasks_Proposal
*
* ELIS(TM): Enterprise Learning Intelligence Suite
* Copyright (C) 2008-2013 Remote-Learner.net Inc (http://www.remote-learner.net)
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package local_eliscore
* @author Remote-Learner.net Inc
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright (C) 2008-2013 Remote-Learner.net Inc (http://www.remote-learner.net)
*
*/
defined('MOODLE_INTERNAL') || die();
require_once(dirname(__FILE__).'/lib/setup.php');
define('ELIS_TASKS_CRONSECS', 10 * 60); // TBD: 10 min max total runtime (save 1 min for other cron?)
/**
* Run scheduled tasks according to a cron spec.
*
* uses $CFG, $DB
*/
function local_eliscore_cron() {
global $CFG, $DB;
require($CFG->dirroot.'/local/eliscore/lib/tasklib.php');
$timenow = time();
// get all tasks that are (over-)due
$params = array('timenow' => $timenow);
$tasks = $DB->get_recordset_select('local_eliscore_sched_tasks', 'nextruntime <= :timenow', $params, 'nextruntime ASC');
$numtasks = $DB->count_records_select('local_eliscore_sched_tasks', 'nextruntime <= :timenow', $params);
// Check if the maximum cron run time is overridden
$remtime = ELIS_TASKS_CRONSECS;
if (isset($CFG->elistaskscronsecs) && is_int($CFG->elistaskscronsecs) && 0 < $CFG->elistaskscronsecs) {
$remtime = $CFG->elistaskscronsecs;
}
if (empty($tasks) || !$tasks->valid()) {
return;
}
foreach ($tasks as $task) {
$starttime = microtime();
mtrace("Running {$task->callfunction}({$task->taskname}) from {$task->plugin}...");
if ($task->enddate !== null && $task->enddate < $timenow) {
mtrace('* Cancelling task: past end date');
$DB->delete_records('local_eliscore_sched_tasks', array('id' => $task->id));
--$numtasks;
continue;
}
// Check for blocking tasks.
if (!empty($task->blocked) && $timenow < $task->blocked) {
// Task is still running - do not start another instance of it.
mtrace("{$task->plugin}: Previous {$task->taskname} process has not yet completed - aborting!");
continue;
}
// FIXME: check if task is locked
// See if some other cron has already run the function while we were
// doing something else -- if so, skip it.
$nextrun = $DB->get_field('local_eliscore_sched_tasks', 'nextruntime', array('id' => $task->id));
if ($nextrun > $timenow) {
mtrace('* Skipped (someone else already ran it)');
--$numtasks;
continue;
}
// calculate the next run time
$newtask = new stdClass;
$newtask->id = $task->id;
$newtask->lastruntime = time();
$newtask->nextruntime = cron_next_run_time($newtask->lastruntime, (array)$task);
// see if we have any runs left
if ($task->runsremaining !== null) {
$newtask->runsremaining = $task->runsremaining - 1;
if ($newtask->runsremaining <= 0) {
mtrace('* Cancelling task: no runs left');
$DB->delete_records('local_eliscore_sched_tasks', array('id' => $task->id));
} else {
$DB->update_record('local_eliscore_sched_tasks', $newtask);
}
} else {
$DB->update_record('local_eliscore_sched_tasks', $newtask);
}
// load the file and call the function
if ($task->callfile) {
$callfile = $CFG->dirroot.$task->callfile;
if (!is_readable($callfile)) {
mtrace('* Skipped (file not found)');
--$numtasks;
continue;
}
require_once ($callfile);
}
$starttask = time();
$denom = ($numtasks > 0) ? $numtasks-- : 1; // prevent div by 0
$runtime = floor((float)$remtime / (float)$denom);
call_user_func(unserialize($task->callfunction), $task->taskname, $runtime);
$remtime -= time() - $starttask;
$difftime = microtime_diff($starttime, microtime());
mtrace("* {$difftime} seconds");
// TBD: exit if over cron processing time
if ($remtime <= 0) {
break;
}
}
}
/**
* Returns a persons full name.
*
* Wrapper for the Moodle fullname() function that ensures all user fields exist.
*
* @param stdClass $user A {@link $USER} object to get full name of.
* @param bool $override If true then the name will be firstname followed by lastname rather than adhering to fullnamedisplay.
* @return string
*/
function elis_fullname($user, $override = false) {
if ($user instanceof elis_data_object) {
$user = $user->to_object();
}
if (!isset($user->firstname) && !isset($user->lastname)) {
return '';
}
$allnames = get_all_user_name_fields();
foreach ($allnames as $allname) {
if (!property_exists($user, $allname)) {
$user->$allname = null;
}
}
return fullname($user, $override);
}