mod/quiz/review quiz options loaded once per pageload, not once per qn.
[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 }
29d5d0b4 33
03d1753c 34 if (!count_records('question_sessions', 'attemptid', $attempt->uniqueid)) {
ee1fb969 35 // this question has not yet been upgraded to the new model
36 quiz_upgrade_states($attempt);
37 }
29d5d0b4 38
ec81373f 39 require_login($course->id, false, $cm);
ee1fb969 40 $isteacher = isteacher($course->id);
9f7f01db 41 $options = quiz_get_reviewoptions($quiz, $attempt, $isteacher);
bacd044d 42 $popup = $isteacher ? 0 : $quiz->popup; // Controls whether this is shown in a javascript-protected window.
29d5d0b4 43
ee1fb969 44 if (!$isteacher) {
45 if (!$attempt->timefinish) {
46 redirect('attempt.php?q='.$quiz->id);
29d5d0b4 47 }
ee1fb969 48 // If not even responses are to be shown in review then we
49 // don't allow any review
50 if (!($quiz->review & QUIZ_REVIEW_RESPONSES)) {
e484e8f9 51 if (empty($popup)) {
52 redirect('view.php?q='.$quiz->id);
53 } else {
54 ?><script type="text/javascript">
55 opener.document.location.reload();
56 self.close();
57 </script><?php
58 die();
59 }
34283aa8 60 }
ee1fb969 61 if ((time() - $attempt->timefinish) > 120) { // always allow review right after attempt
bb52d44c 62 if ((!$quiz->timeclose or time() < $quiz->timeclose) and !($quiz->review & QUIZ_REVIEW_OPEN)) {
7bbe08a2 63 redirect('view.php?q='.$quiz->id, get_string("noreviewuntil", "quiz", userdate($quiz->timeclose)));
ee1fb969 64 }
bb52d44c 65 if ($quiz->timeclose and time() >= $quiz->timeclose and !($quiz->review & QUIZ_REVIEW_CLOSED)) {
7bbe08a2 66 redirect('view.php?q='.$quiz->id, get_string("noreview", "quiz"));
ee1fb969 67 }
ee1fb969 68 }
29d5d0b4 69 if ($attempt->userid != $USER->id) {
7bbe08a2 70 error("This is not your attempt!", 'view.php?q='.$quiz->id);
29d5d0b4 71 }
72 }
73
839f2456 74 add_to_log($course->id, "quiz", "review", "review.php?id=$cm->id&amp;attempt=$attempt->id", "$quiz->id", "$cm->id");
29d5d0b4 75
ee1fb969 76/// Print the page header
29d5d0b4 77
29d5d0b4 78 $strquizzes = get_string("modulenameplural", "quiz");
29d5d0b4 79 $strreview = get_string("review", "quiz");
29d5d0b4 80 $strscore = get_string("score", "quiz");
81 $strgrade = get_string("grade");
82 $strbestgrade = get_string("bestgrade", "quiz");
83 $strtimetaken = get_string("timetaken", "quiz");
ee1fb969 84 $strtimecompleted = get_string("completedon", "quiz");
d4961620 85 $stroverdue = get_string("overdue", "quiz");
29d5d0b4 86
ee1fb969 87 if (!empty($popup)) {
88 define('MESSAGE_WINDOW', true); // This prevents the message window coming up
89 print_header($course->shortname.': '.format_string($quiz->name), '', '', '', '', false, '', '', false, '');
90 /// Include Javascript protection for this page
91 include('protect_js.php');
92 } else {
93 $strupdatemodule = isteacheredit($course->id)
94 ? update_module_button($cm->id, $course->id, get_string('modulename', 'quiz'))
95 : "";
96 print_header_simple(format_string($quiz->name), "",
ec81373f 97 "<a href=\"index.php?id=$course->id\">$strquizzes</a>
b6810004 98 -> <a href=\"view.php?id=$cm->id\">".format_string($quiz->name,true)."</a> -> $strreview",
ee1fb969 99 "", "", true, $strupdatemodule);
100 }
b7f35820 101 echo '<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>'; // for overlib
102
ee1fb969 103/// Print heading and tabs if this is part of a preview
104 if ($isteacher) {
0a5b58af 105 if ($attempt->userid == $USER->id) { // this is the report on a preview
106 $currenttab = 'preview';
107 } else {
108 $currenttab = 'reports';
109 $mode = '';
110 }
ee1fb969 111 include('tabs.php');
112 } else {
113 print_heading(format_string($quiz->name));
114 }
ad1a74e0 115
ee1fb969 116/// Load all the questions and states needed by this script
117
118 // load the questions needed by page
119 $pagelist = $showall ? quiz_questions_in_quiz($attempt->layout) : quiz_questions_on_page($attempt->layout, $page);
5a82dafa 120 $sql = "SELECT q.*, i.grade AS maxgrade, i.id AS instance".
4f48fb42 121 " FROM {$CFG->prefix}question q,".
5a82dafa 122 " {$CFG->prefix}quiz_question_instances i".
123 " WHERE i.quiz = '$quiz->id' AND q.id = i.question".
124 " AND q.id IN ($pagelist)";
125 if (!$questions = get_records_sql($sql)) {
ee1fb969 126 error('No questions found');
ad1a74e0 127 }
128
ee1fb969 129 // Load the question type specific information
4f48fb42 130 if (!get_question_options($questions)) {
ee1fb969 131 error('Could not load question options');
29d5d0b4 132 }
133
ee1fb969 134 // Restore the question sessions to their most recent states
135 // creating new sessions where required
4f48fb42 136 if (!$states = get_question_states($questions, $quiz, $attempt)) {
ee1fb969 137 error('Could not restore question sessions');
29d5d0b4 138 }
139
ee1fb969 140/// Print infobox
141
3a00dbfd 142 $timelimit = (int)$quiz->timelimit * 60;
235987c5 143 $overtime = 0;
d4961620 144
ee1fb969 145 if ($attempt->timefinish) {
146 if ($timetaken = ($attempt->timefinish - $attempt->timestart)) {
147 if($timelimit && $timetaken > ($timelimit + 60)) {
148 $overtime = $timetaken - $timelimit;
149 $overtime = format_time($overtime);
150 }
151 $timetaken = format_time($timetaken);
152 } else {
153 $timetaken = "-";
d4961620 154 }
29d5d0b4 155 } else {
ee1fb969 156 $timetaken = get_string('unfinished', 'quiz');
29d5d0b4 157 }
158
159 $table->align = array("right", "left");
76cacec8 160 if ($attempt->userid <> $USER->id) {
161 $student = get_record('user', 'id', $attempt->userid);
162 $picture = print_user_picture($student->id, $course->id, $student->picture, false, true);
7a0b035b 163 $table->data[] = array($picture, '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$student->id.'&amp;course='.$course->id.'">'.fullname($student, true).'</a>');
76cacec8 164 }
ee1fb969 165 if ($isteacher and count($attempts = get_records_select('quiz_attempts', "quiz = '$quiz->id' AND userid = '$attempt->userid'", 'attempt ASC')) > 1) {
166 // print list of attempts
167 $attemptlist = '';
168 foreach ($attempts as $at) {
169 $attemptlist .= ($at->id == $attempt->id)
170 ? '<b>'.$at->attempt.'</b>, '
171 : '<a href="review.php?attempt='.$at->id.($showall?'&amp;showall=true':'').'">'.$at->attempt.'</a>, ';
172 }
173 $table->data[] = array(get_string('attempts', 'quiz').':', trim($attemptlist, ' ,'));
174 }
175
176 $table->data[] = array(get_string('startedon', 'quiz').':', userdate($attempt->timestart));
177 if ($attempt->timefinish) {
178 $table->data[] = array("$strtimecompleted:", userdate($attempt->timefinish));
179 $table->data[] = array("$strtimetaken:", $timetaken);
180 }
3a00dbfd 181 if (!empty($overtime)) {
d4961620 182 $table->data[] = array("$stroverdue:", $overtime);
183 }
8779eab5 184 if ($quiz->grade and $quiz->sumgrades) {
d4961620 185 if($overtime) {
186 $result->sumgrades = "0";
d4961620 187 $result->grade = "0.0";
188 }
b9376ebe 189
1105b32d 190 $percentage = round(($attempt->sumgrades/$quiz->sumgrades)*100, 0);
191 $grade = round(($attempt->sumgrades/$quiz->sumgrades)*$quiz->grade, $CFG->quiz_decimalpoints);
85b9b86f 192 $rawscore = round($attempt->sumgrades, $CFG->quiz_decimalpoints);
193 $table->data[] = array("$strscore:", "$rawscore/$quiz->sumgrades ($percentage %)");
e51b142a 194 $table->data[] = array("$strgrade:", $grade.get_string('outof', 'quiz').$quiz->grade);
ee1fb969 195 }
196 if ($isteacher and $attempt->userid == $USER->id) {
197 // the teacher is at the end of a preview. Print button to start new preview
198 unset($buttonoptions);
199 $buttonoptions['q'] = $quiz->id;
200 $buttonoptions['forcenew'] = true;
201 echo '<center>';
202 print_single_button($CFG->wwwroot.'/mod/quiz/attempt.php', $buttonoptions, get_string('startagain', 'quiz'));
203 echo '</center>';
204 } else { // print number of the attempt
205 print_heading(get_string('reviewofattempt', 'quiz', $attempt->attempt));
ed1daaa9 206 }
29d5d0b4 207 print_table($table);
208
9bc2d82a 209 // print javascript button to close the window, if necessary
210 if (!$isteacher) {
211 include('attempt_close_js.php');
212 }
213
ee1fb969 214/// Print the navigation panel if required
215 $numpages = quiz_number_of_pages($attempt->layout);
216 if ($numpages > 1 and !$showall) {
217 print_paging_bar($numpages, $page, 1, 'review.php?attempt='.$attempt->id.'&amp;');
218 echo '<center><a href="review.php?attempt='.$attempt->id.'&amp;showall=true">';
219 print_string('showall', 'quiz');
220 echo '</a></center>';
29d5d0b4 221 }
222
ee1fb969 223/// Print all the questions
224
225 $pagequestions = explode(',', $pagelist);
226 $number = quiz_first_questionnumber($attempt->layout, $pagelist);
227 foreach ($pagequestions as $i) {
7bbe08a2 228 if (!isset($questions[$i])) {
229 print_simple_box_start('center', '90%');
230 echo '<b><font size="+1">' . $number . '</font></b><br />';
231 notify(get_string('errormissingquestion', 'quiz', $i));
232 print_simple_box_end();
233 $number++; // Just guessing that the missing question would have lenght 1
234 continue;
235 }
4f48fb42 236 $options->validation = QUESTION_EVENTVALIDATE === $states[$i]->event;
ee1fb969 237 $options->history = ($isteacher and !$attempt->preview) ? 'all' : 'graded';
238 // Print the question
239 if ($i > 0) {
240 echo "<br />\n";
241 }
4f48fb42 242 print_question($questions[$i], $states[$i], $number, $quiz, $options);
ee1fb969 243 $number += $questions[$i]->length;
244 }
29d5d0b4 245
ee1fb969 246 // Print the navigation panel if required
247 if ($numpages > 1 and !$showall) {
248 print_paging_bar($numpages, $page, 1, 'review.php?attempt='.$attempt->id.'&amp;');
29d5d0b4 249 }
250
ee1fb969 251 // print javascript button to close the window, if necessary
252 if (!$isteacher) {
253 include('attempt_close_js.php');
254 }
29d5d0b4 255
ee1fb969 256 if (empty($popup)) {
257 print_footer($course);
258 }
29d5d0b4 259?>