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