MDL-40006 searchableselector does not work in IE or Safari.
[moodle.git] / mod / quiz / overrides.php
CommitLineData
990650f9 1<?php
990650f9
TH
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.
13//
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/>.
16
990650f9
TH
17/**
18 * This page handles listing of quiz overrides
19 *
9e83f3d1 20 * @package mod_quiz
ba643847
TH
21 * @copyright 2010 Matt Petro
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
990650f9
TH
23 */
24
ba643847 25
990650f9
TH
26require_once(dirname(__FILE__) . '/../../config.php');
27require_once($CFG->dirroot.'/mod/quiz/lib.php');
28require_once($CFG->dirroot.'/mod/quiz/locallib.php');
29require_once($CFG->dirroot.'/mod/quiz/override_form.php');
30
31
9e83f3d1
TH
32$cmid = required_param('cmid', PARAM_INT);
33$mode = optional_param('mode', '', PARAM_ALPHA); // One of 'user' or 'group', default is 'group'.
990650f9 34
c234c340 35list($course, $cm) = get_course_and_cm_from_cmid($cmid, 'quiz');
36$quiz = $DB->get_record('quiz', array('id' => $cm->instance), '*', MUST_EXIST);
990650f9 37
9e83f3d1 38// Get the course groups.
7bc488dc
TH
39$groups = groups_get_all_groups($cm->course);
40if ($groups === false) {
41 $groups = array();
42}
43
9e83f3d1 44// Default mode is "group", unless there are no groups.
7bc488dc
TH
45if ($mode != "user" and $mode != "group") {
46 if (!empty($groups)) {
47 $mode = "group";
48 } else {
49 $mode = "user";
50 }
51}
52$groupmode = ($mode == "group");
53
990650f9
TH
54$url = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id, 'mode'=>$mode));
55
56$PAGE->set_url($url);
57
56ed242b 58require_login($course, false, $cm);
990650f9 59
c492a78e 60$context = context_module::instance($cm->id);
990650f9 61
9e83f3d1 62// Check the user has the required capabilities to list overrides.
990650f9
TH
63require_capability('mod/quiz:manageoverrides', $context);
64
9e83f3d1 65// Display a list of overrides.
e07cccfc 66$PAGE->set_pagelayout('admin');
990650f9 67$PAGE->set_title(get_string('overrides', 'quiz'));
56ed242b 68$PAGE->set_heading($course->fullname);
990650f9 69echo $OUTPUT->header();
c544ee92 70echo $OUTPUT->heading(format_string($quiz->name, true, array('context' => $context)));
990650f9 71
9e83f3d1 72// Delete orphaned group overrides.
7bc488dc
TH
73$sql = 'SELECT o.id
74 FROM {quiz_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.quiz = ?';
79$params = array($quiz->id);
80$orphaned = $DB->get_records_sql($sql, $params);
81if (!empty($orphaned)) {
82 $DB->delete_records_list('quiz_overrides', 'id', array_keys($orphaned));
83}
990650f9 84
9e83f3d1 85// Fetch all overrides.
990650f9
TH
86if ($groupmode) {
87 $colname = get_string('group');
88 $sql = 'SELECT o.*, g.name
9695ff81
TH
89 FROM {quiz_overrides} o
90 JOIN {groups} g ON o.groupid = g.id
91 WHERE o.quiz = :quizid
990650f9 92 ORDER BY g.name';
9695ff81 93 $params = array('quizid' => $quiz->id);
25a03faa 94} else {
990650f9 95 $colname = get_string('user');
9695ff81 96 list($sort, $params) = users_order_by_sql('u');
12bb281e
MN
97 $sql = 'SELECT o.*, ' . get_all_user_name_fields(true, 'u') . '
98 FROM {quiz_overrides} o
99 JOIN {user} u ON o.userid = u.id
100 WHERE o.quiz = :quizid
101 ORDER BY ' . $sort;
9695ff81 102 $params['quizid'] = $quiz->id;
990650f9
TH
103}
104
990650f9
TH
105$overrides = $DB->get_records_sql($sql, $params);
106
9e83f3d1 107// Initialise table.
990650f9 108$table = new html_table();
25a03faa
TH
109$table->headspan = array(1, 2, 1);
110$table->colclasses = array('colname', 'colsetting', 'colvalue', 'colaction');
990650f9
TH
111$table->head = array(
112 $colname,
113 get_string('overrides', 'quiz'),
114 get_string('action'),
115);
116
117$userurl = new moodle_url('/user/view.php', array());
118$groupurl = new moodle_url('/group/overview.php', array('id' => $cm->course));
119
120$overridedeleteurl = new moodle_url('/mod/quiz/overridedelete.php');
121$overrideediturl = new moodle_url('/mod/quiz/overrideedit.php');
122
9e83f3d1 123$hasinactive = false; // Whether there are any inactive overrides.
7bc488dc 124
990650f9
TH
125foreach ($overrides as $override) {
126
127 $fields = array();
128 $values = array();
7bc488dc
TH
129 $active = true;
130
9e83f3d1 131 // Check for inactive overrides.
7bc488dc
TH
132 if (!$groupmode) {
133 if (!has_capability('mod/quiz:attempt', $context, $override->userid)) {
9e83f3d1 134 // User not allowed to take the quiz.
7bc488dc 135 $active = false;
c234c340 136 } else if (!\core_availability\info_module::is_user_visible($cm, $override->userid)) {
137 // User cannot access the module.
7bc488dc
TH
138 $active = false;
139 }
990650f9
TH
140 }
141
9e83f3d1 142 // Format timeopen.
990650f9
TH
143 if (isset($override->timeopen)) {
144 $fields[] = get_string('quizopens', 'quiz');
25a03faa
TH
145 $values[] = $override->timeopen > 0 ?
146 userdate($override->timeopen) : get_string('noopen', 'quiz');
990650f9
TH
147 }
148
9e83f3d1 149 // Format timeclose.
990650f9
TH
150 if (isset($override->timeclose)) {
151 $fields[] = get_string('quizcloses', 'quiz');
25a03faa
TH
152 $values[] = $override->timeclose > 0 ?
153 userdate($override->timeclose) : get_string('noclose', 'quiz');
990650f9
TH
154 }
155
9e83f3d1 156 // Format timelimit.
990650f9
TH
157 if (isset($override->timelimit)) {
158 $fields[] = get_string('timelimit', 'quiz');
25a03faa
TH
159 $values[] = $override->timelimit > 0 ?
160 format_time($override->timelimit) : get_string('none', 'quiz');
990650f9
TH
161 }
162
9e83f3d1 163 // Format number of attempts.
990650f9
TH
164 if (isset($override->attempts)) {
165 $fields[] = get_string('attempts', 'quiz');
25a03faa
TH
166 $values[] = $override->attempts > 0 ?
167 $override->attempts : get_string('unlimited');
990650f9
TH
168 }
169
9e83f3d1 170 // Format password.
990650f9
TH
171 if (isset($override->password)) {
172 $fields[] = get_string('requirepassword', 'quiz');
25a03faa
TH
173 $values[] = $override->password !== '' ?
174 get_string('enabled', 'quiz') : get_string('none', 'quiz');
990650f9
TH
175 }
176
9e83f3d1 177 // Icons.
7bc488dc
TH
178 $iconstr = '';
179
180 if ($active) {
9e83f3d1 181 // Edit.
7bc488dc
TH
182 $editurlstr = $overrideediturl->out(true, array('id' => $override->id));
183 $iconstr = '<a title="' . get_string('edit') . '" href="'. $editurlstr . '">' .
25a03faa
TH
184 '<img src="' . $OUTPUT->pix_url('t/edit') . '" class="iconsmall" alt="' .
185 get_string('edit') . '" /></a> ';
9e83f3d1 186 // Duplicate.
25a03faa
TH
187 $copyurlstr = $overrideediturl->out(true,
188 array('id' => $override->id, 'action' => 'duplicate'));
7bc488dc 189 $iconstr .= '<a title="' . get_string('copy') . '" href="' . $copyurlstr . '">' .
25a03faa
TH
190 '<img src="' . $OUTPUT->pix_url('t/copy') . '" class="iconsmall" alt="' .
191 get_string('copy') . '" /></a> ';
7bc488dc 192 }
9e83f3d1 193 // Delete.
25a03faa
TH
194 $deleteurlstr = $overridedeleteurl->out(true,
195 array('id' => $override->id, 'sesskey' => sesskey()));
990650f9 196 $iconstr .= '<a title="' . get_string('delete') . '" href="' . $deleteurlstr . '">' .
25a03faa
TH
197 '<img src="' . $OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="' .
198 get_string('delete') . '" /></a> ';
990650f9
TH
199
200 if ($groupmode) {
25a03faa
TH
201 $usergroupstr = '<a href="' . $groupurl->out(true,
202 array('group' => $override->groupid)) . '" >' . $override->name . '</a>';
203 } else {
204 $usergroupstr = '<a href="' . $userurl->out(true,
205 array('id' => $override->userid)) . '" >' . fullname($override) . '</a>';
990650f9
TH
206 }
207
7bc488dc
TH
208 $class = '';
209 if (!$active) {
210 $class = "dimmed_text";
211 $usergroupstr .= '*';
212 $hasinactive = true;
990650f9
TH
213 }
214
215 $usergroupcell = new html_table_cell();
216 $usergroupcell->rowspan = count($fields);
217 $usergroupcell->text = $usergroupstr;
218 $actioncell = new html_table_cell();
219 $actioncell->rowspan = count($fields);
220 $actioncell->text = $iconstr;
221
222 for ($i = 0; $i < count($fields); ++$i) {
223 $row = new html_table_row();
7bc488dc 224 $row->attributes['class'] = $class;
990650f9
TH
225 if ($i == 0) {
226 $row->cells[] = $usergroupcell;
227 }
228 $cell1 = new html_table_cell();
229 $cell1->text = $fields[$i];
230 $row->cells[] = $cell1;
231 $cell2 = new html_table_cell();
232 $cell2->text = $values[$i];
233 $row->cells[] = $cell2;
234 if ($i == 0) {
235 $row->cells[] = $actioncell;
236 }
237 $table->data[] = $row;
238 }
239}
240
9e83f3d1 241// Output the table and button.
990650f9
TH
242echo html_writer::start_tag('div', array('id' => 'quizoverrides'));
243if (count($table->data)) {
16be8974 244 echo html_writer::table($table);
990650f9 245}
7bc488dc
TH
246if ($hasinactive) {
247 echo $OUTPUT->notification(get_string('inactiveoverridehelp', 'quiz'), 'dimmed_text');
248}
990650f9
TH
249
250echo html_writer::start_tag('div', array('class' => 'buttons'));
7bc488dc 251$options = array();
990650f9 252if ($groupmode) {
7bc488dc 253 if (empty($groups)) {
9e83f3d1 254 // There are no groups.
7bc488dc
TH
255 echo $OUTPUT->notification(get_string('groupsnone', 'quiz'), 'error');
256 $options['disabled'] = true;
257 }
25a03faa
TH
258 echo $OUTPUT->single_button($overrideediturl->out(true,
259 array('action' => 'addgroup', 'cmid' => $cm->id)),
260 get_string('addnewgroupoverride', 'quiz'), 'post', $options);
990650f9 261} else {
7bc488dc 262 $users = array();
9e83f3d1 263 // See if there are any students in the quiz.
c234c340 264 $users = get_users_by_capability($context, 'mod/quiz:attempt', 'u.id');
265 $info = new \core_availability\info_module($cm);
266 $users = $info->filter_user_list($users);
7bc488dc
TH
267
268 if (empty($users)) {
9e83f3d1 269 // There are no students.
7bc488dc
TH
270 echo $OUTPUT->notification(get_string('usersnone', 'quiz'), 'error');
271 $options['disabled'] = true;
272 }
25a03faa
TH
273 echo $OUTPUT->single_button($overrideediturl->out(true,
274 array('action' => 'adduser', 'cmid' => $cm->id)),
0d4075d5 275 get_string('addnewuseroverride', 'quiz'), 'get', $options);
990650f9
TH
276}
277echo html_writer::end_tag('div');
278echo html_writer::end_tag('div');
279
9e83f3d1 280// Finish the page.
990650f9 281echo $OUTPUT->footer();