2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * This page handles listing of quiz overrides
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) {
49 // Default mode is "group", unless there are no groups.
50 if ($mode != "user" and $mode != "group") {
51 if (!empty($groups)) {
57 $groupmode = ($mode == "group");
59 $url = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id
, 'mode'=>$mode));
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.
79 FROM {quiz_overrides} o LEFT JOIN {groups} g
81 WHERE o.groupid IS NOT NULL
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.
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
98 $params = array('quizid' => $quiz->id
);
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
107 $params['quizid'] = $quiz->id
;
110 $overrides = $DB->get_records_sql($sql, $params);
113 $table = new html_table();
114 $table->headspan
= array(1, 2, 1);
115 $table->colclasses
= array('colname', 'colsetting', 'colvalue', 'colaction');
116 $table->head
= array(
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) {
136 // Check for inactive overrides.
138 if (!has_capability('mod/quiz:attempt', $context, $override->userid
)) {
139 // User not allowed to take the quiz.
141 } else if (!empty($CFG->enablegroupmembersonly
) && $cm->groupmembersonly
&&
142 !groups_has_membership($cm, $override->userid
)) {
143 // User does not belong to the current grouping.
149 if (isset($override->timeopen
)) {
150 $fields[] = get_string('quizopens', 'quiz');
151 $values[] = $override->timeopen
> 0 ?
152 userdate($override->timeopen
) : get_string('noopen', 'quiz');
156 if (isset($override->timeclose
)) {
157 $fields[] = get_string('quizcloses', 'quiz');
158 $values[] = $override->timeclose
> 0 ?
159 userdate($override->timeclose
) : get_string('noclose', 'quiz');
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');
177 if (isset($override->password
)) {
178 $fields[] = get_string('requirepassword', 'quiz');
179 $values[] = $override->password
!== '' ?
180 get_string('enabled', 'quiz') : get_string('none', 'quiz');
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> ';
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> ';
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> ';
207 $usergroupstr = '<a href="' . $groupurl->out(true,
208 array('group' => $override->groupid
)) . '" >' . $override->name
. '</a>';
210 $usergroupstr = '<a href="' . $userurl->out(true,
211 array('id' => $override->userid
)) . '" >' . fullname($override) . '</a>';
216 $class = "dimmed_text";
217 $usergroupstr .= '*';
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;
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;
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);
253 echo $OUTPUT->notification(get_string('inactiveoverridehelp', 'quiz'), 'dimmed_text');
256 echo html_writer
::start_tag('div', array('class' => 'buttons'));
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);
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.
278 // Limit to one user for speed.
279 $users = get_users_by_capability($context, 'mod/quiz:attempt', 'u.id');
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');
295 echo $OUTPUT->footer();