Stop "Undefined variable: ALLOWED_TAGS" errors all over the place.
[moodle.git] / mod / quiz / review.php
CommitLineData
d1290cec 1<?php // $Id$
ee1fb969 2/**
3* This page prints a review of a particular quiz attempt
4*
5* @version $Id$
6* @author Martin Dougiamas and many others. This has recently been completely
7* rewritten by Alex Smith, Julian Sedding and Gustav Delius as part of
8* the Serving Mathematics project
9* {@link http://maths.york.ac.uk/serving_maths}
10* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11* @package quiz
12*/
29d5d0b4 13
14 require_once("../../config.php");
76cacec8 15 require_once("locallib.php");
29d5d0b4 16
ee1fb969 17 $attempt = required_param('attempt', PARAM_INT); // A particular attempt ID for review
18 $page = optional_param('page', 0, PARAM_INT); // The required page
19 $showall = optional_param('showall', 0, PARAM_BOOL);
29d5d0b4 20
21 if (! $attempt = get_record("quiz_attempts", "id", $attempt)) {
22 error("No such attempt ID exists");
23 }
ee1fb969 24 if (! $quiz = get_record("quiz", "id", $attempt->quiz)) {
7bbe08a2 25 error("The quiz with id $attempt->quiz belonging to attempt $attempt is missing");
ee1fb969 26 }
27 if (! $course = get_record("course", "id", $quiz->course)) {
7bbe08a2 28 error("The course with id $quiz->course that the quiz with id $quiz->id belongs to is missing");
ee1fb969 29 }
30 if (! $cm = get_coursemodule_from_instance("quiz", $quiz->id, $course->id)) {
7bbe08a2 31 error("The course module for the quiz with id $quiz->id is missing");
ee1fb969 32 }
77ed3ba5 33
212b7b8c 34 $grade = quiz_rescale_grade($attempt->sumgrades, $quiz);
35 $feedback = quiz_feedback_for_grade($grade, $attempt->quiz);
29d5d0b4 36
03d1753c 37 if (!count_records('question_sessions', 'attemptid', $attempt->uniqueid)) {
ee1fb969 38 // this question has not yet been upgraded to the new model
39 quiz_upgrade_states($attempt);
40 }
29d5d0b4 41
ec81373f 42 require_login($course->id, false, $cm);
5cf38a57 43 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
a18fa6d5 44 $coursecontext = get_context_instance(CONTEXT_COURSE, $cm->course);
4f83ae95 45 $isteacher = has_capability('mod/quiz:preview', get_context_instance(CONTEXT_MODULE, $cm->id));
cd06115f 46 $options = quiz_get_reviewoptions($quiz, $attempt, $context);
bacd044d 47 $popup = $isteacher ? 0 : $quiz->popup; // Controls whether this is shown in a javascript-protected window.
29d5d0b4 48
121609d8 49 if (!has_capability('mod/quiz:viewreports', $context)) {
ee1fb969 50 if (!$attempt->timefinish) {
51 redirect('attempt.php?q='.$quiz->id);
29d5d0b4 52 }
ee1fb969 53 // If not even responses are to be shown in review then we
54 // don't allow any review
55 if (!($quiz->review & QUIZ_REVIEW_RESPONSES)) {
e484e8f9 56 if (empty($popup)) {
57 redirect('view.php?q='.$quiz->id);
58 } else {
59 ?><script type="text/javascript">
60 opener.document.location.reload();
61 self.close();
62 </script><?php
63 die();
64 }
34283aa8 65 }
ee1fb969 66 if ((time() - $attempt->timefinish) > 120) { // always allow review right after attempt
bb52d44c 67 if ((!$quiz->timeclose or time() < $quiz->timeclose) and !($quiz->review & QUIZ_REVIEW_OPEN)) {
7bbe08a2 68 redirect('view.php?q='.$quiz->id, get_string("noreviewuntil", "quiz", userdate($quiz->timeclose)));
ee1fb969 69 }
bb52d44c 70 if ($quiz->timeclose and time() >= $quiz->timeclose and !($quiz->review & QUIZ_REVIEW_CLOSED)) {
7bbe08a2 71 redirect('view.php?q='.$quiz->id, get_string("noreview", "quiz"));
ee1fb969 72 }
ee1fb969 73 }
29d5d0b4 74 if ($attempt->userid != $USER->id) {
7bbe08a2 75 error("This is not your attempt!", 'view.php?q='.$quiz->id);
29d5d0b4 76 }
77 }
78
839f2456 79 add_to_log($course->id, "quiz", "review", "review.php?id=$cm->id&amp;attempt=$attempt->id", "$quiz->id", "$cm->id");
29d5d0b4 80
ee1fb969 81/// Print the page header
29d5d0b4 82
29d5d0b4 83 $strquizzes = get_string("modulenameplural", "quiz");
29d5d0b4 84 $strreview = get_string("review", "quiz");
29d5d0b4 85 $strscore = get_string("score", "quiz");
86 $strgrade = get_string("grade");
87 $strbestgrade = get_string("bestgrade", "quiz");
88 $strtimetaken = get_string("timetaken", "quiz");
ee1fb969 89 $strtimecompleted = get_string("completedon", "quiz");
d4961620 90 $stroverdue = get_string("overdue", "quiz");
29d5d0b4 91
ee1fb969 92 if (!empty($popup)) {
93 define('MESSAGE_WINDOW', true); // This prevents the message window coming up
94 print_header($course->shortname.': '.format_string($quiz->name), '', '', '', '', false, '', '', false, '');
95 /// Include Javascript protection for this page
96 include('protect_js.php');
97 } else {
5cf38a57 98 $strupdatemodule = has_capability('moodle/course:manageactivities', $coursecontext)
ee1fb969 99 ? update_module_button($cm->id, $course->id, get_string('modulename', 'quiz'))
100 : "";
9ac0c1b3 101
102 $crumbs[] = array('name' => $strquizzes, 'link' => "index.php?id=$course->id", 'type' => 'activity');
103 $crumbs[] = array('name' => format_string($quiz->name), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
104 $crumbs[] = array('name' => $strreview, 'link' => '', 'type' => 'title');
105
70c6c0ad 106 $navigation = build_navigation($crumbs);
9ac0c1b3 107
108 print_header_simple(format_string($quiz->name), "", $navigation, "", "", true, $strupdatemodule);
ee1fb969 109 }
b7f35820 110 echo '<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>'; // for overlib
111
ee1fb969 112/// Print heading and tabs if this is part of a preview
5cf38a57 113 if (has_capability('mod/quiz:preview', $context)) {
0a5b58af 114 if ($attempt->userid == $USER->id) { // this is the report on a preview
115 $currenttab = 'preview';
116 } else {
117 $currenttab = 'reports';
118 $mode = '';
119 }
ee1fb969 120 include('tabs.php');
121 } else {
122 print_heading(format_string($quiz->name));
123 }
ad1a74e0 124
ee1fb969 125/// Load all the questions and states needed by this script
126
127 // load the questions needed by page
128 $pagelist = $showall ? quiz_questions_in_quiz($attempt->layout) : quiz_questions_on_page($attempt->layout, $page);
5a82dafa 129 $sql = "SELECT q.*, i.grade AS maxgrade, i.id AS instance".
4f48fb42 130 " FROM {$CFG->prefix}question q,".
5a82dafa 131 " {$CFG->prefix}quiz_question_instances i".
132 " WHERE i.quiz = '$quiz->id' AND q.id = i.question".
133 " AND q.id IN ($pagelist)";
134 if (!$questions = get_records_sql($sql)) {
ee1fb969 135 error('No questions found');
ad1a74e0 136 }
137
ee1fb969 138 // Load the question type specific information
4f48fb42 139 if (!get_question_options($questions)) {
ee1fb969 140 error('Could not load question options');
29d5d0b4 141 }
142
ee1fb969 143 // Restore the question sessions to their most recent states
144 // creating new sessions where required
4f48fb42 145 if (!$states = get_question_states($questions, $quiz, $attempt)) {
ee1fb969 146 error('Could not restore question sessions');
29d5d0b4 147 }
148
ee1fb969 149/// Print infobox
150
3a00dbfd 151 $timelimit = (int)$quiz->timelimit * 60;
235987c5 152 $overtime = 0;
d4961620 153
ee1fb969 154 if ($attempt->timefinish) {
155 if ($timetaken = ($attempt->timefinish - $attempt->timestart)) {
156 if($timelimit && $timetaken > ($timelimit + 60)) {
157 $overtime = $timetaken - $timelimit;
158 $overtime = format_time($overtime);
159 }
160 $timetaken = format_time($timetaken);
161 } else {
162 $timetaken = "-";
d4961620 163 }
29d5d0b4 164 } else {
ee1fb969 165 $timetaken = get_string('unfinished', 'quiz');
29d5d0b4 166 }
167
168 $table->align = array("right", "left");
76cacec8 169 if ($attempt->userid <> $USER->id) {
170 $student = get_record('user', 'id', $attempt->userid);
171 $picture = print_user_picture($student->id, $course->id, $student->picture, false, true);
7a0b035b 172 $table->data[] = array($picture, '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$student->id.'&amp;course='.$course->id.'">'.fullname($student, true).'</a>');
76cacec8 173 }
5cf38a57 174 if (has_capability('mod/quiz:grade', $context) and count($attempts = get_records_select('quiz_attempts', "quiz = '$quiz->id' AND userid = '$attempt->userid'", 'attempt ASC')) > 1) {
ee1fb969 175 // print list of attempts
176 $attemptlist = '';
177 foreach ($attempts as $at) {
178 $attemptlist .= ($at->id == $attempt->id)
09275894 179 ? '<strong>'.$at->attempt.'</strong>, '
ee1fb969 180 : '<a href="review.php?attempt='.$at->id.($showall?'&amp;showall=true':'').'">'.$at->attempt.'</a>, ';
181 }
182 $table->data[] = array(get_string('attempts', 'quiz').':', trim($attemptlist, ' ,'));
183 }
184
185 $table->data[] = array(get_string('startedon', 'quiz').':', userdate($attempt->timestart));
186 if ($attempt->timefinish) {
187 $table->data[] = array("$strtimecompleted:", userdate($attempt->timefinish));
188 $table->data[] = array("$strtimetaken:", $timetaken);
189 }
3a00dbfd 190 if (!empty($overtime)) {
d4961620 191 $table->data[] = array("$stroverdue:", $overtime);
192 }
15db443c 193 //if the student is allowed to see their score
194 if ($options->scores) {
195 if ($quiz->grade and $quiz->sumgrades) {
196 if($overtime) {
197 $result->sumgrades = "0";
198 $result->grade = "0.0";
199 }
77ed3ba5 200
3b48ba9d 201 $a = new stdClass;
15db443c 202 $percentage = round(($attempt->sumgrades/$quiz->sumgrades)*100, 0);
212b7b8c 203 $a->grade = $grade;
3b48ba9d 204 $a->maxgrade = $quiz->grade;
15db443c 205 $rawscore = round($attempt->sumgrades, $CFG->quiz_decimalpoints);
206 $table->data[] = array("$strscore:", "$rawscore/$quiz->sumgrades ($percentage %)");
3b48ba9d 207 $table->data[] = array("$strgrade:", get_string('outof', 'quiz', $a));
d4961620 208 }
ee1fb969 209 }
77ed3ba5 210 if ($options->overallfeedback && $feedback) {
212b7b8c 211 $table->data[] = array(get_string('feedback', 'quiz'), $feedback);
212 }
ee1fb969 213 if ($isteacher and $attempt->userid == $USER->id) {
214 // the teacher is at the end of a preview. Print button to start new preview
215 unset($buttonoptions);
216 $buttonoptions['q'] = $quiz->id;
217 $buttonoptions['forcenew'] = true;
09275894 218 echo '<div class="controls">';
ee1fb969 219 print_single_button($CFG->wwwroot.'/mod/quiz/attempt.php', $buttonoptions, get_string('startagain', 'quiz'));
09275894 220 echo '</div>';
ee1fb969 221 } else { // print number of the attempt
222 print_heading(get_string('reviewofattempt', 'quiz', $attempt->attempt));
ed1daaa9 223 }
29d5d0b4 224 print_table($table);
225
9bc2d82a 226 // print javascript button to close the window, if necessary
227 if (!$isteacher) {
228 include('attempt_close_js.php');
229 }
230
ee1fb969 231/// Print the navigation panel if required
232 $numpages = quiz_number_of_pages($attempt->layout);
233 if ($numpages > 1 and !$showall) {
234 print_paging_bar($numpages, $page, 1, 'review.php?attempt='.$attempt->id.'&amp;');
09275894 235 echo '<div class="controls"><a href="review.php?attempt='.$attempt->id.'&amp;showall=true">';
ee1fb969 236 print_string('showall', 'quiz');
09275894 237 echo '</a></div>';
29d5d0b4 238 }
239
ee1fb969 240/// Print all the questions
241
242 $pagequestions = explode(',', $pagelist);
243 $number = quiz_first_questionnumber($attempt->layout, $pagelist);
244 foreach ($pagequestions as $i) {
7bbe08a2 245 if (!isset($questions[$i])) {
246 print_simple_box_start('center', '90%');
09275894 247 echo '<strong><font size="+1">' . $number . '</font></strong><br />';
7bbe08a2 248 notify(get_string('errormissingquestion', 'quiz', $i));
249 print_simple_box_end();
250 $number++; // Just guessing that the missing question would have lenght 1
251 continue;
252 }
4f48fb42 253 $options->validation = QUESTION_EVENTVALIDATE === $states[$i]->event;
ee1fb969 254 $options->history = ($isteacher and !$attempt->preview) ? 'all' : 'graded';
255 // Print the question
256 if ($i > 0) {
257 echo "<br />\n";
258 }
4f48fb42 259 print_question($questions[$i], $states[$i], $number, $quiz, $options);
ee1fb969 260 $number += $questions[$i]->length;
261 }
29d5d0b4 262
ee1fb969 263 // Print the navigation panel if required
264 if ($numpages > 1 and !$showall) {
265 print_paging_bar($numpages, $page, 1, 'review.php?attempt='.$attempt->id.'&amp;');
29d5d0b4 266 }
267
ee1fb969 268 // print javascript button to close the window, if necessary
269 if (!$isteacher) {
270 include('attempt_close_js.php');
271 }
29d5d0b4 272
ee1fb969 273 if (empty($popup)) {
274 print_footer($course);
275 }
29d5d0b4 276?>