|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00027 require_once(dirname(__FILE__) . '/../../config.php'); 00028 require_once($CFG->dirroot.'/mod/quiz/lib.php'); 00029 require_once($CFG->dirroot.'/mod/quiz/locallib.php'); 00030 require_once($CFG->dirroot.'/mod/quiz/override_form.php'); 00031 00032 00033 $cmid = required_param('cmid', PARAM_INT); // course module ID, or 00034 $mode = optional_param('mode', '', PARAM_ALPHA); // one of 'user' or 'group', default is 'group' 00035 00036 if (! $cm = get_coursemodule_from_id('quiz', $cmid)) { 00037 print_error('invalidcoursemodule'); 00038 } 00039 if (! $quiz = $DB->get_record('quiz', array('id' => $cm->instance))) { 00040 print_error('invalidcoursemodule'); 00041 } 00042 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST); 00043 00044 // Get the course groups 00045 $groups = groups_get_all_groups($cm->course); 00046 if ($groups === false) { 00047 $groups = array(); 00048 } 00049 00050 // Default mode is "group", unless there are no groups 00051 if ($mode != "user" and $mode != "group") { 00052 if (!empty($groups)) { 00053 $mode = "group"; 00054 } else { 00055 $mode = "user"; 00056 } 00057 } 00058 $groupmode = ($mode == "group"); 00059 00060 $url = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id, 'mode'=>$mode)); 00061 00062 $PAGE->set_url($url); 00063 00064 require_login($course, false, $cm); 00065 00066 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 00067 00068 // Check the user has the required capabilities to list overrides 00069 require_capability('mod/quiz:manageoverrides', $context); 00070 00071 // Display a list of overrides 00072 00073 $PAGE->set_pagelayout('admin'); 00074 $PAGE->set_title(get_string('overrides', 'quiz')); 00075 $PAGE->set_heading($course->fullname); 00076 echo $OUTPUT->header(); 00077 00078 // Delete orphaned group overrides 00079 $sql = 'SELECT o.id 00080 FROM {quiz_overrides} o LEFT JOIN {groups} g 00081 ON o.groupid = g.id 00082 WHERE o.groupid IS NOT NULL 00083 AND g.id IS NULL 00084 AND o.quiz = ?'; 00085 $params = array($quiz->id); 00086 $orphaned = $DB->get_records_sql($sql, $params); 00087 if (!empty($orphaned)) { 00088 $DB->delete_records_list('quiz_overrides', 'id', array_keys($orphaned)); 00089 } 00090 00091 // Fetch all overrides 00092 if ($groupmode) { 00093 $colname = get_string('group'); 00094 $sql = 'SELECT o.*, g.name 00095 FROM {quiz_overrides} o JOIN {groups} g 00096 ON o.groupid = g.id 00097 WHERE o.quiz = ? 00098 ORDER BY g.name'; 00099 } else { 00100 $colname = get_string('user'); 00101 $sql = 'SELECT o.*, u.firstname, u.lastname 00102 FROM {quiz_overrides} o JOIN {user} u 00103 ON o.userid = u.id 00104 WHERE o.quiz = ? 00105 ORDER BY u.lastname, u.firstname'; 00106 } 00107 00108 $params = array($quiz->id); 00109 $overrides = $DB->get_records_sql($sql, $params); 00110 00111 // Initialise table 00112 $table = new html_table(); 00113 $table->headspan = array(1, 2, 1); 00114 $table->colclasses = array('colname', 'colsetting', 'colvalue', 'colaction'); 00115 $table->head = array( 00116 $colname, 00117 get_string('overrides', 'quiz'), 00118 get_string('action'), 00119 ); 00120 00121 $userurl = new moodle_url('/user/view.php', array()); 00122 $groupurl = new moodle_url('/group/overview.php', array('id' => $cm->course)); 00123 00124 $overridedeleteurl = new moodle_url('/mod/quiz/overridedelete.php'); 00125 $overrideediturl = new moodle_url('/mod/quiz/overrideedit.php'); 00126 00127 $hasinactive = false; // are there any inactive overrides 00128 00129 foreach ($overrides as $override) { 00130 00131 $fields = array(); 00132 $values = array(); 00133 $active = true; 00134 00135 // check for inactive overrides 00136 if (!$groupmode) { 00137 if (!has_capability('mod/quiz:attempt', $context, $override->userid)) { 00138 // user not allowed to take the quiz 00139 $active = false; 00140 } else if (!empty($CFG->enablegroupmembersonly) && $cm->groupmembersonly && 00141 !groups_has_membership($cm, $override->userid)) { 00142 // user does not belong to the current grouping 00143 $active = false; 00144 } 00145 } 00146 00147 // Format timeopen 00148 if (isset($override->timeopen)) { 00149 $fields[] = get_string('quizopens', 'quiz'); 00150 $values[] = $override->timeopen > 0 ? 00151 userdate($override->timeopen) : get_string('noopen', 'quiz'); 00152 } 00153 00154 // Format timeclose 00155 if (isset($override->timeclose)) { 00156 $fields[] = get_string('quizcloses', 'quiz'); 00157 $values[] = $override->timeclose > 0 ? 00158 userdate($override->timeclose) : get_string('noclose', 'quiz'); 00159 } 00160 00161 // Format timelimit 00162 if (isset($override->timelimit)) { 00163 $fields[] = get_string('timelimit', 'quiz'); 00164 $values[] = $override->timelimit > 0 ? 00165 format_time($override->timelimit) : get_string('none', 'quiz'); 00166 } 00167 00168 // Format number of attempts 00169 if (isset($override->attempts)) { 00170 $fields[] = get_string('attempts', 'quiz'); 00171 $values[] = $override->attempts > 0 ? 00172 $override->attempts : get_string('unlimited'); 00173 } 00174 00175 // Format password 00176 if (isset($override->password)) { 00177 $fields[] = get_string('requirepassword', 'quiz'); 00178 $values[] = $override->password !== '' ? 00179 get_string('enabled', 'quiz') : get_string('none', 'quiz'); 00180 } 00181 00182 // Icons: 00183 00184 $iconstr = ''; 00185 00186 if ($active) { 00187 // edit 00188 $editurlstr = $overrideediturl->out(true, array('id' => $override->id)); 00189 $iconstr = '<a title="' . get_string('edit') . '" href="'. $editurlstr . '">' . 00190 '<img src="' . $OUTPUT->pix_url('t/edit') . '" class="iconsmall" alt="' . 00191 get_string('edit') . '" /></a> '; 00192 // duplicate 00193 $copyurlstr = $overrideediturl->out(true, 00194 array('id' => $override->id, 'action' => 'duplicate')); 00195 $iconstr .= '<a title="' . get_string('copy') . '" href="' . $copyurlstr . '">' . 00196 '<img src="' . $OUTPUT->pix_url('t/copy') . '" class="iconsmall" alt="' . 00197 get_string('copy') . '" /></a> '; 00198 } 00199 // delete 00200 $deleteurlstr = $overridedeleteurl->out(true, 00201 array('id' => $override->id, 'sesskey' => sesskey())); 00202 $iconstr .= '<a title="' . get_string('delete') . '" href="' . $deleteurlstr . '">' . 00203 '<img src="' . $OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="' . 00204 get_string('delete') . '" /></a> '; 00205 00206 if ($groupmode) { 00207 $usergroupstr = '<a href="' . $groupurl->out(true, 00208 array('group' => $override->groupid)) . '" >' . $override->name . '</a>'; 00209 } else { 00210 $usergroupstr = '<a href="' . $userurl->out(true, 00211 array('id' => $override->userid)) . '" >' . fullname($override) . '</a>'; 00212 } 00213 00214 $class = ''; 00215 if (!$active) { 00216 $class = "dimmed_text"; 00217 $usergroupstr .= '*'; 00218 $hasinactive = true; 00219 } 00220 00221 $usergroupcell = new html_table_cell(); 00222 $usergroupcell->rowspan = count($fields); 00223 $usergroupcell->text = $usergroupstr; 00224 $actioncell = new html_table_cell(); 00225 $actioncell->rowspan = count($fields); 00226 $actioncell->text = $iconstr; 00227 00228 for ($i = 0; $i < count($fields); ++$i) { 00229 $row = new html_table_row(); 00230 $row->attributes['class'] = $class; 00231 if ($i == 0) { 00232 $row->cells[] = $usergroupcell; 00233 } 00234 $cell1 = new html_table_cell(); 00235 $cell1->text = $fields[$i]; 00236 $row->cells[] = $cell1; 00237 $cell2 = new html_table_cell(); 00238 $cell2->text = $values[$i]; 00239 $row->cells[] = $cell2; 00240 if ($i == 0) { 00241 $row->cells[] = $actioncell; 00242 } 00243 $table->data[] = $row; 00244 } 00245 } 00246 00247 // Output the table and button 00248 00249 echo html_writer::start_tag('div', array('id' => 'quizoverrides')); 00250 if (count($table->data)) { 00251 echo html_writer::table($table); 00252 } 00253 if ($hasinactive) { 00254 echo $OUTPUT->notification(get_string('inactiveoverridehelp', 'quiz'), 'dimmed_text'); 00255 } 00256 00257 echo html_writer::start_tag('div', array('class' => 'buttons')); 00258 $options = array(); 00259 if ($groupmode) { 00260 if (empty($groups)) { 00261 // there are no groups 00262 echo $OUTPUT->notification(get_string('groupsnone', 'quiz'), 'error'); 00263 $options['disabled'] = true; 00264 } 00265 echo $OUTPUT->single_button($overrideediturl->out(true, 00266 array('action' => 'addgroup', 'cmid' => $cm->id)), 00267 get_string('addnewgroupoverride', 'quiz'), 'post', $options); 00268 } else { 00269 $users = array(); 00270 // See if there are any students in the quiz 00271 if (!empty($CFG->enablegroupmembersonly) && $cm->groupmembersonly) { 00272 // restrict to grouping 00273 $limitgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); 00274 if (!empty($limitgroups)) { 00275 $users = get_users_by_capability($context, 'mod/quiz:attempt', 'u.id', 00276 '', '', 1, array_keys($limitgroups)); // Limit to one user for speed 00277 } 00278 } else { 00279 // Limit to one user for speed. 00280 $users = get_users_by_capability($context, 'mod/quiz:attempt', 'u.id'); 00281 } 00282 00283 if (empty($users)) { 00284 // there are no students 00285 echo $OUTPUT->notification(get_string('usersnone', 'quiz'), 'error'); 00286 $options['disabled'] = true; 00287 } 00288 echo $OUTPUT->single_button($overrideediturl->out(true, 00289 array('action' => 'adduser', 'cmid' => $cm->id)), 00290 get_string('addnewuseroverride', 'quiz'), 'post', $options); 00291 } 00292 echo html_writer::end_tag('div'); 00293 echo html_writer::end_tag('div'); 00294 00295 // Finish the page 00296 echo $OUTPUT->footer();