MDL-46775: Fix call to unavailable function when loading an invalid JS file
[moodle.git] / mod / quiz / overrides.php
blobbb7e4e77560955889e0699d935413ede9c370406
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();
75 echo $OUTPUT->heading(format_string($quiz->name, true, array('context' => $context)));
77 // Delete orphaned group overrides.
78 $sql = 'SELECT o.id
79 FROM {quiz_overrides} o LEFT JOIN {groups} g
80 ON o.groupid = g.id
81 WHERE o.groupid IS NOT NULL
82 AND g.id IS NULL
83 AND o.quiz = ?';
84 $params = array($quiz->id);
85 $orphaned = $DB->get_records_sql($sql, $params);
86 if (!empty($orphaned)) {
87 $DB->delete_records_list('quiz_overrides', 'id', array_keys($orphaned));
90 // Fetch all overrides.
91 if ($groupmode) {
92 $colname = get_string('group');
93 $sql = 'SELECT o.*, g.name
94 FROM {quiz_overrides} o
95 JOIN {groups} g ON o.groupid = g.id
96 WHERE o.quiz = :quizid
97 ORDER BY g.name';
98 $params = array('quizid' => $quiz->id);
99 } else {
100 $colname = get_string('user');
101 list($sort, $params) = users_order_by_sql('u');
102 $sql = 'SELECT o.*, ' . get_all_user_name_fields(true, 'u') . '
103 FROM {quiz_overrides} o
104 JOIN {user} u ON o.userid = u.id
105 WHERE o.quiz = :quizid
106 ORDER BY ' . $sort;
107 $params['quizid'] = $quiz->id;
110 $overrides = $DB->get_records_sql($sql, $params);
112 // Initialise table.
113 $table = new html_table();
114 $table->headspan = array(1, 2, 1);
115 $table->colclasses = array('colname', 'colsetting', 'colvalue', 'colaction');
116 $table->head = array(
117 $colname,
118 get_string('overrides', 'quiz'),
119 get_string('action'),
122 $userurl = new moodle_url('/user/view.php', array());
123 $groupurl = new moodle_url('/group/overview.php', array('id' => $cm->course));
125 $overridedeleteurl = new moodle_url('/mod/quiz/overridedelete.php');
126 $overrideediturl = new moodle_url('/mod/quiz/overrideedit.php');
128 $hasinactive = false; // Whether there are any inactive overrides.
130 foreach ($overrides as $override) {
132 $fields = array();
133 $values = array();
134 $active = true;
136 // Check for inactive overrides.
137 if (!$groupmode) {
138 if (!has_capability('mod/quiz:attempt', $context, $override->userid)) {
139 // User not allowed to take the quiz.
140 $active = false;
141 } else if (!empty($CFG->enablegroupmembersonly) && $cm->groupmembersonly &&
142 !groups_has_membership($cm, $override->userid)) {
143 // User does not belong to the current grouping.
144 $active = false;
148 // Format timeopen.
149 if (isset($override->timeopen)) {
150 $fields[] = get_string('quizopens', 'quiz');
151 $values[] = $override->timeopen > 0 ?
152 userdate($override->timeopen) : get_string('noopen', 'quiz');
155 // Format timeclose.
156 if (isset($override->timeclose)) {
157 $fields[] = get_string('quizcloses', 'quiz');
158 $values[] = $override->timeclose > 0 ?
159 userdate($override->timeclose) : get_string('noclose', 'quiz');
162 // Format timelimit.
163 if (isset($override->timelimit)) {
164 $fields[] = get_string('timelimit', 'quiz');
165 $values[] = $override->timelimit > 0 ?
166 format_time($override->timelimit) : get_string('none', 'quiz');
169 // Format number of attempts.
170 if (isset($override->attempts)) {
171 $fields[] = get_string('attempts', 'quiz');
172 $values[] = $override->attempts > 0 ?
173 $override->attempts : get_string('unlimited');
176 // Format password.
177 if (isset($override->password)) {
178 $fields[] = get_string('requirepassword', 'quiz');
179 $values[] = $override->password !== '' ?
180 get_string('enabled', 'quiz') : get_string('none', 'quiz');
183 // Icons.
184 $iconstr = '';
186 if ($active) {
187 // Edit.
188 $editurlstr = $overrideediturl->out(true, array('id' => $override->id));
189 $iconstr = '<a title="' . get_string('edit') . '" href="'. $editurlstr . '">' .
190 '<img src="' . $OUTPUT->pix_url('t/edit') . '" class="iconsmall" alt="' .
191 get_string('edit') . '" /></a> ';
192 // Duplicate.
193 $copyurlstr = $overrideediturl->out(true,
194 array('id' => $override->id, 'action' => 'duplicate'));
195 $iconstr .= '<a title="' . get_string('copy') . '" href="' . $copyurlstr . '">' .
196 '<img src="' . $OUTPUT->pix_url('t/copy') . '" class="iconsmall" alt="' .
197 get_string('copy') . '" /></a> ';
199 // Delete.
200 $deleteurlstr = $overridedeleteurl->out(true,
201 array('id' => $override->id, 'sesskey' => sesskey()));
202 $iconstr .= '<a title="' . get_string('delete') . '" href="' . $deleteurlstr . '">' .
203 '<img src="' . $OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="' .
204 get_string('delete') . '" /></a> ';
206 if ($groupmode) {
207 $usergroupstr = '<a href="' . $groupurl->out(true,
208 array('group' => $override->groupid)) . '" >' . $override->name . '</a>';
209 } else {
210 $usergroupstr = '<a href="' . $userurl->out(true,
211 array('id' => $override->userid)) . '" >' . fullname($override) . '</a>';
214 $class = '';
215 if (!$active) {
216 $class = "dimmed_text";
217 $usergroupstr .= '*';
218 $hasinactive = true;
221 $usergroupcell = new html_table_cell();
222 $usergroupcell->rowspan = count($fields);
223 $usergroupcell->text = $usergroupstr;
224 $actioncell = new html_table_cell();
225 $actioncell->rowspan = count($fields);
226 $actioncell->text = $iconstr;
228 for ($i = 0; $i < count($fields); ++$i) {
229 $row = new html_table_row();
230 $row->attributes['class'] = $class;
231 if ($i == 0) {
232 $row->cells[] = $usergroupcell;
234 $cell1 = new html_table_cell();
235 $cell1->text = $fields[$i];
236 $row->cells[] = $cell1;
237 $cell2 = new html_table_cell();
238 $cell2->text = $values[$i];
239 $row->cells[] = $cell2;
240 if ($i == 0) {
241 $row->cells[] = $actioncell;
243 $table->data[] = $row;
247 // Output the table and button.
248 echo html_writer::start_tag('div', array('id' => 'quizoverrides'));
249 if (count($table->data)) {
250 echo html_writer::table($table);
252 if ($hasinactive) {
253 echo $OUTPUT->notification(get_string('inactiveoverridehelp', 'quiz'), 'dimmed_text');
256 echo html_writer::start_tag('div', array('class' => 'buttons'));
257 $options = array();
258 if ($groupmode) {
259 if (empty($groups)) {
260 // There are no groups.
261 echo $OUTPUT->notification(get_string('groupsnone', 'quiz'), 'error');
262 $options['disabled'] = true;
264 echo $OUTPUT->single_button($overrideediturl->out(true,
265 array('action' => 'addgroup', 'cmid' => $cm->id)),
266 get_string('addnewgroupoverride', 'quiz'), 'post', $options);
267 } else {
268 $users = array();
269 // See if there are any students in the quiz.
270 if (!empty($CFG->enablegroupmembersonly) && $cm->groupmembersonly) {
271 // Restrict to grouping.
272 $limitgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid);
273 if (!empty($limitgroups)) {
274 $users = get_users_by_capability($context, 'mod/quiz:attempt', 'u.id',
275 '', '', 1, array_keys($limitgroups)); // Limit to one user for speed.
277 } else {
278 // Limit to one user for speed.
279 $users = get_users_by_capability($context, 'mod/quiz:attempt', 'u.id');
282 if (empty($users)) {
283 // There are no students.
284 echo $OUTPUT->notification(get_string('usersnone', 'quiz'), 'error');
285 $options['disabled'] = true;
287 echo $OUTPUT->single_button($overrideediturl->out(true,
288 array('action' => 'adduser', 'cmid' => $cm->id)),
289 get_string('addnewuseroverride', 'quiz'), 'post', $options);
291 echo html_writer::end_tag('div');
292 echo html_writer::end_tag('div');
294 // Finish the page.
295 echo $OUTPUT->footer();