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