MDL-37961 webservice: PARAM_BOOL with PARAM_DEFAULT accepts boolean default
[moodle.git] / mod / quiz / overrides.php
blobe6a5d840cd17c95a1ff1e19ec1006461e4f8b5b2
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(dirname(__FILE__) . '/../../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 if (! $cm = get_coursemodule_from_id('quiz', $cmid)) {
36 print_error('invalidcoursemodule');
38 if (! $quiz = $DB->get_record('quiz', array('id' => $cm->instance))) {
39 print_error('invalidcoursemodule');
41 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
43 // Get the course groups.
44 $groups = groups_get_all_groups($cm->course);
45 if ($groups === false) {
46 $groups = array();
49 // Default mode is "group", unless there are no groups.
50 if ($mode != "user" and $mode != "group") {
51 if (!empty($groups)) {
52 $mode = "group";
53 } else {
54 $mode = "user";
57 $groupmode = ($mode == "group");
59 $url = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id, 'mode'=>$mode));
61 $PAGE->set_url($url);
63 require_login($course, false, $cm);
65 $context = context_module::instance($cm->id);
67 // Check the user has the required capabilities to list overrides.
68 require_capability('mod/quiz:manageoverrides', $context);
70 // Display a list of overrides.
71 $PAGE->set_pagelayout('admin');
72 $PAGE->set_title(get_string('overrides', 'quiz'));
73 $PAGE->set_heading($course->fullname);
74 echo $OUTPUT->header();
76 // Delete orphaned group overrides.
77 $sql = 'SELECT o.id
78 FROM {quiz_overrides} o LEFT JOIN {groups} g
79 ON o.groupid = g.id
80 WHERE o.groupid IS NOT NULL
81 AND g.id IS NULL
82 AND o.quiz = ?';
83 $params = array($quiz->id);
84 $orphaned = $DB->get_records_sql($sql, $params);
85 if (!empty($orphaned)) {
86 $DB->delete_records_list('quiz_overrides', 'id', array_keys($orphaned));
89 // Fetch all overrides.
90 if ($groupmode) {
91 $colname = get_string('group');
92 $sql = 'SELECT o.*, g.name
93 FROM {quiz_overrides} o
94 JOIN {groups} g ON o.groupid = g.id
95 WHERE o.quiz = :quizid
96 ORDER BY g.name';
97 $params = array('quizid' => $quiz->id);
98 } else {
99 $colname = get_string('user');
100 list($sort, $params) = users_order_by_sql('u');
101 $sql = 'SELECT o.*, u.firstname, u.lastname
102 FROM {quiz_overrides} o
103 JOIN {user} u ON o.userid = u.id
104 WHERE o.quiz = :quizid
105 ORDER BY ' . $sort;
106 $params['quizid'] = $quiz->id;
109 $overrides = $DB->get_records_sql($sql, $params);
111 // Initialise table.
112 $table = new html_table();
113 $table->headspan = array(1, 2, 1);
114 $table->colclasses = array('colname', 'colsetting', 'colvalue', 'colaction');
115 $table->head = array(
116 $colname,
117 get_string('overrides', 'quiz'),
118 get_string('action'),
121 $userurl = new moodle_url('/user/view.php', array());
122 $groupurl = new moodle_url('/group/overview.php', array('id' => $cm->course));
124 $overridedeleteurl = new moodle_url('/mod/quiz/overridedelete.php');
125 $overrideediturl = new moodle_url('/mod/quiz/overrideedit.php');
127 $hasinactive = false; // Whether there are any inactive overrides.
129 foreach ($overrides as $override) {
131 $fields = array();
132 $values = array();
133 $active = true;
135 // Check for inactive overrides.
136 if (!$groupmode) {
137 if (!has_capability('mod/quiz:attempt', $context, $override->userid)) {
138 // User not allowed to take the quiz.
139 $active = false;
140 } else if (!empty($CFG->enablegroupmembersonly) && $cm->groupmembersonly &&
141 !groups_has_membership($cm, $override->userid)) {
142 // User does not belong to the current grouping.
143 $active = false;
147 // Format timeopen.
148 if (isset($override->timeopen)) {
149 $fields[] = get_string('quizopens', 'quiz');
150 $values[] = $override->timeopen > 0 ?
151 userdate($override->timeopen) : get_string('noopen', 'quiz');
154 // Format timeclose.
155 if (isset($override->timeclose)) {
156 $fields[] = get_string('quizcloses', 'quiz');
157 $values[] = $override->timeclose > 0 ?
158 userdate($override->timeclose) : get_string('noclose', 'quiz');
161 // Format timelimit.
162 if (isset($override->timelimit)) {
163 $fields[] = get_string('timelimit', 'quiz');
164 $values[] = $override->timelimit > 0 ?
165 format_time($override->timelimit) : get_string('none', 'quiz');
168 // Format number of attempts.
169 if (isset($override->attempts)) {
170 $fields[] = get_string('attempts', 'quiz');
171 $values[] = $override->attempts > 0 ?
172 $override->attempts : get_string('unlimited');
175 // Format password.
176 if (isset($override->password)) {
177 $fields[] = get_string('requirepassword', 'quiz');
178 $values[] = $override->password !== '' ?
179 get_string('enabled', 'quiz') : get_string('none', 'quiz');
182 // Icons.
183 $iconstr = '';
185 if ($active) {
186 // Edit.
187 $editurlstr = $overrideediturl->out(true, array('id' => $override->id));
188 $iconstr = '<a title="' . get_string('edit') . '" href="'. $editurlstr . '">' .
189 '<img src="' . $OUTPUT->pix_url('t/edit') . '" class="iconsmall" alt="' .
190 get_string('edit') . '" /></a> ';
191 // Duplicate.
192 $copyurlstr = $overrideediturl->out(true,
193 array('id' => $override->id, 'action' => 'duplicate'));
194 $iconstr .= '<a title="' . get_string('copy') . '" href="' . $copyurlstr . '">' .
195 '<img src="' . $OUTPUT->pix_url('t/copy') . '" class="iconsmall" alt="' .
196 get_string('copy') . '" /></a> ';
198 // Delete.
199 $deleteurlstr = $overridedeleteurl->out(true,
200 array('id' => $override->id, 'sesskey' => sesskey()));
201 $iconstr .= '<a title="' . get_string('delete') . '" href="' . $deleteurlstr . '">' .
202 '<img src="' . $OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="' .
203 get_string('delete') . '" /></a> ';
205 if ($groupmode) {
206 $usergroupstr = '<a href="' . $groupurl->out(true,
207 array('group' => $override->groupid)) . '" >' . $override->name . '</a>';
208 } else {
209 $usergroupstr = '<a href="' . $userurl->out(true,
210 array('id' => $override->userid)) . '" >' . fullname($override) . '</a>';
213 $class = '';
214 if (!$active) {
215 $class = "dimmed_text";
216 $usergroupstr .= '*';
217 $hasinactive = true;
220 $usergroupcell = new html_table_cell();
221 $usergroupcell->rowspan = count($fields);
222 $usergroupcell->text = $usergroupstr;
223 $actioncell = new html_table_cell();
224 $actioncell->rowspan = count($fields);
225 $actioncell->text = $iconstr;
227 for ($i = 0; $i < count($fields); ++$i) {
228 $row = new html_table_row();
229 $row->attributes['class'] = $class;
230 if ($i == 0) {
231 $row->cells[] = $usergroupcell;
233 $cell1 = new html_table_cell();
234 $cell1->text = $fields[$i];
235 $row->cells[] = $cell1;
236 $cell2 = new html_table_cell();
237 $cell2->text = $values[$i];
238 $row->cells[] = $cell2;
239 if ($i == 0) {
240 $row->cells[] = $actioncell;
242 $table->data[] = $row;
246 // Output the table and button.
247 echo html_writer::start_tag('div', array('id' => 'quizoverrides'));
248 if (count($table->data)) {
249 echo html_writer::table($table);
251 if ($hasinactive) {
252 echo $OUTPUT->notification(get_string('inactiveoverridehelp', 'quiz'), 'dimmed_text');
255 echo html_writer::start_tag('div', array('class' => 'buttons'));
256 $options = array();
257 if ($groupmode) {
258 if (empty($groups)) {
259 // There are no groups.
260 echo $OUTPUT->notification(get_string('groupsnone', 'quiz'), 'error');
261 $options['disabled'] = true;
263 echo $OUTPUT->single_button($overrideediturl->out(true,
264 array('action' => 'addgroup', 'cmid' => $cm->id)),
265 get_string('addnewgroupoverride', 'quiz'), 'post', $options);
266 } else {
267 $users = array();
268 // See if there are any students in the quiz.
269 if (!empty($CFG->enablegroupmembersonly) && $cm->groupmembersonly) {
270 // Restrict to grouping.
271 $limitgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid);
272 if (!empty($limitgroups)) {
273 $users = get_users_by_capability($context, 'mod/quiz:attempt', 'u.id',
274 '', '', 1, array_keys($limitgroups)); // Limit to one user for speed.
276 } else {
277 // Limit to one user for speed.
278 $users = get_users_by_capability($context, 'mod/quiz:attempt', 'u.id');
281 if (empty($users)) {
282 // There are no students.
283 echo $OUTPUT->notification(get_string('usersnone', 'quiz'), 'error');
284 $options['disabled'] = true;
286 echo $OUTPUT->single_button($overrideediturl->out(true,
287 array('action' => 'adduser', 'cmid' => $cm->id)),
288 get_string('addnewuseroverride', 'quiz'), 'post', $options);
290 echo html_writer::end_tag('div');
291 echo html_writer::end_tag('div');
293 // Finish the page.
294 echo $OUTPUT->footer();