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');
60 if (!empty($CFG->progresstrackedroles)) {
61 $roles = ' AND ra.roleid IN ('.$CFG->progresstrackedroles.')';
63 // This causes it to default to everyone (if there is no student role)
68 * A quick explaination of this horrible looking query
70 * It's purpose is to locate all the active participants
71 * of a course with course completion enabled.
73 * We also only want the users with no course_completions
74 * record as this functions job is to create the missing
77 * We want to record the user's enrolment start time for the
78 * course. This gets tricky because there can be multiple
79 * enrolment plugins active in a course, hence the possibility
80 * of multiple records for each couse/user in the results
86 crc.id AS completionid,
87 ue.timestart AS timeenrolled,
101 {role_assignments} ra
104 {course_completions} crc
106 AND crc.userid = u.id
108 c.enablecompletion = 1
109 AND crc.timeenrolled IS NULL
114 AND (ue.timeend > ? OR ue.timeend = 0)
121 // Check if result is empty
123 if (!$rs = $DB->get_recordset_sql($sql, array($now, $now, $now, $now))) {
128 * An explaination of the following loop
130 * We are essentially doing a group by in the code here (as I can't find
131 * a decent way of doing it in the sql).
133 * Since there can be multiple enrolment plugins for each course, we can have
134 * multiple rows for each particpant in the query result. This isn't really
135 * a problem until you combine it with the fact that the enrolment plugins
136 * can save the enrol start time in either timestart or timeenrolled.
138 * The purpose of this loop is to find the earliest enrolment start time for
139 * each participant in each course.
142 while ($rs->valid() || $prev) {
144 $current = $rs->current();
146 if (!isset($current->course)) {
150 // Not all enrol plugins fill out timestart correctly, so use whichever
152 $current->timeenrolled = max($current->timecreated, $current->timeenrolled);
155 // If we are at the last record,
156 // or we aren't at the first and the record is for a diff user/course
159 ($current->course != $prev->course || $current->userid != $prev->userid))) {
161 $completion = new completion_completion();
162 $completion->userid = $prev->userid;
163 $completion->course = $prev->course;
164 $completion->timeenrolled = (string) $prev->timeenrolled;
165 $completion->timestarted = 0;
166 $completion->reaggregate = time();
168 if ($prev->completionid) {
169 $completion->id = $prev->completionid;
172 $completion->mark_enrolled();
175 mtrace('Marked started user '.$prev->userid.' in course '.$prev->course);
178 // Else, if this record is for the same user/course
179 elseif ($prev && $current) {
180 // Use oldest timeenrolled
181 $current->timeenrolled = min($current->timeenrolled, $prev->timeenrolled);
184 // Move current record to previous
187 // Move to next record
195 * Run installed criteria's data aggregation methods
197 * Loop through each installed criteria and run the
198 * cron() method if it exists
202 function completion_cron_criteria() {
204 // Process each criteria type
205 global $CFG, $COMPLETION_CRITERIA_TYPES;
207 foreach ($COMPLETION_CRITERIA_TYPES as $type) {
209 $object = 'completion_criteria_'.$type;
210 require_once $CFG->libdir.'/completion/'.$object.'.php';
212 $class = new $object();
214 // Run the criteria type's cron method, if it has one
215 if (method_exists($class, 'cron')) {
218 mtrace('Running '.$object.'->cron()');
226 * Aggregate each user's criteria completions
230 function completion_cron_completions() {
234 mtrace('Aggregating completions');
238 $timestarted = time();
240 // Grab all criteria and their associated criteria completions
245 crc.userid AS userid,
246 cr.criteriatype AS criteriatype,
247 cc.timecompleted AS timecompleted
249 {course_completion_criteria} cr
254 {course_completions} crc
257 {course_completion_crit_compl} cc
258 ON cc.criteriaid = cr.id
259 AND crc.userid = cc.userid
261 c.enablecompletion = 1
262 AND crc.timecompleted IS NULL
263 AND crc.reaggregate > 0
264 AND crc.reaggregate < :timestarted
270 // Check if result is empty
271 if (!$rs = $DB->get_recordset_sql($sql, array('timestarted' => $timestarted))) {
275 $current_user = null;
276 $current_course = null;
277 $completions = array();
281 // Grab records for current user/course
282 foreach ($rs as $record) {
283 // If we are still grabbing the same users completions
284 if ($record->userid === $current_user && $record->course === $current_course) {
285 $completions[$record->criteriaid] = $record;
292 if (!empty($completions)) {
295 mtrace('Aggregating completions for user '.$current_user.' in course '.$current_course);
298 // Get course info object
299 $info = new completion_info((object)array('id' => $current_course));
302 $overall = $info->get_aggregation_method();
303 $activity = $info->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ACTIVITY);
304 $prerequisite = $info->get_aggregation_method(COMPLETION_CRITERIA_TYPE_COURSE);
305 $role = $info->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ROLE);
307 $overall_status = null;
308 $activity_status = null;
309 $prerequisite_status = null;
312 // Get latest timecompleted
313 $timecompleted = null;
315 // Check each of the criteria
316 foreach ($completions as $params) {
317 $timecompleted = max($timecompleted, $params->timecompleted);
319 $completion = new completion_criteria_completion($params, false);
321 // Handle aggregation special cases
322 if ($params->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
323 completion_cron_aggregate($activity, $completion->is_complete(), $activity_status);
324 } else if ($params->criteriatype == COMPLETION_CRITERIA_TYPE_COURSE) {
325 completion_cron_aggregate($prerequisite, $completion->is_complete(), $prerequisite_status);
326 } else if ($params->criteriatype == COMPLETION_CRITERIA_TYPE_ROLE) {
327 completion_cron_aggregate($role, $completion->is_complete(), $role_status);
329 completion_cron_aggregate($overall, $completion->is_complete(), $overall_status);
333 // Include role criteria aggregation in overall aggregation
334 if ($role_status !== null) {
335 completion_cron_aggregate($overall, $role_status, $overall_status);
338 // Include activity criteria aggregation in overall aggregation
339 if ($activity_status !== null) {
340 completion_cron_aggregate($overall, $activity_status, $overall_status);
343 // Include prerequisite criteria aggregation in overall aggregation
344 if ($prerequisite_status !== null) {
345 completion_cron_aggregate($overall, $prerequisite_status, $overall_status);
348 // If aggregation status is true, mark course complete for user
349 if ($overall_status) {
351 mtrace('Marking complete');
354 $ccompletion = new completion_completion(array('course' => $params->course, 'userid' => $params->userid));
355 $ccompletion->mark_complete($timecompleted);
359 // If this is the end of the recordset, break the loop
365 // New/next user, update user details, reset completions
366 $current_user = $record->userid;
367 $current_course = $record->course;
368 $completions = array();
369 $completions[$record->criteriaid] = $record;
372 // Mark all users as aggregated
379 reaggregate < {$timestarted}
386 * Aggregate criteria status's as per configured aggregation method
388 * @param int $method COMPLETION_AGGREGATION_* constant
389 * @param bool $data Criteria completion status
390 * @param bool|null $state Aggregation state
393 function completion_cron_aggregate($method, $data, &$state) {
394 if ($method == COMPLETION_AGGREGATION_ALL) {
395 if ($data && $state !== false) {
400 } elseif ($method == COMPLETION_AGGREGATION_ANY) {
403 } else if (!$data && $state === null) {