Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / quiz / review.php
blob97d9f39ea8478fa2094d7fe0104cf31f8539788b
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', get_context_instance(CONTEXT_MODULE, $cm->id));
42 $options = quiz_get_reviewoptions($quiz, $attempt, $context);
43 $popup = $isteacher ? 0 : $quiz->popup; // Controls whether this is shown in a javascript-protected window.
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 // Can't review if Student's may review ... Responses is turned on.
56 if (!$options->responses) {
57 if ($options->quizstate == QUIZ_STATE_IMMEDIATELY) {
58 $message = '';
59 } else if ($options->quizstate == QUIZ_STATE_OPEN && $quiz->timeclose &&
60 ($quiz->review & QUIZ_REVIEW_CLOSED & QUIZ_REVIEW_RESPONSES)) {
61 $message = get_string('noreviewuntil', 'quiz', userdate($quiz->timeclose));
62 } else {
63 $message = get_string('noreview', 'quiz');
65 if (empty($popup)) {
66 redirect('view.php?q=' . $quiz->id, $message);
67 } else {
68 ?><script type="text/javascript">
69 opener.document.location.reload();
70 self.close();
71 </script><?php
72 die();
77 add_to_log($course->id, "quiz", "review", "review.php?id=$cm->id&amp;attempt=$attempt->id", "$quiz->id", "$cm->id");
79 /// Load all the questions and states needed by this script
81 // load the questions needed by page
82 $pagelist = $showall ? quiz_questions_in_quiz($attempt->layout) : quiz_questions_on_page($attempt->layout, $page);
83 $sql = "SELECT q.*, i.grade AS maxgrade, i.id AS instance".
84 " FROM {$CFG->prefix}question q,".
85 " {$CFG->prefix}quiz_question_instances i".
86 " WHERE i.quiz = '$quiz->id' AND q.id = i.question".
87 " AND q.id IN ($pagelist)";
88 if (!$questions = get_records_sql($sql)) {
89 error('No questions found');
92 // Load the question type specific information
93 if (!get_question_options($questions)) {
94 error('Could not load question options');
97 // Restore the question sessions to their most recent states
98 // creating new sessions where required
99 if (!$states = get_question_states($questions, $quiz, $attempt)) {
100 error('Could not restore question sessions');
103 /// Work out appropriate title.
104 if ($isteacher and $attempt->userid == $USER->id) {
105 $strreviewtitle = get_string('reviewofpreview', 'quiz');
106 } else {
107 $strreviewtitle = get_string('reviewofattempt', 'quiz', $attempt->attempt);
110 /// Print the page header
111 $pagequestions = explode(',', $pagelist);
112 $headtags = get_html_head_contributions($pagequestions, $questions, $states);
113 if (!empty($popup)) {
114 define('MESSAGE_WINDOW', true); // This prevents the message window coming up
115 print_header($course->shortname.': '.format_string($quiz->name), '', '', '', $headtags, false, '', '', false, '');
116 /// Include Javascript protection for this page
117 include('protect_js.php');
118 } else {
119 $strupdatemodule = has_capability('moodle/course:manageactivities', $coursecontext)
120 ? update_module_button($cm->id, $course->id, get_string('modulename', 'quiz'))
121 : "";
122 get_string('reviewofattempt', 'quiz', $attempt->attempt);
123 $navigation = build_navigation($strreviewtitle, $cm);
124 print_header_simple(format_string($quiz->name), "", $navigation, "", $headtags, true, $strupdatemodule);
126 echo '<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>'; // for overlib
128 /// Print heading and tabs if this is part of a preview
129 if (has_capability('mod/quiz:preview', $context)) {
130 if ($attempt->userid == $USER->id) { // this is the report on a preview
131 $currenttab = 'preview';
132 } else {
133 $currenttab = 'reports';
134 $mode = '';
136 include('tabs.php');
139 /// Print heading.
140 print_heading(format_string($quiz->name));
141 if ($isteacher and $attempt->userid == $USER->id) {
142 // the teacher is at the end of a preview. Print button to start new preview
143 unset($buttonoptions);
144 $buttonoptions['q'] = $quiz->id;
145 $buttonoptions['forcenew'] = true;
146 echo '<div class="controls">';
147 print_single_button($CFG->wwwroot.'/mod/quiz/attempt.php', $buttonoptions, get_string('startagain', 'quiz'));
148 echo '</div>';
150 print_heading($strreviewtitle);
152 // print javascript button to close the window, if necessary
153 if (!$isteacher) {
154 include('attempt_close_js.php');
157 /// Work out some time-related things.
158 $timelimit = (int)$quiz->timelimit * 60;
159 $overtime = 0;
161 if ($attempt->timefinish) {
162 if ($timetaken = ($attempt->timefinish - $attempt->timestart)) {
163 if($timelimit && $timetaken > ($timelimit + 60)) {
164 $overtime = $timetaken - $timelimit;
165 $overtime = format_time($overtime);
167 $timetaken = format_time($timetaken);
168 } else {
169 $timetaken = "-";
171 } else {
172 $timetaken = get_string('unfinished', 'quiz');
175 /// Print summary table about the whole attempt.
176 /// First we assemble all the rows that are appopriate to the current situation in
177 /// an array, then later we only output the table if there are any rows to show.
178 $rows = array();
179 if ($attempt->userid <> $USER->id) {
180 $student = get_record('user', 'id', $attempt->userid);
181 $picture = print_user_picture($student, $course->id, $student->picture, false, true);
182 $rows[] = '<tr><th scope="row" class="cell">' . $picture . '</th><td class="cell"><a href="' .
183 $CFG->wwwroot . '/user/view.php?id=' . $student->id . '&amp;course=' . $course->id . '">' .
184 fullname($student, true) . '</a></td></tr>';
186 if (has_capability('mod/quiz:viewreports', $context) &&
187 count($attempts = get_records_select('quiz_attempts', "quiz = '$quiz->id' AND userid = '$attempt->userid'", 'attempt ASC')) > 1) {
188 /// List of all this user's attempts for people who can see reports.
189 $urloptions = '';
190 if ($showall) {
191 $urloptions .= '&amp;showall=true';
192 } else if ($page > 0) {
193 $urloptions .= '&amp;page=' . $page;
195 $attemptlist = array();
196 foreach ($attempts as $at) {
197 if ($at->id == $attempt->id) {
198 $attemptlist[] = '<strong>' . $at->attempt . '</strong>';
199 } else {
200 $attemptlist[] = '<a href="review.php?attempt=' . $at->id . $urloptions . ' ">' . $at->attempt . '</a>';
203 $rows[] = '<tr><th scope="row" class="cell">' . get_string('attempts', 'quiz') .
204 '</th><td class="cell">' . implode(', ', $attemptlist) . '</td></tr>';
207 /// Timing information.
208 $rows[] = '<tr><th scope="row" class="cell">' . get_string('startedon', 'quiz') .
209 '</th><td class="cell">' . userdate($attempt->timestart) . '</td></tr>';
210 if ($attempt->timefinish) {
211 $rows[] = '<tr><th scope="row" class="cell">' . get_string('completedon', 'quiz') . '</th><td class="cell">' .
212 userdate($attempt->timefinish) . '</td></tr>';
213 $rows[] = '<tr><th scope="row" class="cell">' . get_string('timetaken', 'quiz') . '</th><td class="cell">' .
214 $timetaken . '</td></tr>';
216 if (!empty($overtime)) {
217 $rows[] = '<tr><th scope="row" class="cell">' . get_string('overdue', 'quiz') . '</th><td class="cell">' . $overtime . '</td></tr>';
220 /// Show scores (if the user is allowed to see scores at the moment).
221 $grade = quiz_rescale_grade($attempt->sumgrades, $quiz);
222 if ($options->scores) {
223 if ($quiz->grade and $quiz->sumgrades) {
224 if($overtime) {
225 $result->sumgrades = "0";
226 $result->grade = "0.0";
229 /// Show raw marks only if they are different from the grade (like on the view page.
230 if ($quiz->grade != $quiz->sumgrades) {
231 $a = new stdClass;
232 $a->grade = round($attempt->sumgrades, $CFG->quiz_decimalpoints);
233 $a->maxgrade = $quiz->sumgrades;
234 $rows[] = '<tr><th scope="row" class="cell">' . get_string('marks', 'quiz') . '</th><td class="cell">' .
235 get_string('outofshort', 'quiz', $a) . '</td></tr>';
238 /// Now the scaled grade.
239 $a = new stdClass;
240 $a->grade = '<b>' . $grade . '</b>';
241 $a->maxgrade = $quiz->grade;
242 $a->percent = '<b>' . round(($attempt->sumgrades/$quiz->sumgrades)*100, 0) . '</b>';
243 $rows[] = '<tr><th scope="row" class="cell">' . get_string('grade') . '</th><td class="cell">' .
244 get_string('outofpercent', 'quiz', $a) . '</td></tr>';
248 /// Feedback if there is any, and the user is allowed to see it now.
249 $feedback = quiz_feedback_for_grade($grade, $attempt->quiz);
250 if ($options->overallfeedback && $feedback) {
251 $rows[] = '<tr><th scope="row" class="cell">' . get_string('feedback', 'quiz') .
252 '</th><td class="cell">' . $feedback . '</td></tr>';
255 /// Now output the summary table, if there are any rows to be shown.
256 if (!empty($rows)) {
257 echo '<table class="generaltable generalbox quizreviewsummary"><tbody>', "\n";
258 echo implode("\n", $rows);
259 echo "\n</tbody></table>\n";
262 /// Print the navigation panel if required
263 $numpages = quiz_number_of_pages($attempt->layout);
264 if ($numpages > 1 and !$showall) {
265 print_paging_bar($numpages, $page, 1, 'review.php?attempt='.$attempt->id.'&amp;');
266 echo '<div class="controls"><a href="review.php?attempt='.$attempt->id.'&amp;showall=true">';
267 print_string('showall', 'quiz');
268 echo '</a></div>';
271 /// Print all the questions
272 $number = quiz_first_questionnumber($attempt->layout, $pagelist);
273 foreach ($pagequestions as $i) {
274 if (!isset($questions[$i])) {
275 print_simple_box_start('center', '90%');
276 echo '<strong><font size="+1">' . $number . '</font></strong><br />';
277 notify(get_string('errormissingquestion', 'quiz', $i));
278 print_simple_box_end();
279 $number++; // Just guessing that the missing question would have lenght 1
280 continue;
282 $options->validation = QUESTION_EVENTVALIDATE === $states[$i]->event;
283 $options->history = ($isteacher and !$attempt->preview) ? 'all' : 'graded';
284 // Print the question
285 print_question($questions[$i], $states[$i], $number, $quiz, $options);
286 $number += $questions[$i]->length;
289 // Print the navigation panel if required
290 if ($numpages > 1 and !$showall) {
291 print_paging_bar($numpages, $page, 1, 'review.php?attempt='.$attempt->id.'&amp;');
294 // print javascript button to close the window, if necessary
295 if (!$isteacher) {
296 include('attempt_close_js.php');
299 if (empty($popup)) {
300 print_footer($course);