MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / quiz / review.php
blobc21ac84f8d1d9fea87b5243da37d44c2e7300ed6
1 <?php // $Id$
2 /**
3 * This page prints a review of a particular quiz attempt
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
14 require_once("../../config.php");
15 require_once("locallib.php");
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);
21 if (! $attempt = get_record("quiz_attempts", "id", $attempt)) {
22 error("No such attempt ID exists");
24 if (! $quiz = get_record("quiz", "id", $attempt->quiz)) {
25 error("The quiz with id $attempt->quiz belonging to attempt $attempt is missing");
27 if (! $course = get_record("course", "id", $quiz->course)) {
28 error("The course with id $quiz->course that the quiz with id $quiz->id belongs to is missing");
30 if (! $cm = get_coursemodule_from_instance("quiz", $quiz->id, $course->id)) {
31 error("The course module for the quiz with id $quiz->id is missing");
34 $grade = quiz_rescale_grade($attempt->sumgrades, $quiz);
35 $feedback = quiz_feedback_for_grade($grade, $attempt->quiz);
37 if (!count_records('question_sessions', 'attemptid', $attempt->uniqueid)) {
38 // this question has not yet been upgraded to the new model
39 quiz_upgrade_states($attempt);
42 require_login($course->id, false, $cm);
43 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
44 $coursecontext = get_context_instance(CONTEXT_COURSE, $cm->course);
45 $isteacher = has_capability('mod/quiz:preview', get_context_instance(CONTEXT_MODULE, $cm->id));
46 $options = quiz_get_reviewoptions($quiz, $attempt, $context);
47 $popup = $isteacher ? 0 : $quiz->popup; // Controls whether this is shown in a javascript-protected window.
49 $timenow = time();
50 if (!has_capability('mod/quiz:viewreports', $context)) {
51 // Can't review during the attempt.
52 if (!$attempt->timefinish) {
53 redirect('attempt.php?q=' . $quiz->id);
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);
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');
69 if (empty($popup)) {
70 redirect('view.php?q=' . $quiz->id, $message);
71 } else {
72 ?><script type="text/javascript">
73 opener.document.location.reload();
74 self.close();
75 </script><?php
76 die();
81 add_to_log($course->id, "quiz", "review", "review.php?id=$cm->id&amp;attempt=$attempt->id", "$quiz->id", "$cm->id");
83 /// Load all the questions and states needed by this script
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');
96 // Load the question type specific information
97 if (!get_question_options($questions)) {
98 error('Could not load question options');
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');
107 /// Print the page header
109 $strquizzes = get_string("modulenameplural", "quiz");
110 $strreview = get_string("review", "quiz");
111 $strscore = get_string("score", "quiz");
112 $strgrade = get_string("grade");
113 $strbestgrade = get_string("bestgrade", "quiz");
114 $strtimetaken = get_string("timetaken", "quiz");
115 $strtimecompleted = get_string("completedon", "quiz");
116 $stroverdue = get_string("overdue", "quiz");
118 $pagequestions = explode(',', $pagelist);
119 $headtags = get_html_head_contributions($pagequestions, $questions, $states);
120 if (!empty($popup)) {
121 define('MESSAGE_WINDOW', true); // This prevents the message window coming up
122 print_header($course->shortname.': '.format_string($quiz->name), '', '', '', $headtags, false, '', '', false, '');
123 /// Include Javascript protection for this page
124 include('protect_js.php');
125 } else {
126 $strupdatemodule = has_capability('moodle/course:manageactivities', $coursecontext)
127 ? update_module_button($cm->id, $course->id, get_string('modulename', 'quiz'))
128 : "";
130 $navlinks = array();
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');
135 $navigation = build_navigation($navlinks);
137 print_header_simple(format_string($quiz->name), "", $navigation, "", $headtags, true, $strupdatemodule);
139 echo '<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>'; // for overlib
141 /// Print heading and tabs if this is part of a preview
142 if (has_capability('mod/quiz:preview', $context)) {
143 if ($attempt->userid == $USER->id) { // this is the report on a preview
144 $currenttab = 'preview';
145 } else {
146 $currenttab = 'reports';
147 $mode = '';
149 include('tabs.php');
150 } else {
151 print_heading(format_string($quiz->name));
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));
165 // print javascript button to close the window, if necessary
166 if (!$isteacher) {
167 include('attempt_close_js.php');
170 /// Print infobox
172 $timelimit = (int)$quiz->timelimit * 60;
173 $overtime = 0;
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);
181 $timetaken = format_time($timetaken);
182 } else {
183 $timetaken = "-";
185 } else {
186 $timetaken = get_string('unfinished', 'quiz');
188 echo '<table class="generaltable generalbox quizreviewsummary"><tbody>';
189 if ($attempt->userid <> $USER->id) {
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>';
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) {
198 // print list of attempts
199 $attemptlist = '';
200 foreach ($attempts as $at) {
201 $attemptlist .= ($at->id == $attempt->id)
202 ? '<strong>'.$at->attempt.'</strong>, '
203 : '<a href="review.php?attempt='.$at->id.($showall?'&amp;showall=true':'').'">'.$at->attempt.'</a>, ';
205 echo '<tr><th scope="row" class="cell">', get_string('attempts', 'quiz'), '</th><td class="cell">',
206 trim($attemptlist, ' ,'), '</td></tr>';
209 echo '<tr><th scope="row" class="cell">', get_string('startedon', 'quiz'), '</th><td class="cell">',
210 userdate($attempt->timestart), '</td></tr>';
211 if ($attempt->timefinish) {
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>';
217 if (!empty($overtime)) {
218 echo '<tr><th scope="row" class="cell">', $stroverdue, '</th><td class="cell">',$overtime, '</td></tr>';
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";
228 $a = new stdClass;
229 $percentage = round(($attempt->sumgrades/$quiz->sumgrades)*100, 0);
230 $a->grade = $grade;
231 $a->maxgrade = $quiz->grade;
232 $rawscore = round($attempt->sumgrades, $CFG->quiz_decimalpoints);
233 echo '<tr><th scope="row" class="cell">', $strscore, '</th><td class="cell">',
234 "$rawscore/$quiz->sumgrades ($percentage%)", '</td></tr>';
235 echo '<tr><th scope="row" class="cell">', $strgrade, '</th><td class="cell">',
236 get_string('outof', 'quiz', $a), '</td></tr>';
239 if ($options->overallfeedback && $feedback) {
240 echo '<tr><th scope="row" class="cell">', get_string('feedback', 'quiz'), '</th><td class="cell">',
241 $feedback, '</td></tr>';
243 echo '</tbody></table>';
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;');
249 echo '<div class="controls"><a href="review.php?attempt='.$attempt->id.'&amp;showall=true">';
250 print_string('showall', 'quiz');
251 echo '</a></div>';
254 /// Print all the questions
256 $number = quiz_first_questionnumber($attempt->layout, $pagelist);
257 foreach ($pagequestions as $i) {
258 if (!isset($questions[$i])) {
259 print_simple_box_start('center', '90%');
260 echo '<strong><font size="+1">' . $number . '</font></strong><br />';
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;
266 $options->validation = QUESTION_EVENTVALIDATE === $states[$i]->event;
267 $options->history = ($isteacher and !$attempt->preview) ? 'all' : 'graded';
268 // Print the question
269 print_question($questions[$i], $states[$i], $number, $quiz, $options);
270 $number += $questions[$i]->length;
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;');
278 // print javascript button to close the window, if necessary
279 if (!$isteacher) {
280 include('attempt_close_js.php');
283 if (empty($popup)) {
284 print_footer($course);