3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * This page handles listing of quiz overrides
23 * @copyright 2010 Matt Petro
24 * @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) {
50 // Default mode is "group", unless there are no groups
51 if ($mode != "user" and $mode != "group") {
52 if (!empty($groups)) {
58 $groupmode = ($mode == "group");
60 $url = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id, 'mode'=>$mode));
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_title(get_string('overrides', 'quiz'));
74 $PAGE->set_heading($course->fullname);
75 echo $OUTPUT->header();
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 JOIN {groups} g
100 $colname = get_string('user');
101 $sql = 'SELECT o.*, u.firstname, u.lastname, u.id as uid
102 FROM {quiz_overrides} o JOIN {user} u
105 ORDER BY u.lastname, u.firstname';
108 $params = array($quiz->id);
109 $overrides = $DB->get_records_sql($sql, $params);
112 $table = new html_table();
113 $table->headspan = array(1,2,1);
114 $table->colclasses = array('colname','colsetting','colvalue','colaction');
115 $table->head = array(
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) {
135 // check for inactive overrides
137 if (!has_capability('mod/quiz:attempt', $context, $override->userid)) {
138 // user not allowed to take the quiz
140 } else if (!empty($CFG->enablegroupmembersonly) && $cm->groupmembersonly && !groups_has_membership($cm, $override->userid)) {
141 // user does not belong to the current grouping
147 if (isset($override->timeopen)) {
148 $fields[] = get_string('quizopens', 'quiz');
149 $values[] = ($override->timeopen > 0)? userdate($override->timeopen) : get_string('noopen', 'quiz');
153 if (isset($override->timeclose)) {
154 $fields[] = get_string('quizcloses', 'quiz');
155 $values[] = ($override->timeclose > 0)? userdate($override->timeclose) : get_string('noclose', 'quiz');
159 if (isset($override->timelimit)) {
160 $fields[] = get_string('timelimit', 'quiz');
161 $values[] = ($override->timelimit > 0)? format_time($override->timelimit) : get_string('none', 'quiz');
164 // Format number of attempts
165 if (isset($override->attempts)) {
166 $fields[] = get_string('attempts', 'quiz');
167 $values[] = ($override->attempts > 0)? $override->attempts : get_string('unlimited');
171 if (isset($override->password)) {
172 $fields[] = get_string('requirepassword', 'quiz');
173 $values[] = ($override->password !== '')? get_string('enabled', 'quiz') : get_string('none', 'quiz');
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="' . get_string('edit') . '" /></a> ';
186 $copyurlstr = $overrideediturl->out(true, array('id' => $override->id, 'action' => 'duplicate'));
187 $iconstr .= '<a title="' . get_string('copy') . '" href="' . $copyurlstr . '">' .
188 '<img src="' . $OUTPUT->pix_url('t/copy') . '" class="iconsmall" alt="' . get_string('copy') . '" /></a> ';
191 $deleteurlstr = $overridedeleteurl->out(true, array('id' => $override->id, 'sesskey' => sesskey()));
192 $iconstr .= '<a title="' . get_string('delete') . '" href="' . $deleteurlstr . '">' .
193 '<img src="' . $OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="' . get_string('delete') . '" /></a> ';
196 $usergroupstr = '<a href="' . $groupurl->out(true, array('group' => $override->groupid)) . '" >' . $override->name . '</a>';
199 $usergroupstr = '<a href="' . $userurl->out(true, array('id' => $override->userid)) . '" >' . fullname($override) . '</a>';
204 $class = "dimmed_text";
205 $usergroupstr .= '*';
209 $usergroupcell = new html_table_cell();
210 $usergroupcell->rowspan = count($fields);
211 $usergroupcell->text = $usergroupstr;
212 $actioncell = new html_table_cell();
213 $actioncell->rowspan = count($fields);
214 $actioncell->text = $iconstr;
216 for ($i = 0; $i < count($fields); ++$i) {
217 $row = new html_table_row();
218 $row->attributes['class'] = $class;
220 $row->cells[] = $usergroupcell;
222 $cell1 = new html_table_cell();
223 $cell1->text = $fields[$i];
224 $row->cells[] = $cell1;
225 $cell2 = new html_table_cell();
226 $cell2->text = $values[$i];
227 $row->cells[] = $cell2;
229 $row->cells[] = $actioncell;
231 $table->data[] = $row;
235 // Output the table and button
237 echo html_writer::start_tag('div', array('id' => 'quizoverrides'));
238 if (count($table->data)) {
239 echo html_writer::table($table);
242 echo $OUTPUT->notification(get_string('inactiveoverridehelp', 'quiz'), 'dimmed_text');
245 echo html_writer::start_tag('div', array('class' => 'buttons'));
248 if (empty($groups)) {
249 // there are no groups
250 echo $OUTPUT->notification(get_string('groupsnone', 'quiz'), 'error');
251 $options['disabled'] = true;
253 echo $OUTPUT->single_button($overrideediturl->out(true, array('action' => 'addgroup', 'cmid' => $cm->id)),
254 get_string('addnewgroupoverride', 'quiz'), 'post', $options);
257 // See if there are any students in the quiz
258 if (!empty($CFG->enablegroupmembersonly) && $cm->groupmembersonly) {
259 // restrict to grouping
260 $limitgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid);
261 if (!empty($limitgroups)) {
262 $users = get_users_by_capability($context, 'mod/quiz:attempt', 'u.id', '', '', 1, array_keys($limitgroups)); // Limit to one user for speed
267 $users = get_users_by_capability($context, 'mod/quiz:attempt', 'u.id'); // Limit to one user for speed
271 // there are no students
272 echo $OUTPUT->notification(get_string('usersnone', 'quiz'), 'error');
273 $options['disabled'] = true;
275 echo $OUTPUT->single_button($overrideediturl->out(true, array('action' => 'adduser', 'cmid' => $cm->id)),
276 get_string('addnewuseroverride', 'quiz'), 'post', $options);
278 echo html_writer::end_tag('div');
279 echo html_writer::end_tag('div');
282 echo $OUTPUT->footer();