MDL-65276 core: fix CiBoT complains
[moodle.git] / lib / classes / task / completion_daily_task.php
CommitLineData
309ae892
DW
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * A scheduled task.
19 *
20 * @package core
21 * @copyright 2013 onwards Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24namespace core\task;
25
26/**
51e488ea
JW
27 * Simple task to run the daily completion cron.
28 * @copyright 2013 onwards Martin Dougiamas http://dougiamas.com.
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
309ae892 30 */
160ccd3d 31class completion_daily_task extends scheduled_task {
309ae892
DW
32
33 /**
34 * Get a descriptive name for this task (shown to admins).
35 *
36 * @return string
37 */
38 public function get_name() {
160ccd3d 39 return get_string('taskcompletiondaily', 'admin');
309ae892
DW
40 }
41
42 /**
43 * Do the job.
44 * Throw exceptions on errors (the job will be retried).
45 */
46 public function execute() {
a5a0d10e 47 global $CFG, $DB;
309ae892
DW
48
49 if ($CFG->enablecompletion) {
a5a0d10e
SL
50 require_once($CFG->libdir . "/completionlib.php");
51
52 if (debugging()) {
53 mtrace('Marking users as started');
54 }
846c5859
SL
55
56 // This causes it to default to everyone (if there is no student role).
57 $sqlroles = '';
a5a0d10e 58 if (!empty($CFG->gradebookroles)) {
846c5859 59 $sqlroles = ' AND ra.roleid IN (' . $CFG->gradebookroles.')';
a5a0d10e 60 }
846c5859
SL
61
62 // It's purpose is to locate all the active participants of a course with course completion enabled.
63 // We also only want the users with no course_completions record as this functions job is to create
64 // the missing ones :)
65 // We want to record the user's enrolment start time for the course. This gets tricky because there can be
66 // multiple enrolment plugins active in a course, hence the possibility of multiple records for each
67 // couse/user in the results.
68 $sql = "SELECT c.id AS course, u.id AS userid, crc.id AS completionid, ue.timestart AS timeenrolled,
69 ue.timecreated
70 FROM {user} u
71 INNER JOIN {user_enrolments} ue ON ue.userid = u.id
72 INNER JOIN {enrol} e ON e.id = ue.enrolid
73 INNER JOIN {course} c ON c.id = e.courseid
74 INNER JOIN {role_assignments} ra ON ra.userid = u.id
75 LEFT JOIN {course_completions} crc ON crc.course = c.id AND crc.userid = u.id
76 WHERE c.enablecompletion = 1
77 AND crc.timeenrolled IS NULL
78 AND ue.status = 0
79 AND e.status = 0
80 AND u.deleted = 0
81 AND ue.timestart < ?
82 AND (ue.timeend > ? OR ue.timeend = 0)
83 $sqlroles
84 ORDER BY course, userid";
a5a0d10e 85 $now = time();
846c5859
SL
86 $rs = $DB->get_recordset_sql($sql, [$now, $now, $now, $now]);
87
88 // Check if result is empty.
a5a0d10e 89 if (!$rs->valid()) {
846c5859
SL
90 // Not going to iterate (but exit), close rs.
91 $rs->close();
a5a0d10e
SL
92 return;
93 }
846c5859
SL
94
95 // We are essentially doing a group by in the code here (as I can't find a decent way of doing it
96 // in the sql). Since there can be multiple enrolment plugins for each course, we can have multiple rows
97 // for each participant in the query result. This isn't really a problem until you combine it with the fact
98 // that the enrolment plugins can save the enrol start time in either timestart or timeenrolled.
99 // The purpose of the loop is to find the earliest enrolment start time for each participant in each course.
a5a0d10e
SL
100 $prev = null;
101 while ($rs->valid() || $prev) {
102 $current = $rs->current();
103 if (!isset($current->course)) {
104 $current = false;
846c5859
SL
105 } else {
106 // Not all enrol plugins fill out timestart correctly, so use whichever is non-zero.
a5a0d10e
SL
107 $current->timeenrolled = max($current->timecreated, $current->timeenrolled);
108 }
846c5859
SL
109
110 // If we are at the last record, or we aren't at the first and the record is for a diff user/course.
111 if ($prev && (!$rs->valid() ||
a5a0d10e 112 ($current->course != $prev->course || $current->userid != $prev->userid))) {
846c5859
SL
113
114 $completion = new \completion_completion();
a5a0d10e
SL
115 $completion->userid = $prev->userid;
116 $completion->course = $prev->course;
117 $completion->timeenrolled = (string) $prev->timeenrolled;
118 $completion->timestarted = 0;
119 $completion->reaggregate = time();
120 if ($prev->completionid) {
121 $completion->id = $prev->completionid;
122 }
123 $completion->mark_enrolled();
846c5859 124
a5a0d10e 125 if (debugging()) {
846c5859 126 mtrace('Marked started user ' . $prev->userid . ' in course ' . $prev->course);
a5a0d10e 127 }
846c5859
SL
128 } else if ($prev && $current) {
129 // Else, if this record is for the same user/course use oldest timeenrolled.
a5a0d10e
SL
130 $current->timeenrolled = min($current->timeenrolled, $prev->timeenrolled);
131 }
846c5859 132 // Move current record to previous.
a5a0d10e 133 $prev = $current;
846c5859 134 // Move to next record.
a5a0d10e
SL
135 $rs->next();
136 }
137 $rs->close();
309ae892
DW
138 }
139 }
309ae892 140}