1 |
efrain |
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 |
* Contains classes, functions and constants used during the tracking
|
|
|
19 |
* of activity completion for users.
|
|
|
20 |
*
|
|
|
21 |
* Completion top-level options (admin setting enablecompletion)
|
|
|
22 |
*
|
|
|
23 |
* @package core_completion
|
|
|
24 |
* @category completion
|
|
|
25 |
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
*/
|
|
|
28 |
|
|
|
29 |
use core_completion\activity_custom_completion;
|
|
|
30 |
use core_courseformat\base as course_format;
|
|
|
31 |
|
|
|
32 |
defined('MOODLE_INTERNAL') || die();
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Include the required completion libraries
|
|
|
36 |
*/
|
|
|
37 |
require_once $CFG->dirroot.'/completion/completion_aggregation.php';
|
|
|
38 |
require_once $CFG->dirroot.'/completion/criteria/completion_criteria.php';
|
|
|
39 |
require_once $CFG->dirroot.'/completion/completion_completion.php';
|
|
|
40 |
require_once $CFG->dirroot.'/completion/completion_criteria_completion.php';
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* The completion system is enabled in this site/course
|
|
|
45 |
*/
|
|
|
46 |
define('COMPLETION_ENABLED', 1);
|
|
|
47 |
/**
|
|
|
48 |
* The completion system is not enabled in this site/course
|
|
|
49 |
*/
|
|
|
50 |
define('COMPLETION_DISABLED', 0);
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Completion tracking is disabled for this activity
|
|
|
54 |
* This is a completion tracking option per-activity (course_modules/completion)
|
|
|
55 |
*/
|
|
|
56 |
define('COMPLETION_TRACKING_NONE', 0);
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Manual completion tracking (user ticks box) is enabled for this activity
|
|
|
60 |
* This is a completion tracking option per-activity (course_modules/completion)
|
|
|
61 |
*/
|
|
|
62 |
define('COMPLETION_TRACKING_MANUAL', 1);
|
|
|
63 |
/**
|
|
|
64 |
* Automatic completion tracking (system ticks box) is enabled for this activity
|
|
|
65 |
* This is a completion tracking option per-activity (course_modules/completion)
|
|
|
66 |
*/
|
|
|
67 |
define('COMPLETION_TRACKING_AUTOMATIC', 2);
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* The user has not completed this activity.
|
|
|
71 |
* This is a completion state value (course_modules_completion/completionstate)
|
|
|
72 |
*/
|
|
|
73 |
define('COMPLETION_INCOMPLETE', 0);
|
|
|
74 |
/**
|
|
|
75 |
* The user has completed this activity. It is not specified whether they have
|
|
|
76 |
* passed or failed it.
|
|
|
77 |
* This is a completion state value (course_modules_completion/completionstate)
|
|
|
78 |
*/
|
|
|
79 |
define('COMPLETION_COMPLETE', 1);
|
|
|
80 |
/**
|
|
|
81 |
* The user has completed this activity with a grade above the pass mark.
|
|
|
82 |
* This is a completion state value (course_modules_completion/completionstate)
|
|
|
83 |
*/
|
|
|
84 |
define('COMPLETION_COMPLETE_PASS', 2);
|
|
|
85 |
/**
|
|
|
86 |
* The user has completed this activity but their grade is less than the pass mark
|
|
|
87 |
* This is a completion state value (course_modules_completion/completionstate)
|
|
|
88 |
*/
|
|
|
89 |
define('COMPLETION_COMPLETE_FAIL', 3);
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Indicates that the user has received a failing grade for a hidden grade item.
|
|
|
93 |
*/
|
|
|
94 |
define('COMPLETION_COMPLETE_FAIL_HIDDEN', 4);
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* The effect of this change to completion status is unknown.
|
|
|
98 |
* A completion effect changes (used only in update_state)
|
|
|
99 |
*/
|
|
|
100 |
define('COMPLETION_UNKNOWN', -1);
|
|
|
101 |
/**
|
|
|
102 |
* The user's grade has changed, so their new state might be
|
|
|
103 |
* COMPLETION_COMPLETE_PASS or COMPLETION_COMPLETE_FAIL.
|
|
|
104 |
* A completion effect changes (used only in update_state)
|
|
|
105 |
*/
|
|
|
106 |
define('COMPLETION_GRADECHANGE', -2);
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* User must view this activity.
|
|
|
110 |
* Whether view is required to create an activity (course_modules/completionview)
|
|
|
111 |
*/
|
|
|
112 |
define('COMPLETION_VIEW_REQUIRED', 1);
|
|
|
113 |
/**
|
|
|
114 |
* User does not need to view this activity
|
|
|
115 |
* Whether view is required to create an activity (course_modules/completionview)
|
|
|
116 |
*/
|
|
|
117 |
define('COMPLETION_VIEW_NOT_REQUIRED', 0);
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* User has viewed this activity.
|
|
|
121 |
* Completion viewed state (course_modules_completion/viewed)
|
|
|
122 |
*/
|
|
|
123 |
define('COMPLETION_VIEWED', 1);
|
|
|
124 |
/**
|
|
|
125 |
* User has not viewed this activity.
|
|
|
126 |
* Completion viewed state (course_modules_completion/viewed)
|
|
|
127 |
*/
|
|
|
128 |
define('COMPLETION_NOT_VIEWED', 0);
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Completion details should be ORed together and you should return false if
|
|
|
132 |
* none apply.
|
|
|
133 |
*/
|
|
|
134 |
define('COMPLETION_OR', false);
|
|
|
135 |
/**
|
|
|
136 |
* Completion details should be ANDed together and you should return true if
|
|
|
137 |
* none apply
|
|
|
138 |
*/
|
|
|
139 |
define('COMPLETION_AND', true);
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* Course completion criteria aggregation method.
|
|
|
143 |
*/
|
|
|
144 |
define('COMPLETION_AGGREGATION_ALL', 1);
|
|
|
145 |
/**
|
|
|
146 |
* Course completion criteria aggregation method.
|
|
|
147 |
*/
|
|
|
148 |
define('COMPLETION_AGGREGATION_ANY', 2);
|
|
|
149 |
|
|
|
150 |
/**
|
|
|
151 |
* Completion conditions will be displayed to user.
|
|
|
152 |
*/
|
|
|
153 |
define('COMPLETION_SHOW_CONDITIONS', 1);
|
|
|
154 |
|
|
|
155 |
/**
|
|
|
156 |
* Completion conditions will be hidden from user.
|
|
|
157 |
*/
|
|
|
158 |
define('COMPLETION_HIDE_CONDITIONS', 0);
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Utility function for checking if the logged in user can view
|
|
|
162 |
* another's completion data for a particular course
|
|
|
163 |
*
|
|
|
164 |
* @access public
|
|
|
165 |
* @param int $userid Completion data's owner
|
|
|
166 |
* @param mixed $course Course object or Course ID (optional)
|
|
|
167 |
* @return boolean
|
|
|
168 |
*/
|
|
|
169 |
function completion_can_view_data($userid, $course = null) {
|
|
|
170 |
global $USER;
|
|
|
171 |
|
|
|
172 |
if (!isloggedin()) {
|
|
|
173 |
return false;
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
if (!is_object($course)) {
|
|
|
177 |
$cid = $course;
|
|
|
178 |
$course = new stdClass();
|
|
|
179 |
$course->id = $cid;
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
// Check if this is the site course
|
|
|
183 |
if ($course->id == SITEID) {
|
|
|
184 |
$course = null;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
// Check if completion is enabled
|
|
|
188 |
if ($course) {
|
|
|
189 |
$cinfo = new completion_info($course);
|
|
|
190 |
if (!$cinfo->is_enabled()) {
|
|
|
191 |
return false;
|
|
|
192 |
}
|
|
|
193 |
} else {
|
|
|
194 |
if (!completion_info::is_enabled_for_site()) {
|
|
|
195 |
return false;
|
|
|
196 |
}
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
// Is own user's data?
|
|
|
200 |
if ($USER->id == $userid) {
|
|
|
201 |
return true;
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
// Check capabilities
|
|
|
205 |
$personalcontext = context_user::instance($userid);
|
|
|
206 |
|
|
|
207 |
if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)) {
|
|
|
208 |
return true;
|
|
|
209 |
} elseif (has_capability('report/completion:view', $personalcontext)) {
|
|
|
210 |
return true;
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
if ($course->id) {
|
|
|
214 |
$coursecontext = context_course::instance($course->id);
|
|
|
215 |
} else {
|
|
|
216 |
$coursecontext = context_system::instance();
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
if (has_capability('report/completion:view', $coursecontext)) {
|
|
|
220 |
return true;
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
return false;
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
|
|
|
227 |
/**
|
|
|
228 |
* Class represents completion information for a course.
|
|
|
229 |
*
|
|
|
230 |
* Does not contain any data, so you can safely construct it multiple times
|
|
|
231 |
* without causing any problems.
|
|
|
232 |
*
|
|
|
233 |
* @package core
|
|
|
234 |
* @category completion
|
|
|
235 |
* @copyright 2008 Sam Marshall
|
|
|
236 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
237 |
*/
|
|
|
238 |
class completion_info {
|
|
|
239 |
|
|
|
240 |
/* @var stdClass Course object passed during construction */
|
|
|
241 |
private $course;
|
|
|
242 |
|
|
|
243 |
/* @var int Course id */
|
|
|
244 |
public $course_id;
|
|
|
245 |
|
|
|
246 |
/* @var array Completion criteria {@link completion_info::get_criteria()} */
|
|
|
247 |
private $criteria;
|
|
|
248 |
|
|
|
249 |
/**
|
|
|
250 |
* Return array of aggregation methods
|
|
|
251 |
* @return array
|
|
|
252 |
*/
|
|
|
253 |
public static function get_aggregation_methods() {
|
|
|
254 |
return array(
|
|
|
255 |
COMPLETION_AGGREGATION_ALL => get_string('all'),
|
|
|
256 |
COMPLETION_AGGREGATION_ANY => get_string('any', 'completion'),
|
|
|
257 |
);
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
/**
|
|
|
261 |
* Constructs with course details.
|
|
|
262 |
*
|
|
|
263 |
* When instantiating a new completion info object you must provide a course
|
|
|
264 |
* object with at least id, and enablecompletion properties. Property
|
|
|
265 |
* cacherev is needed if you check completion of the current user since
|
|
|
266 |
* it is used for cache validation.
|
|
|
267 |
*
|
|
|
268 |
* @param stdClass $course Moodle course object.
|
|
|
269 |
*/
|
|
|
270 |
public function __construct($course) {
|
|
|
271 |
$this->course = $course;
|
|
|
272 |
$this->course_id = $course->id;
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
/**
|
|
|
276 |
* Determines whether completion is enabled across entire site.
|
|
|
277 |
*
|
|
|
278 |
* @return bool COMPLETION_ENABLED (true) if completion is enabled for the site,
|
|
|
279 |
* COMPLETION_DISABLED (false) if it's complete
|
|
|
280 |
*/
|
|
|
281 |
public static function is_enabled_for_site() {
|
|
|
282 |
global $CFG;
|
|
|
283 |
return !empty($CFG->enablecompletion);
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
/**
|
|
|
287 |
* Checks whether completion is enabled in a particular course and possibly
|
|
|
288 |
* activity.
|
|
|
289 |
*
|
|
|
290 |
* @param stdClass|cm_info $cm Course-module object. If not specified, returns the course
|
|
|
291 |
* completion enable state.
|
|
|
292 |
* @return mixed COMPLETION_ENABLED or COMPLETION_DISABLED (==0) in the case of
|
|
|
293 |
* site and course; COMPLETION_TRACKING_MANUAL, _AUTOMATIC or _NONE (==0)
|
|
|
294 |
* for a course-module.
|
|
|
295 |
*/
|
|
|
296 |
public function is_enabled($cm = null) {
|
|
|
297 |
global $CFG, $DB;
|
|
|
298 |
|
|
|
299 |
// First check global completion
|
|
|
300 |
if (!isset($CFG->enablecompletion) || $CFG->enablecompletion == COMPLETION_DISABLED) {
|
|
|
301 |
return COMPLETION_DISABLED;
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
// Load data if we do not have enough
|
|
|
305 |
if (!isset($this->course->enablecompletion)) {
|
|
|
306 |
$this->course = get_course($this->course_id);
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
// Check course completion
|
|
|
310 |
if ($this->course->enablecompletion == COMPLETION_DISABLED) {
|
|
|
311 |
return COMPLETION_DISABLED;
|
|
|
312 |
}
|
|
|
313 |
|
|
|
314 |
// If there was no $cm and we got this far, then it's enabled
|
|
|
315 |
if (!$cm) {
|
|
|
316 |
return COMPLETION_ENABLED;
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
// Return course-module completion value
|
|
|
320 |
return $cm->completion;
|
|
|
321 |
}
|
|
|
322 |
|
|
|
323 |
/**
|
|
|
324 |
* @deprecated since Moodle 2.0 - Use display_help_icon instead.
|
|
|
325 |
*/
|
|
|
326 |
public function print_help_icon() {
|
|
|
327 |
throw new coding_exception(__FUNCTION__ . '() has been removed.');
|
|
|
328 |
}
|
|
|
329 |
|
|
|
330 |
/**
|
|
|
331 |
* @deprecated since Moodle 4.0 - The 'Your progress' info isn't displayed any more.
|
|
|
332 |
*/
|
|
|
333 |
public function display_help_icon() {
|
|
|
334 |
throw new coding_exception(__FUNCTION__ . '() has been removed.');
|
|
|
335 |
}
|
|
|
336 |
|
|
|
337 |
/**
|
|
|
338 |
* Get a course completion for a user
|
|
|
339 |
*
|
|
|
340 |
* @param int $user_id User id
|
|
|
341 |
* @param int $criteriatype Specific criteria type to return
|
|
|
342 |
* @return bool|completion_criteria_completion returns false on fail
|
|
|
343 |
*/
|
|
|
344 |
public function get_completion($user_id, $criteriatype) {
|
|
|
345 |
$completions = $this->get_completions($user_id, $criteriatype);
|
|
|
346 |
|
|
|
347 |
if (empty($completions)) {
|
|
|
348 |
return false;
|
|
|
349 |
} elseif (count($completions) > 1) {
|
|
|
350 |
throw new \moodle_exception('multipleselfcompletioncriteria', 'completion');
|
|
|
351 |
}
|
|
|
352 |
|
|
|
353 |
return $completions[0];
|
|
|
354 |
}
|
|
|
355 |
|
|
|
356 |
/**
|
|
|
357 |
* Get all course criteria's completion objects for a user
|
|
|
358 |
*
|
|
|
359 |
* @param int $user_id User id
|
|
|
360 |
* @param int $criteriatype Specific criteria type to return (optional)
|
|
|
361 |
* @return array
|
|
|
362 |
*/
|
|
|
363 |
public function get_completions($user_id, $criteriatype = null) {
|
|
|
364 |
$criteria = $this->get_criteria($criteriatype);
|
|
|
365 |
|
|
|
366 |
$completions = array();
|
|
|
367 |
|
|
|
368 |
foreach ($criteria as $criterion) {
|
|
|
369 |
$params = array(
|
|
|
370 |
'course' => $this->course_id,
|
|
|
371 |
'userid' => $user_id,
|
|
|
372 |
'criteriaid' => $criterion->id
|
|
|
373 |
);
|
|
|
374 |
|
|
|
375 |
$completion = new completion_criteria_completion($params);
|
|
|
376 |
$completion->attach_criteria($criterion);
|
|
|
377 |
|
|
|
378 |
$completions[] = $completion;
|
|
|
379 |
}
|
|
|
380 |
|
|
|
381 |
return $completions;
|
|
|
382 |
}
|
|
|
383 |
|
|
|
384 |
/**
|
|
|
385 |
* Get completion object for a user and a criteria
|
|
|
386 |
*
|
|
|
387 |
* @param int $user_id User id
|
|
|
388 |
* @param completion_criteria $criteria Criteria object
|
|
|
389 |
* @return completion_criteria_completion
|
|
|
390 |
*/
|
|
|
391 |
public function get_user_completion($user_id, $criteria) {
|
|
|
392 |
$params = array(
|
|
|
393 |
'course' => $this->course_id,
|
|
|
394 |
'userid' => $user_id,
|
|
|
395 |
'criteriaid' => $criteria->id,
|
|
|
396 |
);
|
|
|
397 |
|
|
|
398 |
$completion = new completion_criteria_completion($params);
|
|
|
399 |
return $completion;
|
|
|
400 |
}
|
|
|
401 |
|
|
|
402 |
/**
|
|
|
403 |
* Check if course has completion criteria set
|
|
|
404 |
*
|
|
|
405 |
* @return bool Returns true if there are criteria
|
|
|
406 |
*/
|
|
|
407 |
public function has_criteria() {
|
|
|
408 |
$criteria = $this->get_criteria();
|
|
|
409 |
|
|
|
410 |
return (bool) count($criteria);
|
|
|
411 |
}
|
|
|
412 |
|
|
|
413 |
/**
|
|
|
414 |
* Get course completion criteria
|
|
|
415 |
*
|
|
|
416 |
* @param int $criteriatype Specific criteria type to return (optional)
|
|
|
417 |
*/
|
|
|
418 |
public function get_criteria($criteriatype = null) {
|
|
|
419 |
|
|
|
420 |
// Fill cache if empty
|
|
|
421 |
if (!is_array($this->criteria)) {
|
|
|
422 |
global $DB;
|
|
|
423 |
|
|
|
424 |
$params = array(
|
|
|
425 |
'course' => $this->course->id
|
|
|
426 |
);
|
|
|
427 |
|
|
|
428 |
// Load criteria from database
|
|
|
429 |
$records = (array)$DB->get_records('course_completion_criteria', $params);
|
|
|
430 |
|
|
|
431 |
// Order records so activities are in the same order as they appear on the course view page.
|
|
|
432 |
if ($records) {
|
|
|
433 |
$activitiesorder = array_keys(get_fast_modinfo($this->course)->get_cms());
|
|
|
434 |
usort($records, function ($a, $b) use ($activitiesorder) {
|
|
|
435 |
$aidx = ($a->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) ?
|
|
|
436 |
array_search($a->moduleinstance, $activitiesorder) : false;
|
|
|
437 |
$bidx = ($b->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) ?
|
|
|
438 |
array_search($b->moduleinstance, $activitiesorder) : false;
|
|
|
439 |
if ($aidx === false || $bidx === false || $aidx == $bidx) {
|
|
|
440 |
return 0;
|
|
|
441 |
}
|
|
|
442 |
return ($aidx < $bidx) ? -1 : 1;
|
|
|
443 |
});
|
|
|
444 |
}
|
|
|
445 |
|
|
|
446 |
// Build array of criteria objects
|
|
|
447 |
$this->criteria = array();
|
|
|
448 |
foreach ($records as $record) {
|
|
|
449 |
$this->criteria[$record->id] = completion_criteria::factory((array)$record);
|
|
|
450 |
}
|
|
|
451 |
}
|
|
|
452 |
|
|
|
453 |
// If after all criteria
|
|
|
454 |
if ($criteriatype === null) {
|
|
|
455 |
return $this->criteria;
|
|
|
456 |
}
|
|
|
457 |
|
|
|
458 |
// If we are only after a specific criteria type
|
|
|
459 |
$criteria = array();
|
|
|
460 |
foreach ($this->criteria as $criterion) {
|
|
|
461 |
|
|
|
462 |
if ($criterion->criteriatype != $criteriatype) {
|
|
|
463 |
continue;
|
|
|
464 |
}
|
|
|
465 |
|
|
|
466 |
$criteria[$criterion->id] = $criterion;
|
|
|
467 |
}
|
|
|
468 |
|
|
|
469 |
return $criteria;
|
|
|
470 |
}
|
|
|
471 |
|
|
|
472 |
/**
|
|
|
473 |
* Get aggregation method
|
|
|
474 |
*
|
|
|
475 |
* @param int $criteriatype If none supplied, get overall aggregation method (optional)
|
|
|
476 |
* @return int One of COMPLETION_AGGREGATION_ALL or COMPLETION_AGGREGATION_ANY
|
|
|
477 |
*/
|
|
|
478 |
public function get_aggregation_method($criteriatype = null) {
|
|
|
479 |
$params = array(
|
|
|
480 |
'course' => $this->course_id,
|
|
|
481 |
'criteriatype' => $criteriatype
|
|
|
482 |
);
|
|
|
483 |
|
|
|
484 |
$aggregation = new completion_aggregation($params);
|
|
|
485 |
|
|
|
486 |
if (!$aggregation->id) {
|
|
|
487 |
$aggregation->method = COMPLETION_AGGREGATION_ALL;
|
|
|
488 |
}
|
|
|
489 |
|
|
|
490 |
return $aggregation->method;
|
|
|
491 |
}
|
|
|
492 |
|
|
|
493 |
/**
|
|
|
494 |
* @deprecated since Moodle 2.8 MDL-46290.
|
|
|
495 |
*/
|
|
|
496 |
public function get_incomplete_criteria() {
|
|
|
497 |
throw new coding_exception('completion_info->get_incomplete_criteria() is removed.');
|
|
|
498 |
}
|
|
|
499 |
|
|
|
500 |
/**
|
|
|
501 |
* Clear old course completion criteria
|
|
|
502 |
*/
|
|
|
503 |
public function clear_criteria() {
|
|
|
504 |
global $DB;
|
|
|
505 |
|
|
|
506 |
// Remove completion criteria records for the course itself, and any records that refer to the course.
|
|
|
507 |
$select = 'course = :course OR (criteriatype = :type AND courseinstance = :courseinstance)';
|
|
|
508 |
$params = [
|
|
|
509 |
'course' => $this->course_id,
|
|
|
510 |
'type' => COMPLETION_CRITERIA_TYPE_COURSE,
|
|
|
511 |
'courseinstance' => $this->course_id,
|
|
|
512 |
];
|
|
|
513 |
|
|
|
514 |
$DB->delete_records_select('course_completion_criteria', $select, $params);
|
|
|
515 |
$DB->delete_records('course_completion_aggr_methd', array('course' => $this->course_id));
|
|
|
516 |
|
|
|
517 |
$this->delete_course_completion_data();
|
|
|
518 |
}
|
|
|
519 |
|
|
|
520 |
/**
|
|
|
521 |
* Has the supplied user completed this course
|
|
|
522 |
*
|
|
|
523 |
* @param int $user_id User's id
|
|
|
524 |
* @return boolean
|
|
|
525 |
*/
|
|
|
526 |
public function is_course_complete($user_id) {
|
|
|
527 |
$params = array(
|
|
|
528 |
'userid' => $user_id,
|
|
|
529 |
'course' => $this->course_id
|
|
|
530 |
);
|
|
|
531 |
|
|
|
532 |
$ccompletion = new completion_completion($params);
|
|
|
533 |
return $ccompletion->is_complete();
|
|
|
534 |
}
|
|
|
535 |
|
|
|
536 |
/**
|
|
|
537 |
* Check whether the supplied user can override the activity completion statuses within the current course.
|
|
|
538 |
*
|
|
|
539 |
* @param stdClass $user The user object.
|
|
|
540 |
* @return bool True if the user can override, false otherwise.
|
|
|
541 |
*/
|
|
|
542 |
public function user_can_override_completion($user) {
|
|
|
543 |
return has_capability('moodle/course:overridecompletion', context_course::instance($this->course_id), $user);
|
|
|
544 |
}
|
|
|
545 |
|
|
|
546 |
/**
|
|
|
547 |
* Updates (if necessary) the completion state of activity $cm for the given
|
|
|
548 |
* user.
|
|
|
549 |
*
|
|
|
550 |
* For manual completion, this function is called when completion is toggled
|
|
|
551 |
* with $possibleresult set to the target state.
|
|
|
552 |
*
|
|
|
553 |
* For automatic completion, this function should be called every time a module
|
|
|
554 |
* does something which might influence a user's completion state. For example,
|
|
|
555 |
* if a forum provides options for marking itself 'completed' once a user makes
|
|
|
556 |
* N posts, this function should be called every time a user makes a new post.
|
|
|
557 |
* [After the post has been saved to the database]. When calling, you do not
|
|
|
558 |
* need to pass in the new completion state. Instead this function carries out completion
|
|
|
559 |
* calculation by checking grades and viewed state itself, and calling the involved module
|
|
|
560 |
* via mod_{modulename}\\completion\\custom_completion::get_overall_completion_state() to
|
|
|
561 |
* check module-specific conditions.
|
|
|
562 |
*
|
|
|
563 |
* @param stdClass|cm_info $cm Course-module
|
|
|
564 |
* @param int $possibleresult Expected completion result. If the event that
|
|
|
565 |
* has just occurred (e.g. add post) can only result in making the activity
|
|
|
566 |
* complete when it wasn't before, use COMPLETION_COMPLETE. If the event that
|
|
|
567 |
* has just occurred (e.g. delete post) can only result in making the activity
|
|
|
568 |
* not complete when it was previously complete, use COMPLETION_INCOMPLETE.
|
|
|
569 |
* Otherwise use COMPLETION_UNKNOWN. Setting this value to something other than
|
|
|
570 |
* COMPLETION_UNKNOWN significantly improves performance because it will abandon
|
|
|
571 |
* processing early if the user's completion state already matches the expected
|
|
|
572 |
* result. For manual events, COMPLETION_COMPLETE or COMPLETION_INCOMPLETE
|
|
|
573 |
* must be used; these directly set the specified state.
|
|
|
574 |
* @param int $userid User ID to be updated. Default 0 = current user
|
|
|
575 |
* @param bool $override Whether manually overriding the existing completion state.
|
|
|
576 |
* @param bool $isbulkupdate If bulk grade update is happening.
|
|
|
577 |
* @return void
|
|
|
578 |
* @throws moodle_exception if trying to override without permission.
|
|
|
579 |
*/
|
|
|
580 |
public function update_state($cm, $possibleresult=COMPLETION_UNKNOWN, $userid=0,
|
|
|
581 |
$override = false, $isbulkupdate = false) {
|
|
|
582 |
global $USER;
|
|
|
583 |
|
|
|
584 |
// Do nothing if completion is not enabled for that activity
|
|
|
585 |
if (!$this->is_enabled($cm)) {
|
|
|
586 |
return;
|
|
|
587 |
}
|
|
|
588 |
|
|
|
589 |
// If we're processing an override and the current user isn't allowed to do so, then throw an exception.
|
|
|
590 |
if ($override) {
|
|
|
591 |
if (!$this->user_can_override_completion($USER)) {
|
|
|
592 |
throw new required_capability_exception(context_course::instance($this->course_id),
|
|
|
593 |
'moodle/course:overridecompletion', 'nopermission', '');
|
|
|
594 |
}
|
|
|
595 |
}
|
|
|
596 |
|
|
|
597 |
// Default to current user if one is not provided.
|
|
|
598 |
if ($userid == 0) {
|
|
|
599 |
$userid = $USER->id;
|
|
|
600 |
}
|
|
|
601 |
|
|
|
602 |
// Delete the cm's cached completion data for this user if automatic completion is enabled.
|
|
|
603 |
// This ensures any changes to the status of individual completion conditions in the activity will be fetched.
|
|
|
604 |
if ($cm->completion == COMPLETION_TRACKING_AUTOMATIC) {
|
|
|
605 |
$completioncache = cache::make('core', 'completion');
|
|
|
606 |
$completionkey = $userid . '_' . $this->course->id;
|
|
|
607 |
$completiondata = $completioncache->get($completionkey);
|
|
|
608 |
|
|
|
609 |
if ($completiondata !== false) {
|
|
|
610 |
unset($completiondata[$cm->id]);
|
|
|
611 |
$completioncache->set($completionkey, $completiondata);
|
|
|
612 |
}
|
|
|
613 |
}
|
|
|
614 |
|
|
|
615 |
// Get current value of completion state and do nothing if it's same as
|
|
|
616 |
// the possible result of this change. If the change is to COMPLETE and the
|
|
|
617 |
// current value is one of the COMPLETE_xx subtypes, ignore that as well
|
|
|
618 |
$current = $this->get_data($cm, false, $userid);
|
|
|
619 |
if ($possibleresult == $current->completionstate ||
|
|
|
620 |
($possibleresult == COMPLETION_COMPLETE &&
|
|
|
621 |
($current->completionstate == COMPLETION_COMPLETE_PASS ||
|
|
|
622 |
$current->completionstate == COMPLETION_COMPLETE_FAIL))) {
|
|
|
623 |
return;
|
|
|
624 |
}
|
|
|
625 |
|
|
|
626 |
// The activity completion alters the course state cache for this particular user.
|
|
|
627 |
$course = get_course($cm->course);
|
|
|
628 |
if ($course) {
|
|
|
629 |
course_format::session_cache_reset($course);
|
|
|
630 |
}
|
|
|
631 |
|
|
|
632 |
// For auto tracking, if the status is overridden to 'COMPLETION_COMPLETE', then disallow further changes,
|
|
|
633 |
// unless processing another override.
|
|
|
634 |
// Basically, we want those activities which have been overridden to COMPLETE to hold state, and those which have been
|
|
|
635 |
// overridden to INCOMPLETE to still be processed by normal completion triggers.
|
|
|
636 |
if ($cm->completion == COMPLETION_TRACKING_AUTOMATIC && !is_null($current->overrideby)
|
|
|
637 |
&& $current->completionstate == COMPLETION_COMPLETE && !$override) {
|
|
|
638 |
return;
|
|
|
639 |
}
|
|
|
640 |
|
|
|
641 |
// For manual tracking, or if overriding the completion state, we set the state directly.
|
|
|
642 |
if ($cm->completion == COMPLETION_TRACKING_MANUAL || $override) {
|
|
|
643 |
switch($possibleresult) {
|
|
|
644 |
case COMPLETION_COMPLETE:
|
|
|
645 |
case COMPLETION_INCOMPLETE:
|
|
|
646 |
$newstate = $possibleresult;
|
|
|
647 |
break;
|
|
|
648 |
default:
|
|
|
649 |
$this->internal_systemerror("Unexpected manual completion state for {$cm->id}: $possibleresult");
|
|
|
650 |
}
|
|
|
651 |
|
|
|
652 |
} else {
|
|
|
653 |
$newstate = $this->internal_get_state($cm, $userid, $current);
|
|
|
654 |
}
|
|
|
655 |
|
|
|
656 |
// If the overall completion state has changed, update it in the cache.
|
|
|
657 |
if ($newstate != $current->completionstate) {
|
|
|
658 |
$current->completionstate = $newstate;
|
|
|
659 |
$current->timemodified = time();
|
|
|
660 |
$current->overrideby = $override ? $USER->id : null;
|
|
|
661 |
$this->internal_set_data($cm, $current, $isbulkupdate);
|
|
|
662 |
}
|
|
|
663 |
}
|
|
|
664 |
|
|
|
665 |
/**
|
|
|
666 |
* Calculates the completion state for an activity and user.
|
|
|
667 |
*
|
|
|
668 |
* Internal function. Not private, so we can unit-test it.
|
|
|
669 |
*
|
|
|
670 |
* @param stdClass|cm_info $cm Activity
|
|
|
671 |
* @param int $userid ID of user
|
|
|
672 |
* @param stdClass $current Previous completion information from database
|
|
|
673 |
* @return mixed
|
|
|
674 |
*/
|
|
|
675 |
public function internal_get_state($cm, $userid, $current) {
|
|
|
676 |
global $USER, $DB;
|
|
|
677 |
|
|
|
678 |
// Get user ID
|
|
|
679 |
if (!$userid) {
|
|
|
680 |
$userid = $USER->id;
|
|
|
681 |
}
|
|
|
682 |
|
|
|
683 |
$newstate = COMPLETION_COMPLETE;
|
|
|
684 |
if ($cm instanceof stdClass) {
|
|
|
685 |
// Modname hopefully is provided in $cm but just in case it isn't, let's grab it.
|
|
|
686 |
if (!isset($cm->modname)) {
|
|
|
687 |
$cm->modname = $DB->get_field('modules', 'name', array('id' => $cm->module));
|
|
|
688 |
}
|
|
|
689 |
// Some functions call this method and pass $cm as an object with ID only. Make sure course is set as well.
|
|
|
690 |
if (!isset($cm->course)) {
|
|
|
691 |
$cm->course = $this->course_id;
|
|
|
692 |
}
|
|
|
693 |
}
|
|
|
694 |
// Make sure we're using a cm_info object.
|
|
|
695 |
$cminfo = cm_info::create($cm, $userid);
|
|
|
696 |
$completionstate = $this->get_core_completion_state($cminfo, $userid);
|
|
|
697 |
|
|
|
698 |
if (plugin_supports('mod', $cminfo->modname, FEATURE_COMPLETION_HAS_RULES)) {
|
|
|
699 |
$cmcompletionclass = activity_custom_completion::get_cm_completion_class($cminfo->modname);
|
|
|
700 |
if ($cmcompletionclass) {
|
|
|
701 |
/** @var activity_custom_completion $cmcompletion */
|
|
|
702 |
$cmcompletion = new $cmcompletionclass($cminfo, $userid, $completionstate);
|
|
|
703 |
$customstate = $cmcompletion->get_overall_completion_state();
|
|
|
704 |
if ($customstate == COMPLETION_INCOMPLETE) {
|
|
|
705 |
return $customstate;
|
|
|
706 |
}
|
|
|
707 |
$completionstate[] = $customstate;
|
|
|
708 |
}
|
|
|
709 |
}
|
|
|
710 |
|
|
|
711 |
if ($completionstate) {
|
|
|
712 |
// We have allowed the plugins to do it's thing and run their own checks.
|
|
|
713 |
// We have now reached a state where we need to AND all the calculated results.
|
|
|
714 |
// Preference for COMPLETION_COMPLETE_PASS over COMPLETION_COMPLETE for proper indication in reports.
|
|
|
715 |
$newstate = array_reduce($completionstate, function($carry, $value) {
|
|
|
716 |
if (in_array(COMPLETION_INCOMPLETE, [$carry, $value])) {
|
|
|
717 |
return COMPLETION_INCOMPLETE;
|
|
|
718 |
} else if (in_array(COMPLETION_COMPLETE_FAIL, [$carry, $value])) {
|
|
|
719 |
return COMPLETION_COMPLETE_FAIL;
|
|
|
720 |
} else {
|
|
|
721 |
return in_array(COMPLETION_COMPLETE_PASS, [$carry, $value]) ? COMPLETION_COMPLETE_PASS : $value;
|
|
|
722 |
}
|
|
|
723 |
|
|
|
724 |
}, COMPLETION_COMPLETE);
|
|
|
725 |
}
|
|
|
726 |
|
|
|
727 |
return $newstate;
|
|
|
728 |
|
|
|
729 |
}
|
|
|
730 |
|
|
|
731 |
/**
|
|
|
732 |
* Fetches the completion state for an activity completion's require grade completion requirement.
|
|
|
733 |
*
|
|
|
734 |
* @param cm_info $cm The course module information.
|
|
|
735 |
* @param int $userid The user ID.
|
|
|
736 |
* @return int The completion state.
|
|
|
737 |
*/
|
|
|
738 |
public function get_grade_completion(cm_info $cm, int $userid): int {
|
|
|
739 |
global $CFG;
|
|
|
740 |
|
|
|
741 |
require_once($CFG->libdir . '/gradelib.php');
|
|
|
742 |
$item = grade_item::fetch([
|
|
|
743 |
'courseid' => $cm->course,
|
|
|
744 |
'itemtype' => 'mod',
|
|
|
745 |
'itemmodule' => $cm->modname,
|
|
|
746 |
'iteminstance' => $cm->instance,
|
|
|
747 |
'itemnumber' => $cm->completiongradeitemnumber
|
|
|
748 |
]);
|
|
|
749 |
if ($item) {
|
|
|
750 |
// Fetch 'grades' (will be one or none).
|
|
|
751 |
$grades = grade_grade::fetch_users_grades($item, [$userid], false);
|
|
|
752 |
if (empty($grades)) {
|
|
|
753 |
// No grade for user.
|
|
|
754 |
return COMPLETION_INCOMPLETE;
|
|
|
755 |
}
|
|
|
756 |
if (count($grades) > 1) {
|
|
|
757 |
$this->internal_systemerror("Unexpected result: multiple grades for
|
|
|
758 |
item '{$item->id}', user '{$userid}'");
|
|
|
759 |
}
|
|
|
760 |
$returnpassfail = !empty($cm->completionpassgrade);
|
|
|
761 |
return self::internal_get_grade_state($item, reset($grades), $returnpassfail);
|
|
|
762 |
}
|
|
|
763 |
|
|
|
764 |
return COMPLETION_INCOMPLETE;
|
|
|
765 |
}
|
|
|
766 |
|
|
|
767 |
/**
|
|
|
768 |
* Marks a module as viewed.
|
|
|
769 |
*
|
|
|
770 |
* Should be called whenever a module is 'viewed' (it is up to the module how to
|
|
|
771 |
* determine that). Has no effect if viewing is not set as a completion condition.
|
|
|
772 |
*
|
|
|
773 |
* Note that this function must be called before you print the page header because
|
|
|
774 |
* it is possible that the navigation block may depend on it. If you call it after
|
|
|
775 |
* printing the header, it shows a developer debug warning.
|
|
|
776 |
*
|
|
|
777 |
* @param stdClass|cm_info $cm Activity
|
|
|
778 |
* @param int $userid User ID or 0 (default) for current user
|
|
|
779 |
* @return void
|
|
|
780 |
*/
|
|
|
781 |
public function set_module_viewed($cm, $userid=0) {
|
|
|
782 |
global $PAGE;
|
|
|
783 |
if ($PAGE->headerprinted) {
|
|
|
784 |
debugging('set_module_viewed must be called before header is printed',
|
|
|
785 |
DEBUG_DEVELOPER);
|
|
|
786 |
}
|
|
|
787 |
|
|
|
788 |
// Don't do anything if view condition is not turned on
|
|
|
789 |
if ($cm->completionview == COMPLETION_VIEW_NOT_REQUIRED || !$this->is_enabled($cm)) {
|
|
|
790 |
return;
|
|
|
791 |
}
|
|
|
792 |
|
|
|
793 |
// Get current completion state
|
|
|
794 |
$data = $this->get_data($cm, false, $userid);
|
|
|
795 |
|
|
|
796 |
// If we already viewed it, don't do anything unless the completion status is overridden.
|
|
|
797 |
// If the completion status is overridden, then we need to allow this 'view' to trigger automatic completion again.
|
|
|
798 |
if ($data->viewed == COMPLETION_VIEWED && empty($data->overrideby)) {
|
|
|
799 |
return;
|
|
|
800 |
}
|
|
|
801 |
|
|
|
802 |
// OK, change state, save it, and update completion
|
|
|
803 |
$data->viewed = COMPLETION_VIEWED;
|
|
|
804 |
$this->internal_set_data($cm, $data);
|
|
|
805 |
$this->update_state($cm, COMPLETION_COMPLETE, $userid);
|
|
|
806 |
}
|
|
|
807 |
|
|
|
808 |
/**
|
|
|
809 |
* Determines how much completion data exists for an activity. This is used when
|
|
|
810 |
* deciding whether completion information should be 'locked' in the module
|
|
|
811 |
* editing form.
|
|
|
812 |
*
|
|
|
813 |
* @param cm_info $cm Activity
|
|
|
814 |
* @return int The number of users who have completion data stored for this
|
|
|
815 |
* activity, 0 if none
|
|
|
816 |
*/
|
|
|
817 |
public function count_user_data($cm) {
|
|
|
818 |
global $DB;
|
|
|
819 |
|
|
|
820 |
return $DB->get_field_sql("
|
|
|
821 |
SELECT
|
|
|
822 |
COUNT(1)
|
|
|
823 |
FROM
|
|
|
824 |
{course_modules_completion}
|
|
|
825 |
WHERE
|
|
|
826 |
coursemoduleid=? AND completionstate<>0", array($cm->id));
|
|
|
827 |
}
|
|
|
828 |
|
|
|
829 |
/**
|
|
|
830 |
* Determines how much course completion data exists for a course. This is used when
|
|
|
831 |
* deciding whether completion information should be 'locked' in the completion
|
|
|
832 |
* settings form and activity completion settings.
|
|
|
833 |
*
|
|
|
834 |
* @param int $user_id Optionally only get course completion data for a single user
|
|
|
835 |
* @return int The number of users who have completion data stored for this
|
|
|
836 |
* course, 0 if none
|
|
|
837 |
*/
|
|
|
838 |
public function count_course_user_data($user_id = null) {
|
|
|
839 |
global $DB;
|
|
|
840 |
|
|
|
841 |
$sql = '
|
|
|
842 |
SELECT
|
|
|
843 |
COUNT(1)
|
|
|
844 |
FROM
|
|
|
845 |
{course_completion_crit_compl}
|
|
|
846 |
WHERE
|
|
|
847 |
course = ?
|
|
|
848 |
';
|
|
|
849 |
|
|
|
850 |
$params = array($this->course_id);
|
|
|
851 |
|
|
|
852 |
// Limit data to a single user if an ID is supplied
|
|
|
853 |
if ($user_id) {
|
|
|
854 |
$sql .= ' AND userid = ?';
|
|
|
855 |
$params[] = $user_id;
|
|
|
856 |
}
|
|
|
857 |
|
|
|
858 |
return $DB->get_field_sql($sql, $params);
|
|
|
859 |
}
|
|
|
860 |
|
|
|
861 |
/**
|
|
|
862 |
* Check if this course's completion criteria should be locked
|
|
|
863 |
*
|
|
|
864 |
* @return boolean
|
|
|
865 |
*/
|
|
|
866 |
public function is_course_locked() {
|
|
|
867 |
return (bool) $this->count_course_user_data();
|
|
|
868 |
}
|
|
|
869 |
|
|
|
870 |
/**
|
|
|
871 |
* Deletes all course completion completion data.
|
|
|
872 |
*
|
|
|
873 |
* Intended to be used when unlocking completion criteria settings.
|
|
|
874 |
*/
|
|
|
875 |
public function delete_course_completion_data() {
|
|
|
876 |
global $DB;
|
|
|
877 |
|
|
|
878 |
$DB->delete_records('course_completions', array('course' => $this->course_id));
|
|
|
879 |
$DB->delete_records('course_completion_crit_compl', array('course' => $this->course_id));
|
|
|
880 |
|
|
|
881 |
// Difficult to find affected users, just purge all completion cache.
|
|
|
882 |
cache::make('core', 'completion')->purge();
|
|
|
883 |
cache::make('core', 'coursecompletion')->purge();
|
|
|
884 |
}
|
|
|
885 |
|
|
|
886 |
/**
|
|
|
887 |
* Deletes all activity and course completion data for an entire course
|
|
|
888 |
* (the below delete_all_state function does this for a single activity).
|
|
|
889 |
*
|
|
|
890 |
* Used by course reset page.
|
|
|
891 |
*/
|
|
|
892 |
public function delete_all_completion_data() {
|
|
|
893 |
global $DB;
|
|
|
894 |
|
|
|
895 |
// Delete from database.
|
|
|
896 |
$DB->delete_records_select('course_modules_completion',
|
|
|
897 |
'coursemoduleid IN (SELECT id FROM {course_modules} WHERE course=?)',
|
|
|
898 |
array($this->course_id));
|
|
|
899 |
$DB->delete_records_select('course_modules_viewed',
|
|
|
900 |
'coursemoduleid IN (SELECT id FROM {course_modules} WHERE course=?)',
|
|
|
901 |
[$this->course_id]);
|
|
|
902 |
// Wipe course completion data too.
|
|
|
903 |
$this->delete_course_completion_data();
|
|
|
904 |
}
|
|
|
905 |
|
|
|
906 |
/**
|
|
|
907 |
* Deletes completion state related to an activity for all users.
|
|
|
908 |
*
|
|
|
909 |
* Intended for use only when the activity itself is deleted.
|
|
|
910 |
*
|
|
|
911 |
* @param stdClass|cm_info $cm Activity
|
|
|
912 |
*/
|
|
|
913 |
public function delete_all_state($cm) {
|
|
|
914 |
global $DB;
|
|
|
915 |
|
|
|
916 |
// Delete from database
|
|
|
917 |
$DB->delete_records('course_modules_completion', array('coursemoduleid'=>$cm->id));
|
|
|
918 |
|
|
|
919 |
// Check if there is an associated course completion criteria
|
|
|
920 |
$criteria = $this->get_criteria(COMPLETION_CRITERIA_TYPE_ACTIVITY);
|
|
|
921 |
$acriteria = false;
|
|
|
922 |
foreach ($criteria as $criterion) {
|
|
|
923 |
if ($criterion->moduleinstance == $cm->id) {
|
|
|
924 |
$acriteria = $criterion;
|
|
|
925 |
break;
|
|
|
926 |
}
|
|
|
927 |
}
|
|
|
928 |
|
|
|
929 |
if ($acriteria) {
|
|
|
930 |
// Delete all criteria completions relating to this activity
|
|
|
931 |
$DB->delete_records('course_completion_crit_compl', array('course' => $this->course_id, 'criteriaid' => $acriteria->id));
|
|
|
932 |
$DB->delete_records('course_completions', array('course' => $this->course_id));
|
|
|
933 |
}
|
|
|
934 |
|
|
|
935 |
// Difficult to find affected users, just purge all completion cache.
|
|
|
936 |
cache::make('core', 'completion')->purge();
|
|
|
937 |
cache::make('core', 'coursecompletion')->purge();
|
|
|
938 |
}
|
|
|
939 |
|
|
|
940 |
/**
|
|
|
941 |
* Recalculates completion state related to an activity for all users.
|
|
|
942 |
*
|
|
|
943 |
* Intended for use if completion conditions change. (This should be avoided
|
|
|
944 |
* as it may cause some things to become incomplete when they were previously
|
|
|
945 |
* complete, with the effect - for example - of hiding a later activity that
|
|
|
946 |
* was previously available.)
|
|
|
947 |
*
|
|
|
948 |
* Resetting state of manual tickbox has same result as deleting state for
|
|
|
949 |
* it.
|
|
|
950 |
*
|
|
|
951 |
* @param stdClass|cm_info $cm Activity
|
|
|
952 |
*/
|
|
|
953 |
public function reset_all_state($cm) {
|
|
|
954 |
global $DB;
|
|
|
955 |
|
|
|
956 |
if ($cm->completion == COMPLETION_TRACKING_MANUAL) {
|
|
|
957 |
$this->delete_all_state($cm);
|
|
|
958 |
return;
|
|
|
959 |
}
|
|
|
960 |
// Get current list of users with completion state
|
|
|
961 |
$rs = $DB->get_recordset('course_modules_completion', array('coursemoduleid'=>$cm->id), '', 'userid');
|
|
|
962 |
$keepusers = array();
|
|
|
963 |
foreach ($rs as $rec) {
|
|
|
964 |
$keepusers[] = $rec->userid;
|
|
|
965 |
}
|
|
|
966 |
$rs->close();
|
|
|
967 |
|
|
|
968 |
// Delete all existing state.
|
|
|
969 |
$this->delete_all_state($cm);
|
|
|
970 |
|
|
|
971 |
// Merge this with list of planned users (according to roles)
|
|
|
972 |
$trackedusers = $this->get_tracked_users();
|
|
|
973 |
foreach ($trackedusers as $trackeduser) {
|
|
|
974 |
$keepusers[] = $trackeduser->id;
|
|
|
975 |
}
|
|
|
976 |
$keepusers = array_unique($keepusers);
|
|
|
977 |
|
|
|
978 |
// Recalculate state for each kept user
|
|
|
979 |
foreach ($keepusers as $keepuser) {
|
|
|
980 |
$this->update_state($cm, COMPLETION_UNKNOWN, $keepuser);
|
|
|
981 |
}
|
|
|
982 |
}
|
|
|
983 |
|
|
|
984 |
/**
|
|
|
985 |
* Obtains completion data for a particular activity and user (from the
|
|
|
986 |
* completion cache if available, or by SQL query)
|
|
|
987 |
*
|
|
|
988 |
* @param stdClass|cm_info $cm Activity; only required field is ->id
|
|
|
989 |
* @param bool $wholecourse If true (default false) then, when necessary to
|
|
|
990 |
* fill the cache, retrieves information from the entire course not just for
|
|
|
991 |
* this one activity
|
|
|
992 |
* @param int $userid User ID or 0 (default) for current user
|
|
|
993 |
* @param mixed $unused This parameter has been deprecated since 4.0 and should not be used anymore.
|
|
|
994 |
* @return object Completion data. Record from course_modules_completion plus other completion statuses such as
|
|
|
995 |
* - Completion status for 'must-receive-grade' completion rule.
|
|
|
996 |
* - Custom completion statuses defined by the activity module plugin.
|
|
|
997 |
*/
|
|
|
998 |
public function get_data($cm, $wholecourse = false, $userid = 0, $unused = null) {
|
|
|
999 |
global $USER, $DB;
|
|
|
1000 |
|
|
|
1001 |
if ($unused !== null) {
|
|
|
1002 |
debugging('Deprecated argument passed to ' . __FUNCTION__, DEBUG_DEVELOPER);
|
|
|
1003 |
}
|
|
|
1004 |
|
|
|
1005 |
$completioncache = cache::make('core', 'completion');
|
|
|
1006 |
|
|
|
1007 |
// Get user ID
|
|
|
1008 |
if (!$userid) {
|
|
|
1009 |
$userid = $USER->id;
|
|
|
1010 |
}
|
|
|
1011 |
|
|
|
1012 |
// Some call completion_info::get_data and pass $cm as an object with ID only. Make sure course is set as well.
|
|
|
1013 |
if ($cm instanceof stdClass && !isset($cm->course)) {
|
|
|
1014 |
$cm->course = $this->course_id;
|
|
|
1015 |
}
|
|
|
1016 |
// Make sure we're working on a cm_info object.
|
|
|
1017 |
$cminfo = cm_info::create($cm, $userid);
|
|
|
1018 |
|
|
|
1019 |
// Create an anonymous function to remove the 'other_cm_completion_data_fetched' key.
|
|
|
1020 |
$returnfilteredvalue = function(array $value): stdClass {
|
|
|
1021 |
return (object) array_filter($value, function(string $key): bool {
|
|
|
1022 |
return $key !== 'other_cm_completion_data_fetched';
|
|
|
1023 |
}, ARRAY_FILTER_USE_KEY);
|
|
|
1024 |
};
|
|
|
1025 |
|
|
|
1026 |
// See if requested data is present in cache (use cache for current user only).
|
|
|
1027 |
$usecache = $userid == $USER->id;
|
|
|
1028 |
$cacheddata = array();
|
|
|
1029 |
if ($usecache) {
|
|
|
1030 |
$key = $userid . '_' . $this->course->id;
|
|
|
1031 |
if (!isset($this->course->cacherev)) {
|
|
|
1032 |
$this->course = get_course($this->course_id);
|
|
|
1033 |
}
|
|
|
1034 |
if ($cacheddata = ($completioncache->get($key) ?: [])) {
|
|
|
1035 |
if ($cacheddata['cacherev'] != $this->course->cacherev) {
|
|
|
1036 |
// Course structure has been changed since the last caching, forget the cache.
|
|
|
1037 |
$cacheddata = array();
|
|
|
1038 |
} else if (isset($cacheddata[$cminfo->id])) {
|
|
|
1039 |
$data = (array) $cacheddata[$cminfo->id];
|
|
|
1040 |
if (empty($data['other_cm_completion_data_fetched'])) {
|
|
|
1041 |
$data += $this->get_other_cm_completion_data($cminfo, $userid);
|
|
|
1042 |
$data['other_cm_completion_data_fetched'] = true;
|
|
|
1043 |
|
|
|
1044 |
// Put in cache.
|
|
|
1045 |
$cacheddata[$cminfo->id] = $data;
|
|
|
1046 |
$completioncache->set($key, $cacheddata);
|
|
|
1047 |
}
|
|
|
1048 |
|
|
|
1049 |
return $returnfilteredvalue($cacheddata[$cminfo->id]);
|
|
|
1050 |
}
|
|
|
1051 |
}
|
|
|
1052 |
}
|
|
|
1053 |
|
|
|
1054 |
// Default data to return when no completion data is found.
|
|
|
1055 |
$defaultdata = [
|
|
|
1056 |
'id' => 0,
|
|
|
1057 |
'coursemoduleid' => $cminfo->id,
|
|
|
1058 |
'userid' => $userid,
|
|
|
1059 |
'completionstate' => 0,
|
|
|
1060 |
'viewed' => 0,
|
|
|
1061 |
'overrideby' => null,
|
|
|
1062 |
'timemodified' => 0,
|
|
|
1063 |
];
|
|
|
1064 |
|
|
|
1065 |
// If cached completion data is not found, fetch via SQL.
|
|
|
1066 |
// Fetch completion data for all of the activities in the course ONLY if we're caching the fetched completion data.
|
|
|
1067 |
// If we're not caching the completion data, then just fetch the completion data for the user in this course module.
|
|
|
1068 |
if ($usecache && $wholecourse) {
|
|
|
1069 |
// Get whole course data for cache.
|
|
|
1070 |
$alldatabycmc = $DB->get_records_sql("SELECT cm.id AS cmid, cmc.*,
|
|
|
1071 |
CASE WHEN cmv.id IS NULL THEN 0 ELSE 1 END AS viewed
|
|
|
1072 |
FROM {course_modules} cm
|
|
|
1073 |
LEFT JOIN {course_modules_completion} cmc
|
|
|
1074 |
ON cmc.coursemoduleid = cm.id AND cmc.userid = ?
|
|
|
1075 |
LEFT JOIN {course_modules_viewed} cmv
|
|
|
1076 |
ON cmv.coursemoduleid = cm.id AND cmv.userid = ?
|
|
|
1077 |
INNER JOIN {modules} m ON m.id = cm.module
|
|
|
1078 |
WHERE m.visible = 1 AND cm.course = ?",
|
|
|
1079 |
[$userid, $userid, $this->course->id]);
|
|
|
1080 |
$cminfos = get_fast_modinfo($cm->course, $userid)->get_cms();
|
|
|
1081 |
|
|
|
1082 |
// Reindex by course module id.
|
|
|
1083 |
foreach ($alldatabycmc as $data) {
|
|
|
1084 |
|
|
|
1085 |
// Filter acitivites with no cm_info (missing plugins or other causes).
|
|
|
1086 |
if (!isset($cminfos[$data->cmid])) {
|
|
|
1087 |
continue;
|
|
|
1088 |
}
|
|
|
1089 |
|
|
|
1090 |
if (empty($data->coursemoduleid)) {
|
|
|
1091 |
$cacheddata[$data->cmid] = $defaultdata;
|
|
|
1092 |
if ($data->viewed) {
|
|
|
1093 |
$cacheddata[$data->cmid]['viewed'] = $data->viewed;
|
|
|
1094 |
}
|
|
|
1095 |
$cacheddata[$data->cmid]['coursemoduleid'] = $data->cmid;
|
|
|
1096 |
} else {
|
|
|
1097 |
unset($data->cmid);
|
|
|
1098 |
$cacheddata[$data->coursemoduleid] = (array) $data;
|
|
|
1099 |
}
|
|
|
1100 |
}
|
|
|
1101 |
|
|
|
1102 |
if (!isset($cacheddata[$cminfo->id])) {
|
|
|
1103 |
$errormessage = "Unexpected error: course-module {$cminfo->id} could not be found on course {$this->course->id}";
|
|
|
1104 |
$this->internal_systemerror($errormessage);
|
|
|
1105 |
}
|
|
|
1106 |
|
|
|
1107 |
$data = $cacheddata[$cminfo->id];
|
|
|
1108 |
} else {
|
|
|
1109 |
// Get single record
|
|
|
1110 |
$data = $this->get_completion_data($cminfo->id, $userid, $defaultdata);
|
|
|
1111 |
// Put in cache.
|
|
|
1112 |
$cacheddata[$cminfo->id] = $data;
|
|
|
1113 |
}
|
|
|
1114 |
|
|
|
1115 |
// Fill the other completion data for this user in this module instance.
|
|
|
1116 |
$data += $this->get_other_cm_completion_data($cminfo, $userid);
|
|
|
1117 |
$data['other_cm_completion_data_fetched'] = true;
|
|
|
1118 |
|
|
|
1119 |
// Put in cache
|
|
|
1120 |
$cacheddata[$cminfo->id] = $data;
|
|
|
1121 |
|
|
|
1122 |
if ($usecache) {
|
|
|
1123 |
$cacheddata['cacherev'] = $this->course->cacherev;
|
|
|
1124 |
$completioncache->set($key, $cacheddata);
|
|
|
1125 |
}
|
|
|
1126 |
|
|
|
1127 |
return $returnfilteredvalue($cacheddata[$cminfo->id]);
|
|
|
1128 |
}
|
|
|
1129 |
|
|
|
1130 |
/**
|
|
|
1131 |
* Get the latest completion state for each criteria used in the module
|
|
|
1132 |
*
|
|
|
1133 |
* @param cm_info $cm The corresponding module's information
|
|
|
1134 |
* @param int $userid The id for the user we are calculating core completion state
|
|
|
1135 |
* @return array $data The individualised core completion state used in the module.
|
|
|
1136 |
* Consists of the following keys completiongrade, passgrade, viewed
|
|
|
1137 |
*/
|
|
|
1138 |
public function get_core_completion_state(cm_info $cm, int $userid): array {
|
|
|
1139 |
global $DB;
|
|
|
1140 |
$data = [];
|
|
|
1141 |
// Include in the completion info the grade completion, if necessary.
|
|
|
1142 |
if (!is_null($cm->completiongradeitemnumber)) {
|
|
|
1143 |
$newstate = $this->get_grade_completion($cm, $userid);
|
|
|
1144 |
$data['completiongrade'] = $newstate;
|
|
|
1145 |
|
|
|
1146 |
if ($cm->completionpassgrade) {
|
|
|
1147 |
// If we are asking to use pass grade completion but haven't set it properly,
|
|
|
1148 |
// then default to COMPLETION_COMPLETE_PASS.
|
|
|
1149 |
if ($newstate == COMPLETION_COMPLETE) {
|
|
|
1150 |
$newstate = COMPLETION_COMPLETE_PASS;
|
|
|
1151 |
}
|
|
|
1152 |
|
|
|
1153 |
// No need to show failing status for the completiongrade condition when passing grade condition is set.
|
|
|
1154 |
if (in_array($newstate, [COMPLETION_COMPLETE_FAIL, COMPLETION_COMPLETE_FAIL_HIDDEN])) {
|
|
|
1155 |
$data['completiongrade'] = COMPLETION_COMPLETE;
|
|
|
1156 |
|
|
|
1157 |
// If the grade received by the user is a failing grade for a hidden grade item,
|
|
|
1158 |
// the 'Require passing grade' criterion is considered incomplete.
|
|
|
1159 |
if ($newstate == COMPLETION_COMPLETE_FAIL_HIDDEN) {
|
|
|
1160 |
$newstate = COMPLETION_INCOMPLETE;
|
|
|
1161 |
}
|
|
|
1162 |
}
|
|
|
1163 |
$data['passgrade'] = $newstate;
|
|
|
1164 |
}
|
|
|
1165 |
}
|
|
|
1166 |
|
|
|
1167 |
// If view is required, try and fetch from the db. In some cases, cache can be invalid.
|
|
|
1168 |
if ($cm->completionview == COMPLETION_VIEW_REQUIRED) {
|
|
|
1169 |
$data['viewed'] = COMPLETION_INCOMPLETE;
|
|
|
1170 |
$record = $DB->record_exists('course_modules_viewed', ['coursemoduleid' => $cm->id, 'userid' => $userid]);
|
|
|
1171 |
$data['viewed'] = $record ? COMPLETION_COMPLETE : COMPLETION_INCOMPLETE;
|
|
|
1172 |
}
|
|
|
1173 |
|
|
|
1174 |
return $data;
|
|
|
1175 |
}
|
|
|
1176 |
|
|
|
1177 |
/**
|
|
|
1178 |
* Adds the user's custom completion data on the given course module.
|
|
|
1179 |
*
|
|
|
1180 |
* @param cm_info $cm The course module information.
|
|
|
1181 |
* @param int $userid The user ID.
|
|
|
1182 |
* @return array The additional completion data.
|
|
|
1183 |
*/
|
|
|
1184 |
protected function get_other_cm_completion_data(cm_info $cm, int $userid): array {
|
|
|
1185 |
$data = $this->get_core_completion_state($cm, $userid);
|
|
|
1186 |
|
|
|
1187 |
// Custom activity module completion data.
|
|
|
1188 |
|
|
|
1189 |
// Cast custom data to array before checking for custom completion rules.
|
|
|
1190 |
// We call ->get_custom_data() instead of ->customdata here because there is the chance of recursive calling,
|
|
|
1191 |
// and we cannot call a getter from a getter in PHP.
|
|
|
1192 |
$customdata = (array) $cm->get_custom_data();
|
|
|
1193 |
// Return early if the plugin does not define custom completion rules.
|
|
|
1194 |
if (empty($customdata['customcompletionrules'])) {
|
|
|
1195 |
return $data;
|
|
|
1196 |
}
|
|
|
1197 |
|
|
|
1198 |
// Return early if the activity modules doe not implement the activity_custom_completion class.
|
|
|
1199 |
$cmcompletionclass = activity_custom_completion::get_cm_completion_class($cm->modname);
|
|
|
1200 |
if (!$cmcompletionclass) {
|
|
|
1201 |
return $data;
|
|
|
1202 |
}
|
|
|
1203 |
|
|
|
1204 |
/** @var activity_custom_completion $customcmcompletion */
|
|
|
1205 |
$customcmcompletion = new $cmcompletionclass($cm, $userid, $data);
|
|
|
1206 |
foreach ($customdata['customcompletionrules'] as $rule => $enabled) {
|
|
|
1207 |
if (!$enabled) {
|
|
|
1208 |
// Skip inactive completion rules.
|
|
|
1209 |
continue;
|
|
|
1210 |
}
|
|
|
1211 |
// Get this custom completion rule's completion state.
|
|
|
1212 |
$data['customcompletion'][$rule] = $customcmcompletion->get_state($rule);
|
|
|
1213 |
}
|
|
|
1214 |
|
|
|
1215 |
return $data;
|
|
|
1216 |
}
|
|
|
1217 |
|
|
|
1218 |
/**
|
|
|
1219 |
* Updates completion data for a particular coursemodule and user (user is
|
|
|
1220 |
* determined from $data).
|
|
|
1221 |
*
|
|
|
1222 |
* (Internal function. Not private, so we can unit-test it.)
|
|
|
1223 |
*
|
|
|
1224 |
* @param stdClass|cm_info $cm Activity
|
|
|
1225 |
* @param stdClass $data Data about completion for that user
|
|
|
1226 |
* @param bool $isbulkupdate If bulk grade update is happening.
|
|
|
1227 |
*/
|
|
|
1228 |
public function internal_set_data($cm, $data, $isbulkupdate = false) {
|
|
|
1229 |
global $USER, $DB, $CFG;
|
|
|
1230 |
require_once($CFG->dirroot.'/completion/criteria/completion_criteria_activity.php');
|
|
|
1231 |
|
|
|
1232 |
$transaction = $DB->start_delegated_transaction();
|
|
|
1233 |
if (!$data->id) {
|
|
|
1234 |
// Check there isn't really a row
|
|
|
1235 |
$data->id = $DB->get_field('course_modules_completion', 'id',
|
|
|
1236 |
array('coursemoduleid'=>$data->coursemoduleid, 'userid'=>$data->userid));
|
|
|
1237 |
}
|
|
|
1238 |
if (!$data->id) {
|
|
|
1239 |
// Didn't exist before, needs creating
|
|
|
1240 |
$data->id = $DB->insert_record('course_modules_completion', $data);
|
|
|
1241 |
} else {
|
|
|
1242 |
// Has real (nonzero) id meaning that a database row exists, update
|
|
|
1243 |
$DB->update_record('course_modules_completion', $data);
|
|
|
1244 |
}
|
|
|
1245 |
$dataview = new stdClass();
|
|
|
1246 |
$dataview->coursemoduleid = $data->coursemoduleid;
|
|
|
1247 |
$dataview->userid = $data->userid;
|
|
|
1248 |
$dataview->id = $DB->get_field('course_modules_viewed', 'id',
|
|
|
1249 |
['coursemoduleid' => $dataview->coursemoduleid, 'userid' => $dataview->userid]);
|
|
|
1250 |
if (!$data->viewed && $dataview->id) {
|
|
|
1251 |
$DB->delete_records('course_modules_viewed', ['id' => $dataview->id]);
|
|
|
1252 |
}
|
|
|
1253 |
|
|
|
1254 |
if (!$dataview->id && $data->viewed) {
|
|
|
1255 |
$dataview->timecreated = time();
|
|
|
1256 |
$dataview->id = $DB->insert_record('course_modules_viewed', $dataview);
|
|
|
1257 |
}
|
|
|
1258 |
$transaction->allow_commit();
|
|
|
1259 |
|
|
|
1260 |
$cmcontext = context_module::instance($data->coursemoduleid);
|
|
|
1261 |
|
|
|
1262 |
$completioncache = cache::make('core', 'completion');
|
|
|
1263 |
$cachekey = "{$data->userid}_{$cm->course}";
|
|
|
1264 |
if ($data->userid == $USER->id) {
|
|
|
1265 |
// Fetch other completion data to cache (e.g. require grade completion status, custom completion rule statues).
|
|
|
1266 |
$cminfo = cm_info::create($cm, $data->userid); // Make sure we're working on a cm_info object.
|
|
|
1267 |
$otherdata = $this->get_other_cm_completion_data($cminfo, $data->userid);
|
|
|
1268 |
foreach ($otherdata as $key => $value) {
|
|
|
1269 |
$data->$key = $value;
|
|
|
1270 |
}
|
|
|
1271 |
|
|
|
1272 |
// Update module completion in user's cache.
|
|
|
1273 |
if (!($cachedata = $completioncache->get($cachekey))
|
|
|
1274 |
|| $cachedata['cacherev'] != $this->course->cacherev) {
|
|
|
1275 |
$cachedata = array('cacherev' => $this->course->cacherev);
|
|
|
1276 |
}
|
|
|
1277 |
$cachedata[$cm->id] = (array) $data;
|
|
|
1278 |
$cachedata[$cm->id]['other_cm_completion_data_fetched'] = true;
|
|
|
1279 |
$completioncache->set($cachekey, $cachedata);
|
|
|
1280 |
|
|
|
1281 |
// reset modinfo for user (no need to call rebuild_course_cache())
|
|
|
1282 |
get_fast_modinfo($cm->course, 0, true);
|
|
|
1283 |
} else {
|
|
|
1284 |
// Remove another user's completion cache for this course.
|
|
|
1285 |
$completioncache->delete($cachekey);
|
|
|
1286 |
}
|
|
|
1287 |
|
|
|
1288 |
// For single user actions the code must reevaluate some completion state instantly, see MDL-32103.
|
|
|
1289 |
if ($isbulkupdate) {
|
|
|
1290 |
return;
|
|
|
1291 |
} else {
|
|
|
1292 |
$userdata = ['userid' => $data->userid, 'courseid' => $this->course_id];
|
|
|
1293 |
$coursecompletionid = \core_completion\api::mark_course_completions_activity_criteria($userdata);
|
|
|
1294 |
if ($coursecompletionid) {
|
|
|
1295 |
aggregate_completions($coursecompletionid);
|
|
|
1296 |
}
|
|
|
1297 |
}
|
|
|
1298 |
|
|
|
1299 |
// Trigger an event for course module completion changed.
|
|
|
1300 |
$event = \core\event\course_module_completion_updated::create(array(
|
|
|
1301 |
'objectid' => $data->id,
|
|
|
1302 |
'context' => $cmcontext,
|
|
|
1303 |
'relateduserid' => $data->userid,
|
|
|
1304 |
'other' => array(
|
|
|
1305 |
'relateduserid' => $data->userid,
|
|
|
1306 |
'overrideby' => $data->overrideby,
|
|
|
1307 |
'completionstate' => $data->completionstate
|
|
|
1308 |
)
|
|
|
1309 |
));
|
|
|
1310 |
$event->add_record_snapshot('course_modules_completion', $data);
|
|
|
1311 |
$event->trigger();
|
|
|
1312 |
}
|
|
|
1313 |
|
|
|
1314 |
/**
|
|
|
1315 |
* Return whether or not the course has activities with completion enabled.
|
|
|
1316 |
*
|
|
|
1317 |
* @return boolean true when there is at least one activity with completion enabled.
|
|
|
1318 |
*/
|
|
|
1319 |
public function has_activities() {
|
|
|
1320 |
$modinfo = get_fast_modinfo($this->course);
|
|
|
1321 |
foreach ($modinfo->get_cms() as $cm) {
|
|
|
1322 |
if ($cm->completion != COMPLETION_TRACKING_NONE) {
|
|
|
1323 |
return true;
|
|
|
1324 |
}
|
|
|
1325 |
}
|
|
|
1326 |
return false;
|
|
|
1327 |
}
|
|
|
1328 |
|
|
|
1329 |
/**
|
|
|
1330 |
* Obtains a list of activities for which completion is enabled on the
|
|
|
1331 |
* course. The list is ordered by the section order of those activities.
|
|
|
1332 |
*
|
|
|
1333 |
* @return cm_info[] Array from $cmid => $cm of all activities with completion enabled,
|
|
|
1334 |
* empty array if none
|
|
|
1335 |
*/
|
|
|
1336 |
public function get_activities() {
|
|
|
1337 |
$modinfo = get_fast_modinfo($this->course);
|
|
|
1338 |
$result = array();
|
|
|
1339 |
foreach ($modinfo->get_cms() as $cm) {
|
|
|
1340 |
if ($cm->completion != COMPLETION_TRACKING_NONE && !$cm->deletioninprogress) {
|
|
|
1341 |
$result[$cm->id] = $cm;
|
|
|
1342 |
}
|
|
|
1343 |
}
|
|
|
1344 |
return $result;
|
|
|
1345 |
}
|
|
|
1346 |
|
|
|
1347 |
/**
|
|
|
1348 |
* Checks to see if the userid supplied has a tracked role in
|
|
|
1349 |
* this course
|
|
|
1350 |
*
|
|
|
1351 |
* @param int $userid User id
|
|
|
1352 |
* @return bool
|
|
|
1353 |
*/
|
|
|
1354 |
public function is_tracked_user($userid) {
|
|
|
1355 |
return is_enrolled(context_course::instance($this->course->id), $userid, 'moodle/course:isincompletionreports', true);
|
|
|
1356 |
}
|
|
|
1357 |
|
|
|
1358 |
/**
|
|
|
1359 |
* Returns the number of users whose progress is tracked in this course.
|
|
|
1360 |
*
|
|
|
1361 |
* Optionally supply a search's where clause, or a group id.
|
|
|
1362 |
*
|
|
|
1363 |
* @param string $where Where clause sql (use 'u.whatever' for user table fields)
|
|
|
1364 |
* @param array $whereparams Where clause params
|
|
|
1365 |
* @param int $groupid Group id
|
|
|
1366 |
* @return int Number of tracked users
|
|
|
1367 |
*/
|
|
|
1368 |
public function get_num_tracked_users($where = '', $whereparams = array(), $groupid = 0) {
|
|
|
1369 |
global $DB;
|
|
|
1370 |
|
|
|
1371 |
list($enrolledsql, $enrolledparams) = get_enrolled_sql(
|
|
|
1372 |
context_course::instance($this->course->id), 'moodle/course:isincompletionreports', $groupid, true);
|
|
|
1373 |
$sql = 'SELECT COUNT(eu.id) FROM (' . $enrolledsql . ') eu JOIN {user} u ON u.id = eu.id';
|
|
|
1374 |
if ($where) {
|
|
|
1375 |
$sql .= " WHERE $where";
|
|
|
1376 |
}
|
|
|
1377 |
|
|
|
1378 |
$params = array_merge($enrolledparams, $whereparams);
|
|
|
1379 |
return $DB->count_records_sql($sql, $params);
|
|
|
1380 |
}
|
|
|
1381 |
|
|
|
1382 |
/**
|
|
|
1383 |
* Return array of users whose progress is tracked in this course.
|
|
|
1384 |
*
|
|
|
1385 |
* Optionally supply a search's where clause, group id, sorting, paging.
|
|
|
1386 |
*
|
|
|
1387 |
* @param string $where Where clause sql, referring to 'u.' fields (optional)
|
|
|
1388 |
* @param array $whereparams Where clause params (optional)
|
|
|
1389 |
* @param int $groupid Group ID to restrict to (optional)
|
|
|
1390 |
* @param string $sort Order by clause (optional)
|
|
|
1391 |
* @param int $limitfrom Result start (optional)
|
|
|
1392 |
* @param int $limitnum Result max size (optional)
|
|
|
1393 |
* @param context $extracontext If set, includes extra user information fields
|
|
|
1394 |
* as appropriate to display for current user in this context
|
|
|
1395 |
* @return array Array of user objects with user fields (including all identity fields)
|
|
|
1396 |
*/
|
|
|
1397 |
public function get_tracked_users($where = '', $whereparams = array(), $groupid = 0,
|
|
|
1398 |
$sort = '', $limitfrom = '', $limitnum = '', context $extracontext = null) {
|
|
|
1399 |
|
|
|
1400 |
global $DB;
|
|
|
1401 |
|
|
|
1402 |
list($enrolledsql, $params) = get_enrolled_sql(
|
|
|
1403 |
context_course::instance($this->course->id),
|
|
|
1404 |
'moodle/course:isincompletionreports', $groupid, true);
|
|
|
1405 |
|
|
|
1406 |
$userfieldsapi = \core_user\fields::for_identity($extracontext)->with_name()->excluding('id', 'idnumber');
|
|
|
1407 |
$fieldssql = $userfieldsapi->get_sql('u', true);
|
|
|
1408 |
$sql = 'SELECT u.id, u.idnumber ' . $fieldssql->selects;
|
|
|
1409 |
$sql .= ' FROM (' . $enrolledsql . ') eu JOIN {user} u ON u.id = eu.id';
|
|
|
1410 |
|
|
|
1411 |
if ($where) {
|
|
|
1412 |
$sql .= " AND $where";
|
|
|
1413 |
$params = array_merge($params, $whereparams);
|
|
|
1414 |
}
|
|
|
1415 |
|
|
|
1416 |
$sql .= $fieldssql->joins;
|
|
|
1417 |
$params = array_merge($params, $fieldssql->params);
|
|
|
1418 |
|
|
|
1419 |
if ($sort) {
|
|
|
1420 |
$sql .= " ORDER BY $sort";
|
|
|
1421 |
}
|
|
|
1422 |
|
|
|
1423 |
return $DB->get_records_sql($sql, $params, $limitfrom, $limitnum);
|
|
|
1424 |
}
|
|
|
1425 |
|
|
|
1426 |
/**
|
|
|
1427 |
* Obtains progress information across a course for all users on that course, or
|
|
|
1428 |
* for all users in a specific group. Intended for use when displaying progress.
|
|
|
1429 |
*
|
|
|
1430 |
* This includes only users who, in course context, have one of the roles for
|
|
|
1431 |
* which progress is tracked (the gradebookroles admin option) and are enrolled in course.
|
|
|
1432 |
*
|
|
|
1433 |
* Users are included (in the first array) even if they do not have
|
|
|
1434 |
* completion progress for any course-module.
|
|
|
1435 |
*
|
|
|
1436 |
* @param bool $sortfirstname If true, sort by first name, otherwise sort by
|
|
|
1437 |
* last name
|
|
|
1438 |
* @param string $where Where clause sql (optional)
|
|
|
1439 |
* @param array $where_params Where clause params (optional)
|
|
|
1440 |
* @param int $groupid Group ID or 0 (default)/false for all groups
|
|
|
1441 |
* @param int $pagesize Number of users to actually return (optional)
|
|
|
1442 |
* @param int $start User to start at if paging (optional)
|
|
|
1443 |
* @param context $extracontext If set, includes extra user information fields
|
|
|
1444 |
* as appropriate to display for current user in this context
|
|
|
1445 |
* @return array with ->total and ->start (same as $start) and ->users;
|
|
|
1446 |
* an array of user objects (like mdl_user id, firstname, lastname)
|
|
|
1447 |
* containing an additional ->progress array of coursemoduleid => completionstate
|
|
|
1448 |
*/
|
|
|
1449 |
public function get_progress_all($where = '', $where_params = array(), $groupid = 0,
|
|
|
1450 |
$sort = '', $pagesize = '', $start = '', context $extracontext = null) {
|
|
|
1451 |
global $CFG, $DB;
|
|
|
1452 |
|
|
|
1453 |
// Get list of applicable users
|
|
|
1454 |
$users = $this->get_tracked_users($where, $where_params, $groupid, $sort,
|
|
|
1455 |
$start, $pagesize, $extracontext);
|
|
|
1456 |
|
|
|
1457 |
// Get progress information for these users in groups of 1, 000 (if needed)
|
|
|
1458 |
// to avoid making the SQL IN too long
|
|
|
1459 |
$results = array();
|
|
|
1460 |
$userids = array();
|
|
|
1461 |
foreach ($users as $user) {
|
|
|
1462 |
$userids[] = $user->id;
|
|
|
1463 |
$results[$user->id] = $user;
|
|
|
1464 |
$results[$user->id]->progress = array();
|
|
|
1465 |
}
|
|
|
1466 |
|
|
|
1467 |
for($i=0; $i<count($userids); $i+=1000) {
|
|
|
1468 |
$blocksize = count($userids)-$i < 1000 ? count($userids)-$i : 1000;
|
|
|
1469 |
|
|
|
1470 |
list($insql, $params) = $DB->get_in_or_equal(array_slice($userids, $i, $blocksize));
|
|
|
1471 |
array_splice($params, 0, 0, array($this->course->id));
|
|
|
1472 |
$rs = $DB->get_recordset_sql("
|
|
|
1473 |
SELECT
|
|
|
1474 |
cmc.*
|
|
|
1475 |
FROM
|
|
|
1476 |
{course_modules} cm
|
|
|
1477 |
INNER JOIN {course_modules_completion} cmc ON cm.id=cmc.coursemoduleid
|
|
|
1478 |
WHERE
|
|
|
1479 |
cm.course=? AND cmc.userid $insql", $params);
|
|
|
1480 |
foreach ($rs as $progress) {
|
|
|
1481 |
$progress = (object)$progress;
|
|
|
1482 |
$results[$progress->userid]->progress[$progress->coursemoduleid] = $progress;
|
|
|
1483 |
}
|
|
|
1484 |
$rs->close();
|
|
|
1485 |
}
|
|
|
1486 |
|
|
|
1487 |
return $results;
|
|
|
1488 |
}
|
|
|
1489 |
|
|
|
1490 |
/**
|
|
|
1491 |
* Called by grade code to inform the completion system when a grade has
|
|
|
1492 |
* been changed. If the changed grade is used to determine completion for
|
|
|
1493 |
* the course-module, then the completion status will be updated.
|
|
|
1494 |
*
|
|
|
1495 |
* @param stdClass|cm_info $cm Course-module for item that owns grade
|
|
|
1496 |
* @param grade_item $item Grade item
|
|
|
1497 |
* @param stdClass|grade_grade $grade
|
|
|
1498 |
* @param bool $deleted
|
|
|
1499 |
* @param bool $isbulkupdate If bulk grade update is happening.
|
|
|
1500 |
*/
|
|
|
1501 |
public function inform_grade_changed($cm, $item, $grade, $deleted, $isbulkupdate = false) {
|
|
|
1502 |
// Bail out now if completion is not enabled for course-module, it is enabled
|
|
|
1503 |
// but is set to manual, grade is not used to compute completion, or this
|
|
|
1504 |
// is a different numbered grade
|
|
|
1505 |
if (!$this->is_enabled($cm) ||
|
|
|
1506 |
$cm->completion == COMPLETION_TRACKING_MANUAL ||
|
|
|
1507 |
is_null($cm->completiongradeitemnumber) ||
|
|
|
1508 |
$item->itemnumber != $cm->completiongradeitemnumber) {
|
|
|
1509 |
return;
|
|
|
1510 |
}
|
|
|
1511 |
|
|
|
1512 |
// What is the expected result based on this grade?
|
|
|
1513 |
if ($deleted) {
|
|
|
1514 |
// Grade being deleted, so only change could be to make it incomplete
|
|
|
1515 |
$possibleresult = COMPLETION_INCOMPLETE;
|
|
|
1516 |
} else {
|
|
|
1517 |
$possibleresult = self::internal_get_grade_state($item, $grade);
|
|
|
1518 |
}
|
|
|
1519 |
|
|
|
1520 |
// OK, let's update state based on this
|
|
|
1521 |
$this->update_state($cm, $possibleresult, $grade->userid, false, $isbulkupdate);
|
|
|
1522 |
}
|
|
|
1523 |
|
|
|
1524 |
/**
|
|
|
1525 |
* Calculates the completion state that would result from a graded item
|
|
|
1526 |
* (where grade-based completion is turned on) based on the actual grade
|
|
|
1527 |
* and settings.
|
|
|
1528 |
*
|
|
|
1529 |
* Internal function. Not private, so we can unit-test it.
|
|
|
1530 |
*
|
|
|
1531 |
* @param grade_item $item an instance of grade_item
|
|
|
1532 |
* @param grade_grade $grade an instance of grade_grade
|
|
|
1533 |
* @param bool $returnpassfail If course module has pass grade completion criteria
|
|
|
1534 |
* @return int Completion state e.g. COMPLETION_INCOMPLETE
|
|
|
1535 |
*/
|
|
|
1536 |
public static function internal_get_grade_state($item, $grade, bool $returnpassfail = false) {
|
|
|
1537 |
// If no grade is supplied or the grade doesn't have an actual value, then
|
|
|
1538 |
// this is not complete.
|
|
|
1539 |
if (!$grade || (is_null($grade->finalgrade) && is_null($grade->rawgrade))) {
|
|
|
1540 |
return COMPLETION_INCOMPLETE;
|
|
|
1541 |
}
|
|
|
1542 |
|
|
|
1543 |
// Conditions to show pass/fail:
|
|
|
1544 |
// a) Completion criteria to achieve pass grade is enabled
|
|
|
1545 |
// or
|
|
|
1546 |
// a) Grade has pass mark (default is 0.00000 which is boolean true so be careful)
|
|
|
1547 |
// b) Grade is visible (neither hidden nor hidden-until)
|
|
|
1548 |
if ($item->gradepass && $item->gradepass > 0.000009 && ($returnpassfail || !$item->hidden)) {
|
|
|
1549 |
// Use final grade if set otherwise raw grade
|
|
|
1550 |
$score = !is_null($grade->finalgrade) ? $grade->finalgrade : $grade->rawgrade;
|
|
|
1551 |
|
|
|
1552 |
// We are displaying and tracking pass/fail
|
|
|
1553 |
if ($score >= $item->gradepass) {
|
|
|
1554 |
return COMPLETION_COMPLETE_PASS;
|
|
|
1555 |
} else if ($item->hidden) {
|
|
|
1556 |
return COMPLETION_COMPLETE_FAIL_HIDDEN;
|
|
|
1557 |
} else {
|
|
|
1558 |
return COMPLETION_COMPLETE_FAIL;
|
|
|
1559 |
}
|
|
|
1560 |
} else {
|
|
|
1561 |
// Not displaying pass/fail, so just if there is a grade
|
|
|
1562 |
if (!is_null($grade->finalgrade) || !is_null($grade->rawgrade)) {
|
|
|
1563 |
// Grade exists, so maybe complete now
|
|
|
1564 |
return COMPLETION_COMPLETE;
|
|
|
1565 |
} else {
|
|
|
1566 |
// Grade does not exist, so maybe incomplete now
|
|
|
1567 |
return COMPLETION_INCOMPLETE;
|
|
|
1568 |
}
|
|
|
1569 |
}
|
|
|
1570 |
}
|
|
|
1571 |
|
|
|
1572 |
/**
|
|
|
1573 |
* Aggregate activity completion state
|
|
|
1574 |
*
|
|
|
1575 |
* @param int $type Aggregation type (COMPLETION_* constant)
|
|
|
1576 |
* @param bool $old Old state
|
|
|
1577 |
* @param bool $new New state
|
|
|
1578 |
* @return bool
|
|
|
1579 |
*/
|
|
|
1580 |
public static function aggregate_completion_states($type, $old, $new) {
|
|
|
1581 |
if ($type == COMPLETION_AND) {
|
|
|
1582 |
return $old && $new;
|
|
|
1583 |
} else {
|
|
|
1584 |
return $old || $new;
|
|
|
1585 |
}
|
|
|
1586 |
}
|
|
|
1587 |
|
|
|
1588 |
/**
|
|
|
1589 |
* This is to be used only for system errors (things that shouldn't happen)
|
|
|
1590 |
* and not user-level errors.
|
|
|
1591 |
*
|
|
|
1592 |
* @param string $error Error string (will not be displayed to user unless debugging is enabled)
|
|
|
1593 |
* @throws moodle_exception Exception with the error string as debug info
|
|
|
1594 |
*/
|
|
|
1595 |
public function internal_systemerror($error) {
|
|
|
1596 |
global $CFG;
|
|
|
1597 |
throw new moodle_exception('err_system','completion',
|
|
|
1598 |
$CFG->wwwroot.'/course/view.php?id='.$this->course->id,null,$error);
|
|
|
1599 |
}
|
|
|
1600 |
|
|
|
1601 |
/**
|
|
|
1602 |
* Get completion data include viewed field.
|
|
|
1603 |
*
|
|
|
1604 |
* @param int $coursemoduleid The course module id.
|
|
|
1605 |
* @param int $userid The User ID.
|
|
|
1606 |
* @param array $defaultdata Default data completion.
|
|
|
1607 |
* @return array Data completion retrieved.
|
|
|
1608 |
*/
|
|
|
1609 |
public function get_completion_data(int $coursemoduleid, int $userid, array $defaultdata): array {
|
|
|
1610 |
global $DB;
|
|
|
1611 |
|
|
|
1612 |
// MySQL doesn't support FULL JOIN syntax, so we use UNION in the below SQL to help MySQL.
|
|
|
1613 |
$sql = "SELECT cmc.*, cmv.coursemoduleid as cmvcoursemoduleid, cmv.userid as cmvuserid
|
|
|
1614 |
FROM {course_modules_completion} cmc
|
|
|
1615 |
LEFT JOIN {course_modules_viewed} cmv ON cmc.coursemoduleid = cmv.coursemoduleid AND cmc.userid = cmv.userid
|
|
|
1616 |
WHERE cmc.coursemoduleid = :cmccoursemoduleid AND cmc.userid = :cmcuserid
|
|
|
1617 |
UNION
|
|
|
1618 |
SELECT cmc2.*, cmv2.coursemoduleid as cmvcoursemoduleid, cmv2.userid as cmvuserid
|
|
|
1619 |
FROM {course_modules_completion} cmc2
|
|
|
1620 |
RIGHT JOIN {course_modules_viewed} cmv2
|
|
|
1621 |
ON cmc2.coursemoduleid = cmv2.coursemoduleid AND cmc2.userid = cmv2.userid
|
|
|
1622 |
WHERE cmv2.coursemoduleid = :cmvcoursemoduleid AND cmv2.userid = :cmvuserid";
|
|
|
1623 |
|
|
|
1624 |
$data = $DB->get_record_sql($sql, ['cmccoursemoduleid' => $coursemoduleid, 'cmcuserid' => $userid,
|
|
|
1625 |
'cmvcoursemoduleid' => $coursemoduleid, 'cmvuserid' => $userid]);
|
|
|
1626 |
|
|
|
1627 |
if (!$data) {
|
|
|
1628 |
$data = $defaultdata;
|
|
|
1629 |
} else {
|
|
|
1630 |
if (empty($data->coursemoduleid) && empty($data->userid)) {
|
|
|
1631 |
$data->coursemoduleid = $data->cmvcoursemoduleid;
|
|
|
1632 |
$data->userid = $data->cmvuserid;
|
|
|
1633 |
}
|
11 |
efrain |
1634 |
// When reseting all state in the completion, we need to keep current view state.
|
|
|
1635 |
// We cannot assume the activity has been viewed, so we should check if there is any course_modules_viewed already.
|
|
|
1636 |
$data->viewed = is_null($data->cmvuserid) ? 0 : 1;
|
|
|
1637 |
|
1 |
efrain |
1638 |
unset($data->cmvcoursemoduleid);
|
|
|
1639 |
unset($data->cmvuserid);
|
|
|
1640 |
}
|
|
|
1641 |
|
|
|
1642 |
return (array)$data;
|
|
|
1643 |
}
|
|
|
1644 |
}
|
|
|
1645 |
|
|
|
1646 |
/**
|
|
|
1647 |
* Aggregate criteria status's as per configured aggregation method.
|
|
|
1648 |
*
|
|
|
1649 |
* @param int $method COMPLETION_AGGREGATION_* constant.
|
|
|
1650 |
* @param bool $data Criteria completion status.
|
|
|
1651 |
* @param bool|null $state Aggregation state.
|
|
|
1652 |
*/
|
|
|
1653 |
function completion_cron_aggregate($method, $data, &$state) {
|
|
|
1654 |
if ($method == COMPLETION_AGGREGATION_ALL) {
|
|
|
1655 |
if ($data && $state !== false) {
|
|
|
1656 |
$state = true;
|
|
|
1657 |
} else {
|
|
|
1658 |
$state = false;
|
|
|
1659 |
}
|
|
|
1660 |
} else if ($method == COMPLETION_AGGREGATION_ANY) {
|
|
|
1661 |
if ($data) {
|
|
|
1662 |
$state = true;
|
|
|
1663 |
} else if (!$data && $state === null) {
|
|
|
1664 |
$state = false;
|
|
|
1665 |
}
|
|
|
1666 |
}
|
|
|
1667 |
}
|
|
|
1668 |
|
|
|
1669 |
/**
|
|
|
1670 |
* Aggregate courses completions. This function is called when activity completion status is updated
|
|
|
1671 |
* for single user. Also when regular completion task runs it aggregates completions for all courses and users.
|
|
|
1672 |
*
|
|
|
1673 |
* @param int $coursecompletionid Course completion ID to update (if 0 - update for all courses and users)
|
|
|
1674 |
* @param bool $mtraceprogress To output debug info
|
|
|
1675 |
* @since Moodle 4.0
|
|
|
1676 |
*/
|
|
|
1677 |
function aggregate_completions(int $coursecompletionid, bool $mtraceprogress = false) {
|
|
|
1678 |
global $DB;
|
|
|
1679 |
|
|
|
1680 |
if (!$coursecompletionid && $mtraceprogress) {
|
|
|
1681 |
mtrace('Aggregating completions');
|
|
|
1682 |
}
|
|
|
1683 |
// Save time started.
|
|
|
1684 |
$timestarted = time();
|
|
|
1685 |
|
|
|
1686 |
// Grab all criteria and their associated criteria completions.
|
|
|
1687 |
$sql = "SELECT DISTINCT c.id AS courseid, cr.id AS criteriaid, cco.userid, cr.criteriatype, ccocr.timecompleted
|
|
|
1688 |
FROM {course_completion_criteria} cr
|
|
|
1689 |
INNER JOIN {course} c ON cr.course = c.id
|
|
|
1690 |
INNER JOIN {course_completions} cco ON cco.course = c.id
|
|
|
1691 |
LEFT JOIN {course_completion_crit_compl} ccocr
|
|
|
1692 |
ON ccocr.criteriaid = cr.id AND cco.userid = ccocr.userid
|
|
|
1693 |
WHERE c.enablecompletion = 1
|
|
|
1694 |
AND cco.timecompleted IS NULL
|
|
|
1695 |
AND cco.reaggregate > 0";
|
|
|
1696 |
|
|
|
1697 |
if ($coursecompletionid) {
|
|
|
1698 |
$sql .= " AND cco.id = ?";
|
|
|
1699 |
$param = $coursecompletionid;
|
|
|
1700 |
} else {
|
|
|
1701 |
$sql .= " AND cco.reaggregate < ? ORDER BY courseid, cco.userid";
|
|
|
1702 |
$param = $timestarted;
|
|
|
1703 |
}
|
|
|
1704 |
$rs = $DB->get_recordset_sql($sql, [$param]);
|
|
|
1705 |
|
|
|
1706 |
// Check if result is empty.
|
|
|
1707 |
if (!$rs->valid()) {
|
|
|
1708 |
$rs->close();
|
|
|
1709 |
return;
|
|
|
1710 |
}
|
|
|
1711 |
|
|
|
1712 |
$currentuser = null;
|
|
|
1713 |
$currentcourse = null;
|
|
|
1714 |
$completions = [];
|
|
|
1715 |
while (1) {
|
|
|
1716 |
// Grab records for current user/course.
|
|
|
1717 |
foreach ($rs as $record) {
|
|
|
1718 |
// If we are still grabbing the same users completions.
|
|
|
1719 |
if ($record->userid === $currentuser && $record->courseid === $currentcourse) {
|
|
|
1720 |
$completions[$record->criteriaid] = $record;
|
|
|
1721 |
} else {
|
|
|
1722 |
break;
|
|
|
1723 |
}
|
|
|
1724 |
}
|
|
|
1725 |
|
|
|
1726 |
// Aggregate.
|
|
|
1727 |
if (!empty($completions)) {
|
|
|
1728 |
if (!$coursecompletionid && $mtraceprogress) {
|
|
|
1729 |
mtrace('Aggregating completions for user ' . $currentuser . ' in course ' . $currentcourse);
|
|
|
1730 |
}
|
|
|
1731 |
|
|
|
1732 |
// Get course info object.
|
|
|
1733 |
$info = new \completion_info((object)['id' => $currentcourse]);
|
|
|
1734 |
|
|
|
1735 |
// Setup aggregation.
|
|
|
1736 |
$overall = $info->get_aggregation_method();
|
|
|
1737 |
$activity = $info->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ACTIVITY);
|
|
|
1738 |
$prerequisite = $info->get_aggregation_method(COMPLETION_CRITERIA_TYPE_COURSE);
|
|
|
1739 |
$role = $info->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ROLE);
|
|
|
1740 |
|
|
|
1741 |
$overallstatus = null;
|
|
|
1742 |
$activitystatus = null;
|
|
|
1743 |
$prerequisitestatus = null;
|
|
|
1744 |
$rolestatus = null;
|
|
|
1745 |
|
|
|
1746 |
// Get latest timecompleted.
|
|
|
1747 |
$timecompleted = null;
|
|
|
1748 |
|
|
|
1749 |
// Check each of the criteria.
|
|
|
1750 |
foreach ($completions as $params) {
|
|
|
1751 |
$timecompleted = max($timecompleted, $params->timecompleted);
|
|
|
1752 |
$completion = new \completion_criteria_completion((array)$params, false);
|
|
|
1753 |
|
|
|
1754 |
// Handle aggregation special cases.
|
|
|
1755 |
if ($params->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
|
|
|
1756 |
completion_cron_aggregate($activity, $completion->is_complete(), $activitystatus);
|
|
|
1757 |
} else if ($params->criteriatype == COMPLETION_CRITERIA_TYPE_COURSE) {
|
|
|
1758 |
completion_cron_aggregate($prerequisite, $completion->is_complete(), $prerequisitestatus);
|
|
|
1759 |
} else if ($params->criteriatype == COMPLETION_CRITERIA_TYPE_ROLE) {
|
|
|
1760 |
completion_cron_aggregate($role, $completion->is_complete(), $rolestatus);
|
|
|
1761 |
} else {
|
|
|
1762 |
completion_cron_aggregate($overall, $completion->is_complete(), $overallstatus);
|
|
|
1763 |
}
|
|
|
1764 |
}
|
|
|
1765 |
|
|
|
1766 |
// Include role criteria aggregation in overall aggregation.
|
|
|
1767 |
if ($rolestatus !== null) {
|
|
|
1768 |
completion_cron_aggregate($overall, $rolestatus, $overallstatus);
|
|
|
1769 |
}
|
|
|
1770 |
|
|
|
1771 |
// Include activity criteria aggregation in overall aggregation.
|
|
|
1772 |
if ($activitystatus !== null) {
|
|
|
1773 |
completion_cron_aggregate($overall, $activitystatus, $overallstatus);
|
|
|
1774 |
}
|
|
|
1775 |
|
|
|
1776 |
// Include prerequisite criteria aggregation in overall aggregation.
|
|
|
1777 |
if ($prerequisitestatus !== null) {
|
|
|
1778 |
completion_cron_aggregate($overall, $prerequisitestatus, $overallstatus);
|
|
|
1779 |
}
|
|
|
1780 |
|
|
|
1781 |
// If aggregation status is true, mark course complete for user.
|
|
|
1782 |
if ($overallstatus) {
|
|
|
1783 |
if (!$coursecompletionid && $mtraceprogress) {
|
|
|
1784 |
mtrace('Marking complete');
|
|
|
1785 |
}
|
|
|
1786 |
|
|
|
1787 |
$ccompletion = new \completion_completion([
|
|
|
1788 |
'course' => $params->courseid,
|
|
|
1789 |
'userid' => $params->userid
|
|
|
1790 |
]);
|
|
|
1791 |
$ccompletion->mark_complete($timecompleted);
|
|
|
1792 |
}
|
|
|
1793 |
}
|
|
|
1794 |
|
|
|
1795 |
// If this is the end of the recordset, break the loop.
|
|
|
1796 |
if (!$rs->valid()) {
|
|
|
1797 |
$rs->close();
|
|
|
1798 |
break;
|
|
|
1799 |
}
|
|
|
1800 |
|
|
|
1801 |
// New/next user, update user details, reset completions.
|
|
|
1802 |
$currentuser = $record->userid;
|
|
|
1803 |
$currentcourse = $record->courseid;
|
|
|
1804 |
$completions = [];
|
|
|
1805 |
$completions[$record->criteriaid] = $record;
|
|
|
1806 |
}
|
|
|
1807 |
|
|
|
1808 |
// Mark all users as aggregated.
|
|
|
1809 |
if ($coursecompletionid) {
|
|
|
1810 |
$select = "reaggregate > 0 AND id = ?";
|
|
|
1811 |
$param = $coursecompletionid;
|
|
|
1812 |
} else {
|
|
|
1813 |
$select = "reaggregate > 0 AND reaggregate < ?";
|
|
|
1814 |
$param = $timestarted;
|
|
|
1815 |
if (PHPUNIT_TEST) {
|
|
|
1816 |
// MDL-33320: for instant completions we need aggregate to work in a single run.
|
|
|
1817 |
$DB->set_field('course_completions', 'reaggregate', $timestarted - 2);
|
|
|
1818 |
}
|
|
|
1819 |
}
|
|
|
1820 |
$DB->set_field_select('course_completions', 'reaggregate', 0, $select, [$param]);
|
|
|
1821 |
}
|