3 * This page prints a particular instance of quiz
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
14 require_once("../../config.php");
15 require_once("locallib.php");
17 // remember the current time as the time any responses were submitted
18 // (so as to make sure students don't get penalized for slow processing on this page)
21 // Get submitted parameters.
22 $id = optional_param('id', 0, PARAM_INT
); // Course Module ID
23 $q = optional_param('q', 0, PARAM_INT
); // or quiz ID
24 $page = optional_param('page', 0, PARAM_INT
);
25 $questionids = optional_param('questionids', '');
26 $finishattempt = optional_param('finishattempt', 0, PARAM_BOOL
);
27 $timeup = optional_param('timeup', 0, PARAM_BOOL
); // True if form was submitted by timer.
28 $forcenew = optional_param('forcenew', false, PARAM_BOOL
); // Teacher has requested new preview
31 if (! $cm = get_coursemodule_from_id('quiz', $id)) {
32 error("There is no coursemodule with id $id");
34 if (! $course = get_record("course", "id", $cm->course
)) {
35 error("Course is misconfigured");
37 if (! $quiz = get_record("quiz", "id", $cm->instance
)) {
38 error("The quiz with id $cm->instance corresponding to this coursemodule $id is missing");
41 if (! $quiz = get_record("quiz", "id", $q)) {
42 error("There is no quiz with id $q");
44 if (! $course = get_record("course", "id", $quiz->course
)) {
45 error("The course with id $quiz->course that the quiz with id $q belongs to is missing");
47 if (! $cm = get_coursemodule_from_instance("quiz", $quiz->id
, $course->id
)) {
48 error("The course module for the quiz with id $q is missing");
52 // We treat automatically closed attempts just like normally closed attempts
57 require_login($course->id
, false, $cm);
59 $coursecontext = get_context_instance(CONTEXT_COURSE
, $cm->course
); // course context
60 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
61 $ispreviewing = has_capability('mod/quiz:preview', $context);
63 // if no questions have been set up yet redirect to edit.php
64 if (!$quiz->questions
and has_capability('mod/quiz:manage', $context)) {
65 redirect($CFG->wwwroot
. '/mod/quiz/edit.php?quizid=' . $quiz->id
);
69 require_capability('mod/quiz:attempt', $context);
72 /// Get number for the next or unfinished attempt
73 if(!$attemptnumber = (int)get_field_sql('SELECT MAX(attempt)+1 FROM ' .
74 "{$CFG->prefix}quiz_attempts WHERE quiz = '{$quiz->id}' AND " .
75 "userid = '{$USER->id}' AND timefinish > 0 AND preview != 1")) {
79 $strattemptnum = get_string('attempt', 'quiz', $attemptnumber);
80 $strquizzes = get_string("modulenameplural", "quiz");
81 $popup = $quiz->popup
&& !$ispreviewing; // Controls whether this is shown in a javascript-protected window.
83 /// Check number of attempts
84 $numberofpreviousattempts = count_records_select('quiz_attempts', "quiz = '{$quiz->id}' AND " .
85 "userid = '{$USER->id}' AND timefinish > 0 AND preview != 1");
86 if ($quiz->attempts
and $numberofpreviousattempts >= $quiz->attempts
) {
87 error(get_string('nomoreattempts', 'quiz'), "view.php?id={$cm->id}");
90 /// Check subnet access
91 if ($quiz->subnet
and !address_in_subnet(getremoteaddr(), $quiz->subnet
)) {
93 notify(get_string('subnetnotice', 'quiz'));
95 error(get_string("subneterror", "quiz"), "view.php?id=$cm->id");
99 /// Check password access
100 if ($ispreviewing && $forcenew) {
101 unset($SESSION->passwordcheckedquizzes
[$quiz->id
]);
104 if ($quiz->password
and empty($SESSION->passwordcheckedquizzes
[$quiz->id
])) {
105 $enteredpassword = optional_param('quizpassword', '', PARAM_RAW
);
106 if (optional_param('cancelpassword', false)) {
107 // User clicked cancel in the password form.
108 redirect($CFG->wwwroot
. '/mod/quiz/view.php?q=' . $quiz->id
);
109 } else if (strcmp($quiz->password
, $enteredpassword) === 0) {
110 // User entered the correct password.
111 $SESSION->passwordcheckedquizzes
[$quiz->id
] = true;
113 // User entered the wrong password, or has not entered one yet.
114 $url = $CFG->wwwroot
. '/mod/quiz/attempt.php?q=' . $quiz->id
;
117 print_header('', '', '', 'quizpassword');
120 if (trim(strip_tags($quiz->intro
))) {
121 $formatoptions->noclean
= true;
122 print_box(format_text($quiz->intro
, FORMAT_MOODLE
, $formatoptions), 'generalbox', 'intro');
124 print_box_start('generalbox', 'passwordbox');
125 if (!empty($enteredpassword)) {
126 echo '<p class="notifyproblem">', get_string('passworderror', 'quiz'), '</p>';
129 <p
><?php
print_string('requirepasswordmessage', 'quiz'); ?
></p
>
130 <form id
="passwordform" method
="post" action
="<?php echo $url; ?>" onclick
="this.autocomplete='off'">
132 <label
for="quizpassword"><?php
print_string('password'); ?
></label
>
133 <input name
="quizpassword" id
="quizpassword" type
="password" value
=""/>
134 <input type
="submit" value
="<?php print_string('ok'); ?>" />
135 <input type
="submit" name
="cancelpassword" value
="<?php print_string('cancel'); ?>" />
147 if ($quiz->delay1
or $quiz->delay2
) {
148 //quiz enforced time delay
149 if ($attempts = quiz_get_user_attempts($quiz->id
, $USER->id
)) {
150 $numattempts = count($attempts);
155 $lastattempt_obj = get_record_select('quiz_attempts', "quiz = $quiz->id AND attempt = $numattempts AND userid = $USER->id", 'timefinish');
156 if ($lastattempt_obj) {
157 $lastattempt = $lastattempt_obj->timefinish
;
159 if ($numattempts == 1 && $quiz->delay1
) {
160 if ($timenow - $quiz->delay1
< $lastattempt) {
161 error(get_string('timedelay', 'quiz'), 'view.php?q='.$quiz->id
);
163 } else if($numattempts > 1 && $quiz->delay2
) {
164 if ($timenow - $quiz->delay2
< $lastattempt) {
165 error(get_string('timedelay', 'quiz'), 'view.php?q='.$quiz->id
);
170 /// Load attempt or create a new attempt if there is no unfinished one
172 if ($ispreviewing and $forcenew) { // teacher wants a new preview
173 // so we set a finish time on the current attempt (if any).
174 // It will then automatically be deleted below
175 set_field('quiz_attempts', 'timefinish', $timestamp, 'quiz', $quiz->id
, 'userid', $USER->id
);
178 $attempt = quiz_get_user_attempt_unfinished($quiz->id
, $USER->id
);
182 // Delete any previous preview attempts belonging to this user.
183 if ($oldattempts = get_records_select('quiz_attempts', "quiz = '$quiz->id'
184 AND userid = '$USER->id' AND preview = 1")) {
185 foreach ($oldattempts as $oldattempt) {
186 quiz_delete_attempt($oldattempt, $quiz);
190 // Start a new attempt and initialize the question sessions
191 $attempt = quiz_create_attempt($quiz, $attemptnumber);
192 // If this is an attempt by a teacher mark it as a preview
194 $attempt->preview
= 1;
197 if (!$attempt->id
= insert_record('quiz_attempts', $attempt)) {
198 error('Could not create new attempt');
202 add_to_log($course->id
, 'quiz', 'preview',
203 "attempt.php?id=$cm->id",
204 "$quiz->id", $cm->id
);
206 add_to_log($course->id
, 'quiz', 'attempt',
207 "review.php?attempt=$attempt->id",
208 "$quiz->id", $cm->id
);
211 // log continuation of attempt only if some time has lapsed
212 if (($timestamp - $attempt->timemodified
) > 600) { // 10 minutes have elapsed
213 add_to_log($course->id
, 'quiz', 'continue attemp', // this action used to be called 'continue attempt' but the database field has only 15 characters
214 "review.php?attempt=$attempt->id",
215 "$quiz->id", $cm->id
);
218 if (!$attempt->timestart
) { // shouldn't really happen, just for robustness
219 debugging('timestart was not set for this attempt. That should be impossible.', DEBUG_DEVELOPER
);
220 $attempt->timestart
= $timestamp - 1;
223 /// Load all the questions and states needed by this script
225 // list of questions needed by page
226 $pagelist = quiz_questions_on_page($attempt->layout
, $page);
229 $questionlist = quiz_questions_in_quiz($attempt->layout
);
231 $questionlist = $pagelist;
234 // add all questions that are on the submitted form
236 $questionlist .= ','.$questionids;
239 if (!$questionlist) {
240 error(get_string('noquestionsfound', 'quiz'), 'view.php?q='.$quiz->id
);
243 $sql = "SELECT q.*, i.grade AS maxgrade, i.id AS instance".
244 " FROM {$CFG->prefix}question q,".
245 " {$CFG->prefix}quiz_question_instances i".
246 " WHERE i.quiz = '$quiz->id' AND q.id = i.question".
247 " AND q.id IN ($questionlist)";
249 // Load the questions
250 if (!$questions = get_records_sql($sql)) {
251 error(get_string('noquestionsfound', 'quiz'), 'view.php?q='.$quiz->id
);
254 // Load the question type specific information
255 if (!get_question_options($questions)) {
256 error('Could not load question options');
259 // If the new attempt is to be based on a previous attempt find its id
260 $lastattemptid = false;
261 if ($newattempt and $attempt->attempt
> 1 and $quiz->attemptonlast
and !$attempt->preview
) {
262 // Find the previous attempt
263 if (!$lastattemptid = get_field('quiz_attempts', 'uniqueid', 'quiz', $attempt->quiz
, 'userid', $attempt->userid
, 'attempt', $attempt->attempt
-1)) {
264 error('Could not find previous attempt to build on');
268 // Restore the question sessions to their most recent states
269 // creating new sessions where required
270 if (!$states = get_question_states($questions, $quiz, $attempt, $lastattemptid)) {
271 error('Could not restore question sessions');
274 // Save all the newly created states
276 foreach ($questions as $i => $question) {
277 save_question_session($questions[$i], $states[$i]);
281 /// Process form data /////////////////////////////////////////////////
283 if ($responses = data_submitted() and empty($_POST['quizpassword'])) {
285 // set the default event. This can be overruled by individual buttons.
286 $event = (array_key_exists('markall', $responses)) ? QUESTION_EVENTSUBMIT
:
287 ($finishattempt ? QUESTION_EVENTCLOSE
: QUESTION_EVENTSAVE
);
289 // Unset any variables we know are not responses
290 unset($responses->id
);
291 unset($responses->q
);
292 unset($responses->oldpage
);
293 unset($responses->newpage
);
294 unset($responses->review
);
295 unset($responses->questionids
);
296 unset($responses->saveattempt
); // responses get saved anway
297 unset($responses->finishattempt
); // same as $finishattempt
298 unset($responses->markall
);
299 unset($responses->forcenewattempt
);
302 // $actions is an array indexed by the questions ids
303 $actions = question_extract_responses($questions, $responses, $event);
305 // Process each question in turn
307 $questionidarray = explode(',', $questionids);
308 foreach($questionidarray as $i) {
309 if (!isset($actions[$i])) {
310 $actions[$i]->responses
= array('' => '');
311 $actions[$i]->event
= QUESTION_EVENTOPEN
;
313 $actions[$i]->timestamp
= $timestamp;
314 question_process_responses($questions[$i], $states[$i], $actions[$i], $quiz, $attempt);
315 save_question_session($questions[$i], $states[$i]);
318 $attempt->timemodified
= $timestamp;
320 // We have now finished processing form data
323 /// Finish attempt if requested
324 if ($finishattempt) {
326 // Set the attempt to be finished
327 $attempt->timefinish
= $timestamp;
329 // load all the questions
330 $closequestionlist = quiz_questions_in_quiz($attempt->layout
);
331 $sql = "SELECT q.*, i.grade AS maxgrade, i.id AS instance".
332 " FROM {$CFG->prefix}question q,".
333 " {$CFG->prefix}quiz_question_instances i".
334 " WHERE i.quiz = '$quiz->id' AND q.id = i.question".
335 " AND q.id IN ($closequestionlist)";
336 if (!$closequestions = get_records_sql($sql)) {
337 error('Questions missing');
340 // Load the question type specific information
341 if (!get_question_options($closequestions)) {
342 error('Could not load question options');
345 // Restore the question sessions
346 if (!$closestates = get_question_states($closequestions, $quiz, $attempt)) {
347 error('Could not restore question sessions');
350 foreach($closequestions as $key => $question) {
351 $action->event
= QUESTION_EVENTCLOSE
;
352 $action->responses
= $closestates[$key]->responses
;
353 $action->timestamp
= $closestates[$key]->timestamp
;
354 question_process_responses($question, $closestates[$key], $action, $quiz, $attempt);
355 save_question_session($question, $closestates[$key]);
358 add_to_log($course->id
, 'quiz', 'close attempt',
359 "review.php?attempt=$attempt->id",
360 "$quiz->id", $cm->id
);
363 /// Update the quiz attempt and the overall grade for the quiz
364 if ($responses ||
$finishattempt) {
365 if (!update_record('quiz_attempts', $attempt)) {
366 error('Failed to save the current quiz attempt!');
368 if (($attempt->attempt
> 1 ||
$attempt->timefinish
> 0) and !$attempt->preview
) {
369 quiz_save_best_grade($quiz);
373 /// Send emails to those who have the capability set
374 if ($finishattempt && !$attempt->preview
) {
375 quiz_send_notification_emails($course, $quiz, $attempt, $context, $cm);
378 /// Check access to quiz page
380 // check the quiz times
381 if ($timestamp < $quiz->timeopen ||
($quiz->timeclose
and $timestamp > $quiz->timeclose
)) {
383 notify(get_string('notavailabletostudents', 'quiz'));
385 notice(get_string('notavailable', 'quiz'), "view.php?id={$cm->id}");
389 if ($finishattempt) {
390 unset($SESSION->passwordcheckedquizzes
[$quiz->id
]);
391 redirect($CFG->wwwroot
. '/mod/quiz/review.php?attempt='.$attempt->id
, 0);
394 /// Print the quiz page ////////////////////////////////////////////////////////
396 // Print the page header
397 require_js($CFG->wwwroot
. '/mod/quiz/quiz.js');
398 $pagequestions = explode(',', $pagelist);
399 $headtags = get_html_head_contributions($pagequestions, $questions, $states);
400 if (!empty($popup)) {
401 define('MESSAGE_WINDOW', true); // This prevents the message window coming up
402 print_header($course->shortname
.': '.format_string($quiz->name
), '', '', '', $headtags, false, '', '', false, '');
403 include('protect_js.php');
405 $strupdatemodule = has_capability('moodle/course:manageactivities', $coursecontext)
406 ?
update_module_button($cm->id
, $course->id
, get_string('modulename', 'quiz'))
409 $navlinks[] = array('name' => $strquizzes, 'link' => "index.php?id=$course->id", 'type' => 'activity');
410 $navlinks[] = array('name' => format_string($quiz->name
), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
411 $navlinks[] = array('name' => $strattemptnum, 'link' => '', 'type' => 'title');
413 $navigation = build_navigation($navlinks);
415 print_header_simple(format_string($quiz->name
), "", $navigation, "", $headtags, true, $strupdatemodule);
418 echo '<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>'; // for overlib
420 // Print the quiz name heading and tabs for teacher, etc.
422 $currenttab = 'preview';
425 print_heading(get_string('previewquiz', 'quiz', format_string($quiz->name
)));
426 unset($buttonoptions);
427 $buttonoptions['q'] = $quiz->id
;
428 $buttonoptions['forcenew'] = true;
429 print_single_button($CFG->wwwroot
.'/mod/quiz/attempt.php', $buttonoptions, get_string('startagain', 'quiz'));
431 notify(get_string('popupnotice', 'quiz'));
434 if ($quiz->attempts
!= 1) {
435 print_heading(format_string($quiz->name
).' - '.$strattemptnum);
437 print_heading(format_string($quiz->name
));
442 echo '<form id="responseform" method="post" action="attempt.php?q=', s($quiz->id
), '&page=', s($page),
443 '" enctype="multipart/form-data"' .
444 ' onclick="this.autocomplete=\'off\'" onkeypress="return check_enter(event);">', "\n";
445 if($quiz->timelimit
> 0) {
446 // Make sure javascript is enabled for time limited quizzes
448 <script type
="text/javascript">
449 // Do nothing, but you have to have a script tag before a noscript tag.
453 <?php
print_heading(get_string('noscript', 'quiz')); ?
>
460 /// Print the navigation panel if required
461 $numpages = quiz_number_of_pages($attempt->layout
);
463 quiz_print_navigation_panel($page, $numpages);
466 /// Print all the questions
467 $number = quiz_first_questionnumber($attempt->layout
, $pagelist);
468 foreach ($pagequestions as $i) {
469 $options = quiz_get_renderoptions($quiz->review
, $states[$i]);
470 // Print the question
471 print_question($questions[$i], $states[$i], $number, $quiz, $options);
472 save_question_session($questions[$i], $states[$i]);
473 $number +
= $questions[$i]->length
;
476 /// Print the submit buttons
477 $strconfirmattempt = addslashes(get_string("confirmclose", "quiz"));
478 $onclick = "return confirm('$strconfirmattempt')";
479 echo "<div class=\"submitbtns mdl-align\">\n";
481 echo "<input type=\"submit\" name=\"saveattempt\" value=\"".get_string("savenosubmit", "quiz")."\" />\n";
482 if ($quiz->optionflags
& QUESTION_ADAPTIVE
) {
483 echo "<input type=\"submit\" name=\"markall\" value=\"".get_string("markall", "quiz")."\" />\n";
485 echo "<input type=\"submit\" name=\"finishattempt\" value=\"".get_string("finishattempt", "quiz")."\" onclick=\"$onclick\" />\n";
489 // Print the navigation panel if required
491 quiz_print_navigation_panel($page, $numpages);
496 echo '<input type="hidden" name="timeup" id="timeup" value="0" />';
498 // Add a hidden field with questionids. Do this at the end of the form, so
499 // if you navigate before the form has finished loading, it does not wipe all
500 // the student's answers.
501 echo '<input type="hidden" name="questionids" value="'.$pagelist."\" />\n";
505 // If the quiz has a time limit, or if we are close to the close time, include a floating timer.
507 $timerstartvalue = 999999999999;
508 if ($quiz->timeclose
) {
509 $timerstartvalue = min($timerstartvalue, $quiz->timeclose
- time());
510 $showtimer = $timerstartvalue < 60*60; // Show the timer if we are less than 60 mins from the deadline.
512 if ($quiz->timelimit
> 0 && !has_capability('mod/quiz:ignoretimelimits', $context, NULL, false)) {
513 $timerstartvalue = min($timerstartvalue, $attempt->timestart +
$quiz->timelimit
*60- time());
516 if ($showtimer && (!$ispreviewing ||
$timerstartvalue > 0)) {
517 $timerstartvalue = max($timerstartvalue, 1); // Make sure it starts just above zero.
518 require('jstimer.php');
523 print_footer($course);