Moodle release 1.9.16
[moodle.git] / mod / quiz / review.php
blob47c17764964ad5c28c6eba696f3de091e99105d1
1 <?php // $Id$
2 /**
3 * This page prints a review of a particular quiz attempt
5 * @author Martin Dougiamas and many others. This has recently been completely
6 * rewritten by Alex Smith, Julian Sedding and Gustav Delius as part of
7 * the Serving Mathematics project
8 * {@link http://maths.york.ac.uk/serving_maths}
9 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
10 * @package quiz
13 require_once("../../config.php");
14 require_once("locallib.php");
16 $attempt = required_param('attempt', PARAM_INT); // A particular attempt ID for review
17 $page = optional_param('page', 0, PARAM_INT); // The required page
18 $showall = optional_param('showall', 0, PARAM_BOOL);
20 if (! $attempt = get_record("quiz_attempts", "id", $attempt)) {
21 error("No such attempt ID exists");
23 if (! $quiz = get_record("quiz", "id", $attempt->quiz)) {
24 error("The quiz with id $attempt->quiz belonging to attempt $attempt is missing");
26 if (! $course = get_record("course", "id", $quiz->course)) {
27 error("The course with id $quiz->course that the quiz with id $quiz->id belongs to is missing");
29 if (! $cm = get_coursemodule_from_instance("quiz", $quiz->id, $course->id)) {
30 error("The course module for the quiz with id $quiz->id is missing");
33 if (!count_records('question_sessions', 'attemptid', $attempt->uniqueid)) {
34 // this question has not yet been upgraded to the new model
35 quiz_upgrade_states($attempt);
38 require_login($course->id, false, $cm);
39 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
40 $coursecontext = get_context_instance(CONTEXT_COURSE, $cm->course);
41 $isteacher = has_capability('mod/quiz:preview', $context);
42 $options = quiz_get_reviewoptions($quiz, $attempt, $context);
43 $popup = $isteacher ? 0 : $quiz->popup; // Controls whether this is shown in a javascript-protected window or with a safe browser.
45 $timenow = time();
46 if (!has_capability('mod/quiz:viewreports', $context)) {
47 // Can't review during the attempt.
48 if (!$attempt->timefinish) {
49 redirect('attempt.php?q=' . $quiz->id);
51 // Can't review other student's attempts.
52 if ($attempt->userid != $USER->id) {
53 error("This is not your attempt!", 'view.php?q=' . $quiz->id);
55 // Check capabilities.
56 if ($options->quizstate == QUIZ_STATE_IMMEDIATELY) {
57 require_capability('mod/quiz:attempt', $context);
58 } else {
59 require_capability('mod/quiz:reviewmyattempts', $context);
61 // Can't review if Student's may review ... Responses is turned on.
62 if (!$options->responses) {
63 if ($options->quizstate == QUIZ_STATE_IMMEDIATELY) {
64 $message = '';
65 } else if ($options->quizstate == QUIZ_STATE_OPEN && $quiz->timeclose &&
66 ($quiz->review & QUIZ_REVIEW_CLOSED & QUIZ_REVIEW_RESPONSES)) {
67 $message = get_string('noreviewuntil', 'quiz', userdate($quiz->timeclose));
68 } else {
69 $message = get_string('noreview', 'quiz');
71 if (!empty($popup) && $popup == 1) {
72 ?><script type="text/javascript">
73 opener.document.location.reload();
74 self.close();
75 </script><?php
76 die();
77 } else {
78 redirect('view.php?q=' . $quiz->id, $message);
82 } else if (!has_capability('moodle/site:accessallgroups', $context) &&
83 groups_get_activity_groupmode($cm) == SEPARATEGROUPS) {
84 // Check the users have at least one group in common.
85 $teachersgroups = groups_get_activity_allowed_groups($cm);
86 $studentsgroups = groups_get_all_groups($cm->course, $attempt->userid, $cm->groupingid);
87 if (!($teachersgroups && $studentsgroups &&
88 array_intersect(array_keys($teachersgroups), array_keys($studentsgroups)))) {
89 print_error('noreview', 'quiz', 'view.php?q=' . $quiz->id);
93 /// Bits needed to print a good URL for this page.
94 $urloptions = '';
95 if ($showall) {
96 $urloptions .= '&amp;showall=true';
97 } else if ($page > 0) {
98 $urloptions .= '&amp;page=' . $page;
101 add_to_log($course->id, 'quiz', 'review', 'review.php?attempt=' . $attempt->id . $urloptions, $quiz->id, $cm->id);
103 /// Load all the questions and states needed by this script
105 // load the questions needed by page
106 $pagelist = $showall ? quiz_questions_in_quiz($attempt->layout) : quiz_questions_on_page($attempt->layout, $page);
107 $sql = "SELECT q.*, i.grade AS maxgrade, i.id AS instance".
108 " FROM {$CFG->prefix}question q,".
109 " {$CFG->prefix}quiz_question_instances i".
110 " WHERE i.quiz = '$quiz->id' AND q.id = i.question".
111 " AND q.id IN ($pagelist)";
112 if (!$questions = get_records_sql($sql)) {
113 error('No questions found');
116 // Load the question type specific information
117 if (!get_question_options($questions)) {
118 error('Could not load question options');
121 // Restore the question sessions to their most recent states
122 // creating new sessions where required
123 if (!$states = get_question_states($questions, $quiz, $attempt)) {
124 error('Could not restore question sessions');
127 /// Work out appropriate title.
128 if ($isteacher and $attempt->userid == $USER->id) {
129 $strreviewtitle = get_string('reviewofpreview', 'quiz');
130 } else {
131 $strreviewtitle = get_string('reviewofattempt', 'quiz', $attempt->attempt);
134 /// Print the page header
135 $pagequestions = explode(',', $pagelist);
136 $headtags = get_html_head_contributions($pagequestions, $questions, $states);
137 if (!$isteacher && $quiz->popup) {
138 define('MESSAGE_WINDOW', true); // This prevents the message window coming up
139 print_header($course->shortname.': '.format_string($quiz->name), '', '', '', $headtags, false, '', '', false, '');
140 if ($quiz->popup == 1) {
141 include('protect_js.php');
143 } else {
144 $strupdatemodule = has_capability('moodle/course:manageactivities', $coursecontext)
145 ? update_module_button($cm->id, $course->id, get_string('modulename', 'quiz'))
146 : "";
147 get_string('reviewofattempt', 'quiz', $attempt->attempt);
148 $navigation = build_navigation($strreviewtitle, $cm);
149 print_header_simple(format_string($quiz->name), "", $navigation, "", $headtags, true, $strupdatemodule);
151 echo '<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>'; // for overlib
153 /// Print heading and tabs if this is part of a preview
154 if ($isteacher) {
155 if ($attempt->userid == $USER->id) { // this is the report on a preview
156 $currenttab = 'preview';
157 } else {
158 $currenttab = 'reports';
159 $mode = '';
161 include('tabs.php');
164 /// Print heading.
165 print_heading(format_string($quiz->name));
166 if ($isteacher and $attempt->userid == $USER->id) {
167 // the teacher is at the end of a preview. Print button to start new preview
168 unset($buttonoptions);
169 $buttonoptions['q'] = $quiz->id;
170 $buttonoptions['forcenew'] = true;
171 echo '<div class="controls">';
172 print_single_button($CFG->wwwroot.'/mod/quiz/attempt.php', $buttonoptions, get_string('startagain', 'quiz'));
173 echo '</div>';
175 print_heading($strreviewtitle);
177 // print javascript button to close the window, if necessary
178 if (!$isteacher) {
179 include('attempt_close_js.php');
182 /// Work out some time-related things.
183 $timelimit = (int)$quiz->timelimit * 60;
184 $overtime = 0;
186 if ($attempt->timefinish) {
187 if ($timetaken = ($attempt->timefinish - $attempt->timestart)) {
188 if($timelimit && $timetaken > ($timelimit + 60)) {
189 $overtime = $timetaken - $timelimit;
190 $overtime = format_time($overtime);
192 $timetaken = format_time($timetaken);
193 } else {
194 $timetaken = "-";
196 } else {
197 $timetaken = get_string('unfinished', 'quiz');
200 /// Print summary table about the whole attempt.
201 /// First we assemble all the rows that are appopriate to the current situation in
202 /// an array, then later we only output the table if there are any rows to show.
203 $rows = array();
204 if ($attempt->userid <> $USER->id) {
205 $student = get_record('user', 'id', $attempt->userid);
206 $picture = print_user_picture($student, $course->id, $student->picture, false, true);
207 $rows[] = '<tr><th scope="row" class="cell">' . $picture . '</th><td class="cell"><a href="' .
208 $CFG->wwwroot . '/user/view.php?id=' . $student->id . '&amp;course=' . $course->id . '">' .
209 fullname($student, true) . '</a></td></tr>';
211 if (has_capability('mod/quiz:viewreports', $context) &&
212 count($attempts = get_records_select('quiz_attempts', "quiz = '$quiz->id' AND userid = '$attempt->userid'", 'attempt ASC')) > 1) {
213 /// List of all this user's attempts for people who can see reports.
214 $attemptlist = array();
215 foreach ($attempts as $at) {
216 if ($at->id == $attempt->id) {
217 $attemptlist[] = '<strong>' . $at->attempt . '</strong>';
218 } else {
219 $attemptlist[] = '<a href="review.php?attempt=' . $at->id . $urloptions . ' ">' . $at->attempt . '</a>';
222 $rows[] = '<tr><th scope="row" class="cell">' . get_string('attempts', 'quiz') .
223 '</th><td class="cell">' . implode(', ', $attemptlist) . '</td></tr>';
226 /// Timing information.
227 $rows[] = '<tr><th scope="row" class="cell">' . get_string('startedon', 'quiz') .
228 '</th><td class="cell">' . userdate($attempt->timestart) . '</td></tr>';
229 if ($attempt->timefinish) {
230 $rows[] = '<tr><th scope="row" class="cell">' . get_string('completedon', 'quiz') . '</th><td class="cell">' .
231 userdate($attempt->timefinish) . '</td></tr>';
232 $rows[] = '<tr><th scope="row" class="cell">' . get_string('timetaken', 'quiz') . '</th><td class="cell">' .
233 $timetaken . '</td></tr>';
235 if (!empty($overtime)) {
236 $rows[] = '<tr><th scope="row" class="cell">' . get_string('overdue', 'quiz') . '</th><td class="cell">' . $overtime . '</td></tr>';
239 /// Show scores (if the user is allowed to see scores at the moment).
240 $grade = quiz_rescale_grade($attempt->sumgrades, $quiz);
241 if ($options->scores) {
242 if ($quiz->grade and $quiz->sumgrades) {
243 if($overtime) {
244 $result->sumgrades = "0";
245 $result->grade = "0.0";
248 /// Show raw marks only if they are different from the grade (like on the view page.
249 if ($quiz->grade != $quiz->sumgrades) {
250 $a = new stdClass;
251 $a->grade = round($attempt->sumgrades, $CFG->quiz_decimalpoints);
252 $a->maxgrade = $quiz->sumgrades;
253 $rows[] = '<tr><th scope="row" class="cell">' . get_string('marks', 'quiz') . '</th><td class="cell">' .
254 get_string('outofshort', 'quiz', $a) . '</td></tr>';
257 /// Now the scaled grade.
258 $a = new stdClass;
259 $a->grade = '<b>' . $grade . '</b>';
260 $a->maxgrade = $quiz->grade;
261 $a->percent = '<b>' . round(($attempt->sumgrades/$quiz->sumgrades)*100, 0) . '</b>';
262 $rows[] = '<tr><th scope="row" class="cell">' . get_string('grade') . '</th><td class="cell">' .
263 get_string('outofpercent', 'quiz', $a) . '</td></tr>';
267 /// Feedback if there is any, and the user is allowed to see it now.
268 $feedback = quiz_feedback_for_grade(quiz_rescale_grade(
269 $attempt->sumgrades, $quiz, false), $attempt->quiz);
270 if ($options->overallfeedback && $feedback) {
271 $rows[] = '<tr><th scope="row" class="cell">' . get_string('feedback', 'quiz') .
272 '</th><td class="cell">' . $feedback . '</td></tr>';
275 /// Now output the summary table, if there are any rows to be shown.
276 if (!empty($rows)) {
277 echo '<table class="generaltable generalbox quizreviewsummary"><tbody>', "\n";
278 echo implode("\n", $rows);
279 echo "\n</tbody></table>\n";
282 /// Print the navigation panel if required
283 $numpages = quiz_number_of_pages($attempt->layout);
284 if ($numpages > 1 and !$showall) {
285 print_paging_bar($numpages, $page, 1, 'review.php?attempt='.$attempt->id.'&amp;');
286 echo '<div class="controls"><a href="review.php?attempt='.$attempt->id.'&amp;showall=true">';
287 print_string('showall', 'quiz');
288 echo '</a></div>';
291 /// Print all the questions
292 $quiz->thispageurl = $CFG->wwwroot . '/mod/quiz/review.php?attempt=' . $attempt->id . $urloptions;
293 $quiz->cmid = $cm->id;
294 $number = quiz_first_questionnumber($attempt->layout, $pagelist);
295 foreach ($pagequestions as $i) {
296 if (!isset($questions[$i])) {
297 print_simple_box_start('center', '90%');
298 echo '<strong><font size="+1">' . $number . '</font></strong><br />';
299 notify(get_string('errormissingquestion', 'quiz', $i));
300 print_simple_box_end();
301 $number++; // Just guessing that the missing question would have lenght 1
302 continue;
304 $options->validation = QUESTION_EVENTVALIDATE === $states[$i]->event;
305 $options->history = ($isteacher and !$attempt->preview) ? 'all' : 'graded';
306 // Print the question
307 print_question($questions[$i], $states[$i], $number, $quiz, $options);
308 $number += $questions[$i]->length;
311 // Print the navigation panel if required
312 if ($numpages > 1 and !$showall) {
313 print_paging_bar($numpages, $page, 1, 'review.php?attempt='.$attempt->id.'&amp;');
316 // print javascript button to close the window, if necessary
317 if (!$isteacher) {
318 include('attempt_close_js.php');
321 if (empty($popup)) {
322 print_footer($course);