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
);
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");
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();
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).
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_GRADE_METHOD[$quiz->grademethod
]."</p>";
107 // Print information about timings.
109 $available = ($quiz->timeopen
< $timenow and ($timenow < $quiz->timeclose
or !$quiz->timeclose
));
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>";
118 echo "<p>".get_string("quizclosed", "quiz", userdate($quiz->timeclose
))."</p>";
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&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.
137 // TODO, work out what to do about this under roles and permissions.
138 // You have to be logged in to do a quiz, because attempts are tied to
139 // userid, and so if guests were allowed to attempt quizzes, all guests
140 // would see all attempts, and it would be confusing.
142 // So for courses that allow guest access, it is good to offer people an easy
143 // way to log in at this point if they have got this far before logging in.
145 $loginurl = $CFG->wwwroot
.'/login/index.php';
146 if (!empty($CFG->loginhttps
)) {
147 $loginurl = str_replace('http:','https:', $loginurl);
150 notice_yesno('<p>' . get_string('guestsno', 'quiz') . "</p>\n\n</p>" .
151 get_string('liketologin') . '</p>', $loginurl, get_referer(false));
154 if (has_capability('mod/quiz:attempt', $context)) {
156 // Get this user's attempts.
157 $attempts = quiz_get_user_attempts($quiz->id
, $USER->id
);
159 if ($unfinishedattempt = quiz_get_user_attempt_unfinished($quiz->id
, $USER->id
)) {
160 $attempts[] = $unfinishedattempt;
163 $numattempts = count($attempts);
165 $mygrade = quiz_get_best_grade($quiz, $USER->id
);
168 $strattempt = get_string("attempt", "quiz");
169 $strtimetaken = get_string("timetaken", "quiz");
170 $strtimecompleted = get_string("timecompleted", "quiz");
171 $strgrade = get_string("grade");
172 $strmarks = get_string('marks', 'quiz');
173 $strfeedback = get_string('feedback', 'quiz');
175 // Print table with existing attempts
178 print_heading('Summary of your previous attempts');
180 // Work out which columns we need, taking account what data is available in each attempt.
181 list($someoptions, $alloptions) = quiz_get_combined_reviewoptions($quiz, $attempts, $context);
183 $gradecolumn = $someoptions->scores
&& $quiz->grade
&& $quiz->sumgrades
;
184 $markcolumn = $gradecolumn && ($quiz->grade
!= $quiz->sumgrades
);
185 $overallstats = $alloptions->scores
;
187 $feedbackcolumn = quiz_has_feedback($quiz->id
);
188 $overallfeedback = $feedbackcolumn && $alloptions->overallfeedback
;
190 // prepare table header
191 $table->class = 'generaltable quizattemptsummary';
192 $table->head
= array($strattempt, $strtimecompleted);
193 $table->align
= array("center", "left");
194 $table->size
= array("", "");
196 $table->head
[] = "$strmarks / $quiz->sumgrades";
197 $table->align
[] = 'center';
201 $table->head
[] = "$strgrade / $quiz->grade";
202 $table->align
[] = 'center';
205 if ($feedbackcolumn) {
206 $table->head
[] = $strfeedback;
207 $table->align
[] = 'left';
210 if (isset($quiz->showtimetaken
)) {
211 $table->head
[] = $strtimetaken;
212 $table->align
[] = 'left';
216 // One row for each attempt
217 foreach ($attempts as $attempt) {
218 $attemptoptions = quiz_get_reviewoptions($quiz, $attempt, $context);
221 // Add the attempt number, making it a link, if appropriate.
222 if ($attempt->preview
) {
223 $row[] = make_review_link(get_string('preview', 'quiz'), $quiz, $attempt);
225 $row[] = make_review_link($attempt->attempt
, $quiz, $attempt);
228 // prepare strings for time taken and date completed
231 if ($attempt->timefinish
> 0) {
232 // attempt has finished
233 $timetaken = format_time($attempt->timefinish
- $attempt->timestart
);
234 $datecompleted = userdate($attempt->timefinish
);
235 } else if ($available) {
236 // The attempt is still in progress.
237 $timetaken = format_time(time() - $attempt->timestart
);
239 } else if ($quiz->timeclose
) {
240 // The attempt was not completed but is also not available any more becuase the quiz is closed.
241 $timetaken = format_time($quiz->timeclose
- $attempt->timestart
);
242 $datecompleted = userdate($quiz->timeclose
);
244 // Something wheird happened.
248 $row[] = $datecompleted;
251 if ($attemptoptions->scores
) {
252 $row[] = make_review_link(round($attempt->sumgrades
, $quiz->decimalpoints
), $quiz, $attempt);
258 // Ouside the if because we may be showing feedback but not grades.
259 $attemptgrade = quiz_rescale_grade($attempt->sumgrades
, $quiz);
262 if ($attemptoptions->scores
) {
263 $formattedgrade = $attemptgrade;
264 // highlight the highest grade if appropriate
265 if ($overallstats && $numattempts > 1 && !is_null($mygrade) && $attemptgrade == $mygrade && $quiz->grademethod
== QUIZ_GRADEHIGHEST
) {
266 $table->rowclass
[$attempt->attempt
] = 'bestrow';
269 $row[] = make_review_link($formattedgrade, $quiz, $attempt);
275 if ($feedbackcolumn) {
276 if ($attemptoptions->overallfeedback
) {
277 $row[] = quiz_feedback_for_grade($attemptgrade, $quiz->id
);
283 if (isset($quiz->showtimetaken
)) {
287 $table->data
[$attempt->attempt
] = $row;
288 } // End of loop over attempts.
292 // Print information about the student's best score for this quiz if possible.
293 $moreattempts = $unfinished ||
$numattempts < $quiz->attempts ||
$quiz->attempts
== 0;
294 if (!$moreattempts) {
295 print_heading(get_string("nomoreattempts", "quiz"));
298 if ($numattempts && $quiz->sumgrades
&& !is_null($mygrade)) {
300 if ($available && $moreattempts) {
302 $a->method
= $QUIZ_GRADE_METHOD[$quiz->grademethod
];
303 $a->mygrade
= $mygrade;
304 $a->quizgrade
= $quiz->grade
;
305 print_heading(get_string('gradesofar', 'quiz', $a));
307 print_heading(get_string('yourfinalgradeis', 'quiz', "$mygrade / $quiz->grade"));
311 if ($overallfeedback) {
312 echo '<p class="quizgradefeedback">'.quiz_feedback_for_grade($mygrade, $quiz->id
).'</p>';
316 // Print a button to start/continue an attempt, if appropriate.
317 if (!$quiz->questions
) {
318 print_heading(get_string("noquestions", "quiz"));
320 } else if ($available && $moreattempts) {
322 echo "<div class=\"quizattempt\">";
325 if (has_capability('mod/quiz:preview', $context)) {
326 $buttontext = get_string('continuepreview', 'quiz');
328 $buttontext = get_string('continueattemptquiz', 'quiz');
332 // Work out the appropriate button caption.
333 if (has_capability('mod/quiz:preview', $context)) {
334 $buttontext = get_string('previewquiznow', 'quiz');
335 } else if ($numattempts == 0) {
336 $buttontext = get_string('attemptquiznow', 'quiz');
338 $buttontext = get_string('reattemptquiz', 'quiz');
341 // Work out if the quiz is temporarily unavailable because of the delay option.
342 if (!empty($attempts)) {
343 $tempunavailable = '';
344 $lastattempt = end($attempts);
345 $lastattempttime = $lastattempt->timefinish
;
346 if ($numattempts == 1 && $quiz->delay1
&& $timenow <= $lastattempttime +
$quiz->delay1
) {
347 $tempunavailable = get_string('temporaryblocked', 'quiz') .
348 ' <strong>'. userdate($lastattempttime +
$quiz->delay1
). '</strong>';
349 } else if ($numattempts > 1 && $quiz->delay2
&& $timenow <= $lastattempttime +
$quiz->delay2
) {
350 $tempunavailable = get_string('temporaryblocked', 'quiz') .
351 ' <strong>'. userdate($lastattempttime +
$quiz->delay2
). '</strong>';
354 // If so, display a message and prevent the start button from appearing.
355 if ($tempunavailable) {
356 print_simple_box($tempunavailable, "center");
357 print_continue($CFG->wwwroot
. '/course/view.php?id=' . $course->id
);
363 // Actually print the start button.
365 $buttontext = htmlspecialchars($buttontext, ENT_QUOTES
);
367 // Do we need a confirm javascript alert?
369 $strconfirmstartattempt = '';
370 } else if ($quiz->timelimit
&& $quiz->attempts
) {
371 $strconfirmstartattempt = addslashes(get_string('confirmstartattempttimelimit','quiz', $quiz->attempts
));
372 } else if ($quiz->timelimit
) {
373 $strconfirmstartattempt = addslashes(get_string('confirmstarttimelimit','quiz'));
374 } else if ($quiz->attempts
) {
375 $strconfirmstartattempt = addslashes(get_string('confirmstartattemptlimit','quiz', $quiz->attempts
));
377 $strconfirmstartattempt = '';
380 // Prepare options depending on whether the quiz should be a popup.
381 if (!empty($quiz->popup
)) {
382 $window = 'quizpopup';
383 $windowoptions = "left=0, top=0, height='+window.screen.height+', " .
384 "width='+window.screen.width+', channelmode=yes, fullscreen=yes, " .
385 "scrollbars=yes, resizeable=no, directories=no, toolbar=no, " .
386 "titlebar=no, location=no, status=no, menubar=no";
392 // Determine the URL to use.
393 $attempturl = "attempt.php?id=$cm->id";
394 if (!empty($CFG->usesid
) && !isset($_COOKIE[session_name()])) {
395 $attempturl = sid_process_url($attempturl);
398 echo '<input type="button" value="'.$buttontext.'" onclick="javascript:';
399 if ($strconfirmstartattempt) {
400 echo "if (confirm('".addslashes_js($strconfirmstartattempt)."')) ";
402 echo "window.open('$attempturl','$window','$windowoptions');", '" />';
407 <?php
print_heading(get_string('noscript', 'quiz')); ?
>
415 print_continue($CFG->wwwroot
. '/course/view.php?id=' . $course->id
);
419 // Should we not be seeing if we need to print right-hand-side blocks?
422 if (!empty($THEME->customcorners
)) print_custom_corners_end();
423 echo '</td></tr></table>';
424 print_footer($course);
426 // Utility functions =================================================================
428 function quiz_review_allowed($quiz) {
432 /** Make some text into a link to review the quiz, if that is appropriate. */
433 function make_review_link($linktext, $quiz, $attempt) {
434 // If not even responses are to be shown in review then we don't allow any review
435 if (!($quiz->review
& QUIZ_REVIEW_RESPONSES
)) {
439 // If the quiz is still open, are reviews allowed?
440 if ((!$quiz->timeclose
or time() < $quiz->timeclose
) and !($quiz->review
& QUIZ_REVIEW_OPEN
)) {
441 // If not, don't link.
445 // If the quiz is closed, are reviews allowed?
446 if (($quiz->timeclose
and time() > $quiz->timeclose
) and !($quiz->review
& QUIZ_REVIEW_CLOSED
)) {
447 // If not, don't link.
451 // If the attempt is still open, don't link.
452 if (!$attempt->timefinish
) {
456 $url = "review.php?q=$quiz->id&attempt=$attempt->id";
458 $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";
459 return link_to_popup_window('/mod/quiz/' . $url, 'quizpopup', $linktext, '+window.screen.height+', '+window.screen.width+', '', $windowoptions, true);
461 return "<a href='$url'>$linktext</a>";