MDL-51834 auth,profile: locks custom fields based on auth settings
[moodle.git] / mod / quiz / overrides.php
blob7814b108b0b7fe2f66aad1c5175252b303a6e337
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 list($course, $cm) = get_course_and_cm_from_cmid($cmid, 'quiz');
36 $quiz = $DB->get_record('quiz', array('id' => $cm->instance), '*', MUST_EXIST);
38 // Get the course groups.
39 $groups = groups_get_all_groups($cm->course);
40 if ($groups === false) {
41 $groups = array();
44 // Default mode is "group", unless there are no groups.
45 if ($mode != "user" and $mode != "group") {
46 if (!empty($groups)) {
47 $mode = "group";
48 } else {
49 $mode = "user";
52 $groupmode = ($mode == "group");
54 $url = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id, 'mode'=>$mode));
56 $PAGE->set_url($url);
58 require_login($course, false, $cm);
60 $context = context_module::instance($cm->id);
62 // Check the user has the required capabilities to list overrides.
63 require_capability('mod/quiz:manageoverrides', $context);
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 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);
81 if (!empty($orphaned)) {
82 $DB->delete_records_list('quiz_overrides', 'id', array_keys($orphaned));
85 // Fetch all overrides.
86 if ($groupmode) {
87 $colname = get_string('group');
88 $sql = 'SELECT o.*, g.name
89 FROM {quiz_overrides} o
90 JOIN {groups} g ON o.groupid = g.id
91 WHERE o.quiz = :quizid
92 ORDER BY g.name';
93 $params = array('quizid' => $quiz->id);
94 } else {
95 $colname = get_string('user');
96 list($sort, $params) = users_order_by_sql('u');
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;
102 $params['quizid'] = $quiz->id;
105 $overrides = $DB->get_records_sql($sql, $params);
107 // Initialise table.
108 $table = new html_table();
109 $table->headspan = array(1, 2, 1);
110 $table->colclasses = array('colname', 'colsetting', 'colvalue', 'colaction');
111 $table->head = array(
112 $colname,
113 get_string('overrides', 'quiz'),
114 get_string('action'),
117 $userurl = new moodle_url('/user/view.php', array());
118 $groupurl = new moodle_url('/group/overview.php', array('id' => $cm->course));
120 $overridedeleteurl = new moodle_url('/mod/quiz/overridedelete.php');
121 $overrideediturl = new moodle_url('/mod/quiz/overrideedit.php');
123 $hasinactive = false; // Whether there are any inactive overrides.
125 foreach ($overrides as $override) {
127 $fields = array();
128 $values = array();
129 $active = true;
131 // Check for inactive overrides.
132 if (!$groupmode) {
133 if (!has_capability('mod/quiz:attempt', $context, $override->userid)) {
134 // User not allowed to take the quiz.
135 $active = false;
136 } else if (!\core_availability\info_module::is_user_visible($cm, $override->userid)) {
137 // User cannot access the module.
138 $active = false;
142 // Format timeopen.
143 if (isset($override->timeopen)) {
144 $fields[] = get_string('quizopens', 'quiz');
145 $values[] = $override->timeopen > 0 ?
146 userdate($override->timeopen) : get_string('noopen', 'quiz');
149 // Format timeclose.
150 if (isset($override->timeclose)) {
151 $fields[] = get_string('quizcloses', 'quiz');
152 $values[] = $override->timeclose > 0 ?
153 userdate($override->timeclose) : get_string('noclose', 'quiz');
156 // Format timelimit.
157 if (isset($override->timelimit)) {
158 $fields[] = get_string('timelimit', 'quiz');
159 $values[] = $override->timelimit > 0 ?
160 format_time($override->timelimit) : get_string('none', 'quiz');
163 // Format number of attempts.
164 if (isset($override->attempts)) {
165 $fields[] = get_string('attempts', 'quiz');
166 $values[] = $override->attempts > 0 ?
167 $override->attempts : get_string('unlimited');
170 // Format password.
171 if (isset($override->password)) {
172 $fields[] = get_string('requirepassword', 'quiz');
173 $values[] = $override->password !== '' ?
174 get_string('enabled', 'quiz') : get_string('none', 'quiz');
177 // Icons.
178 $iconstr = '';
180 if ($active) {
181 // Edit.
182 $editurlstr = $overrideediturl->out(true, array('id' => $override->id));
183 $iconstr = '<a title="' . get_string('edit') . '" href="'. $editurlstr . '">' .
184 '<img src="' . $OUTPUT->pix_url('t/edit') . '" class="iconsmall" alt="' .
185 get_string('edit') . '" /></a> ';
186 // Duplicate.
187 $copyurlstr = $overrideediturl->out(true,
188 array('id' => $override->id, 'action' => 'duplicate'));
189 $iconstr .= '<a title="' . get_string('copy') . '" href="' . $copyurlstr . '">' .
190 '<img src="' . $OUTPUT->pix_url('t/copy') . '" class="iconsmall" alt="' .
191 get_string('copy') . '" /></a> ';
193 // Delete.
194 $deleteurlstr = $overridedeleteurl->out(true,
195 array('id' => $override->id, 'sesskey' => sesskey()));
196 $iconstr .= '<a title="' . get_string('delete') . '" href="' . $deleteurlstr . '">' .
197 '<img src="' . $OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="' .
198 get_string('delete') . '" /></a> ';
200 if ($groupmode) {
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>';
208 $class = '';
209 if (!$active) {
210 $class = "dimmed_text";
211 $usergroupstr .= '*';
212 $hasinactive = true;
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;
222 for ($i = 0; $i < count($fields); ++$i) {
223 $row = new html_table_row();
224 $row->attributes['class'] = $class;
225 if ($i == 0) {
226 $row->cells[] = $usergroupcell;
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;
237 $table->data[] = $row;
241 // Output the table and button.
242 echo html_writer::start_tag('div', array('id' => 'quizoverrides'));
243 if (count($table->data)) {
244 echo html_writer::table($table);
246 if ($hasinactive) {
247 echo $OUTPUT->notification(get_string('inactiveoverridehelp', 'quiz'), 'dimmed_text');
250 echo html_writer::start_tag('div', array('class' => 'buttons'));
251 $options = array();
252 if ($groupmode) {
253 if (empty($groups)) {
254 // There are no groups.
255 echo $OUTPUT->notification(get_string('groupsnone', 'quiz'), 'error');
256 $options['disabled'] = true;
258 echo $OUTPUT->single_button($overrideediturl->out(true,
259 array('action' => 'addgroup', 'cmid' => $cm->id)),
260 get_string('addnewgroupoverride', 'quiz'), 'post', $options);
261 } else {
262 $users = array();
263 // See if there are any students in the quiz.
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);
268 if (empty($users)) {
269 // There are no students.
270 echo $OUTPUT->notification(get_string('usersnone', 'quiz'), 'error');
271 $options['disabled'] = true;
273 echo $OUTPUT->single_button($overrideediturl->out(true,
274 array('action' => 'adduser', 'cmid' => $cm->id)),
275 get_string('addnewuseroverride', 'quiz'), 'get', $options);
277 echo html_writer::end_tag('div');
278 echo html_writer::end_tag('div');
280 // Finish the page.
281 echo $OUTPUT->footer();