MDL-58140 completion: Added bulk activity completion page.
[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;
30
31/**
32 * Bulk activity completion manager class
33 *
34 * @package core_completion
35 * @category completion
36 * @copyright 2017 Adrian Greeve
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class manager {
40
41 protected $courseid;
42
43 public function __construct($courseid) {
44 $this->courseid = $courseid;
45 }
46
47 /**
48 * Gets the data (context) to be used with the bulkactivitycompletion template.
49 *
50 * @return stdClass data for use with the bulkactivitycompletion template.
51 */
52 public function get_activities_and_headings() {
53 global $OUTPUT;
54 $moduleinfo = get_fast_modinfo($this->courseid);
55 $sections = $moduleinfo->get_sections();
56 $data = new stdClass;
57 $data->courseid = $this->courseid;
58 $data->sesskey = sesskey();
59 $data->helpicon = $OUTPUT->help_icon('temphelp', 'moodle');
60 $data->sections = [];
61 foreach ($sections as $sectionnumber => $section) {
62 $sectioninfo = $moduleinfo->get_section_info($sectionnumber);
63
64 $sectionobject = new stdClass();
65 $sectionobject->sectionnumber = $sectionnumber;
66 $sectionobject->name = get_section_name($this->courseid, $sectioninfo);
67 $sectionobject->activities = [];
68
69 foreach ($section as $cmid) {
70 $mod = $moduleinfo->get_cm($cmid);
71 $moduleobject = new stdClass();
72 $moduleobject->cmid = $cmid;
73 $moduleobject->modname = $mod->get_formatted_name();
74 $moduleobject->icon = $mod->get_icon_url()->out();
75 $moduleobject->url = $mod->url;
76
77 // Get activity completion information.
78 $moduleobject->completionstatus = $this->get_completion_detail($mod);
79
80 $sectionobject->activities[] = $moduleobject;
81 }
82 $data->sections[] = $sectionobject;
83 }
84 return $data;
85 }
86
87 private function get_completion_detail(\cm_info $mod) {
88 global $OUTPUT;
89 $strings = [];
90 switch ($mod->completion) {
91 case 0:
92 $strings['string'] = get_string('none');
93 break;
94
95 case 1:
96 $strings['string'] = get_string('manual');
97 $strings['icon'] = $OUTPUT->pix_url('i/completion-manual-enabled')->out();
98 break;
99
100 case 2:
101 $strings['string'] = get_string('withconditions');
102
103 // Get the descriptions for all the active completion rules for the module.
104 if ($ruledescriptions = $mod->get_completion_active_rule_descriptions()) {
105 foreach ($ruledescriptions as $ruledescription) {
106 $strings['string'] .= \html_writer::empty_tag('br') . $ruledescription;
107 }
108 }
109
110 $strings['icon'] = $OUTPUT->pix_url('i/completion-auto-enabled')->out();
111 break;
112
113 default:
114 $strings['string'] = get_string('none');
115 break;
116 }
117 return $strings;
118 }
119
120}