MDL-58138 completion: changes after rebasing
[moodle.git] / completion / classes / manager.php
CommitLineData
0b620801 1<?php
0b620801
AG
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 * Bulk activity completion manager class
19 *
20 * @package core_completion
21 * @category completion
22 * @copyright 2017 Adrian Greeve
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26namespace core_completion;
27
28use stdClass;
e8a71f85 29use context_course;
7f53e8aa 30use cm_info;
a64a9f9c
MG
31use tabobject;
32use lang_string;
33use moodle_url;
b17ee682 34defined('MOODLE_INTERNAL') || die;
0b620801
AG
35
36/**
37 * Bulk activity completion manager class
38 *
39 * @package core_completion
40 * @category completion
41 * @copyright 2017 Adrian Greeve
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 */
44class manager {
45
b17ee682
JD
46 /**
47 * @var int $courseid the course id.
48 */
0b620801
AG
49 protected $courseid;
50
b17ee682
JD
51 /**
52 * manager constructor.
53 * @param int $courseid the course id.
54 */
0b620801
AG
55 public function __construct($courseid) {
56 $this->courseid = $courseid;
57 }
58
59 /**
60 * Gets the data (context) to be used with the bulkactivitycompletion template.
61 *
62 * @return stdClass data for use with the bulkactivitycompletion template.
63 */
64 public function get_activities_and_headings() {
65 global $OUTPUT;
66 $moduleinfo = get_fast_modinfo($this->courseid);
67 $sections = $moduleinfo->get_sections();
68 $data = new stdClass;
69 $data->courseid = $this->courseid;
70 $data->sesskey = sesskey();
5976f85c 71 $data->helpicon = $OUTPUT->help_icon('bulkcompletiontracking', 'core_completion');
0b620801
AG
72 $data->sections = [];
73 foreach ($sections as $sectionnumber => $section) {
74 $sectioninfo = $moduleinfo->get_section_info($sectionnumber);
75
76 $sectionobject = new stdClass();
77 $sectionobject->sectionnumber = $sectionnumber;
78 $sectionobject->name = get_section_name($this->courseid, $sectioninfo);
7f53e8aa 79 $sectionobject->activities = $this->get_activities($section, true);
0cbc248d
MG
80 $data->sections[] = $sectionobject;
81 }
82 return $data;
83 }
0b620801 84
0cbc248d
MG
85 /**
86 * Gets the data (context) to be used with the activityinstance template
87 *
88 * @param array $cmids list of course module ids
89 * @param bool $withcompletiondetails include completion details
7f53e8aa 90 * @return array
0cbc248d
MG
91 */
92 public function get_activities($cmids, $withcompletiondetails = false) {
93 $moduleinfo = get_fast_modinfo($this->courseid);
94 $activities = [];
95 foreach ($cmids as $cmid) {
96 $mod = $moduleinfo->get_cm($cmid);
97 if (!$mod->uservisible) {
98 continue;
99 }
100 $moduleobject = new stdClass();
101 $moduleobject->cmid = $cmid;
102 $moduleobject->modname = $mod->get_formatted_name();
103 $moduleobject->icon = $mod->get_icon_url()->out();
104 $moduleobject->url = $mod->url;
105 $moduleobject->canmanage = $withcompletiondetails && self::can_edit_bulk_completion($this->courseid, $mod);
106
107 // Get activity completion information.
108 if ($moduleobject->canmanage) {
7f53e8aa 109 $moduleobject->completionstatus = $this->get_completion_detail($mod);
0cbc248d
MG
110 } else {
111 $moduleobject->completionstatus = ['icon' => null, 'string' => null];
0b620801 112 }
0cbc248d
MG
113
114 $activities[] = $moduleobject;
0b620801 115 }
7f53e8aa 116 return $activities;
0b620801
AG
117 }
118
7f53e8aa
MG
119
120 /**
121 * Get completion information on the selected module or module type
122 *
123 * @param cm_info|stdClass $mod either instance of cm_info (with 'customcompletionrules' in customdata) or
124 * object with fields ->completion, ->completionview, ->completionexpected, ->completionusegrade
125 * and ->customdata['customcompletionrules']
126 * @return array
127 */
128 private function get_completion_detail($mod) {
0b620801
AG
129 global $OUTPUT;
130 $strings = [];
131 switch ($mod->completion) {
7f53e8aa 132 case COMPLETION_TRACKING_NONE:
0b620801
AG
133 $strings['string'] = get_string('none');
134 break;
135
7f53e8aa 136 case COMPLETION_TRACKING_MANUAL:
5976f85c 137 $strings['string'] = get_string('manual', 'completion');
6b3b9c1d 138 $strings['icon'] = $OUTPUT->pix_icon('i/completion-manual-y', get_string('completion_manual', 'completion'));
0b620801
AG
139 break;
140
7f53e8aa 141 case COMPLETION_TRACKING_AUTOMATIC:
5976f85c 142 $strings['string'] = get_string('withconditions', 'completion');
0b620801
AG
143
144 // Get the descriptions for all the active completion rules for the module.
7f53e8aa 145 if ($ruledescriptions = $this->get_completion_active_rule_descriptions($mod)) {
0b620801
AG
146 foreach ($ruledescriptions as $ruledescription) {
147 $strings['string'] .= \html_writer::empty_tag('br') . $ruledescription;
148 }
149 }
150
6b3b9c1d 151 $strings['icon'] = $OUTPUT->pix_icon('i/completion-auto-y', get_string('completion_automatic', 'completion'));
0b620801
AG
152 break;
153
154 default:
155 $strings['string'] = get_string('none');
156 break;
157 }
158 return $strings;
159 }
160
7f53e8aa
MG
161 /**
162 * Get the descriptions for all active conditional completion rules for the current module.
163 *
164 * @param cm_info|stdClass $moduledata either instance of cm_info (with 'customcompletionrules' in customdata) or
165 * object with fields ->completion, ->completionview, ->completionexpected, ->completionusegrade
166 * and ->customdata['customcompletionrules']
167 * @return array $activeruledescriptions an array of strings describing the active completion rules.
168 */
169 protected function get_completion_active_rule_descriptions($moduledata) {
170 $activeruledescriptions = [];
171
172 // Generate the description strings for the core conditional completion rules (if set).
173 if (!empty($moduledata->completionview)) {
5976f85c 174 $activeruledescriptions[] = get_string('completionview_desc', 'completion');
7f53e8aa
MG
175 }
176 if ($moduledata instanceof cm_info && !is_null($moduledata->completiongradeitemnumber) ||
177 ($moduledata instanceof stdClass && !empty($moduledata->completionusegrade))) {
5976f85c 178 $activeruledescriptions[] = get_string('completionusegrade_desc', 'completion');
7f53e8aa
MG
179 }
180
181 // Now, ask the module to provide descriptions for its custom conditional completion rules.
182 if ($customruledescriptions = component_callback($moduledata->modname,
183 'get_completion_active_rule_descriptions', [$moduledata])) {
184 $activeruledescriptions = array_merge($activeruledescriptions, $customruledescriptions);
185 }
186
187 if (!empty($moduledata->completionexpected)) {
5976f85c 188 $activeruledescriptions[] = get_string('completionexpecteddesc', 'completion',
7f53e8aa
MG
189 userdate($moduledata->completionexpected));
190 }
191
192 return $activeruledescriptions;
193 }
194
b17ee682
JD
195 /**
196 * Gets the course modules for the current course.
197 *
198 * @return stdClass $data containing the modules
199 */
e8a71f85 200 public function get_activities_and_resources() {
7f53e8aa
MG
201 global $DB, $OUTPUT, $CFG;
202 require_once($CFG->dirroot.'/course/lib.php');
203
e8a71f85
AG
204 // Get enabled activities and resources.
205 $modules = $DB->get_records('modules', ['visible' => 1], 'name ASC');
206 $data = new stdClass();
207 $data->courseid = $this->courseid;
208 $data->sesskey = sesskey();
5976f85c 209 $data->helpicon = $OUTPUT->help_icon('bulkcompletiontracking', 'core_completion');
e8a71f85
AG
210 // Add icon information.
211 $data->modules = array_values($modules);
212 $coursecontext = context_course::instance($this->courseid);
7f53e8aa
MG
213 $canmanage = has_capability('moodle/course:manageactivities', $coursecontext);
214 $course = get_course($this->courseid);
e8a71f85 215 foreach ($data->modules as $module) {
6b3b9c1d 216 $module->icon = $OUTPUT->image_url('icon', $module->name)->out();
7f53e8aa
MG
217 $module->formattedname = format_string(get_string('modulenameplural', 'mod_' . $module->name),
218 true, ['context' => $coursecontext]);
219 $module->canmanage = $canmanage && \course_allowed_module($course, $module->name);
220 $defaults = self::get_default_completion($course, $module, false);
221 $defaults->modname = $module->name;
222 $module->completionstatus = $this->get_completion_detail($defaults);
e8a71f85
AG
223 }
224
225 return $data;
226 }
227
0cbc248d
MG
228 /**
229 * Checks if current user can edit activity completion
230 *
231 * @param int|stdClass $courseorid
232 * @param \cm_info|null $cm if specified capability for a given coursemodule will be check,
233 * if not specified capability to edit at least one activity is checked.
234 */
235 public static function can_edit_bulk_completion($courseorid, $cm = null) {
236 if ($cm) {
237 return $cm->uservisible && has_capability('moodle/course:manageactivities', $cm->context);
238 }
239 $coursecontext = context_course::instance(is_object($courseorid) ? $courseorid->id : $courseorid);
240 if (has_capability('moodle/course:manageactivities', $coursecontext)) {
241 return true;
242 }
243 $modinfo = get_fast_modinfo($courseorid);
244 foreach ($modinfo->cms as $mod) {
245 if ($mod->uservisible && has_capability('moodle/course:manageactivities', $mod->context)) {
246 return true;
247 }
248 }
249 return false;
250 }
06cdda46 251
a64a9f9c 252 /**
b17ee682
JD
253 * Gets the available completion tabs for the current course and user.
254 *
255 * @param stdClass|int $courseorid the course object or id.
a64a9f9c
MG
256 * @return tabobject[]
257 */
258 public static function get_available_completion_tabs($courseorid) {
259 $tabs = [];
260
261 $courseid = is_object($courseorid) ? $courseorid->id : $courseorid;
262 $coursecontext = context_course::instance($courseid);
263
264 if (has_capability('moodle/course:update', $coursecontext)) {
265 $tabs[] = new tabobject(
266 'completion',
267 new moodle_url('/course/completion.php', ['id' => $courseid]),
268 new lang_string('coursecompletion', 'completion')
269 );
270 }
271
272 if (has_capability('moodle/course:manageactivities', $coursecontext)) {
273 $tabs[] = new tabobject(
274 'defaultcompletion',
275 new moodle_url('/course/defaultcompletion.php', ['id' => $courseid]),
276 new lang_string('defaultcompletion', 'completion')
277 );
278 }
279
280 if (self::can_edit_bulk_completion($courseorid)) {
281 $tabs[] = new tabobject(
282 'bulkcompletion',
283 new moodle_url('/course/bulkcompletion.php', ['id' => $courseid]),
284 new lang_string('bulkactivitycompletion', 'completion')
285 );
286 }
287
288 return $tabs;
289 }
290
06cdda46
MG
291 /**
292 * Applies completion from the bulk edit form to all selected modules
293 *
294 * @param stdClass $data data received from the core_completion_bulkedit_form
7f53e8aa 295 * @param bool $updateinstances if we need to update the instance tables of the module (i.e. 'assign', 'forum', etc.) -
06cdda46
MG
296 * if no module-specific completion rules were added to the form, update of the module table is not needed.
297 */
298 public function apply_completion($data, $updateinstances) {
299 $updated = [];
300 $modinfo = get_fast_modinfo($this->courseid);
301
302 $cmids = $data->cmid;
303
304 $data = (array)$data;
305 unset($data['id']); // This is a course id, we don't want to confuse it with cmid or instance id.
306 unset($data['cmid']);
307 unset($data['submitbutton']);
308
309 foreach ($cmids as $cmid) {
310 $cm = $modinfo->get_cm($cmid);
311 if (self::can_edit_bulk_completion($this->courseid, $cm) && $this->apply_completion_cm($cm, $data, $updateinstances)) {
312 $updated[] = $cm->id;
313 }
314 }
315 if ($updated) {
316 // Now that modules are fully updated, also update completion data if required.
317 // This will wipe all user completion data and recalculate it.
318 rebuild_course_cache($this->courseid, true);
319 $modinfo = get_fast_modinfo($this->courseid);
320 $completion = new \completion_info($modinfo->get_course());
321 foreach ($updated as $cmid) {
322 $completion->reset_all_state($modinfo->get_cm($cmid));
323 }
22002ed7
JD
324
325 // And notify the user of the result.
326 \core\notification::add(get_string('activitycompletionupdated', 'core_completion'), \core\notification::SUCCESS);
06cdda46
MG
327 }
328 }
329
330 /**
331 * Applies new completion rules to one course module
332 *
333 * @param \cm_info $cm
334 * @param array $data
335 * @param bool $updateinstance if we need to update the instance table of the module (i.e. 'assign', 'forum', etc.) -
336 * if no module-specific completion rules were added to the form, update of the module table is not needed.
337 * @return bool if module was updated
338 */
339 protected function apply_completion_cm(\cm_info $cm, $data, $updateinstance) {
340 global $DB;
341
342 $defaults = ['completion' => COMPLETION_DISABLED, 'completionview' => COMPLETION_VIEW_NOT_REQUIRED,
343 'completionexpected' => 0, 'completiongradeitemnumber' => null];
344
345 if ($cm->completion == $data['completion'] && $cm->completion != COMPLETION_TRACKING_AUTOMATIC) {
346 // If old and new completion are either both "manual" or both "none" - no changes are needed.
347 return false;
348 }
349
350 $data += ['completion' => $cm->completion,
351 'completionexpected' => $cm->completionexpected,
352 'completionview' => $cm->completionview];
353
354 if (array_key_exists('completionusegrade', $data)) {
355 // Convert the 'use grade' checkbox into a grade-item number: 0 if checked, null if not.
356 $data['completiongradeitemnumber'] = !empty($data['completionusegrade']) ? 0 : null;
357 unset($data['completionusegrade']);
358 } else {
359 $data['completiongradeitemnumber'] = $cm->completiongradeitemnumber;
360 }
361
362 // Update module instance table.
363 if ($updateinstance) {
364 $moddata = ['id' => $cm->instance, 'timemodified' => time()] + array_diff_key($data, $defaults);
365 $DB->update_record($cm->modname, $moddata);
366 }
367
368 // Update course modules table.
369 $cmdata = ['id' => $cm->id, 'timemodified' => time()] + array_intersect_key($data, $defaults);
370 $DB->update_record('course_modules', $cmdata);
371
372 \core\event\course_module_updated::create_from_cm($cm, $cm->context)->trigger();
373
06cdda46
MG
374 return true;
375 }
376
7f53e8aa
MG
377
378 /**
379 * Saves default completion from edit form to all selected module types
380 *
381 * @param stdClass $data data received from the core_completion_bulkedit_form
382 * @param bool $updatecustomrules if we need to update the custom rules of the module -
383 * if no module-specific completion rules were added to the form, update of the module table is not needed.
384 */
385 public function apply_default_completion($data, $updatecustomrules) {
386 global $DB;
387
388 $courseid = $data->id;
389 $coursecontext = context_course::instance($courseid);
390 if (!$modids = $data->modids) {
391 return;
392 }
393 $defaults = ['completion' => COMPLETION_DISABLED, 'completionview' => COMPLETION_VIEW_NOT_REQUIRED,
394 'completionexpected' => 0, 'completionusegrade' => 0];
395
396 $data = (array)$data;
397
398 if ($updatecustomrules) {
399 $customdata = array_diff_key($data, $defaults);
400 $data['customrules'] = $customdata ? json_encode($customdata) : null;
401 $defaults['customrules'] = null;
402 }
403 $data = array_intersect_key($data, $defaults);
404
405 // Get names of the affected modules.
406 list($modidssql, $params) = $DB->get_in_or_equal($modids);
407 $params[] = 1;
408 $modules = $DB->get_records_select_menu('modules', 'id ' . $modidssql . ' and visible = ?', $params, '', 'id, name');
409
410 foreach ($modids as $modid) {
411 if (!array_key_exists($modid, $modules)) {
412 continue;
413 }
414 if ($defaultsid = $DB->get_field('course_completion_defaults', 'id', ['course' => $courseid, 'module' => $modid])) {
415 $DB->update_record('course_completion_defaults', $data + ['id' => $defaultsid]);
416 } else {
417 $defaultsid = $DB->insert_record('course_completion_defaults', $data + ['course' => $courseid, 'module' => $modid]);
418 }
419 // Trigger event.
420 \core\event\completion_defaults_updated::create([
421 'objectid' => $defaultsid,
422 'context' => $coursecontext,
423 'other' => ['modulename' => $modules[$modid]],
424 ])->trigger();
7f53e8aa 425 }
22002ed7
JD
426 // Add notification.
427 \core\notification::add(get_string('defaultcompletionupdated', 'completion'), \core\notification::SUCCESS);
7f53e8aa
MG
428 }
429
430 /**
431 * Returns default completion rules for given module type in the given course
432 *
433 * @param stdClass $course
434 * @param stdClass $module
435 * @param bool $flatten if true all module custom completion rules become properties of the same object,
436 * otherwise they can be found as array in ->customdata['customcompletionrules']
437 * @return stdClass
438 */
439 public static function get_default_completion($course, $module, $flatten = true) {
440 global $DB, $CFG;
441 if ($data = $DB->get_record('course_completion_defaults', ['course' => $course->id, 'module' => $module->id],
442 'completion, completionview, completionexpected, completionusegrade, customrules')) {
443 if ($data->customrules && ($customrules = @json_decode($data->customrules, true))) {
444 if ($flatten) {
445 foreach ($customrules as $key => $value) {
446 $data->$key = $value;
447 }
448 } else {
449 $data->customdata['customcompletionrules'] = $customrules;
450 }
451 }
452 unset($data->customrules);
453 } else {
454 $data = new stdClass();
455 $data->completion = COMPLETION_TRACKING_NONE;
456 if ($CFG->completiondefault) {
457 $completion = new \completion_info(get_fast_modinfo($course->id)->get_course());
458 if ($completion->is_enabled() && plugin_supports('mod', $module->name, FEATURE_MODEDIT_DEFAULT_COMPLETION, true)) {
459 $data->completion = COMPLETION_TRACKING_MANUAL;
32b93ea7 460 $data->completionview = 1;
7f53e8aa
MG
461 }
462 }
463 }
464 return $data;
465 }
0b620801 466}