Merge branch 'MDL-78025_401' of https://github.com/timhunt/moodle into MOODLE_401_STABLE
[moodle.git] / mod / lesson / overrides.php
blob2e81ea404913334d069696fff4393f4391bce2cf
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 require_login($course, false, $cm);
40 $context = context_module::instance($cm->id);
42 // Check the user has the required capabilities to list overrides.
43 require_capability('mod/lesson:manageoverrides', $context);
45 $lessongroupmode = groups_get_activity_groupmode($cm);
46 $accessallgroups = ($lessongroupmode == NOGROUPS) || has_capability('moodle/site:accessallgroups', $context);
48 // Get the course groups that the current user can access.
49 $groups = $accessallgroups ? groups_get_all_groups($cm->course) : groups_get_activity_allowed_groups($cm);
51 // Default mode is "group", unless there are no groups.
52 if ($mode != "user" and $mode != "group") {
53 if (!empty($groups)) {
54 $mode = "group";
55 } else {
56 $mode = "user";
59 $groupmode = ($mode == "group");
61 $url = new moodle_url('/mod/lesson/overrides.php', array('cmid' => $cm->id, 'mode' => $mode));
63 $PAGE->set_url($url);
65 // Display a list of overrides.
66 $PAGE->set_pagelayout('admin');
67 $PAGE->add_body_class('limitedwidth');
68 $PAGE->set_title(get_string('overrides', 'lesson'));
69 $PAGE->set_heading($course->fullname);
70 $PAGE->activityheader->set_attrs([
71 'hidecompletion' => true,
72 'description' => ''
73 ]);
74 navigation_node::override_active_url(new moodle_url('/mod/lesson/overrides.php', ['cmid' => $cmid]));
76 $renderer = $PAGE->get_renderer('mod_lesson');
78 echo $OUTPUT->header();
80 // Delete orphaned group overrides.
81 $sql = 'SELECT o.id
82 FROM {lesson_overrides} o
83 LEFT JOIN {groups} g ON o.groupid = g.id
84 WHERE o.groupid IS NOT NULL
85 AND g.id IS NULL
86 AND o.lessonid = ?';
87 $params = array($lesson->id);
88 $orphaned = $DB->get_records_sql($sql, $params);
89 if (!empty($orphaned)) {
90 $DB->delete_records_list('lesson_overrides', 'id', array_keys($orphaned));
93 $overrides = [];
95 // Fetch all overrides.
96 if ($groupmode) {
97 $colname = get_string('group');
98 // To filter the result by the list of groups that the current user has access to.
99 if ($groups) {
100 $params = ['lessonid' => $lesson->id];
101 list($insql, $inparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
102 $params += $inparams;
104 $sql = "SELECT o.*, g.name
105 FROM {lesson_overrides} o
106 JOIN {groups} g ON o.groupid = g.id
107 WHERE o.lessonid = :lessonid AND g.id $insql
108 ORDER BY g.name";
110 $overrides = $DB->get_records_sql($sql, $params);
112 } else {
113 $colname = get_string('user');
114 list($sort, $params) = users_order_by_sql('u');
115 $params['lessonid'] = $lesson->id;
117 $userfieldsapi = \core_user\fields::for_name();
118 if ($accessallgroups) {
119 $sql = 'SELECT o.*, ' . $userfieldsapi->get_sql('u', false, '', '', false)->selects . '
120 FROM {lesson_overrides} o
121 JOIN {user} u ON o.userid = u.id
122 WHERE o.lessonid = :lessonid
123 ORDER BY ' . $sort;
125 $overrides = $DB->get_records_sql($sql, $params);
126 } else if ($groups) {
127 list($insql, $inparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
128 $params += $inparams;
130 $sql = 'SELECT o.*, ' . $userfieldsapi->get_sql('u', false, '', '', false)->selects . '
131 FROM {lesson_overrides} o
132 JOIN {user} u ON o.userid = u.id
133 JOIN {groups_members} gm ON u.id = gm.userid
134 WHERE o.lessonid = :lessonid AND gm.groupid ' . $insql . '
135 ORDER BY ' . $sort;
137 $overrides = $DB->get_records_sql($sql, $params);
141 $overrides = $DB->get_records_sql($sql, $params);
143 $canoverride = true;
144 $errormessage = '';
146 if ($groupmode) {
147 if (empty($groups)) {
148 // There are no groups.
149 $canoverride = false;
150 $errormessage = get_string('groupsnone', 'lesson');
152 } else {
153 $users = array();
154 // See if there are any users in the lesson.
155 if ($accessallgroups) {
156 $users = get_enrolled_users($context, '', 0, 'u.id');
157 $nousermessage = get_string('usersnone', 'lesson');
158 } else if ($groups) {
159 $enrolledjoin = get_enrolled_join($context, 'u.id');
160 list($ingroupsql, $ingroupparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
161 $params = $enrolledjoin->params + $ingroupparams;
162 $sql = "SELECT u.id
163 FROM {user} u
164 JOIN {groups_members} gm ON gm.userid = u.id
165 {$enrolledjoin->joins}
166 WHERE gm.groupid $ingroupsql
167 AND {$enrolledjoin->wheres}
168 ORDER BY $sort";
169 $users = $DB->get_records_sql($sql, $params);
170 $nousermessage = get_string('usersnone', 'lesson');
171 } else {
172 $nousermessage = get_string('groupsnone', 'lesson');
174 $info = new \core_availability\info_module($cm);
175 $users = $info->filter_user_list($users);
177 if (empty($users)) {
178 // There are no users.
179 $canoverride = false;
180 $errormessage = $nousermessage;
184 // Initialise table.
185 $table = new html_table();
186 $table->headspan = array(1, 2, 1);
187 $table->colclasses = array('colname', 'colsetting', 'colvalue', 'colaction');
188 $table->head = array(
189 $colname,
190 get_string('overrides', 'lesson'),
191 get_string('action'),
194 $userurl = new moodle_url('/user/view.php', array());
195 $groupurl = new moodle_url('/group/overview.php', array('id' => $cm->course));
197 $overridedeleteurl = new moodle_url('/mod/lesson/overridedelete.php');
198 $overrideediturl = new moodle_url('/mod/lesson/overrideedit.php');
200 $hasinactive = false; // Whether there are any inactive overrides.
202 foreach ($overrides as $override) {
204 $fields = array();
205 $values = array();
206 $active = true;
208 // Check for inactive overrides.
209 if (!$groupmode) {
210 if (!is_enrolled($context, $override->userid)) {
211 // User not enrolled.
212 $active = false;
213 } else if (!\core_availability\info_module::is_user_visible($cm, $override->userid)) {
214 // User cannot access the module.
215 $active = false;
219 // Format available.
220 if (isset($override->available)) {
221 $fields[] = get_string('lessonopens', 'lesson');
222 $values[] = $override->available > 0 ?
223 userdate($override->available) : get_string('noopen', 'lesson');
226 // Format deadline.
227 if (isset($override->deadline)) {
228 $fields[] = get_string('lessoncloses', 'lesson');
229 $values[] = $override->deadline > 0 ?
230 userdate($override->deadline) : get_string('noclose', 'lesson');
233 // Format timelimit.
234 if (isset($override->timelimit)) {
235 $fields[] = get_string('timelimit', 'lesson');
236 $values[] = $override->timelimit > 0 ?
237 format_time($override->timelimit) : get_string('none', 'lesson');
240 // Format option to try a question again.
241 if (isset($override->review)) {
242 $fields[] = get_string('displayreview', 'lesson');
243 $values[] = $override->review ?
244 get_string('yes') : get_string('no');
247 // Format number of attempts.
248 if (isset($override->maxattempts)) {
249 $fields[] = get_string('maximumnumberofattempts', 'lesson');
250 $values[] = $override->maxattempts > 0 ?
251 $override->maxattempts : get_string('unlimited');
254 // Format retake allowed.
255 if (isset($override->retake)) {
256 $fields[] = get_string('retakesallowed', 'lesson');
257 $values[] = $override->retake ?
258 get_string('yes') : get_string('no');
261 // Format password.
262 if (isset($override->password)) {
263 $fields[] = get_string('usepassword', 'lesson');
264 $values[] = $override->password !== '' ?
265 get_string('enabled', 'lesson') : get_string('none', 'lesson');
268 // Icons.
269 $iconstr = '';
271 // Edit.
272 $editurlstr = $overrideediturl->out(true, array('id' => $override->id));
273 $iconstr = '<a title="' . get_string('edit') . '" href="'. $editurlstr . '">' .
274 $OUTPUT->pix_icon('t/edit', get_string('edit')) . '</a> ';
275 // Duplicate.
276 $copyurlstr = $overrideediturl->out(true,
277 array('id' => $override->id, 'action' => 'duplicate'));
278 $iconstr .= '<a title="' . get_string('copy') . '" href="' . $copyurlstr . '">' .
279 $OUTPUT->pix_icon('t/copy', get_string('copy')) . '</a> ';
280 // Delete.
281 $deleteurlstr = $overridedeleteurl->out(true,
282 array('id' => $override->id, 'sesskey' => sesskey()));
283 $iconstr .= '<a title="' . get_string('delete') . '" href="' . $deleteurlstr . '">' .
284 $OUTPUT->pix_icon('t/delete', get_string('delete')) . '</a> ';
286 if ($groupmode) {
287 $usergroupstr = '<a href="' . $groupurl->out(true, ['group' => $override->groupid]) . '" >' .
288 format_string($override->name, true, ['context' => $context]) . '</a>';
289 } else {
290 $usergroupstr = '<a href="' . $userurl->out(true,
291 array('id' => $override->userid)) . '" >' . fullname($override) . '</a>';
294 $class = '';
295 if (!$active) {
296 $class = "dimmed_text";
297 $usergroupstr .= '*';
298 $hasinactive = true;
301 $usergroupcell = new html_table_cell();
302 $usergroupcell->rowspan = count($fields);
303 $usergroupcell->text = $usergroupstr;
304 $actioncell = new html_table_cell();
305 $actioncell->rowspan = count($fields);
306 $actioncell->text = $iconstr;
308 for ($i = 0; $i < count($fields); ++$i) {
309 $row = new html_table_row();
310 $row->attributes['class'] = $class;
311 if ($i == 0) {
312 $row->cells[] = $usergroupcell;
314 $cell1 = new html_table_cell();
315 $cell1->text = $fields[$i];
316 $row->cells[] = $cell1;
317 $cell2 = new html_table_cell();
318 $cell2->text = $values[$i];
319 $row->cells[] = $cell2;
320 if ($i == 0) {
321 $row->cells[] = $actioncell;
323 $table->data[] = $row;
327 $overrideselect = new \mod_lesson\output\override_action_menu($cm->id, $url, $canoverride);
328 echo $renderer->render($overrideselect);
330 // Output the table and button.
331 echo html_writer::start_tag('div', array('id' => 'lessonoverrides'));
332 if (count($table->data)) {
333 echo html_writer::table($table);
336 // No overrides to be displayed.
337 if (!$overrides) {
338 echo $OUTPUT->notification(get_string('nooverridecreated', 'lesson'), 'info', false);
341 if ($hasinactive) {
342 echo $OUTPUT->notification(get_string('inactiveoverridehelp', 'lesson'), 'dimmed_text');
345 echo html_writer::start_tag('div', array('class' => 'buttons'));
346 if (!empty($errormessage)) {
347 echo $OUTPUT->notification($errormessage, 'error');
349 echo html_writer::end_tag('div');
350 echo html_writer::end_tag('div');
352 // Finish the page.
353 echo $OUTPUT->footer();