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