Merge branch 'MDL-78858' of https://github.com/paulholden/moodle
[moodle.git] / completion / classes / manager.php
blob3e3cc7964b964b1ba26046eaf63c26fbedda6093
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.
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/>.
17 /**
18 * Bulk activity completion manager class
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
26 namespace core_completion;
28 use stdClass;
29 use context_course;
30 use cm_info;
31 use tabobject;
32 use lang_string;
33 use moodle_url;
34 defined('MOODLE_INTERNAL') || die;
36 /**
37 * Bulk activity completion manager class
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
44 class manager {
46 /**
47 * @var int $courseid the course id.
49 protected $courseid;
51 /**
52 * manager constructor.
53 * @param int $courseid the course id.
55 public function __construct($courseid) {
56 $this->courseid = $courseid;
59 /**
60 * Gets the data (context) to be used with the bulkactivitycompletion template.
62 * @return stdClass data for use with the bulkactivitycompletion template.
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();
71 $data->helpicon = $OUTPUT->help_icon('bulkcompletiontracking', 'core_completion');
72 $data->sections = [];
73 foreach ($sections as $sectionnumber => $section) {
74 $sectioninfo = $moduleinfo->get_section_info($sectionnumber);
76 $sectionobject = new stdClass();
77 $sectionobject->sectionnumber = $sectionnumber;
78 $sectionobject->name = get_section_name($this->courseid, $sectioninfo);
79 $sectionobject->activities = $this->get_activities($section, true);
80 $data->sections[] = $sectionobject;
82 return $data;
85 /**
86 * Gets the data (context) to be used with the activityinstance template
88 * @param array $cmids list of course module ids
89 * @param bool $withcompletiondetails include completion details
90 * @return array
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;
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);
107 // Get activity completion information.
108 if ($moduleobject->canmanage) {
109 $moduleobject->completionstatus = $this->get_completion_detail($mod);
110 } else {
111 $moduleobject->completionstatus = ['icon' => null, 'string' => null];
113 if (self::can_edit_bulk_completion($this->courseid, $mod)) {
114 $activities[] = $moduleobject;
117 return $activities;
122 * Get completion information on the selected module or module type
124 * @param cm_info|stdClass $mod either instance of cm_info (with 'customcompletionrules' in customdata) or
125 * object with fields ->completion, ->completionview, ->completionexpected, ->completionusegrade
126 * and ->customdata['customcompletionrules']
127 * @return array
129 private function get_completion_detail($mod) {
130 global $OUTPUT;
131 $strings = [];
132 switch ($mod->completion) {
133 case COMPLETION_TRACKING_NONE:
134 $strings['string'] = get_string('none');
135 break;
137 case COMPLETION_TRACKING_MANUAL:
138 $strings['string'] = get_string('manual', 'completion');
139 $strings['icon'] = $OUTPUT->pix_icon('i/completion-manual-y', get_string('completion_manual', 'completion'));
140 break;
142 case COMPLETION_TRACKING_AUTOMATIC:
143 $strings['string'] = get_string('withconditions', 'completion');
144 $strings['icon'] = $OUTPUT->pix_icon('i/completion-auto-y', get_string('completion_automatic', 'completion'));
145 break;
147 default:
148 $strings['string'] = get_string('none');
149 break;
152 // Get the descriptions for all the active completion rules for the module.
153 if ($ruledescriptions = $this->get_completion_active_rule_descriptions($mod)) {
154 foreach ($ruledescriptions as $ruledescription) {
155 $strings['string'] .= \html_writer::empty_tag('br') . $ruledescription;
158 return $strings;
162 * Get the descriptions for all active conditional completion rules for the current module.
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.
169 protected function get_completion_active_rule_descriptions($moduledata) {
170 $activeruledescriptions = [];
172 if ($moduledata->completion == COMPLETION_TRACKING_AUTOMATIC) {
173 // Generate the description strings for the core conditional completion rules (if set).
174 if (!empty($moduledata->completionview)) {
175 $activeruledescriptions[] = get_string('completionview_desc', 'completion');
177 if ($moduledata instanceof cm_info && !is_null($moduledata->completiongradeitemnumber) ||
178 ($moduledata instanceof stdClass && !empty($moduledata->completionusegrade))) {
180 $description = 'completionusegrade_desc';
181 if (!empty($moduledata->completionpassgrade)) {
182 $description = 'completionpassgrade_desc';
185 $activeruledescriptions[] = get_string($description, 'completion');
188 // Now, ask the module to provide descriptions for its custom conditional completion rules.
189 if ($customruledescriptions = component_callback($moduledata->modname,
190 'get_completion_active_rule_descriptions', [$moduledata])) {
191 $activeruledescriptions = array_merge($activeruledescriptions, $customruledescriptions);
195 if ($moduledata->completion != COMPLETION_TRACKING_NONE) {
196 if (!empty($moduledata->completionexpected)) {
197 $activeruledescriptions[] = get_string('completionexpecteddesc', 'completion',
198 userdate($moduledata->completionexpected));
202 return $activeruledescriptions;
206 * Gets the course modules for the current course.
208 * @param bool $includedefaults Whether the default values should be included or not.
209 * @return stdClass $data containing the modules
211 public function get_activities_and_resources(bool $includedefaults = true) {
212 global $DB, $OUTPUT, $CFG;
213 require_once($CFG->dirroot.'/course/lib.php');
215 // Get enabled activities and resources.
216 $modules = $DB->get_records('modules', ['visible' => 1], 'name ASC');
217 $data = new stdClass();
218 $data->courseid = $this->courseid;
219 $data->sesskey = sesskey();
220 $data->helpicon = $OUTPUT->help_icon('bulkcompletiontracking', 'core_completion');
221 // Add icon information.
222 $data->modules = array_values($modules);
223 $coursecontext = context_course::instance($this->courseid);
224 $canmanage = has_capability('moodle/course:manageactivities', $coursecontext);
225 $course = get_course($this->courseid);
226 foreach ($data->modules as $module) {
227 $module->icon = $OUTPUT->image_url('monologo', $module->name)->out();
228 $module->formattedname = format_string(get_string('modulename', 'mod_' . $module->name),
229 true, ['context' => $coursecontext]);
230 $module->canmanage = $canmanage && course_allowed_module($course, $module->name);
231 if ($includedefaults) {
232 $defaults = self::get_default_completion($course, $module, false);
233 $defaults->modname = $module->name;
234 $module->completionstatus = $this->get_completion_detail($defaults);
238 return $data;
242 * Checks if current user can edit activity completion
244 * @param int|stdClass $courseorid
245 * @param \cm_info|null $cm if specified capability for a given coursemodule will be check,
246 * if not specified capability to edit at least one activity is checked.
248 public static function can_edit_bulk_completion($courseorid, $cm = null) {
249 if ($cm) {
250 return $cm->uservisible && has_capability('moodle/course:manageactivities', $cm->context);
252 $coursecontext = context_course::instance(is_object($courseorid) ? $courseorid->id : $courseorid);
253 if (has_capability('moodle/course:manageactivities', $coursecontext)) {
254 return true;
256 $modinfo = get_fast_modinfo($courseorid);
257 foreach ($modinfo->cms as $mod) {
258 if ($mod->uservisible && has_capability('moodle/course:manageactivities', $mod->context)) {
259 return true;
262 return false;
266 * Gets the available completion tabs for the current course and user.
268 * @deprecated since Moodle 4.0
269 * @param stdClass|int $courseorid the course object or id.
270 * @return tabobject[]
272 public static function get_available_completion_tabs($courseorid) {
273 debugging('get_available_completion_tabs() has been deprecated. Please use ' .
274 'core_completion\manager::get_available_completion_options() instead.', DEBUG_DEVELOPER);
276 $tabs = [];
278 $courseid = is_object($courseorid) ? $courseorid->id : $courseorid;
279 $coursecontext = context_course::instance($courseid);
281 if (has_capability('moodle/course:update', $coursecontext)) {
282 $tabs[] = new tabobject(
283 'completion',
284 new moodle_url('/course/completion.php', ['id' => $courseid]),
285 new lang_string('coursecompletion', 'completion')
289 if (has_capability('moodle/course:manageactivities', $coursecontext)) {
290 $tabs[] = new tabobject(
291 'defaultcompletion',
292 new moodle_url('/course/defaultcompletion.php', ['id' => $courseid]),
293 new lang_string('defaultcompletion', 'completion')
297 if (self::can_edit_bulk_completion($courseorid)) {
298 $tabs[] = new tabobject(
299 'bulkcompletion',
300 new moodle_url('/course/bulkcompletion.php', ['id' => $courseid]),
301 new lang_string('bulkactivitycompletion', 'completion')
305 return $tabs;
309 * Returns an array with the available completion options (url => name) for the current course and user.
311 * @param int $courseid The course id.
312 * @return array
314 public static function get_available_completion_options(int $courseid): array {
315 $coursecontext = context_course::instance($courseid);
316 $options = [];
318 if (has_capability('moodle/course:update', $coursecontext)) {
319 $completionlink = new moodle_url('/course/completion.php', ['id' => $courseid]);
320 $options[$completionlink->out(false)] = get_string('coursecompletionsettings', 'completion');
323 if (has_capability('moodle/course:manageactivities', $coursecontext)) {
324 $defaultcompletionlink = new moodle_url('/course/defaultcompletion.php', ['id' => $courseid]);
325 $options[$defaultcompletionlink->out(false)] = get_string('defaultcompletion', 'completion');
328 if (self::can_edit_bulk_completion($courseid)) {
329 $bulkcompletionlink = new moodle_url('/course/bulkcompletion.php', ['id' => $courseid]);
330 $options[$bulkcompletionlink->out(false)] = get_string('bulkactivitycompletion', 'completion');
333 return $options;
337 * Applies completion from the bulk edit form to all selected modules
339 * @param stdClass $data data received from the core_completion_bulkedit_form
340 * @param bool $updateinstances if we need to update the instance tables of the module (i.e. 'assign', 'forum', etc.) -
341 * if no module-specific completion rules were added to the form, update of the module table is not needed.
343 public function apply_completion($data, $updateinstances) {
344 $updated = false;
345 $needreset = [];
346 $modinfo = get_fast_modinfo($this->courseid);
348 $cmids = $data->cmid;
350 $data = (array)$data;
351 unset($data['id']); // This is a course id, we don't want to confuse it with cmid or instance id.
352 unset($data['cmid']);
353 unset($data['submitbutton']);
355 foreach ($cmids as $cmid) {
356 $cm = $modinfo->get_cm($cmid);
357 if (self::can_edit_bulk_completion($this->courseid, $cm) && $this->apply_completion_cm($cm, $data, $updateinstances)) {
358 $updated = true;
359 if ($cm->completion != COMPLETION_TRACKING_MANUAL || $data['completion'] != COMPLETION_TRACKING_MANUAL) {
360 // If completion was changed we will need to reset it's state. Exception is when completion was and remains as manual.
361 $needreset[] = $cm->id;
364 // Update completion calendar events.
365 $completionexpected = ($data['completionexpected']) ? $data['completionexpected'] : null;
366 \core_completion\api::update_completion_date_event($cm->id, $cm->modname, $cm->instance, $completionexpected);
368 if ($updated) {
369 // Now that modules are fully updated, also update completion data if required.
370 // This will wipe all user completion data and recalculate it.
371 rebuild_course_cache($this->courseid, true);
372 $modinfo = get_fast_modinfo($this->courseid);
373 $completion = new \completion_info($modinfo->get_course());
374 foreach ($needreset as $cmid) {
375 $completion->reset_all_state($modinfo->get_cm($cmid));
378 // And notify the user of the result.
379 \core\notification::add(get_string('activitycompletionupdated', 'core_completion'), \core\notification::SUCCESS);
384 * Applies new completion rules to one course module
386 * @param \cm_info $cm
387 * @param array $data
388 * @param bool $updateinstance if we need to update the instance table of the module (i.e. 'assign', 'forum', etc.) -
389 * if no module-specific completion rules were added to the form, update of the module table is not needed.
390 * @return bool if module was updated
392 protected function apply_completion_cm(\cm_info $cm, $data, $updateinstance) {
393 global $DB;
395 $defaults = [
396 'completion' => COMPLETION_DISABLED, 'completionview' => COMPLETION_VIEW_NOT_REQUIRED,
397 'completionexpected' => 0, 'completiongradeitemnumber' => null,
398 'completionpassgrade' => 0
401 $data += ['completion' => $cm->completion,
402 'completionexpected' => $cm->completionexpected,
403 'completionview' => $cm->completionview];
405 if ($cm->completion == $data['completion'] && $cm->completion == COMPLETION_TRACKING_NONE) {
406 // If old and new completion are both "none" - no changes are needed.
407 return false;
410 if ($cm->completion == $data['completion'] && $cm->completion == COMPLETION_TRACKING_NONE &&
411 $cm->completionexpected == $data['completionexpected']) {
412 // If old and new completion are both "manual" and completion expected date is not changed - no changes are needed.
413 return false;
416 if (array_key_exists('completionusegrade', $data)) {
417 // Convert the 'use grade' checkbox into a grade-item number: 0 if checked, null if not.
418 $data['completiongradeitemnumber'] = !empty($data['completionusegrade']) ? 0 : null;
419 unset($data['completionusegrade']);
420 } else {
421 // Completion grade item number is classified in mod_edit forms as 'use grade'.
422 $data['completionusegrade'] = is_null($cm->completiongradeitemnumber) ? 0 : 1;
423 $data['completiongradeitemnumber'] = $cm->completiongradeitemnumber;
426 // Update module instance table.
427 if ($updateinstance) {
428 $moddata = ['id' => $cm->instance, 'timemodified' => time()] + array_diff_key($data, $defaults);
429 $DB->update_record($cm->modname, $moddata);
432 // Update course modules table.
433 $cmdata = ['id' => $cm->id, 'timemodified' => time()] + array_intersect_key($data, $defaults);
434 $DB->update_record('course_modules', $cmdata);
436 \core\event\course_module_updated::create_from_cm($cm, $cm->context)->trigger();
438 // We need to reset completion data for this activity.
439 return true;
444 * Saves default completion from edit form to all selected module types
446 * @param stdClass $data data received from the core_completion_bulkedit_form
447 * @param bool $updatecustomrules if we need to update the custom rules of the module -
448 * if no module-specific completion rules were added to the form, update of the module table is not needed.
449 * @param string $suffix the suffix to add to the name of the completion rules.
451 public function apply_default_completion($data, $updatecustomrules, string $suffix = '') {
452 global $DB;
454 if (!empty($suffix)) {
455 // Fields were renamed to avoid conflicts, but they need to be stored in DB with the original name.
456 $modules = property_exists($data, 'modules') ? $data->modules : null;
457 if ($modules !== null) {
458 unset($data->modules);
459 $data = (array)$data;
460 foreach ($data as $name => $value) {
461 if (str_ends_with($name, $suffix)) {
462 $data[substr($name, 0, strpos($name, $suffix))] = $value;
463 unset($data[$name]);
464 } else if ($name == 'customdata') {
465 $customrules = $value['customcompletionrules'];
466 foreach ($customrules as $rulename => $rulevalue) {
467 if (str_ends_with($rulename, $suffix)) {
468 $customrules[substr($rulename, 0, strpos($rulename, $suffix))] = $rulevalue;
469 unset($customrules[$rulename]);
472 $data['customdata'] = $customrules;
475 $data = (object)$data;
479 $courseid = $data->id;
480 // MDL-72375 Unset the id here, it should not be stored in customrules.
481 unset($data->id);
482 $coursecontext = context_course::instance($courseid);
483 if (!$modids = $data->modids) {
484 return;
486 $defaults = [
487 'completion' => COMPLETION_DISABLED,
488 'completionview' => COMPLETION_VIEW_NOT_REQUIRED,
489 'completionexpected' => 0,
490 'completionusegrade' => 0,
491 'completionpassgrade' => 0
494 $data = (array)$data;
496 if ($updatecustomrules) {
497 $customdata = array_diff_key($data, $defaults);
498 $data['customrules'] = $customdata ? json_encode($customdata) : null;
499 $defaults['customrules'] = null;
501 $data = array_intersect_key($data, $defaults);
503 // Get names of the affected modules.
504 list($modidssql, $params) = $DB->get_in_or_equal($modids);
505 $params[] = 1;
506 $modules = $DB->get_records_select_menu('modules', 'id ' . $modidssql . ' and visible = ?', $params, '', 'id, name');
508 // Get an associative array of [module_id => course_completion_defaults_id].
509 list($in, $params) = $DB->get_in_or_equal($modids);
510 $params[] = $courseid;
511 $defaultsids = $DB->get_records_select_menu('course_completion_defaults', 'module ' . $in . ' and course = ?', $params, '',
512 'module, id');
514 foreach ($modids as $modid) {
515 if (!array_key_exists($modid, $modules)) {
516 continue;
518 if (isset($defaultsids[$modid])) {
519 $DB->update_record('course_completion_defaults', $data + ['id' => $defaultsids[$modid]]);
520 } else {
521 $defaultsids[$modid] = $DB->insert_record('course_completion_defaults', $data + ['course' => $courseid,
522 'module' => $modid]);
524 // Trigger event.
525 \core\event\completion_defaults_updated::create([
526 'objectid' => $defaultsids[$modid],
527 'context' => $coursecontext,
528 'other' => ['modulename' => $modules[$modid]],
529 ])->trigger();
532 // Add notification.
533 \core\notification::add(get_string('defaultcompletionupdated', 'completion'), \core\notification::SUCCESS);
537 * Returns default completion rules for given module type in the given course
539 * @param stdClass $course
540 * @param stdClass $module
541 * @param bool $flatten if true all module custom completion rules become properties of the same object,
542 * otherwise they can be found as array in ->customdata['customcompletionrules']
543 * @param string $suffix the suffix to add to the name of the completion rules.
544 * @return stdClass
546 public static function get_default_completion($course, $module, $flatten = true, string $suffix = '') {
547 global $DB, $CFG;
548 if ($data = $DB->get_record('course_completion_defaults', ['course' => $course->id, 'module' => $module->id],
549 'completion, completionview, completionexpected, completionusegrade, completionpassgrade, customrules')) {
550 if ($data->customrules && ($customrules = @json_decode($data->customrules, true))) {
551 // MDL-72375 This will override activity id for new mods. Skip this field, it is already exposed as courseid.
552 unset($customrules['id']);
554 if ($flatten) {
555 foreach ($customrules as $key => $value) {
556 $data->$key = $value;
558 } else {
559 $data->customdata['customcompletionrules'] = $customrules;
562 unset($data->customrules);
563 } else {
564 $data = new stdClass();
565 $data->completion = COMPLETION_TRACKING_NONE;
566 if ($CFG->completiondefault) {
567 $completion = new \completion_info(get_fast_modinfo($course->id)->get_course());
568 if ($completion->is_enabled() && plugin_supports('mod', $module->name, FEATURE_MODEDIT_DEFAULT_COMPLETION, true)) {
569 $data->completion = COMPLETION_TRACKING_MANUAL;
570 $data->completionview = 1;
575 // If the suffix is not empty, the completion rules need to be renamed to avoid conflicts.
576 if (!empty($suffix)) {
577 $data = (array)$data;
578 foreach ($data as $name => $value) {
579 if (str_starts_with($name, 'completion')) {
580 $data[$name . $suffix] = $value;
581 unset($data[$name]);
582 } else if ($name == 'customdata') {
583 $customrules = $value['customcompletionrules'];
584 foreach ($customrules as $rulename => $rulevalue) {
585 if (str_starts_with($rulename, 'completion')) {
586 $customrules[$rulename . $suffix] = $rulevalue;
587 unset($customrules[$rulename]);
590 $data['customdata'] = $customrules;
593 $data = (object)$data;
596 return $data;