fixed typo for parameter value from treu to true.
[moodle.git] / mod / quiz / review.php
CommitLineData
83192608 1<?php
ee1fb969 2/**
f63a4ff2 3 * This page prints a review of a particular quiz attempt
4 *
4fc3d7e5 5 * @author Martin Dougiamas and many others.
f63a4ff2 6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7 * @package quiz
8 */
29d5d0b4 9
4fc3d7e5 10 require_once(dirname(__FILE__) . '/../../config.php');
c52c62d1 11 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
bbf4f440 12 require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
29d5d0b4 13
4fc3d7e5 14 $attemptid = required_param('attempt', PARAM_INT);
15 $page = optional_param('page', 0, PARAM_INT);
ee1fb969 16 $showall = optional_param('showall', 0, PARAM_BOOL);
29d5d0b4 17
a6855934 18 $url = new moodle_url('/mod/quiz/review.php', array('attempt'=>$attemptid));
55f599f0 19 if ($page !== 0) {
20 $url->param('page', $page);
21 }
22 if ($showall !== 0) {
23 $url->param('showall', $showall);
24 }
25 $PAGE->set_url($url);
26
990650f9 27 $attemptobj = quiz_attempt::create($attemptid);
4fc3d7e5 28
29/// Check login.
30 require_login($attemptobj->get_courseid(), false, $attemptobj->get_cm());
96c7d771 31 $attemptobj->check_review_capability();
77ed3ba5 32
c52c62d1 33/// Create an object to manage all the other (non-roles) access rules.
4fc3d7e5 34 $accessmanager = $attemptobj->get_access_manager(time());
35 $options = $attemptobj->get_review_options();
c52c62d1 36
c52c62d1 37/// Permissions checks for normal users who do not have quiz:viewreports capability.
4fc3d7e5 38 if (!$attemptobj->has_capability('mod/quiz:viewreports')) {
82fd2e8b 39 /// Can't review other users' attempts.
40 if (!$attemptobj->is_own_attempt()) {
41 quiz_error($attemptobj->get_quiz(), 'notyourattempt');
42 }
c52c62d1 43 /// Can't review during the attempt - send them back to the attempt page.
4fc3d7e5 44 if (!$attemptobj->is_finished()) {
45 redirect($attemptobj->attempt_url(0, $page));
29d5d0b4 46 }
c52c62d1 47 /// Can't review unless Students may review -> Responses option is turned on.
a4faed69 48 if (!$options->responses) {
4fc3d7e5 49 $accessmanager->back_to_view_page($attemptobj->is_preview_user(),
c52c62d1 50 $accessmanager->cannot_review_message($options));
34283aa8 51 }
29d5d0b4 52 }
53
f62040ed 54/// Load the questions and states needed by this page.
c52c62d1 55 if ($showall) {
4fc3d7e5 56 $questionids = $attemptobj->get_question_ids();
c52c62d1 57 } else {
4fc3d7e5 58 $questionids = $attemptobj->get_question_ids($page);
83192608 59 }
4fc3d7e5 60 $attemptobj->load_questions($questionids);
61 $attemptobj->load_question_states($questionids);
2d05b472 62
f62040ed 63/// Save the flag states, if they are being changed.
64 if ($options->flags == QUESTION_FLAGSEDITABLE && optional_param('savingflags', false, PARAM_BOOL)) {
82165a0c 65 require_sesskey();
f62040ed 66 $formdata = data_submitted();
67
68 question_save_flags($formdata, $attemptid, $questionids);
69 redirect($attemptobj->review_url(0, $page, $showall));
70 }
71
b55797b8 72/// Log this review.
73 add_to_log($attemptobj->get_courseid(), 'quiz', 'review', 'review.php?attempt=' .
74 $attemptobj->get_attemptid(), $attemptobj->get_quizid(), $attemptobj->get_cmid());
75
38e179a4 76/// Work out appropriate title.
b55797b8 77 if ($attemptobj->is_preview_user() && $attemptobj->is_own_attempt()) {
38e179a4 78 $strreviewtitle = get_string('reviewofpreview', 'quiz');
79 } else {
b9b3aa94 80 $strreviewtitle = get_string('reviewofattempt', 'quiz', $attemptobj->get_attempt_number());
38e179a4 81 }
82
d4a03c00 83/// Arrange for the navigation to be displayed.
d18675a8 84 $navbc = $attemptobj->get_navigation_panel('quiz_review_nav_panel', $page, $showall);
d4a03c00 85 $firstregion = reset($PAGE->blocks->get_regions());
86 $PAGE->blocks->add_pretend_block($navbc, $firstregion);
87
60409fe1
PS
88 $PAGE->requires->js('/lib/overlib/overlib.js', true);
89 $PAGE->requires->js('/lib/overlib/overlib_cssstyle.js', true);
458eb0d1 90
c52c62d1 91/// Print the page header
4fc3d7e5 92 $headtags = $attemptobj->get_html_head_contributions($page);
93 if ($accessmanager->securewindow_required($attemptobj->is_preview_user())) {
f330327c 94 $accessmanager->setup_secure_page($attemptobj->get_course()->shortname.': '.format_string($attemptobj->get_quiz_name()), $headtags);
7d4dfc48 95 } elseif ($accessmanager->safebrowser_required($attemptobj->is_preview_user())) {
a09f21d0 96 $PAGE->set_title($attemptobj->get_course()->shortname . ': '.format_string($attemptobj->get_quiz_name()));
97 $PAGE->set_cacheable(false);
98 echo $OUTPUT->header();
ee1fb969 99 } else {
03da0c39 100 $attemptobj->navigation($strreviewtitle);
101 $PAGE->set_title(format_string($attemptobj->get_quiz_name()));
03da0c39 102 echo $OUTPUT->header();
ee1fb969 103 }
b7f35820 104 echo '<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>'; // for overlib
105
c52c62d1 106/// Print tabs if they should be there.
4fc3d7e5 107 if ($attemptobj->is_preview_user()) {
b55797b8 108 if ($attemptobj->is_own_attempt()) {
0a5b58af 109 $currenttab = 'preview';
110 } else {
111 $currenttab = 'reports';
112 $mode = '';
113 }
ee1fb969 114 include('tabs.php');
ee1fb969 115 }
c52c62d1 116
117/// Print heading.
b55797b8 118 if ($attemptobj->is_preview_user() && $attemptobj->is_own_attempt()) {
78e7a3dd 119 $attemptobj->print_restart_preview_button();
99a1bf3d 120 }
90cd54cb 121 echo $OUTPUT->heading($strreviewtitle);
99a1bf3d 122
4fc3d7e5 123/// Summary table start ============================================================================
124
9c22b42e 125/// Work out some time-related things.
4fc3d7e5 126 $attempt = $attemptobj->get_attempt();
127 $quiz = $attemptobj->get_quiz();
235987c5 128 $overtime = 0;
d4961620 129
ee1fb969 130 if ($attempt->timefinish) {
131 if ($timetaken = ($attempt->timefinish - $attempt->timestart)) {
84e628a0 132 if($quiz->timelimit && $timetaken > ($quiz->timelimit + 60)) {
133 $overtime = $timetaken - $quiz->timelimit;
ee1fb969 134 $overtime = format_time($overtime);
135 }
136 $timetaken = format_time($timetaken);
137 } else {
138 $timetaken = "-";
d4961620 139 }
29d5d0b4 140 } else {
ee1fb969 141 $timetaken = get_string('unfinished', 'quiz');
29d5d0b4 142 }
9c22b42e 143
144/// Print summary table about the whole attempt.
145/// First we assemble all the rows that are appopriate to the current situation in
146/// an array, then later we only output the table if there are any rows to show.
147 $rows = array();
a733c4b9 148 if (!$attemptobj->get_quiz()->showuserpicture && $attemptobj->get_userid() <> $USER->id) {
149 /// If showuserpicture is true, the picture is shown elsewhere, so don't repeat it.
b9b3aa94 150 $student = $DB->get_record('user', array('id' => $attemptobj->get_userid()));
812dbaf7 151 $picture = $OUTPUT->user_picture($student, array('courseid'=>$attemptobj->get_courseid()));
9c22b42e 152 $rows[] = '<tr><th scope="row" class="cell">' . $picture . '</th><td class="cell"><a href="' .
b9b3aa94 153 $CFG->wwwroot . '/user/view.php?id=' . $student->id . '&amp;course=' . $attemptobj->get_courseid() . '">' .
9c22b42e 154 fullname($student, true) . '</a></td></tr>';
76cacec8 155 }
b55797b8 156 if ($attemptobj->has_capability('mod/quiz:viewreports')) {
157 $attemptlist = $attemptobj->links_to_other_attempts($attemptobj->review_url(0, $page, $showall));
158 if ($attemptlist) {
159 $rows[] = '<tr><th scope="row" class="cell">' . get_string('attempts', 'quiz') .
160 '</th><td class="cell">' . $attemptlist . '</td></tr>';
ee1fb969 161 }
ee1fb969 162 }
163
9c22b42e 164/// Timing information.
165 $rows[] = '<tr><th scope="row" class="cell">' . get_string('startedon', 'quiz') .
166 '</th><td class="cell">' . userdate($attempt->timestart) . '</td></tr>';
ee1fb969 167 if ($attempt->timefinish) {
9c22b42e 168 $rows[] = '<tr><th scope="row" class="cell">' . get_string('completedon', 'quiz') . '</th><td class="cell">' .
169 userdate($attempt->timefinish) . '</td></tr>';
170 $rows[] = '<tr><th scope="row" class="cell">' . get_string('timetaken', 'quiz') . '</th><td class="cell">' .
171 $timetaken . '</td></tr>';
ee1fb969 172 }
3a00dbfd 173 if (!empty($overtime)) {
9c22b42e 174 $rows[] = '<tr><th scope="row" class="cell">' . get_string('overdue', 'quiz') . '</th><td class="cell">' . $overtime . '</td></tr>';
d4961620 175 }
9c22b42e 176
177/// Show scores (if the user is allowed to see scores at the moment).
2b38499d 178 $grade = quiz_rescale_grade($attempt->sumgrades, $quiz, false);
15db443c 179 if ($options->scores) {
739b0711 180 if (quiz_has_grades($quiz)) {
15db443c 181 if($overtime) {
182 $result->sumgrades = "0";
183 $result->grade = "0.0";
184 }
77ed3ba5 185
9c22b42e 186 /// Show raw marks only if they are different from the grade (like on the view page.
187 if ($quiz->grade != $quiz->sumgrades) {
188 $a = new stdClass;
f88fb62c 189 $a->grade = quiz_format_grade($quiz, $attempt->sumgrades);
190 $a->maxgrade = quiz_format_grade($quiz, $attempt->sumgrades);
9c22b42e 191 $rows[] = '<tr><th scope="row" class="cell">' . get_string('marks', 'quiz') . '</th><td class="cell">' .
192 get_string('outofshort', 'quiz', $a) . '</td></tr>';
193 }
194
195 /// Now the scaled grade.
3b48ba9d 196 $a = new stdClass;
2b38499d 197 $a->grade = '<b>' . quiz_format_grade($quiz, $grade) . '</b>';
f88fb62c 198 $a->maxgrade = quiz_format_grade($quiz, $quiz->grade);
9c22b42e 199 $a->percent = '<b>' . round(($attempt->sumgrades/$quiz->sumgrades)*100, 0) . '</b>';
200 $rows[] = '<tr><th scope="row" class="cell">' . get_string('grade') . '</th><td class="cell">' .
201 get_string('outofpercent', 'quiz', $a) . '</td></tr>';
d4961620 202 }
ee1fb969 203 }
9c22b42e 204
205/// Feedback if there is any, and the user is allowed to see it now.
206 $feedback = quiz_feedback_for_grade($grade, $attempt->quiz);
77ed3ba5 207 if ($options->overallfeedback && $feedback) {
9c22b42e 208 $rows[] = '<tr><th scope="row" class="cell">' . get_string('feedback', 'quiz') .
209 '</th><td class="cell">' . $feedback . '</td></tr>';
210 }
211
212/// Now output the summary table, if there are any rows to be shown.
213 if (!empty($rows)) {
214 echo '<table class="generaltable generalbox quizreviewsummary"><tbody>', "\n";
215 echo implode("\n", $rows);
216 echo "\n</tbody></table>\n";
9bc2d82a 217 }
218
4fc3d7e5 219/// Summary table end ==============================================================================
220
f62040ed 221/// Form for saving flags if necessary.
222 if ($options->flags == QUESTION_FLAGSEDITABLE) {
d4ad9adf 223 echo '<form action="' . s($attemptobj->review_url(0, $page, $showall)) .
f62040ed 224 '" method="post"><div>';
225 echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
226 }
227
228/// Print all the questions.
4fc3d7e5 229 if ($showall) {
baef998b 230 $thispage = 'all';
231 $lastpage = true;
232 } else {
233 $thispage = $page;
234 $lastpage = $attemptobj->is_last_page($page);
ee1fb969 235 }
baef998b 236 foreach ($attemptobj->get_question_ids($thispage) as $id) {
aafdb447 237 $attemptobj->print_question($id, true, $attemptobj->review_url($id, $page, $showall));
29d5d0b4 238 }
239
f62040ed 240/// Close form if we opened it.
241 if ($options->flags == QUESTION_FLAGSEDITABLE) {
242 echo '<div class="submitbtns">' . "\n" .
243 '<input type="submit" id="savingflagssubmit" name="savingflags" value="' .
244 get_string('saveflags', 'question') . '" />' .
245 "</div>\n" .
29305001 246 "\n</div></form>\n";
cf615522 247 $PAGE->requires->js_function_call('question_flag_changer.init_flag_save_form', array('savingflagssubmit'));
f62040ed 248 }
249
baef998b 250/// Print a link to the next page.
251 echo '<div class="submitbtns">';
252 if ($lastpage) {
4fc3d7e5 253 $accessmanager->print_finish_review_link($attemptobj->is_preview_user());
baef998b 254 } else {
d18675a8 255 echo link_arrow_right(get_string('next'), s($attemptobj->review_url(0, $page + 1)));
ee1fb969 256 }
baef998b 257 echo "</div>";
258
867847e3 259 echo $OUTPUT->footer();
83192608 260