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