MDL-69973 xmldb: Fix problem with MariaDB >= 10.2.7 metadata defaults
[moodle.git] / mod / quiz / overrides.php
bloba42c3d6f96fbe9a6e0939ab70f39c706e8e8608f
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 quiz overrides
20 * @package mod_quiz
21 * @copyright 2010 Matt Petro
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/quiz/lib.php');
28 require_once($CFG->dirroot.'/mod/quiz/locallib.php');
29 require_once($CFG->dirroot.'/mod/quiz/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, 'quiz');
36 $quiz = $DB->get_record('quiz', 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/quiz:manageoverrides', $context);
45 $quizgroupmode = groups_get_activity_groupmode($cm);
46 $accessallgroups = ($quizgroupmode == 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/quiz/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->set_title(get_string('overrides', 'quiz'));
68 $PAGE->set_heading($course->fullname);
69 echo $OUTPUT->header();
70 echo $OUTPUT->heading(format_string($quiz->name, true, array('context' => $context)));
72 // Delete orphaned group overrides.
73 $sql = 'SELECT o.id
74 FROM {quiz_overrides} o
75 LEFT JOIN {groups} g ON o.groupid = g.id
76 WHERE o.groupid IS NOT NULL
77 AND g.id IS NULL
78 AND o.quiz = ?';
79 $params = array($quiz->id);
80 $orphaned = $DB->get_records_sql($sql, $params);
81 if (!empty($orphaned)) {
82 $DB->delete_records_list('quiz_overrides', 'id', array_keys($orphaned));
85 $overrides = [];
87 // Fetch all overrides.
88 if ($groupmode) {
89 $colname = get_string('group');
90 // To filter the result by the list of groups that the current user has access to.
91 if ($groups) {
92 $params = ['quizid' => $quiz->id];
93 list($insql, $inparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
94 $params += $inparams;
96 $sql = "SELECT o.*, g.name
97 FROM {quiz_overrides} o
98 JOIN {groups} g ON o.groupid = g.id
99 WHERE o.quiz = :quizid AND g.id $insql
100 ORDER BY g.name";
102 $overrides = $DB->get_records_sql($sql, $params);
104 } else {
105 $colname = get_string('user');
106 list($sort, $params) = users_order_by_sql('u');
107 $params['quizid'] = $quiz->id;
109 if ($accessallgroups) {
110 $sql = 'SELECT o.*, ' . get_all_user_name_fields(true, 'u') . '
111 FROM {quiz_overrides} o
112 JOIN {user} u ON o.userid = u.id
113 WHERE o.quiz = :quizid
114 ORDER BY ' . $sort;
116 $overrides = $DB->get_records_sql($sql, $params);
117 } else if ($groups) {
118 list($insql, $inparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
119 $params += $inparams;
121 $sql = 'SELECT o.*, ' . get_all_user_name_fields(true, 'u') . '
122 FROM {quiz_overrides} o
123 JOIN {user} u ON o.userid = u.id
124 JOIN {groups_members} gm ON u.id = gm.userid
125 WHERE o.quiz = :quizid AND gm.groupid ' . $insql . '
126 ORDER BY ' . $sort;
128 $overrides = $DB->get_records_sql($sql, $params);
132 // Initialise table.
133 $table = new html_table();
134 $table->headspan = array(1, 2, 1);
135 $table->colclasses = array('colname', 'colsetting', 'colvalue', 'colaction');
136 $table->head = array(
137 $colname,
138 get_string('overrides', 'quiz'),
139 get_string('action'),
142 $userurl = new moodle_url('/user/view.php', array());
143 $groupurl = new moodle_url('/group/overview.php', array('id' => $cm->course));
145 $overridedeleteurl = new moodle_url('/mod/quiz/overridedelete.php');
146 $overrideediturl = new moodle_url('/mod/quiz/overrideedit.php');
148 $hasinactive = false; // Whether there are any inactive overrides.
150 foreach ($overrides as $override) {
152 $fields = array();
153 $values = array();
154 $active = true;
156 // Check for inactive overrides.
157 if (!$groupmode) {
158 if (!has_capability('mod/quiz:attempt', $context, $override->userid)) {
159 // User not allowed to take the quiz.
160 $active = false;
161 } else if (!\core_availability\info_module::is_user_visible($cm, $override->userid)) {
162 // User cannot access the module.
163 $active = false;
167 // Format timeopen.
168 if (isset($override->timeopen)) {
169 $fields[] = get_string('quizopens', 'quiz');
170 $values[] = $override->timeopen > 0 ?
171 userdate($override->timeopen) : get_string('noopen', 'quiz');
174 // Format timeclose.
175 if (isset($override->timeclose)) {
176 $fields[] = get_string('quizcloses', 'quiz');
177 $values[] = $override->timeclose > 0 ?
178 userdate($override->timeclose) : get_string('noclose', 'quiz');
181 // Format timelimit.
182 if (isset($override->timelimit)) {
183 $fields[] = get_string('timelimit', 'quiz');
184 $values[] = $override->timelimit > 0 ?
185 format_time($override->timelimit) : get_string('none', 'quiz');
188 // Format number of attempts.
189 if (isset($override->attempts)) {
190 $fields[] = get_string('attempts', 'quiz');
191 $values[] = $override->attempts > 0 ?
192 $override->attempts : get_string('unlimited');
195 // Format password.
196 if (isset($override->password)) {
197 $fields[] = get_string('requirepassword', 'quiz');
198 $values[] = $override->password !== '' ?
199 get_string('enabled', 'quiz') : get_string('none', 'quiz');
202 // Icons.
203 $iconstr = '';
205 // Edit.
206 $editurlstr = $overrideediturl->out(true, array('id' => $override->id));
207 $iconstr = '<a title="' . get_string('edit') . '" href="'. $editurlstr . '">' .
208 $OUTPUT->pix_icon('t/edit', get_string('edit')) . '</a> ';
209 // Duplicate.
210 $copyurlstr = $overrideediturl->out(true,
211 array('id' => $override->id, 'action' => 'duplicate'));
212 $iconstr .= '<a title="' . get_string('copy') . '" href="' . $copyurlstr . '">' .
213 $OUTPUT->pix_icon('t/copy', get_string('copy')) . '</a> ';
214 // Delete.
215 $deleteurlstr = $overridedeleteurl->out(true,
216 array('id' => $override->id, 'sesskey' => sesskey()));
217 $iconstr .= '<a title="' . get_string('delete') . '" href="' . $deleteurlstr . '">' .
218 $OUTPUT->pix_icon('t/delete', get_string('delete')) . '</a> ';
220 if ($groupmode) {
221 $usergroupstr = '<a href="' . $groupurl->out(true,
222 array('group' => $override->groupid)) . '" >' . $override->name . '</a>';
223 } else {
224 $usergroupstr = '<a href="' . $userurl->out(true,
225 array('id' => $override->userid)) . '" >' . fullname($override) . '</a>';
228 $class = '';
229 if (!$active) {
230 $class = "dimmed_text";
231 $usergroupstr .= '*';
232 $hasinactive = true;
235 $usergroupcell = new html_table_cell();
236 $usergroupcell->rowspan = count($fields);
237 $usergroupcell->text = $usergroupstr;
238 $actioncell = new html_table_cell();
239 $actioncell->rowspan = count($fields);
240 $actioncell->text = $iconstr;
242 for ($i = 0; $i < count($fields); ++$i) {
243 $row = new html_table_row();
244 $row->attributes['class'] = $class;
245 if ($i == 0) {
246 $row->cells[] = $usergroupcell;
248 $cell1 = new html_table_cell();
249 $cell1->text = $fields[$i];
250 $row->cells[] = $cell1;
251 $cell2 = new html_table_cell();
252 $cell2->text = $values[$i];
253 $row->cells[] = $cell2;
254 if ($i == 0) {
255 $row->cells[] = $actioncell;
257 $table->data[] = $row;
261 // Output the table and button.
262 echo html_writer::start_tag('div', array('id' => 'quizoverrides'));
263 if (count($table->data)) {
264 echo html_writer::table($table);
266 if ($hasinactive) {
267 echo $OUTPUT->notification(get_string('inactiveoverridehelp', 'quiz'), 'dimmed_text');
270 echo html_writer::start_tag('div', array('class' => 'buttons'));
271 $options = array();
272 if ($groupmode) {
273 if (empty($groups)) {
274 // There are no groups.
275 echo $OUTPUT->notification(get_string('groupsnone', 'quiz'), 'error');
276 $options['disabled'] = true;
278 echo $OUTPUT->single_button($overrideediturl->out(true,
279 array('action' => 'addgroup', 'cmid' => $cm->id)),
280 get_string('addnewgroupoverride', 'quiz'), 'post', $options);
281 } else {
282 $users = array();
283 // See if there are any students in the quiz.
284 if ($accessallgroups) {
285 $users = get_users_by_capability($context, 'mod/quiz:attempt', 'u.id');
286 $nousermessage = get_string('usersnone', 'quiz');
287 } else if ($groups) {
288 $users = get_users_by_capability($context, 'mod/quiz:attempt', 'u.id', '', '', '', array_keys($groups));
289 $nousermessage = get_string('usersnone', 'quiz');
290 } else {
291 $nousermessage = get_string('groupsnone', 'quiz');
293 $info = new \core_availability\info_module($cm);
294 $users = $info->filter_user_list($users);
296 if (empty($users)) {
297 // There are no students.
298 echo $OUTPUT->notification($nousermessage, 'error');
299 $options['disabled'] = true;
301 echo $OUTPUT->single_button($overrideediturl->out(true,
302 array('action' => 'adduser', 'cmid' => $cm->id)),
303 get_string('addnewuseroverride', 'quiz'), 'get', $options);
305 echo html_writer::end_tag('div');
306 echo html_writer::end_tag('div');
308 // Finish the page.
309 echo $OUTPUT->footer();