MDL-11699 - Making one more common case a bit nicer for callers. Merged from MOODLE_1...
[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
a4faed69 49 $timenow = time();
121609d8 50 if (!has_capability('mod/quiz:viewreports', $context)) {
a4faed69 51 // Can't review during the attempt.
ee1fb969 52 if (!$attempt->timefinish) {
a4faed69 53 redirect('attempt.php?q=' . $quiz->id);
29d5d0b4 54 }
a4faed69 55 // Can't review other student's attempts.
56 if ($attempt->userid != $USER->id) {
57 error("This is not your attempt!", 'view.php?q=' . $quiz->id);
58 }
59 // Can't review if Student's may review ... Responses is turned on.
60 if (!$options->responses) {
61 if ($options->quizstate == QUIZ_STATE_IMMEDIATELY) {
62 $message = '';
63 } else if ($options->quizstate == QUIZ_STATE_OPEN && $quiz->timeclose &&
64 ($quiz->review & QUIZ_REVIEW_CLOSED & QUIZ_REVIEW_RESPONSES)) {
65 $message = get_string('noreviewuntil', 'quiz', userdate($quiz->timeclose));
66 } else {
67 $message = get_string('noreview', 'quiz');
68 }
e484e8f9 69 if (empty($popup)) {
a4faed69 70 redirect('view.php?q=' . $quiz->id, $message);
e484e8f9 71 } else {
72 ?><script type="text/javascript">
73 opener.document.location.reload();
74 self.close();
75 </script><?php
76 die();
77 }
34283aa8 78 }
29d5d0b4 79 }
80
839f2456 81 add_to_log($course->id, "quiz", "review", "review.php?id=$cm->id&amp;attempt=$attempt->id", "$quiz->id", "$cm->id");
29d5d0b4 82
2d05b472 83/// Load all the questions and states needed by this script
84
85 // load the questions needed by page
86 $pagelist = $showall ? quiz_questions_in_quiz($attempt->layout) : quiz_questions_on_page($attempt->layout, $page);
87 $sql = "SELECT q.*, i.grade AS maxgrade, i.id AS instance".
88 " FROM {$CFG->prefix}question q,".
89 " {$CFG->prefix}quiz_question_instances i".
90 " WHERE i.quiz = '$quiz->id' AND q.id = i.question".
91 " AND q.id IN ($pagelist)";
92 if (!$questions = get_records_sql($sql)) {
93 error('No questions found');
94 }
95
96 // Load the question type specific information
97 if (!get_question_options($questions)) {
98 error('Could not load question options');
99 }
100
101 // Restore the question sessions to their most recent states
102 // creating new sessions where required
103 if (!$states = get_question_states($questions, $quiz, $attempt)) {
104 error('Could not restore question sessions');
105 }
106
ee1fb969 107/// Print the page header
29d5d0b4 108
29d5d0b4 109 $strquizzes = get_string("modulenameplural", "quiz");
29d5d0b4 110 $strreview = get_string("review", "quiz");
29d5d0b4 111 $strscore = get_string("score", "quiz");
112 $strgrade = get_string("grade");
113 $strbestgrade = get_string("bestgrade", "quiz");
114 $strtimetaken = get_string("timetaken", "quiz");
ee1fb969 115 $strtimecompleted = get_string("completedon", "quiz");
d4961620 116 $stroverdue = get_string("overdue", "quiz");
29d5d0b4 117
2d05b472 118 $pagequestions = explode(',', $pagelist);
119 $headtags = get_html_head_contributions($pagequestions, $questions, $states);
ee1fb969 120 if (!empty($popup)) {
121 define('MESSAGE_WINDOW', true); // This prevents the message window coming up
2d05b472 122 print_header($course->shortname.': '.format_string($quiz->name), '', '', '', $headtags, false, '', '', false, '');
ee1fb969 123 /// Include Javascript protection for this page
124 include('protect_js.php');
125 } else {
5cf38a57 126 $strupdatemodule = has_capability('moodle/course:manageactivities', $coursecontext)
ee1fb969 127 ? update_module_button($cm->id, $course->id, get_string('modulename', 'quiz'))
128 : "";
01e38388 129
130 $navlinks = array();
3b27b0fe 131 $navlinks[] = array('name' => $strquizzes, 'link' => "index.php?id=$course->id", 'type' => 'activity');
132 $navlinks[] = array('name' => format_string($quiz->name), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
133 $navlinks[] = array('name' => $strreview, 'link' => '', 'type' => 'title');
9ac0c1b3 134
3b27b0fe 135 $navigation = build_navigation($navlinks);
9ac0c1b3 136
2d05b472 137 print_header_simple(format_string($quiz->name), "", $navigation, "", $headtags, true, $strupdatemodule);
ee1fb969 138 }
b7f35820 139 echo '<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>'; // for overlib
140
ee1fb969 141/// Print heading and tabs if this is part of a preview
5cf38a57 142 if (has_capability('mod/quiz:preview', $context)) {
0a5b58af 143 if ($attempt->userid == $USER->id) { // this is the report on a preview
144 $currenttab = 'preview';
145 } else {
146 $currenttab = 'reports';
147 $mode = '';
148 }
ee1fb969 149 include('tabs.php');
150 } else {
151 print_heading(format_string($quiz->name));
152 }
99a1bf3d 153 if ($isteacher and $attempt->userid == $USER->id) {
154 // the teacher is at the end of a preview. Print button to start new preview
155 unset($buttonoptions);
156 $buttonoptions['q'] = $quiz->id;
157 $buttonoptions['forcenew'] = true;
158 echo '<div class="controls">';
159 print_single_button($CFG->wwwroot.'/mod/quiz/attempt.php', $buttonoptions, get_string('startagain', 'quiz'));
160 echo '</div>';
161 } else { // print number of the attempt
162 print_heading(get_string('reviewofattempt', 'quiz', $attempt->attempt));
163 }
164
165 // print javascript button to close the window, if necessary
166 if (!$isteacher) {
167 include('attempt_close_js.php');
168 }
ad1a74e0 169
ee1fb969 170/// Print infobox
171
3a00dbfd 172 $timelimit = (int)$quiz->timelimit * 60;
235987c5 173 $overtime = 0;
d4961620 174
ee1fb969 175 if ($attempt->timefinish) {
176 if ($timetaken = ($attempt->timefinish - $attempt->timestart)) {
177 if($timelimit && $timetaken > ($timelimit + 60)) {
178 $overtime = $timetaken - $timelimit;
179 $overtime = format_time($overtime);
180 }
181 $timetaken = format_time($timetaken);
182 } else {
183 $timetaken = "-";
d4961620 184 }
29d5d0b4 185 } else {
ee1fb969 186 $timetaken = get_string('unfinished', 'quiz');
29d5d0b4 187 }
99a1bf3d 188 echo '<table class="generaltable generalbox quizreviewsummary"><tbody>';
76cacec8 189 if ($attempt->userid <> $USER->id) {
99a1bf3d 190 $student = get_record('user', 'id', $attempt->userid);
191 $picture = print_user_picture($student->id, $course->id, $student->picture, false, true);
192 echo '<tr><th scope="row" class="cell">', $picture, '</th><td class="cell"><a href="', $CFG->wwwroot,
193 '/user/view.php?id=', $student->id, '&amp;course='.$course->id.'">',
194 fullname($student, true), '</a></td></tr>';
76cacec8 195 }
99a1bf3d 196 if (has_capability('mod/quiz:grade', $context) and
197 count($attempts = get_records_select('quiz_attempts', "quiz = '$quiz->id' AND userid = '$attempt->userid'", 'attempt ASC')) > 1) {
ee1fb969 198 // print list of attempts
199 $attemptlist = '';
200 foreach ($attempts as $at) {
201 $attemptlist .= ($at->id == $attempt->id)
09275894 202 ? '<strong>'.$at->attempt.'</strong>, '
ee1fb969 203 : '<a href="review.php?attempt='.$at->id.($showall?'&amp;showall=true':'').'">'.$at->attempt.'</a>, ';
204 }
99a1bf3d 205 echo '<tr><th scope="row" class="cell">', get_string('attempts', 'quiz'), '</th><td class="cell">',
206 trim($attemptlist, ' ,'), '</td></tr>';
ee1fb969 207 }
208
99a1bf3d 209 echo '<tr><th scope="row" class="cell">', get_string('startedon', 'quiz'), '</th><td class="cell">',
210 userdate($attempt->timestart), '</td></tr>';
ee1fb969 211 if ($attempt->timefinish) {
99a1bf3d 212 echo '<tr><th scope="row" class="cell">', $strtimecompleted, '</th><td class="cell">',
213 userdate($attempt->timefinish), '</td></tr>';
214 echo '<tr><th scope="row" class="cell">', $strtimetaken, '</th><td class="cell">',
215 $timetaken, '</td></tr>';
ee1fb969 216 }
3a00dbfd 217 if (!empty($overtime)) {
99a1bf3d 218 echo '<tr><th scope="row" class="cell">', $stroverdue, '</th><td class="cell">',$overtime, '</td></tr>';
d4961620 219 }
15db443c 220 //if the student is allowed to see their score
221 if ($options->scores) {
222 if ($quiz->grade and $quiz->sumgrades) {
223 if($overtime) {
224 $result->sumgrades = "0";
225 $result->grade = "0.0";
226 }
77ed3ba5 227
3b48ba9d 228 $a = new stdClass;
15db443c 229 $percentage = round(($attempt->sumgrades/$quiz->sumgrades)*100, 0);
212b7b8c 230 $a->grade = $grade;
3b48ba9d 231 $a->maxgrade = $quiz->grade;
15db443c 232 $rawscore = round($attempt->sumgrades, $CFG->quiz_decimalpoints);
99a1bf3d 233 echo '<tr><th scope="row" class="cell">', $strscore, '</th><td class="cell">',
49e7a95a 234 "$rawscore/$quiz->sumgrades ($percentage%)", '</td></tr>';
99a1bf3d 235 echo '<tr><th scope="row" class="cell">', $strgrade, '</th><td class="cell">',
236 get_string('outof', 'quiz', $a), '</td></tr>';
d4961620 237 }
ee1fb969 238 }
77ed3ba5 239 if ($options->overallfeedback && $feedback) {
99a1bf3d 240 echo '<tr><th scope="row" class="cell">', get_string('feedback', 'quiz'), '</th><td class="cell">',
241 $feedback, '</td></tr>';
9bc2d82a 242 }
99a1bf3d 243 echo '</tbody></table>';
9bc2d82a 244
ee1fb969 245/// Print the navigation panel if required
246 $numpages = quiz_number_of_pages($attempt->layout);
247 if ($numpages > 1 and !$showall) {
248 print_paging_bar($numpages, $page, 1, 'review.php?attempt='.$attempt->id.'&amp;');
09275894 249 echo '<div class="controls"><a href="review.php?attempt='.$attempt->id.'&amp;showall=true">';
ee1fb969 250 print_string('showall', 'quiz');
09275894 251 echo '</a></div>';
29d5d0b4 252 }
253
ee1fb969 254/// Print all the questions
255
ee1fb969 256 $number = quiz_first_questionnumber($attempt->layout, $pagelist);
257 foreach ($pagequestions as $i) {
7bbe08a2 258 if (!isset($questions[$i])) {
259 print_simple_box_start('center', '90%');
09275894 260 echo '<strong><font size="+1">' . $number . '</font></strong><br />';
7bbe08a2 261 notify(get_string('errormissingquestion', 'quiz', $i));
262 print_simple_box_end();
263 $number++; // Just guessing that the missing question would have lenght 1
264 continue;
265 }
4f48fb42 266 $options->validation = QUESTION_EVENTVALIDATE === $states[$i]->event;
ee1fb969 267 $options->history = ($isteacher and !$attempt->preview) ? 'all' : 'graded';
268 // Print the question
4f48fb42 269 print_question($questions[$i], $states[$i], $number, $quiz, $options);
ee1fb969 270 $number += $questions[$i]->length;
271 }
29d5d0b4 272
ee1fb969 273 // Print the navigation panel if required
274 if ($numpages > 1 and !$showall) {
275 print_paging_bar($numpages, $page, 1, 'review.php?attempt='.$attempt->id.'&amp;');
29d5d0b4 276 }
277
ee1fb969 278 // print javascript button to close the window, if necessary
279 if (!$isteacher) {
280 include('attempt_close_js.php');
281 }
29d5d0b4 282
ee1fb969 283 if (empty($popup)) {
284 print_footer($course);
285 }
29d5d0b4 286?>