Merge branch 'MDL-62782-master' of git://github.com/damyon/moodle
[moodle.git] / mod / lesson / overrides.php
blobbe60a7787ab2d9ef0650ec0d31d48aad0fb0eee1
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 * This page handles listing of lesson overrides
20 * @package mod_lesson
21 * @copyright 2015 Jean-Michel Vedrine
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once(__DIR__ . '/../../config.php');
27 require_once($CFG->dirroot.'/mod/lesson/lib.php');
28 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
29 require_once($CFG->dirroot.'/mod/lesson/override_form.php');
32 $cmid = required_param('cmid', PARAM_INT);
33 $mode = optional_param('mode', '', PARAM_ALPHA); // One of 'user' or 'group', default is 'group'.
35 list($course, $cm) = get_course_and_cm_from_cmid($cmid, 'lesson');
36 $lesson = $DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST);
38 // Get the course groups.
39 $groups = groups_get_all_groups($cm->course);
40 if ($groups === false) {
41 $groups = array();
44 // Default mode is "group", unless there are no groups.
45 if ($mode != "user" and $mode != "group") {
46 if (!empty($groups)) {
47 $mode = "group";
48 } else {
49 $mode = "user";
52 $groupmode = ($mode == "group");
54 $url = new moodle_url('/mod/lesson/overrides.php', array('cmid' => $cm->id, 'mode' => $mode));
56 $PAGE->set_url($url);
58 require_login($course, false, $cm);
60 $context = context_module::instance($cm->id);
62 // Check the user has the required capabilities to list overrides.
63 require_capability('mod/lesson:manageoverrides', $context);
65 // Display a list of overrides.
66 $PAGE->set_pagelayout('admin');
67 $PAGE->set_title(get_string('overrides', 'lesson'));
68 $PAGE->set_heading($course->fullname);
69 echo $OUTPUT->header();
70 echo $OUTPUT->heading(format_string($lesson->name, true, array('context' => $context)));
72 // Delete orphaned group overrides.
73 $sql = 'SELECT o.id
74 FROM {lesson_overrides} o LEFT JOIN {groups} g
75 ON o.groupid = g.id
76 WHERE o.groupid IS NOT NULL
77 AND g.id IS NULL
78 AND o.lessonid = ?';
79 $params = array($lesson->id);
80 $orphaned = $DB->get_records_sql($sql, $params);
81 if (!empty($orphaned)) {
82 $DB->delete_records_list('lesson_overrides', 'id', array_keys($orphaned));
85 // Fetch all overrides.
86 if ($groupmode) {
87 $colname = get_string('group');
88 $sql = 'SELECT o.*, g.name
89 FROM {lesson_overrides} o
90 JOIN {groups} g ON o.groupid = g.id
91 WHERE o.lessonid = :lessonid
92 ORDER BY g.name';
93 $params = array('lessonid' => $lesson->id);
94 } else {
95 $colname = get_string('user');
96 list($sort, $params) = users_order_by_sql('u');
97 $sql = 'SELECT o.*, ' . get_all_user_name_fields(true, 'u') . '
98 FROM {lesson_overrides} o
99 JOIN {user} u ON o.userid = u.id
100 WHERE o.lessonid = :lessonid
101 ORDER BY ' . $sort;
102 $params['lessonid'] = $lesson->id;
105 $overrides = $DB->get_records_sql($sql, $params);
107 // Initialise table.
108 $table = new html_table();
109 $table->headspan = array(1, 2, 1);
110 $table->colclasses = array('colname', 'colsetting', 'colvalue', 'colaction');
111 $table->head = array(
112 $colname,
113 get_string('overrides', 'lesson'),
114 get_string('action'),
117 $userurl = new moodle_url('/user/view.php', array());
118 $groupurl = new moodle_url('/group/overview.php', array('id' => $cm->course));
120 $overridedeleteurl = new moodle_url('/mod/lesson/overridedelete.php');
121 $overrideediturl = new moodle_url('/mod/lesson/overrideedit.php');
123 $hasinactive = false; // Whether there are any inactive overrides.
125 foreach ($overrides as $override) {
127 $fields = array();
128 $values = array();
129 $active = true;
131 // Check for inactive overrides.
132 if (!$groupmode) {
133 if (!is_enrolled($context, $override->userid)) {
134 // User not enrolled.
135 $active = false;
136 } else if (!\core_availability\info_module::is_user_visible($cm, $override->userid)) {
137 // User cannot access the module.
138 $active = false;
142 // Format available.
143 if (isset($override->available)) {
144 $fields[] = get_string('lessonopens', 'lesson');
145 $values[] = $override->available > 0 ?
146 userdate($override->available) : get_string('noopen', 'lesson');
149 // Format deadline.
150 if (isset($override->deadline)) {
151 $fields[] = get_string('lessoncloses', 'lesson');
152 $values[] = $override->deadline > 0 ?
153 userdate($override->deadline) : get_string('noclose', 'lesson');
156 // Format timelimit.
157 if (isset($override->timelimit)) {
158 $fields[] = get_string('timelimit', 'lesson');
159 $values[] = $override->timelimit > 0 ?
160 format_time($override->timelimit) : get_string('none', 'lesson');
163 // Format option to try a question again.
164 if (isset($override->review)) {
165 $fields[] = get_string('displayreview', 'lesson');
166 $values[] = $override->review ?
167 get_string('yes') : get_string('no');
170 // Format number of attempts.
171 if (isset($override->maxattempts)) {
172 $fields[] = get_string('maximumnumberofattempts', 'lesson');
173 $values[] = $override->maxattempts > 0 ?
174 $override->maxattempts : get_string('unlimited');
177 // Format retake allowed.
178 if (isset($override->retake)) {
179 $fields[] = get_string('retakesallowed', 'lesson');
180 $values[] = $override->retake ?
181 get_string('yes') : get_string('no');
184 // Format password.
185 if (isset($override->password)) {
186 $fields[] = get_string('usepassword', 'lesson');
187 $values[] = $override->password !== '' ?
188 get_string('enabled', 'lesson') : get_string('none', 'lesson');
191 // Icons.
192 $iconstr = '';
194 if ($active) {
195 // Edit.
196 $editurlstr = $overrideediturl->out(true, array('id' => $override->id));
197 $iconstr = '<a title="' . get_string('edit') . '" href="'. $editurlstr . '">' .
198 $OUTPUT->pix_icon('t/edit', get_string('edit')) . '</a> ';
199 // Duplicate.
200 $copyurlstr = $overrideediturl->out(true,
201 array('id' => $override->id, 'action' => 'duplicate'));
202 $iconstr .= '<a title="' . get_string('copy') . '" href="' . $copyurlstr . '">' .
203 $OUTPUT->pix_icon('t/copy', get_string('copy')) . '</a> ';
205 // Delete.
206 $deleteurlstr = $overridedeleteurl->out(true,
207 array('id' => $override->id, 'sesskey' => sesskey()));
208 $iconstr .= '<a title="' . get_string('delete') . '" href="' . $deleteurlstr . '">' .
209 $OUTPUT->pix_icon('t/delete', get_string('delete')) . '</a> ';
211 if ($groupmode) {
212 $usergroupstr = '<a href="' . $groupurl->out(true,
213 array('group' => $override->groupid)) . '" >' . $override->name . '</a>';
214 } else {
215 $usergroupstr = '<a href="' . $userurl->out(true,
216 array('id' => $override->userid)) . '" >' . fullname($override) . '</a>';
219 $class = '';
220 if (!$active) {
221 $class = "dimmed_text";
222 $usergroupstr .= '*';
223 $hasinactive = true;
226 $usergroupcell = new html_table_cell();
227 $usergroupcell->rowspan = count($fields);
228 $usergroupcell->text = $usergroupstr;
229 $actioncell = new html_table_cell();
230 $actioncell->rowspan = count($fields);
231 $actioncell->text = $iconstr;
233 for ($i = 0; $i < count($fields); ++$i) {
234 $row = new html_table_row();
235 $row->attributes['class'] = $class;
236 if ($i == 0) {
237 $row->cells[] = $usergroupcell;
239 $cell1 = new html_table_cell();
240 $cell1->text = $fields[$i];
241 $row->cells[] = $cell1;
242 $cell2 = new html_table_cell();
243 $cell2->text = $values[$i];
244 $row->cells[] = $cell2;
245 if ($i == 0) {
246 $row->cells[] = $actioncell;
248 $table->data[] = $row;
252 // Output the table and button.
253 echo html_writer::start_tag('div', array('id' => 'lessonoverrides'));
254 if (count($table->data)) {
255 echo html_writer::table($table);
257 if ($hasinactive) {
258 echo $OUTPUT->notification(get_string('inactiveoverridehelp', 'lesson'), 'dimmed_text');
261 echo html_writer::start_tag('div', array('class' => 'buttons'));
262 $options = array();
263 if ($groupmode) {
264 if (empty($groups)) {
265 // There are no groups.
266 echo $OUTPUT->notification(get_string('groupsnone', 'lesson'), 'error');
267 $options['disabled'] = true;
269 echo $OUTPUT->single_button($overrideediturl->out(true,
270 array('action' => 'addgroup', 'cmid' => $cm->id)),
271 get_string('addnewgroupoverride', 'lesson'), 'post', $options);
272 } else {
273 $users = array();
274 // See if there are any users in the lesson.
275 $users = get_enrolled_users($context);
276 $info = new \core_availability\info_module($cm);
277 $users = $info->filter_user_list($users);
279 if (empty($users)) {
280 // There are no users.
281 echo $OUTPUT->notification(get_string('usersnone', 'lesson'), 'error');
282 $options['disabled'] = true;
284 echo $OUTPUT->single_button($overrideediturl->out(true,
285 array('action' => 'adduser', 'cmid' => $cm->id)),
286 get_string('addnewuseroverride', 'lesson'), 'get', $options);
288 echo html_writer::end_tag('div');
289 echo html_writer::end_tag('div');
291 // Finish the page.
292 echo $OUTPUT->footer();