MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / quiz / view.php
blob36cf5bc041c099d4f11cb06e6c52d6010251a918
1 <?php // $Id$
3 // This page prints a particular instance of quiz
5 require_once("../../config.php");
6 require_once("locallib.php");
7 require_once($CFG->libdir.'/blocklib.php');
8 require_once('pagelib.php');
10 if (!empty($THEME->customcorners)) {
11 require_once($CFG->dirroot.'/lib/custom_corners_lib.php');
14 $id = optional_param('id', 0, PARAM_INT); // Course Module ID, or
15 $q = optional_param('q', 0, PARAM_INT); // quiz ID
16 $edit = optional_param('edit', -1, PARAM_BOOL);
18 if ($id) {
19 if (! $cm = get_coursemodule_from_id('quiz', $id)) {
20 error("There is no coursemodule with id $id");
22 if (! $course = get_record("course", "id", $cm->course)) {
23 error("Course is misconfigured");
25 if (! $quiz = get_record("quiz", "id", $cm->instance)) {
26 error("The quiz with id $cm->instance corresponding to this coursemodule $id is missing");
28 } else {
29 if (! $quiz = get_record("quiz", "id", $q)) {
30 error("There is no quiz with id $q");
32 if (! $course = get_record("course", "id", $quiz->course)) {
33 error("The course with id $quiz->course that the quiz with id $q belongs to is missing");
35 if (! $cm = get_coursemodule_from_instance("quiz", $quiz->id, $course->id)) {
36 error("The course module for the quiz with id $q is missing");
40 // Check login and get context.
41 require_login($course->id, false, $cm);
42 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
44 // if no questions have been set up yet redirect to edit.php
45 if (!$quiz->questions and has_capability('mod/quiz:manage', $context)) {
46 redirect('edit.php?cmid='.$cm->id);
49 add_to_log($course->id, "quiz", "view", "view.php?id=$cm->id", $quiz->id, $cm->id);
51 // Initialize $PAGE, compute blocks
52 $PAGE = page_create_instance($quiz->id);
53 $pageblocks = blocks_setup($PAGE);
54 $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]), 210);
56 // Print the page header
57 if ($edit != -1 and $PAGE->user_allowed_editing()) {
58 $USER->editing = $edit;
61 //only check pop ups if the user is not a teacher, and popup is set
63 $bodytags = (has_capability('mod/quiz:attempt', $context) && $quiz->popup)?'onload="popupchecker(\'' . get_string('popupblockerwarning', 'quiz') . '\');"':'';
64 $PAGE->print_header($course->shortname.': %fullname%','',$bodytags);
66 echo '<table id="layout-table"><tr>';
68 if(!empty($CFG->showblocksonmodpages) && (blocks_have_content($pageblocks, BLOCK_POS_LEFT) || $PAGE->user_is_editing())) {
69 echo '<td style="width: '.$blocks_preferred_width.'px;" id="left-column">';
70 if (!empty($THEME->customcorners)) print_custom_corners_start();
71 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
72 if (!empty($THEME->customcorners)) print_custom_corners_end();
73 echo '</td>';
76 echo '<td id="middle-column">';
77 if (!empty($THEME->customcorners)) print_custom_corners_start();
79 // Print the main part of the page
81 // Print heading and tabs (if there is more than one).
82 $currenttab = 'info';
83 include('tabs.php');
85 // Print quiz name
87 print_heading(format_string($quiz->name));
89 if (has_capability('mod/quiz:view', $context)) {
91 // Print quiz description
92 if (trim(strip_tags($quiz->intro))) {
93 $formatoptions->noclean = true;
94 print_box(format_text($quiz->intro, FORMAT_MOODLE, $formatoptions), 'generalbox', 'intro');
97 echo '<div class="quizinfo">';
99 // Print information about number of attempts and grading method.
100 if ($quiz->attempts > 1) {
101 echo "<p>".get_string("attemptsallowed", "quiz").": $quiz->attempts</p>";
103 if ($quiz->attempts != 1) {
104 echo "<p>".get_string("grademethod", "quiz").": ".quiz_get_grading_option_name($quiz->grademethod)."</p>";
107 // Print information about timings.
108 $timenow = time();
109 $available = ($quiz->timeopen < $timenow and ($timenow < $quiz->timeclose or !$quiz->timeclose));
110 if ($available) {
111 if ($quiz->timelimit) {
112 echo "<p>".get_string("quiztimelimit","quiz", format_time($quiz->timelimit * 60))."</p>";
114 quiz_view_dates($quiz);
115 } else if ($timenow < $quiz->timeopen) {
116 echo "<p>".get_string("quiznotavailable", "quiz", userdate($quiz->timeopen))."</p>";
117 } else {
118 echo "<p>".get_string("quizclosed", "quiz", userdate($quiz->timeclose))."</p>";
120 echo '</div>';
121 } else {
122 $available = false;
125 // Show number of attempts summary to those who can view reports.
126 if (has_capability('mod/quiz:viewreports', $context)) {
127 if ($a->attemptnum = count_records('quiz_attempts', 'quiz', $quiz->id, 'preview', 0)) {
128 $a->studentnum = count_records_select('quiz_attempts', "quiz = '$quiz->id' AND preview = '0'", 'COUNT(DISTINCT userid)');
129 $a->studentstring = $course->students;
131 notify("<a href=\"report.php?mode=overview&amp;id=$cm->id\">".get_string('numattempts', 'quiz', $a).'</a>');
135 // Guests can't do a quiz, so offer them a choice of logging in or going back.
136 if (isguestuser()) {
137 $loginurl = $CFG->wwwroot.'/login/index.php';
138 if (!empty($CFG->loginhttps)) {
139 $loginurl = str_replace('http:','https:', $loginurl);
142 notice_yesno('<p>' . get_string('guestsno', 'quiz') . "</p>\n\n</p>" .
143 get_string('liketologin') . '</p>', $loginurl, get_referer(false));
144 finish_page($course);
147 if (!(has_capability('mod/quiz:attempt', $context) || has_capability('mod/quiz:preview', $context))) {
148 print_box('<p>' . get_string('youneedtoenrol', 'quiz') . '</p><p>' .
149 print_continue($CFG->wwwroot . '/course/view.php?id=' . $course->id, true) .
150 '</p>', 'generalbox', 'notice');
151 finish_page($course);
154 // Get this user's attempts.
155 $attempts = quiz_get_user_attempts($quiz->id, $USER->id);
156 $unfinished = false;
157 if ($unfinishedattempt = quiz_get_user_attempt_unfinished($quiz->id, $USER->id)) {
158 $attempts[] = $unfinishedattempt;
159 $unfinished = true;
161 $numattempts = count($attempts);
163 $mygrade = quiz_get_best_grade($quiz, $USER->id);
165 // Print table with existing attempts
166 if ($attempts) {
168 print_heading('Summary of your previous attempts');
170 // Get some strings.
171 $strattempt = get_string("attempt", "quiz");
172 $strtimetaken = get_string("timetaken", "quiz");
173 $strtimecompleted = get_string("timecompleted", "quiz");
174 $strgrade = get_string("grade");
175 $strmarks = get_string('marks', 'quiz');
176 $strfeedback = get_string('feedback', 'quiz');
178 // Work out which columns we need, taking account what data is available in each attempt.
179 list($someoptions, $alloptions) = quiz_get_combined_reviewoptions($quiz, $attempts, $context);
181 $gradecolumn = $someoptions->scores && $quiz->grade && $quiz->sumgrades;
182 $markcolumn = $gradecolumn && ($quiz->grade != $quiz->sumgrades);
183 $overallstats = $alloptions->scores;
185 $feedbackcolumn = quiz_has_feedback($quiz->id);
186 $overallfeedback = $feedbackcolumn && $alloptions->overallfeedback;
188 // prepare table header
189 $table->class = 'generaltable quizattemptsummary';
190 $table->head = array($strattempt, $strtimecompleted);
191 $table->align = array("center", "left");
192 $table->size = array("", "");
193 if ($markcolumn) {
194 $table->head[] = "$strmarks / $quiz->sumgrades";
195 $table->align[] = 'center';
196 $table->size[] = '';
198 if ($gradecolumn) {
199 $table->head[] = "$strgrade / $quiz->grade";
200 $table->align[] = 'center';
201 $table->size[] = '';
203 if ($feedbackcolumn) {
204 $table->head[] = $strfeedback;
205 $table->align[] = 'left';
206 $table->size[] = '';
208 if (isset($quiz->showtimetaken)) {
209 $table->head[] = $strtimetaken;
210 $table->align[] = 'left';
211 $table->size[] = '';
214 // One row for each attempt
215 foreach ($attempts as $attempt) {
216 $attemptoptions = quiz_get_reviewoptions($quiz, $attempt, $context);
217 $row = array();
219 // Add the attempt number, making it a link, if appropriate.
220 if ($attempt->preview) {
221 $row[] = make_review_link(get_string('preview', 'quiz'), $quiz, $attempt);
222 } else {
223 $row[] = make_review_link($attempt->attempt, $quiz, $attempt);
226 // prepare strings for time taken and date completed
227 $timetaken = '';
228 $datecompleted = '';
229 if ($attempt->timefinish > 0) {
230 // attempt has finished
231 $timetaken = format_time($attempt->timefinish - $attempt->timestart);
232 $datecompleted = userdate($attempt->timefinish);
233 } else if ($available) {
234 // The attempt is still in progress.
235 $timetaken = format_time(time() - $attempt->timestart);
236 $datecompleted = '';
237 } else if ($quiz->timeclose) {
238 // The attempt was not completed but is also not available any more becuase the quiz is closed.
239 $timetaken = format_time($quiz->timeclose - $attempt->timestart);
240 $datecompleted = userdate($quiz->timeclose);
241 } else {
242 // Something wheird happened.
243 $timetaken = '';
244 $datecompleted = '';
246 $row[] = $datecompleted;
248 if ($markcolumn) {
249 if ($attemptoptions->scores) {
250 $row[] = make_review_link(round($attempt->sumgrades, $quiz->decimalpoints), $quiz, $attempt);
251 } else {
252 $row[] = '';
256 // Ouside the if because we may be showing feedback but not grades.
257 $attemptgrade = quiz_rescale_grade($attempt->sumgrades, $quiz);
259 if ($gradecolumn) {
260 if ($attemptoptions->scores) {
261 $formattedgrade = $attemptgrade;
262 // highlight the highest grade if appropriate
263 if ($overallstats && $numattempts > 1 && !is_null($mygrade) && $attemptgrade == $mygrade && $quiz->grademethod == QUIZ_GRADEHIGHEST) {
264 $table->rowclass[$attempt->attempt] = 'bestrow';
267 $row[] = make_review_link($formattedgrade, $quiz, $attempt);
268 } else {
269 $row[] = '';
273 if ($feedbackcolumn) {
274 if ($attemptoptions->overallfeedback) {
275 $row[] = quiz_feedback_for_grade($attemptgrade, $quiz->id);
276 } else {
277 $row[] = '';
281 if (isset($quiz->showtimetaken)) {
282 $row[] = $timetaken;
285 $table->data[$attempt->attempt] = $row;
286 } // End of loop over attempts.
287 print_table($table);
290 // Print information about the student's best score for this quiz if possible.
291 $moreattempts = $unfinished || $numattempts < $quiz->attempts || $quiz->attempts == 0;
292 if (!$moreattempts) {
293 print_heading(get_string("nomoreattempts", "quiz"));
296 if ($numattempts && $quiz->sumgrades && !is_null($mygrade)) {
297 if ($overallstats) {
298 if ($available && $moreattempts) {
299 $a = new stdClass;
300 $a->method = quiz_get_grading_option_name($quiz->grademethod);
301 $a->mygrade = $mygrade;
302 $a->quizgrade = $quiz->grade;
303 print_heading(get_string('gradesofar', 'quiz', $a));
304 } else {
305 print_heading(get_string('yourfinalgradeis', 'quiz', "$mygrade / $quiz->grade"));
309 if ($overallfeedback) {
310 echo '<p class="quizgradefeedback">'.quiz_feedback_for_grade($mygrade, $quiz->id).'</p>';
314 // Print a button to start/continue an attempt, if appropriate.
315 if (!$quiz->questions) {
316 print_heading(get_string("noquestions", "quiz"));
318 } else if ($available && $moreattempts) {
319 echo "<br />";
320 echo "<div class=\"quizattempt\">";
322 if ($unfinished) {
323 if (has_capability('mod/quiz:preview', $context)) {
324 $buttontext = get_string('continuepreview', 'quiz');
325 } else {
326 $buttontext = get_string('continueattemptquiz', 'quiz');
328 } else {
330 // Work out the appropriate button caption.
331 if (has_capability('mod/quiz:preview', $context)) {
332 $buttontext = get_string('previewquiznow', 'quiz');
333 } else if ($numattempts == 0) {
334 $buttontext = get_string('attemptquiznow', 'quiz');
335 } else {
336 $buttontext = get_string('reattemptquiz', 'quiz');
339 // Work out if the quiz is temporarily unavailable because of the delay option.
340 if (!empty($attempts)) {
341 $tempunavailable = '';
342 $lastattempt = end($attempts);
343 $lastattempttime = $lastattempt->timefinish;
344 if ($numattempts == 1 && $quiz->delay1 && $timenow <= $lastattempttime + $quiz->delay1) {
345 $tempunavailable = get_string('temporaryblocked', 'quiz') .
346 ' <strong>'. userdate($lastattempttime + $quiz->delay1). '</strong>';
347 } else if ($numattempts > 1 && $quiz->delay2 && $timenow <= $lastattempttime + $quiz->delay2) {
348 $tempunavailable = get_string('temporaryblocked', 'quiz') .
349 ' <strong>'. userdate($lastattempttime + $quiz->delay2). '</strong>';
352 // If so, display a message and prevent the start button from appearing.
353 if ($tempunavailable) {
354 print_simple_box($tempunavailable, "center");
355 print_continue($CFG->wwwroot . '/course/view.php?id=' . $course->id);
356 $buttontext = '';
361 // Actually print the start button.
362 if ($buttontext) {
363 $buttontext = htmlspecialchars($buttontext, ENT_QUOTES);
365 // Do we need a confirm javascript alert?
366 if ($unfinished) {
367 $strconfirmstartattempt = '';
368 } else if ($quiz->timelimit && $quiz->attempts) {
369 $strconfirmstartattempt = addslashes(get_string('confirmstartattempttimelimit','quiz', $quiz->attempts));
370 } else if ($quiz->timelimit) {
371 $strconfirmstartattempt = addslashes(get_string('confirmstarttimelimit','quiz'));
372 } else if ($quiz->attempts) {
373 $strconfirmstartattempt = addslashes(get_string('confirmstartattemptlimit','quiz', $quiz->attempts));
374 } else {
375 $strconfirmstartattempt = '';
378 // Prepare options depending on whether the quiz should be a popup.
379 if (!empty($quiz->popup)) {
380 $window = 'quizpopup';
381 $windowoptions = "left=0, top=0, height='+window.screen.height+', " .
382 "width='+window.screen.width+', channelmode=yes, fullscreen=yes, " .
383 "scrollbars=yes, resizeable=no, directories=no, toolbar=no, " .
384 "titlebar=no, location=no, status=no, menubar=no";
385 } else {
386 $window = '_self';
387 $windowoptions = '';
390 // Determine the URL to use.
391 $attempturl = "attempt.php?id=$cm->id";
392 if (!empty($CFG->usesid) && !isset($_COOKIE[session_name()])) {
393 $attempturl = sid_process_url($attempturl);
396 echo '<input type="button" value="'.$buttontext.'" onclick="javascript:';
397 if ($strconfirmstartattempt) {
398 echo "if (confirm('".addslashes_js($strconfirmstartattempt)."')) ";
400 echo "window.open('$attempturl','$window','$windowoptions');", '" />';
403 <noscript>
404 <div>
405 <?php print_heading(get_string('noscript', 'quiz')); ?>
406 </div>
407 </noscript>
408 <?php
411 echo "</div>\n";
412 } else {
413 print_continue($CFG->wwwroot . '/course/view.php?id=' . $course->id);
416 // Should we not be seeing if we need to print right-hand-side blocks?
418 finish_page($course);
420 // Utility functions =================================================================
422 function finish_page($course) {
423 if (!empty($THEME->customcorners)) print_custom_corners_end();
424 echo '</td></tr></table>';
425 print_footer($course);
426 exit;
429 /** Make some text into a link to review the quiz, if that is appropriate. */
430 function make_review_link($linktext, $quiz, $attempt) {
431 // If not even responses are to be shown in review then we don't allow any review
432 if (!($quiz->review & QUIZ_REVIEW_RESPONSES)) {
433 return $linktext;
436 // If the quiz is still open, are reviews allowed?
437 if ((!$quiz->timeclose or time() < $quiz->timeclose) and !($quiz->review & QUIZ_REVIEW_OPEN)) {
438 // If not, don't link.
439 return $linktext;
442 // If the quiz is closed, are reviews allowed?
443 if (($quiz->timeclose and time() > $quiz->timeclose) and !($quiz->review & QUIZ_REVIEW_CLOSED)) {
444 // If not, don't link.
445 return $linktext;
448 // If the attempt is still open, don't link.
449 if (!$attempt->timefinish) {
450 return $linktext;
453 $url = "review.php?q=$quiz->id&amp;attempt=$attempt->id";
454 if ($quiz->popup) {
455 $windowoptions = "left=0, top=0, channelmode=yes, fullscreen=yes, scrollbars=yes, resizeable=no, directories=no, toolbar=no, titlebar=no, location=no, status=no, menubar=no";
456 return link_to_popup_window('/mod/quiz/' . $url, 'quizpopup', $linktext, '+window.screen.height+', '+window.screen.width+', '', $windowoptions, true);
457 } else {
458 return "<a href='$url'>$linktext</a>";