2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * This page handles listing of lesson overrides
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)) {
59 $groupmode = ($mode == "group");
61 $url = new moodle_url('/mod/lesson/overrides.php', array('cmid' => $cm->id
, 'mode' => $mode));
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,
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.
82 FROM {lesson_overrides} o
83 LEFT JOIN {groups} g ON o.groupid = g.id
84 WHERE o.groupid IS NOT NULL
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));
95 // Fetch all overrides.
97 $colname = get_string('group');
98 // To filter the result by the list of groups that the current user has access to.
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
110 $overrides = $DB->get_records_sql($sql, $params);
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
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 . '
137 $overrides = $DB->get_records_sql($sql, $params);
141 $overrides = $DB->get_records_sql($sql, $params);
147 if (empty($groups)) {
148 // There are no groups.
149 $canoverride = false;
150 $errormessage = get_string('groupsnone', 'lesson');
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;
164 JOIN {groups_members} gm ON gm.userid = u.id
165 {$enrolledjoin->joins}
166 WHERE gm.groupid $ingroupsql
167 AND {$enrolledjoin->wheres}
169 $users = $DB->get_records_sql($sql, $params);
170 $nousermessage = get_string('usersnone', 'lesson');
172 $nousermessage = get_string('groupsnone', 'lesson');
174 $info = new \core_availability\
info_module($cm);
175 $users = $info->filter_user_list($users);
178 // There are no users.
179 $canoverride = false;
180 $errormessage = $nousermessage;
185 $table = new html_table();
186 $table->headspan
= array(1, 2, 1);
187 $table->colclasses
= array('colname', 'colsetting', 'colvalue', 'colaction');
188 $table->head
= array(
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) {
208 // Check for inactive overrides.
210 if (!is_enrolled($context, $override->userid
)) {
211 // User not enrolled.
213 } else if (!\core_availability\info_module
::is_user_visible($cm, $override->userid
)) {
214 // User cannot access the module.
220 if (isset($override->available
)) {
221 $fields[] = get_string('lessonopens', 'lesson');
222 $values[] = $override->available
> 0 ?
223 userdate($override->available
) : get_string('noopen', 'lesson');
227 if (isset($override->deadline
)) {
228 $fields[] = get_string('lessoncloses', 'lesson');
229 $values[] = $override->deadline
> 0 ?
230 userdate($override->deadline
) : get_string('noclose', 'lesson');
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');
262 if (isset($override->password
)) {
263 $fields[] = get_string('usepassword', 'lesson');
264 $values[] = $override->password
!== '' ?
265 get_string('enabled', 'lesson') : get_string('none', 'lesson');
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> ';
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> ';
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> ';
287 $usergroupstr = '<a href="' . $groupurl->out(true, ['group' => $override->groupid
]) . '" >' .
288 format_string($override->name
, true, ['context' => $context]) . '</a>';
290 $usergroupstr = '<a href="' . $userurl->out(true,
291 array('id' => $override->userid
)) . '" >' . fullname($override) . '</a>';
296 $class = "dimmed_text";
297 $usergroupstr .= '*';
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;
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;
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.
338 echo $OUTPUT->notification(get_string('nooverridecreated', 'lesson'), 'info', false);
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');
353 echo $OUTPUT->footer();