3 // This page prints a particular instance of quiz
5 require_once("../../config.php");
6 require_once($CFG->libdir
.'/blocklib.php');
7 require_once($CFG->libdir
.'/gradelib.php');
8 require_once($CFG->dirroot
.'/mod/quiz/locallib.php');
9 require_once($CFG->dirroot
.'/mod/quiz/pagelib.php');
11 $id = optional_param('id', 0, PARAM_INT
); // Course Module ID, or
12 $q = optional_param('q', 0, PARAM_INT
); // quiz ID
13 $edit = optional_param('edit', -1, PARAM_BOOL
);
16 if (! $cm = get_coursemodule_from_id('quiz', $id)) {
17 error("There is no coursemodule with id $id");
19 if (! $course = get_record("course", "id", $cm->course
)) {
20 error("Course is misconfigured");
22 if (! $quiz = get_record("quiz", "id", $cm->instance
)) {
23 error("The quiz with id $cm->instance corresponding to this coursemodule $id is missing");
26 if (! $quiz = get_record("quiz", "id", $q)) {
27 error("There is no quiz with id $q");
29 if (! $course = get_record("course", "id", $quiz->course
)) {
30 error("The course with id $quiz->course that the quiz with id $q belongs to is missing");
32 if (! $cm = get_coursemodule_from_instance("quiz", $quiz->id
, $course->id
)) {
33 error("The course module for the quiz with id $q is missing");
37 // Check login and get context.
38 require_login($course->id
, false, $cm);
39 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
41 // if no questions have been set up yet redirect to edit.php
42 if (!$quiz->questions
and has_capability('mod/quiz:manage', $context)) {
43 redirect($CFG->wwwroot
. '/mod/quiz/edit.php?cmid=' . $cm->id
);
46 add_to_log($course->id
, "quiz", "view", "view.php?id=$cm->id", $quiz->id
, $cm->id
);
48 // Initialize $PAGE, compute blocks
49 $PAGE = page_create_instance($quiz->id
);
50 $pageblocks = blocks_setup($PAGE);
51 $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT
]), 210);
53 // Print the page header
54 if ($edit != -1 and $PAGE->user_allowed_editing()) {
55 $USER->editing
= $edit;
58 //only check pop ups if the user is not a teacher, and popup is set
60 $bodytags = (has_capability('mod/quiz:attempt', $context) && $quiz->popup
)?
'onload="popupchecker(\'' . get_string('popupblockerwarning', 'quiz') . '\');"':'';
61 $PAGE->print_header($course->shortname
.': %fullname%','',$bodytags);
63 echo '<table id="layout-table"><tr>';
65 if(!empty($CFG->showblocksonmodpages
) && (blocks_have_content($pageblocks, BLOCK_POS_LEFT
) ||
$PAGE->user_is_editing())) {
66 echo '<td style="width: '.$blocks_preferred_width.'px;" id="left-column">';
67 print_container_start();
68 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT
);
69 print_container_end();
73 echo '<td id="middle-column">';
74 print_container_start();
76 // Print the main part of the page
78 // Print heading and tabs (if there is more than one).
84 print_heading(format_string($quiz->name
));
86 if (has_capability('mod/quiz:view', $context)) {
88 // Print quiz description
89 if (trim(strip_tags($quiz->intro
))) {
90 $formatoptions->noclean
= true;
91 print_box(format_text($quiz->intro
, FORMAT_MOODLE
, $formatoptions), 'generalbox', 'intro');
94 echo '<div class="quizinfo">';
96 // Print information about number of attempts and grading method.
97 if ($quiz->attempts
> 1) {
98 echo "<p>".get_string("attemptsallowed", "quiz").": $quiz->attempts</p>";
100 if ($quiz->attempts
!= 1) {
101 echo "<p>".get_string("grademethod", "quiz").": ".quiz_get_grading_option_name($quiz->grademethod
)."</p>";
104 // Print information about timings.
106 $available = ($quiz->timeopen
< $timenow and ($timenow < $quiz->timeclose
or !$quiz->timeclose
));
108 if ($quiz->timelimit
) {
109 echo "<p>".get_string("quiztimelimit","quiz", format_time($quiz->timelimit
* 60))."</p>";
111 if ($quiz->timeopen
) {
112 echo '<p>', get_string('quizopens', 'quiz'), ': ', userdate($quiz->timeopen
), '</p>';
114 if ($quiz->timeclose
) {
115 echo '<p>', get_string('quizcloses', 'quiz'), ': ', userdate($quiz->timeclose
), '</p>';
117 } else if ($timenow < $quiz->timeopen
) {
118 echo "<p>".get_string("quiznotavailable", "quiz", userdate($quiz->timeopen
))."</p>";
120 echo "<p>".get_string("quizclosed", "quiz", userdate($quiz->timeclose
))."</p>";
127 // Show number of attempts summary to those who can view reports.
128 if (has_capability('mod/quiz:viewreports', $context)) {
129 if ($strattemptnum = quiz_num_attempt_summary($quiz, $cm)) {
130 echo '<div class="quizattemptcounts"><a href="report.php?mode=overview&id=' .
131 $cm->id
. '">' . $strattemptnum . '</a></div>';
135 // Guests can't do a quiz, so offer them a choice of logging in or going back.
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
);
157 if ($unfinishedattempt = quiz_get_user_attempt_unfinished($quiz->id
, $USER->id
)) {
158 $attempts[] = $unfinishedattempt;
161 $numattempts = count($attempts);
163 // Work out the final grade, checking whether it was overridden in the gradebook.
164 $mygrade = quiz_get_best_grade($quiz, $USER->id
);
165 $mygradeoverridden = false;
166 $gradebookfeedback = '';
168 $grading_info = grade_get_grades($course->id
, 'mod', 'quiz', $quiz->id
, $USER->id
);
169 if (!empty($grading_info->items
)) {
170 $item = $grading_info->items
[0];
171 if (isset($item->grades
[$USER->id
])) {
172 $grade = $item->grades
[$USER->id
];
174 if ($grade->overridden
) {
175 $mygrade = $grade->grade +
0; // Convert to number.
176 $mygradeoverridden = true;
178 if (!empty($grade->str_feedback
)) {
179 $gradebookfeedback = $grade->str_feedback
;
184 // Print table with existing attempts
187 print_heading(get_string('summaryofattempts', 'quiz'));
189 // Work out which columns we need, taking account what data is available in each attempt.
190 list($someoptions, $alloptions) = quiz_get_combined_reviewoptions($quiz, $attempts, $context);
192 $gradecolumn = $someoptions->scores
&& $quiz->grade
&& $quiz->sumgrades
;
193 $markcolumn = $gradecolumn && ($quiz->grade
!= $quiz->sumgrades
);
194 $overallstats = $alloptions->scores
;
196 $feedbackcolumn = quiz_has_feedback($quiz->id
);
197 $overallfeedback = $feedbackcolumn && $alloptions->overallfeedback
;
199 // Prepare table header
200 $table->class = 'generaltable quizattemptsummary';
201 $table->head
= array(get_string('attempt', 'quiz'), get_string('timecompleted', 'quiz'));
202 $table->align
= array('center', 'left');
203 $table->size
= array('', '');
205 $table->head
[] = get_string('marks', 'quiz') . " / $quiz->sumgrades";
206 $table->align
[] = 'center';
210 $table->head
[] = get_string('grade') . " / $quiz->grade";
211 $table->align
[] = 'center';
214 if ($feedbackcolumn) {
215 $table->head
[] = get_string('feedback', 'quiz');
216 $table->align
[] = 'left';
219 if (isset($quiz->showtimetaken
)) {
220 $table->head
[] = get_string('timetaken', 'quiz');
221 $table->align
[] = 'left';
225 // One row for each attempt
226 foreach ($attempts as $attempt) {
227 $attemptoptions = quiz_get_reviewoptions($quiz, $attempt, $context);
230 // Add the attempt number, making it a link, if appropriate.
231 if ($attempt->preview
) {
232 $row[] = make_review_link(get_string('preview', 'quiz'), $quiz, $attempt);
234 $row[] = make_review_link($attempt->attempt
, $quiz, $attempt);
237 // prepare strings for time taken and date completed
240 if ($attempt->timefinish
> 0) {
241 // attempt has finished
242 $timetaken = format_time($attempt->timefinish
- $attempt->timestart
);
243 $datecompleted = userdate($attempt->timefinish
);
244 } else if ($available) {
245 // The attempt is still in progress.
246 $timetaken = format_time(time() - $attempt->timestart
);
248 } else if ($quiz->timeclose
) {
249 // The attempt was not completed but is also not available any more becuase the quiz is closed.
250 $timetaken = format_time($quiz->timeclose
- $attempt->timestart
);
251 $datecompleted = userdate($quiz->timeclose
);
253 // Something weird happened.
257 $row[] = $datecompleted;
259 if ($markcolumn && $attempt->timefinish
> 0) {
260 if ($attemptoptions->scores
) {
261 $row[] = make_review_link(round($attempt->sumgrades
, $quiz->decimalpoints
), $quiz, $attempt);
267 // Ouside the if because we may be showing feedback but not grades.
268 $attemptgrade = quiz_rescale_grade($attempt->sumgrades
, $quiz);
271 if ($attemptoptions->scores
&& $attempt->timefinish
> 0) {
272 $formattedgrade = $attemptgrade;
273 // highlight the highest grade if appropriate
274 if ($overallstats && $numattempts > 1 && !is_null($mygrade) && $attemptgrade == $mygrade && $quiz->grademethod
== QUIZ_GRADEHIGHEST
) {
275 $table->rowclass
[$attempt->attempt
] = 'bestrow';
278 $row[] = make_review_link($formattedgrade, $quiz, $attempt);
284 if ($feedbackcolumn && $attempt->timefinish
> 0) {
285 if ($attemptoptions->overallfeedback
) {
286 $row[] = quiz_feedback_for_grade($attemptgrade, $quiz->id
);
292 if (isset($quiz->showtimetaken
)) {
296 $table->data
[$attempt->attempt
] = $row;
297 } // End of loop over attempts.
301 // Print information about the student's best score for this quiz if possible.
302 $moreattempts = $unfinished ||
$numattempts < $quiz->attempts ||
$quiz->attempts
== 0;
303 if (!$moreattempts) {
304 print_heading(get_string("nomoreattempts", "quiz"));
307 if ($numattempts && $quiz->sumgrades
&& !is_null($mygrade)) {
311 if ($available && $moreattempts) {
313 $a->method
= quiz_get_grading_option_name($quiz->grademethod
);
314 $a->mygrade
= $mygrade;
315 $a->quizgrade
= $quiz->grade
;
316 $resultinfo .= print_heading(get_string('gradesofar', 'quiz', $a), '', 2, 'main', true);
318 $resultinfo .= print_heading(get_string('yourfinalgradeis', 'quiz', "$mygrade / $quiz->grade"), '', 2, 'main', true);
319 if ($mygradeoverridden) {
320 $resultinfo .= '<p class="overriddennotice">'.get_string('overriddennotice', 'grades').'</p>';
325 if ($gradebookfeedback) {
326 $resultinfo .= print_heading(get_string('comment', 'quiz'), '', 3, 'main', true);
327 $resultinfo .= '<p class="quizteacherfeedback">'.$gradebookfeedback.'</p>';
329 if ($overallfeedback) {
330 $resultinfo .= print_heading(get_string('overallfeedback', 'quiz'), '', 3, 'main', true);
331 $resultinfo .= '<p class="quizgradefeedback">'.quiz_feedback_for_grade($mygrade, $quiz->id
).'</p>';
335 print_box($resultinfo, 'generalbox', 'feedback');
339 // Print a button to start/continue an attempt, if appropriate.
340 if (!$quiz->questions
) {
341 print_heading(get_string("noquestions", "quiz"));
343 } else if ($available && $moreattempts) {
345 echo "<div class=\"quizattempt\">";
348 if (has_capability('mod/quiz:preview', $context)) {
349 $buttontext = get_string('continuepreview', 'quiz');
351 $buttontext = get_string('continueattemptquiz', 'quiz');
355 // Work out the appropriate button caption.
356 if (has_capability('mod/quiz:preview', $context)) {
357 $buttontext = get_string('previewquiznow', 'quiz');
358 } else if ($numattempts == 0) {
359 $buttontext = get_string('attemptquiznow', 'quiz');
361 $buttontext = get_string('reattemptquiz', 'quiz');
364 // Work out if the quiz is temporarily unavailable because of the delay option.
365 if (!empty($attempts)) {
366 $tempunavailable = '';
367 $lastattempt = end($attempts);
368 $lastattempttime = $lastattempt->timefinish
;
369 if ($numattempts == 1 && $quiz->delay1
&& $timenow <= $lastattempttime +
$quiz->delay1
) {
370 $tempunavailable = get_string('temporaryblocked', 'quiz') .
371 ' <strong>'. userdate($lastattempttime +
$quiz->delay1
). '</strong>';
372 } else if ($numattempts > 1 && $quiz->delay2
&& $timenow <= $lastattempttime +
$quiz->delay2
) {
373 $tempunavailable = get_string('temporaryblocked', 'quiz') .
374 ' <strong>'. userdate($lastattempttime +
$quiz->delay2
). '</strong>';
377 // If so, display a message and prevent the start button from appearing.
378 if ($tempunavailable) {
379 print_simple_box($tempunavailable, "center");
380 print_continue($CFG->wwwroot
. '/course/view.php?id=' . $course->id
);
386 // Actually print the start button.
388 $buttontext = htmlspecialchars($buttontext, ENT_QUOTES
);
390 // Do we need a confirm javascript alert?
392 $strconfirmstartattempt = '';
393 } else if ($quiz->timelimit
&& $quiz->attempts
) {
394 $strconfirmstartattempt = get_string('confirmstartattempttimelimit','quiz', $quiz->attempts
);
395 } else if ($quiz->timelimit
) {
396 $strconfirmstartattempt = get_string('confirmstarttimelimit','quiz');
397 } else if ($quiz->attempts
) {
398 $strconfirmstartattempt = get_string('confirmstartattemptlimit','quiz', $quiz->attempts
);
400 $strconfirmstartattempt = '';
402 // Determine the URL to use.
403 $attempturl = "attempt.php?id=$cm->id";
405 // Prepare options depending on whether the quiz should be a popup.
406 if (!empty($quiz->popup
)) {
407 $window = 'quizpopup';
408 $windowoptions = "left=0, top=0, height='+window.screen.height+', " .
409 "width='+window.screen.width+', channelmode=yes, fullscreen=yes, " .
410 "scrollbars=yes, resizeable=no, directories=no, toolbar=no, " .
411 "titlebar=no, location=no, status=no, menubar=no";
412 if (!empty($CFG->usesid
) && !isset($_COOKIE[session_name()])) {
413 $attempturl = sid_process_url($attempturl);
416 echo '<input type="button" value="'.$buttontext.'" onclick="javascript:';
417 if ($strconfirmstartattempt) {
418 $strconfirmstartattempt = addslashes($strconfirmstartattempt);
419 echo "if (confirm('".addslashes_js($strconfirmstartattempt)."')) ";
421 echo "window.open('$attempturl','$window','$windowoptions');", '" />';
423 print_single_button("attempt.php", array('id'=>$cm->id
), $buttontext, 'get', '', false, '', false, $strconfirmstartattempt);
430 <?php
print_heading(get_string('noscript', 'quiz')); ?
>
438 print_continue($CFG->wwwroot
. '/course/view.php?id=' . $course->id
);
441 // Should we not be seeing if we need to print right-hand-side blocks?
443 finish_page($course);
445 // Utility functions =================================================================
447 function finish_page($course) {
449 print_container_end();
450 echo '</td></tr></table>';
451 print_footer($course);
455 /** Make some text into a link to review the quiz, if that is appropriate. */
456 function make_review_link($linktext, $quiz, $attempt) {
457 // If not even responses are to be shown in review then we don't allow any review
458 if (!($quiz->review
& QUIZ_REVIEW_RESPONSES
)) {
462 // If the quiz is still open, are reviews allowed?
463 if ((!$quiz->timeclose
or time() < $quiz->timeclose
) and !($quiz->review
& QUIZ_REVIEW_OPEN
& QUIZ_REVIEW_RESPONSES
)) {
464 // If not, don't link.
468 // If the quiz is closed, are reviews allowed?
469 if (($quiz->timeclose
and time() > $quiz->timeclose
) and !($quiz->review
& QUIZ_REVIEW_CLOSED
& QUIZ_REVIEW_RESPONSES
)) {
470 // If not, don't link.
474 // If the attempt is still open, don't link.
475 if (!$attempt->timefinish
) {
479 $url = "review.php?q=$quiz->id&attempt=$attempt->id";
481 $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";
482 return link_to_popup_window('/mod/quiz/' . $url, 'quizpopup', $linktext, '+window.screen.height+', '+window.screen.width+', '', $windowoptions, true);
484 return "<a href='$url'>$linktext</a>";