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