3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * Cron job for reviewing and aggregating course completion criteria
23 * @copyright 2009 Catalyst IT Ltd
24 * @author Aaron Barnes <aaronb@catalyst.net.nz>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once $CFG->libdir.'/completionlib.php';
31 * Update user's course completion statuses
33 * First update all criteria completions, then
34 * aggregate all criteria completions and update
35 * overall course completions
39 function completion_cron() {
41 completion_cron_mark_started();
43 completion_cron_criteria();
45 completion_cron_completions();
49 * Mark users as started if the config option is set
53 function completion_cron_mark_started() {
57 mtrace('Marking users as started');
61 * A quick explaination of this horrible looking query
63 * It's purpose is to locate all the active participants
64 * of a course with course completion enabled.
66 * We also only want the users with no course_completions
67 * record as this functions job is to create the missing
70 * We want to record the user's enrolment start time for the
71 * course. This gets tricky because there can be multiple
72 * enrolment plugins active in a course, hence the possibility
73 * of multiple records for each couse/user in the results
79 crc.id AS completionid,
94 {course_completions} crc
98 c.enablecompletion = 1
99 AND crc.timeenrolled IS NULL
104 AND (ue.timeend > ? OR ue.timeend = 0)
105 AND ue.timecreated < ?
106 AND (ue.timecreated > ? OR ue.timecreated = 0)
112 // Check if result is empty
114 if (!$rs = $DB->get_recordset_sql($sql, array($now, $now, $now, $now))) {
119 * An explaination of the following loop
121 * We are essentially doing a group by in the code here (as I can't find
122 * a decent way of doing it in the sql).
124 * Since there can be multiple enrolment plugins for each course, we can have
125 * multiple rows for each particpant in the query result. This isn't really
126 * a problem until you combine it with the fact that the enrolment plugins
127 * can save the enrol start time in either timestart or timeenrolled.
129 * The purpose of this loop is to find the earliest enrolment start time for
130 * each participant in each course.
133 while ($rs->valid() || $prev) {
135 $current = $rs->current();
137 if (!isset($current->course)) {
141 // Not all enrol plugins fill out timestart correctly, so use whichever
143 $current->timeenrolled = max($current->timestart, $current->timeenrolled);
146 // If we are at the last record,
147 // or we aren't at the first and the record is for a diff user/course
150 ($current->course != $prev->course || $current->userid != $prev->userid))) {
152 $completion = new completion_completion();
153 $completion->userid = $prev->userid;
154 $completion->course = $prev->course;
155 $completion->timeenrolled = (string) $prev->timeenrolled;
156 $completion->timestarted = 0;
158 if ($prev->completionid) {
159 $completion->id = $prev->completionid;
162 $completion->mark_enrolled();
165 mtrace('Marked started user '.$prev->userid.' in course '.$prev->course);
168 // Else, if this record is for the same user/course
169 elseif ($prev && $current) {
170 // Use oldest timeenrolled
171 $current->timeenrolled = min($current->timeenrolled, $prev->timeenrolled);
174 // Move current record to previous
177 // Move to next record
185 * Run installed criteria's data aggregation methods
187 * Loop through each installed criteria and run the
188 * cron() method if it exists
192 function completion_cron_criteria() {
194 // Process each criteria type
195 global $CFG, $COMPLETION_CRITERIA_TYPES;
197 foreach ($COMPLETION_CRITERIA_TYPES as $type) {
199 $object = 'completion_criteria_'.$type;
200 require_once $CFG->libdir.'/completion/'.$object.'.php';
202 $class = new $object();
204 // Run the criteria type's cron method, if it has one
205 if (method_exists($class, 'cron')) {
208 mtrace('Running '.$object.'->cron()');
216 * Aggregate each user's criteria completions
220 function completion_cron_completions() {
224 mtrace('Aggregating completions');
228 $timestarted = time();
230 // Grab all criteria and their associated criteria completions
236 cr.criteriatype AS criteriatype,
237 cc.timecompleted AS timecompleted
239 {course_completion_criteria} cr
244 {course_completions} crc
247 {course_completion_crit_compl} cc
248 ON cc.criteriaid = cr.id
249 AND crc.userid = cc.userid
251 c.enablecompletion = 1
252 AND crc.timecompleted IS NULL
253 AND crc.reaggregate > 0
259 // Check if result is empty
260 if (!$rs = $DB->get_recordset_sql($sql)) {
264 $current_user = null;
265 $current_course = null;
266 $completions = array();
270 // Grab records for current user/course
271 foreach ($rs as $record) {
272 // If we are still grabbing the same users completions
273 if ($record->userid === $current_user && $record->course === $current_course) {
274 $completions[$record->criteriaid] = $record;
281 if (!empty($completions)) {
284 mtrace('Aggregating completions for user '.$current_user.' in course '.$current_course);
287 // Get course info object
288 $info = new completion_info((object)array('id' => $current_course));
291 $overall = $info->get_aggregation_method();
292 $activity = $info->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ACTIVITY);
293 $prerequisite = $info->get_aggregation_method(COMPLETION_CRITERIA_TYPE_COURSE);
294 $role = $info->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ROLE);
296 $overall_status = null;
297 $activity_status = null;
298 $prerequisite_status = null;
301 // Get latest timecompleted
302 $timecompleted = null;
304 // Check each of the criteria
305 foreach ($completions as $params) {
306 $timecompleted = max($timecompleted, $params->timecompleted);
308 $completion = new completion_criteria_completion($params, false);
310 // Handle aggregation special cases
311 if ($params->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
312 completion_cron_aggregate($activity, $completion->is_complete(), $activity_status);
313 } else if ($params->criteriatype == COMPLETION_CRITERIA_TYPE_COURSE) {
314 completion_cron_aggregate($prerequisite, $completion->is_complete(), $prerequisite_status);
315 } else if ($params->criteriatype == COMPLETION_CRITERIA_TYPE_ROLE) {
316 completion_cron_aggregate($role, $completion->is_complete(), $role_status);
318 completion_cron_aggregate($overall, $completion->is_complete(), $overall_status);
322 // Include role criteria aggregation in overall aggregation
323 if ($role_status !== null) {
324 completion_cron_aggregate($overall, $role_status, $overall_status);
327 // Include activity criteria aggregation in overall aggregation
328 if ($activity_status !== null) {
329 completion_cron_aggregate($overall, $activity_status, $overall_status);
332 // Include prerequisite criteria aggregation in overall aggregation
333 if ($prerequisite_status !== null) {
334 completion_cron_aggregate($overall, $prerequisite_status, $overall_status);
337 // If aggregation status is true, mark course complete for user
338 if ($overall_status) {
340 mtrace('Marking complete');
343 $ccompletion = new completion_completion(array('course' => $params->course, 'userid' => $params->userid));
344 $ccompletion->mark_complete($timecompleted);
348 // If this is the end of the recordset, break the loop
354 // New/next user, update user details, reset completions
355 $current_user = $record->userid;
356 $current_course = $record->course;
357 $completions = array();
358 $completions[$record->criteriaid] = $record;
361 // Mark all users as aggregated
368 reaggregate < {$timestarted}
375 * Aggregate criteria status's as per configured aggregation method
377 * @param int $method COMPLETION_AGGREGATION_* constant
378 * @param bool $data Criteria completion status
379 * @param bool|null $state Aggregation state
382 function completion_cron_aggregate($method, $data, &$state) {
383 if ($method == COMPLETION_AGGREGATION_ALL) {
384 if ($data && $state !== false) {
389 } elseif ($method == COMPLETION_AGGREGATION_ANY) {
392 } else if (!$data && $state === null) {