@@ -48,12 +48,19 @@ function completion_cron() {
48
48
* @return void
49
49
*/
50
50
function completion_cron_mark_started () {
51
- global $ DB ;
51
+ global $ CFG , $ DB ;
52
52
53
53
if (debugging ()) {
54
54
mtrace ('Marking users as started ' );
55
55
}
56
56
57
+ if (!empty ($ CFG ->gradebookroles )) {
58
+ $ roles = ' AND ra.roleid IN ( ' .$ CFG ->gradebookroles .') ' ;
59
+ } else {
60
+ // This causes it to default to everyone (if there is no student role)
61
+ $ roles = '' ;
62
+ }
63
+
57
64
/**
58
65
* A quick explaination of this horrible looking query
59
66
*
@@ -70,53 +77,118 @@ function completion_cron_mark_started() {
70
77
* of multiple records for each couse/user in the results
71
78
*/
72
79
$ sql = "
73
- INSERT INTO
74
- {course_completions}
75
- (course, userid, timeenrolled, timestarted, reaggregate)
76
80
SELECT
77
81
c.id AS course,
78
- ue.userid AS userid,
79
- CASE
80
- WHEN MIN(ue.timestart) <> 0
81
- THEN MIN(ue.timestart)
82
- ELSE ?
83
- END,
84
- 0,
85
- ?
82
+ u.id AS userid,
83
+ crc.id AS completionid,
84
+ ue.timestart AS timeenrolled,
85
+ ue.timecreated
86
86
FROM
87
+ {user} u
88
+ INNER JOIN
87
89
{user_enrolments} ue
90
+ ON ue.userid = u.id
88
91
INNER JOIN
89
92
{enrol} e
90
93
ON e.id = ue.enrolid
91
94
INNER JOIN
92
95
{course} c
93
96
ON c.id = e.courseid
97
+ INNER JOIN
98
+ {role_assignments} ra
99
+ ON ra.userid = u.id
94
100
LEFT JOIN
95
101
{course_completions} crc
96
102
ON crc.course = c.id
97
- AND crc.userid = ue.userid
103
+ AND crc.userid = u.id
98
104
WHERE
99
105
c.enablecompletion = 1
100
- AND crc.id IS NULL
101
- AND ue.status = ?
102
- AND e.status = ?
106
+ AND crc.timeenrolled IS NULL
107
+ AND ue.status = 0
108
+ AND e.status = 0
109
+ AND u.deleted = 0
103
110
AND ue.timestart < ?
104
111
AND (ue.timeend > ? OR ue.timeend = 0)
105
- GROUP BY
106
- c.id,
107
- ue.userid
112
+ $ roles
113
+ ORDER BY
114
+ course,
115
+ userid
108
116
" ;
109
117
110
118
$ now = time ();
111
- $ params = array (
112
- $ now ,
113
- $ now ,
114
- ENROL_USER_ACTIVE ,
115
- ENROL_INSTANCE_ENABLED ,
116
- $ now ,
117
- $ now
118
- );
119
- $ affected = $ DB ->execute ($ sql , $ params , true );
119
+ $ rs = $ DB ->get_recordset_sql ($ sql , array ($ now , $ now , $ now , $ now ));
120
+
121
+ // Check if result is empty
122
+ if (!$ rs ->valid ()) {
123
+ $ rs ->close (); // Not going to iterate (but exit), close rs
124
+ return ;
125
+ }
126
+
127
+ /**
128
+ * An explaination of the following loop
129
+ *
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).
132
+ *
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.
137
+ *
138
+ * The purpose of this loop is to find the earliest enrolment start time for
139
+ * each participant in each course.
140
+ */
141
+ $ prev = null ;
142
+ while ($ rs ->valid () || $ prev ) {
143
+
144
+ $ current = $ rs ->current ();
145
+
146
+ if (!isset ($ current ->course )) {
147
+ $ current = false ;
148
+ }
149
+ else {
150
+ // Not all enrol plugins fill out timestart correctly, so use whichever
151
+ // is non-zero
152
+ $ current ->timeenrolled = max ($ current ->timecreated , $ current ->timeenrolled );
153
+ }
154
+
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
157
+ if ($ prev &&
158
+ (!$ rs ->valid () ||
159
+ ($ current ->course != $ prev ->course || $ current ->userid != $ prev ->userid ))) {
160
+
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 ();
167
+
168
+ if ($ prev ->completionid ) {
169
+ $ completion ->id = $ prev ->completionid ;
170
+ }
171
+
172
+ $ completion ->mark_enrolled ();
173
+
174
+ if (debugging ()) {
175
+ mtrace ('Marked started user ' .$ prev ->userid .' in course ' .$ prev ->course );
176
+ }
177
+ }
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 );
182
+ }
183
+
184
+ // Move current record to previous
185
+ $ prev = $ current ;
186
+
187
+ // Move to next record
188
+ $ rs ->next ();
189
+ }
190
+
191
+ $ rs ->close ();
120
192
}
121
193
122
194
/**
0 commit comments