MDL-29254 Fixed whitespace
[moodle.git] / mod / quiz / overrides.php
blobf3e82c7d330bddeed33e31cc560a80abb9e42bc8
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
21 * @subpackage quiz
22 * @copyright 2010 Matt Petro
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once(dirname(__FILE__) . '/../../config.php');
28 require_once($CFG->dirroot.'/mod/quiz/lib.php');
29 require_once($CFG->dirroot.'/mod/quiz/locallib.php');
30 require_once($CFG->dirroot.'/mod/quiz/override_form.php');
33 $cmid = required_param('cmid', PARAM_INT); // course module ID, or
34 $mode = optional_param('mode', '', PARAM_ALPHA); // one of 'user' or 'group', default is 'group'
36 if (! $cm = get_coursemodule_from_id('quiz', $cmid)) {
37 print_error('invalidcoursemodule');
39 if (! $quiz = $DB->get_record('quiz', array('id' => $cm->instance))) {
40 print_error('invalidcoursemodule');
42 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
44 // Get the course groups
45 $groups = groups_get_all_groups($cm->course);
46 if ($groups === false) {
47 $groups = array();
50 // Default mode is "group", unless there are no groups
51 if ($mode != "user" and $mode != "group") {
52 if (!empty($groups)) {
53 $mode = "group";
54 } else {
55 $mode = "user";
58 $groupmode = ($mode == "group");
60 $url = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id, 'mode'=>$mode));
62 $PAGE->set_url($url);
64 require_login($course, false, $cm);
66 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
68 // Check the user has the required capabilities to list overrides
69 require_capability('mod/quiz:manageoverrides', $context);
71 // Display a list of overrides
73 $PAGE->set_pagelayout('admin');
74 $PAGE->set_title(get_string('overrides', 'quiz'));
75 $PAGE->set_heading($course->fullname);
76 echo $OUTPUT->header();
78 // Delete orphaned group overrides
79 $sql = 'SELECT o.id
80 FROM {quiz_overrides} o LEFT JOIN {groups} g
81 ON o.groupid = g.id
82 WHERE o.groupid IS NOT NULL
83 AND g.id IS NULL
84 AND o.quiz = ?';
85 $params = array($quiz->id);
86 $orphaned = $DB->get_records_sql($sql, $params);
87 if (!empty($orphaned)) {
88 $DB->delete_records_list('quiz_overrides', 'id', array_keys($orphaned));
91 // Fetch all overrides
92 if ($groupmode) {
93 $colname = get_string('group');
94 $sql = 'SELECT o.*, g.name
95 FROM {quiz_overrides} o JOIN {groups} g
96 ON o.groupid = g.id
97 WHERE o.quiz = ?
98 ORDER BY g.name';
99 } else {
100 $colname = get_string('user');
101 $sql = 'SELECT o.*, u.firstname, u.lastname
102 FROM {quiz_overrides} o JOIN {user} u
103 ON o.userid = u.id
104 WHERE o.quiz = ?
105 ORDER BY u.lastname, u.firstname';
108 $params = array($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; // are there 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:
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
249 echo html_writer::start_tag('div', array('id' => 'quizoverrides'));
250 if (count($table->data)) {
251 echo html_writer::table($table);
253 if ($hasinactive) {
254 echo $OUTPUT->notification(get_string('inactiveoverridehelp', 'quiz'), 'dimmed_text');
257 echo html_writer::start_tag('div', array('class' => 'buttons'));
258 $options = array();
259 if ($groupmode) {
260 if (empty($groups)) {
261 // there are no groups
262 echo $OUTPUT->notification(get_string('groupsnone', 'quiz'), 'error');
263 $options['disabled'] = true;
265 echo $OUTPUT->single_button($overrideediturl->out(true,
266 array('action' => 'addgroup', 'cmid' => $cm->id)),
267 get_string('addnewgroupoverride', 'quiz'), 'post', $options);
268 } else {
269 $users = array();
270 // See if there are any students in the quiz
271 if (!empty($CFG->enablegroupmembersonly) && $cm->groupmembersonly) {
272 // restrict to grouping
273 $limitgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid);
274 if (!empty($limitgroups)) {
275 $users = get_users_by_capability($context, 'mod/quiz:attempt', 'u.id',
276 '', '', 1, array_keys($limitgroups)); // Limit to one user for speed
278 } else {
279 // Limit to one user for speed.
280 $users = get_users_by_capability($context, 'mod/quiz:attempt', 'u.id');
283 if (empty($users)) {
284 // there are no students
285 echo $OUTPUT->notification(get_string('usersnone', 'quiz'), 'error');
286 $options['disabled'] = true;
288 echo $OUTPUT->single_button($overrideediturl->out(true,
289 array('action' => 'adduser', 'cmid' => $cm->id)),
290 get_string('addnewuseroverride', 'quiz'), 'post', $options);
292 echo html_writer::end_tag('div');
293 echo html_writer::end_tag('div');
295 // Finish the page
296 echo $OUTPUT->footer();