adding some strings
[moodle-linuxchix.git] / mod / quiz / review.php
blobfc60e8bb3f164d4795def1c8819fdd348f088439
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 if (!has_capability('mod/quiz:viewreports', $context)) {
50 if (!$attempt->timefinish) {
51 redirect('attempt.php?q='.$quiz->id);
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)) {
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();
66 if ((time() - $attempt->timefinish) > 120) { // always allow review right after attempt
67 if ((!$quiz->timeclose or time() < $quiz->timeclose) and !($quiz->review & QUIZ_REVIEW_OPEN)) {
68 redirect('view.php?q='.$quiz->id, get_string("noreviewuntil", "quiz", userdate($quiz->timeclose)));
70 if ($quiz->timeclose and time() >= $quiz->timeclose and !($quiz->review & QUIZ_REVIEW_CLOSED)) {
71 redirect('view.php?q='.$quiz->id, get_string("noreview", "quiz"));
74 if ($attempt->userid != $USER->id) {
75 error("This is not your attempt!", 'view.php?q='.$quiz->id);
79 add_to_log($course->id, "quiz", "review", "review.php?id=$cm->id&amp;attempt=$attempt->id", "$quiz->id", "$cm->id");
81 /// Load all the questions and states needed by this script
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');
94 // Load the question type specific information
95 if (!get_question_options($questions)) {
96 error('Could not load question options');
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');
105 /// Print the page header
107 $strquizzes = get_string("modulenameplural", "quiz");
108 $strreview = get_string("review", "quiz");
109 $strscore = get_string("score", "quiz");
110 $strgrade = get_string("grade");
111 $strbestgrade = get_string("bestgrade", "quiz");
112 $strtimetaken = get_string("timetaken", "quiz");
113 $strtimecompleted = get_string("completedon", "quiz");
114 $stroverdue = get_string("overdue", "quiz");
116 $pagequestions = explode(',', $pagelist);
117 $headtags = get_html_head_contributions($pagequestions, $questions, $states);
118 if (!empty($popup)) {
119 define('MESSAGE_WINDOW', true); // This prevents the message window coming up
120 print_header($course->shortname.': '.format_string($quiz->name), '', '', '', $headtags, false, '', '', false, '');
121 /// Include Javascript protection for this page
122 include('protect_js.php');
123 } else {
124 $strupdatemodule = has_capability('moodle/course:manageactivities', $coursecontext)
125 ? update_module_button($cm->id, $course->id, get_string('modulename', 'quiz'))
126 : "";
128 $navlinks = array();
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');
133 $navigation = build_navigation($navlinks);
135 print_header_simple(format_string($quiz->name), "", $navigation, "", $headtags, true, $strupdatemodule);
137 echo '<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>'; // for overlib
139 /// Print heading and tabs if this is part of a preview
140 if (has_capability('mod/quiz:preview', $context)) {
141 if ($attempt->userid == $USER->id) { // this is the report on a preview
142 $currenttab = 'preview';
143 } else {
144 $currenttab = 'reports';
145 $mode = '';
147 include('tabs.php');
148 } else {
149 print_heading(format_string($quiz->name));
151 if ($isteacher and $attempt->userid == $USER->id) {
152 // the teacher is at the end of a preview. Print button to start new preview
153 unset($buttonoptions);
154 $buttonoptions['q'] = $quiz->id;
155 $buttonoptions['forcenew'] = true;
156 echo '<div class="controls">';
157 print_single_button($CFG->wwwroot.'/mod/quiz/attempt.php', $buttonoptions, get_string('startagain', 'quiz'));
158 echo '</div>';
159 } else { // print number of the attempt
160 print_heading(get_string('reviewofattempt', 'quiz', $attempt->attempt));
163 // print javascript button to close the window, if necessary
164 if (!$isteacher) {
165 include('attempt_close_js.php');
168 /// Print infobox
170 $timelimit = (int)$quiz->timelimit * 60;
171 $overtime = 0;
173 if ($attempt->timefinish) {
174 if ($timetaken = ($attempt->timefinish - $attempt->timestart)) {
175 if($timelimit && $timetaken > ($timelimit + 60)) {
176 $overtime = $timetaken - $timelimit;
177 $overtime = format_time($overtime);
179 $timetaken = format_time($timetaken);
180 } else {
181 $timetaken = "-";
183 } else {
184 $timetaken = get_string('unfinished', 'quiz');
186 echo '<table class="generaltable generalbox quizreviewsummary"><tbody>';
187 if ($attempt->userid <> $USER->id) {
188 $student = get_record('user', 'id', $attempt->userid);
189 $picture = print_user_picture($student->id, $course->id, $student->picture, false, true);
190 echo '<tr><th scope="row" class="cell">', $picture, '</th><td class="cell"><a href="', $CFG->wwwroot,
191 '/user/view.php?id=', $student->id, '&amp;course='.$course->id.'">',
192 fullname($student, true), '</a></td></tr>';
194 if (has_capability('mod/quiz:grade', $context) and
195 count($attempts = get_records_select('quiz_attempts', "quiz = '$quiz->id' AND userid = '$attempt->userid'", 'attempt ASC')) > 1) {
196 // print list of attempts
197 $attemptlist = '';
198 foreach ($attempts as $at) {
199 $attemptlist .= ($at->id == $attempt->id)
200 ? '<strong>'.$at->attempt.'</strong>, '
201 : '<a href="review.php?attempt='.$at->id.($showall?'&amp;showall=true':'').'">'.$at->attempt.'</a>, ';
203 echo '<tr><th scope="row" class="cell">', get_string('attempts', 'quiz'), '</th><td class="cell">',
204 trim($attemptlist, ' ,'), '</td></tr>';
207 echo '<tr><th scope="row" class="cell">', get_string('startedon', 'quiz'), '</th><td class="cell">',
208 userdate($attempt->timestart), '</td></tr>';
209 if ($attempt->timefinish) {
210 echo '<tr><th scope="row" class="cell">', $strtimecompleted, '</th><td class="cell">',
211 userdate($attempt->timefinish), '</td></tr>';
212 echo '<tr><th scope="row" class="cell">', $strtimetaken, '</th><td class="cell">',
213 $timetaken, '</td></tr>';
215 if (!empty($overtime)) {
216 echo '<tr><th scope="row" class="cell">', $stroverdue, '</th><td class="cell">',$overtime, '</td></tr>';
218 //if the student is allowed to see their score
219 if ($options->scores) {
220 if ($quiz->grade and $quiz->sumgrades) {
221 if($overtime) {
222 $result->sumgrades = "0";
223 $result->grade = "0.0";
226 $a = new stdClass;
227 $percentage = round(($attempt->sumgrades/$quiz->sumgrades)*100, 0);
228 $a->grade = $grade;
229 $a->maxgrade = $quiz->grade;
230 $rawscore = round($attempt->sumgrades, $CFG->quiz_decimalpoints);
231 echo '<tr><th scope="row" class="cell">', $strscore, '</th><td class="cell">',
232 "$rawscore/$quiz->sumgrades ($percentage %)", '</td></tr>';
233 echo '<tr><th scope="row" class="cell">', $strgrade, '</th><td class="cell">',
234 get_string('outof', 'quiz', $a), '</td></tr>';
237 if ($options->overallfeedback && $feedback) {
238 echo '<tr><th scope="row" class="cell">', get_string('feedback', 'quiz'), '</th><td class="cell">',
239 $feedback, '</td></tr>';
241 echo '</tbody></table>';
243 /// Print the navigation panel if required
244 $numpages = quiz_number_of_pages($attempt->layout);
245 if ($numpages > 1 and !$showall) {
246 print_paging_bar($numpages, $page, 1, 'review.php?attempt='.$attempt->id.'&amp;');
247 echo '<div class="controls"><a href="review.php?attempt='.$attempt->id.'&amp;showall=true">';
248 print_string('showall', 'quiz');
249 echo '</a></div>';
252 /// Print all the questions
254 $number = quiz_first_questionnumber($attempt->layout, $pagelist);
255 foreach ($pagequestions as $i) {
256 if (!isset($questions[$i])) {
257 print_simple_box_start('center', '90%');
258 echo '<strong><font size="+1">' . $number . '</font></strong><br />';
259 notify(get_string('errormissingquestion', 'quiz', $i));
260 print_simple_box_end();
261 $number++; // Just guessing that the missing question would have lenght 1
262 continue;
264 $options->validation = QUESTION_EVENTVALIDATE === $states[$i]->event;
265 $options->history = ($isteacher and !$attempt->preview) ? 'all' : 'graded';
266 // Print the question
267 print_question($questions[$i], $states[$i], $number, $quiz, $options);
268 $number += $questions[$i]->length;
271 // Print the navigation panel if required
272 if ($numpages > 1 and !$showall) {
273 print_paging_bar($numpages, $page, 1, 'review.php?attempt='.$attempt->id.'&amp;');
276 // print javascript button to close the window, if necessary
277 if (!$isteacher) {
278 include('attempt_close_js.php');
281 if (empty($popup)) {
282 print_footer($course);