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('edit.php?quizid=' . $quiz->id
);
68 // Get number for the next or unfinished attempt
69 if(!$attemptnumber = (int)get_field_sql('SELECT MAX(attempt)+1 FROM ' .
70 "{$CFG->prefix}quiz_attempts WHERE quiz = '{$quiz->id}' AND " .
71 "userid = '{$USER->id}' AND timefinish > 0 AND preview != 1")) {
75 $strattemptnum = get_string('attempt', 'quiz', $attemptnumber);
76 $strquizzes = get_string("modulenameplural", "quiz");
77 $popup = $quiz->popup
&& !$ispreviewing; // Controls whether this is shown in a javascript-protected window.
81 print_heading(get_string('guestsno', 'quiz'));
83 print_footer($course);
88 $numberofpreviousattempts = count_records_select('quiz_attempts', "quiz = '{$quiz->id}' AND " .
89 "userid = '{$USER->id}' AND timefinish > 0 AND preview != 1");
90 if ($quiz->attempts
and $numberofpreviousattempts >= $quiz->attempts
) {
91 error(get_string('nomoreattempts', 'quiz'), "view.php?id={$cm->id}");
94 // Check subnet access
95 if ($quiz->subnet
and !address_in_subnet(getremoteaddr(), $quiz->subnet
)) {
97 notify(get_string('subnetnotice', 'quiz'));
99 error(get_string("subneterror", "quiz"), "view.php?id=$cm->id");
103 // Check password access
104 if ($quiz->password
and empty($_POST['q'])) {
105 if (empty($_POST['quizpassword'])) {
107 if (trim(strip_tags($quiz->intro
))) {
108 print_simple_box(format_text($quiz->intro
), "center");
112 echo "<form id=\"passwordform\" method=\"post\" action=\"attempt.php?id=$cm->id\" onclick=\"this.autocomplete='off'\">\n";
113 echo '<fieldset class="invisiblefieldset" style="display: block">';
114 print_simple_box_start("center");
116 echo "<div class=\"boxaligncenter\">\n";
117 print_string("requirepasswordmessage", "quiz");
118 echo "<br /><br />\n";
119 echo " <input name=\"quizpassword\" type=\"password\" value=\"\" alt=\"password\" />";
120 echo " <input type=\"submit\" value=\"".get_string("ok")."\" />\n";
123 print_simple_box_end();
133 if (strcmp($quiz->password
, $_POST['quizpassword']) !== 0) {
134 error(get_string("passworderror", "quiz"), "view.php?id=$cm->id");
139 if ($quiz->delay1
or $quiz->delay2
) {
140 //quiz enforced time delay
141 if ($attempts = quiz_get_user_attempts($quiz->id
, $USER->id
)) {
142 $numattempts = count($attempts);
147 $lastattempt_obj = get_record_select('quiz_attempts', "quiz = $quiz->id AND attempt = $numattempts AND userid = $USER->id", 'timefinish');
148 if ($lastattempt_obj) {
149 $lastattempt = $lastattempt_obj->timefinish
;
151 if ($numattempts == 1 && $quiz->delay1
) {
152 if ($timenow - $quiz->delay1
< $lastattempt) {
153 error(get_string('timedelay', 'quiz'), 'view.php?q='.$quiz->id
);
155 } else if($numattempts > 1 && $quiz->delay2
) {
156 if ($timenow - $quiz->delay2
< $lastattempt) {
157 error(get_string('timedelay', 'quiz'), 'view.php?q='.$quiz->id
);
162 // Load attempt or create a new attempt if there is no unfinished one
164 if ($ispreviewing and $forcenew) { // teacher wants a new preview
165 // so we set a finish time on the current attempt (if any).
166 // It will then automatically be deleted below
167 set_field('quiz_attempts', 'timefinish', $timestamp, 'quiz', $quiz->id
, 'userid', $USER->id
);
170 $attempt = quiz_get_user_attempt_unfinished($quiz->id
, $USER->id
);
174 // Delete any previous preview attempts belonging to this user.
175 if ($oldattempts = get_records_select('quiz_attempts', "quiz = '$quiz->id'
176 AND userid = '$USER->id' AND preview = 1")) {
177 foreach ($oldattempts as $oldattempt) {
178 quiz_delete_attempt($oldattempt, $quiz);
182 // Start a new attempt and initialize the question sessions
183 $attempt = quiz_create_attempt($quiz, $attemptnumber);
184 // If this is an attempt by a teacher mark it as a preview
186 $attempt->preview
= 1;
189 if (!$attempt->id
= insert_record('quiz_attempts', $attempt)) {
190 error('Could not create new attempt');
194 add_to_log($course->id
, 'quiz', 'preview',
195 "attempt.php?id=$cm->id",
196 "$quiz->id", $cm->id
);
198 add_to_log($course->id
, 'quiz', 'attempt',
199 "review.php?attempt=$attempt->id",
200 "$quiz->id", $cm->id
);
203 // log continuation of attempt only if some time has lapsed
204 if (($timestamp - $attempt->timemodified
) > 600) { // 10 minutes have elapsed
205 add_to_log($course->id
, 'quiz', 'continue attemp', // this action used to be called 'continue attempt' but the database field has only 15 characters
206 "review.php?attempt=$attempt->id",
207 "$quiz->id", $cm->id
);
210 if (!$attempt->timestart
) { // shouldn't really happen, just for robustness
211 debugging('timestart was not set for this attempt. That should be impossible.', DEBUG_DEVELOPER
);
212 $attempt->timestart
= $timestamp - 1;
215 /// Load all the questions and states needed by this script
217 // list of questions needed by page
218 $pagelist = quiz_questions_on_page($attempt->layout
, $page);
221 $questionlist = quiz_questions_in_quiz($attempt->layout
);
223 $questionlist = $pagelist;
226 // add all questions that are on the submitted form
228 $questionlist .= ','.$questionids;
231 if (!$questionlist) {
232 error(get_string('noquestionsfound', 'quiz'), 'view.php?q='.$quiz->id
);
235 $sql = "SELECT q.*, i.grade AS maxgrade, i.id AS instance".
236 " FROM {$CFG->prefix}question q,".
237 " {$CFG->prefix}quiz_question_instances i".
238 " WHERE i.quiz = '$quiz->id' AND q.id = i.question".
239 " AND q.id IN ($questionlist)";
241 // Load the questions
242 if (!$questions = get_records_sql($sql)) {
243 error(get_string('noquestionsfound', 'quiz'), 'view.php?q='.$quiz->id
);
246 // Load the question type specific information
247 if (!get_question_options($questions)) {
248 error('Could not load question options');
251 // If the new attempt is to be based on a previous attempt find its id
252 $lastattemptid = false;
253 if ($newattempt and $attempt->attempt
> 1 and $quiz->attemptonlast
and !$attempt->preview
) {
254 // Find the previous attempt
255 if (!$lastattemptid = get_field('quiz_attempts', 'uniqueid', 'quiz', $attempt->quiz
, 'userid', $attempt->userid
, 'attempt', $attempt->attempt
-1)) {
256 error('Could not find previous attempt to build on');
260 // Restore the question sessions to their most recent states
261 // creating new sessions where required
262 if (!$states = get_question_states($questions, $quiz, $attempt, $lastattemptid)) {
263 error('Could not restore question sessions');
266 // Save all the newly created states
268 foreach ($questions as $i => $question) {
269 save_question_session($questions[$i], $states[$i]);
273 /// Process form data /////////////////////////////////////////////////
275 if ($responses = data_submitted() and empty($_POST['quizpassword'])) {
277 // set the default event. This can be overruled by individual buttons.
278 $event = (array_key_exists('markall', $responses)) ? QUESTION_EVENTSUBMIT
:
279 ($finishattempt ? QUESTION_EVENTCLOSE
: QUESTION_EVENTSAVE
);
281 // Unset any variables we know are not responses
282 unset($responses->id
);
283 unset($responses->q
);
284 unset($responses->oldpage
);
285 unset($responses->newpage
);
286 unset($responses->review
);
287 unset($responses->questionids
);
288 unset($responses->saveattempt
); // responses get saved anway
289 unset($responses->finishattempt
); // same as $finishattempt
290 unset($responses->markall
);
291 unset($responses->forcenewattempt
);
294 // $actions is an array indexed by the questions ids
295 $actions = question_extract_responses($questions, $responses, $event);
297 // Process each question in turn
299 $questionidarray = explode(',', $questionids);
300 foreach($questionidarray as $i) {
301 if (!isset($actions[$i])) {
302 $actions[$i]->responses
= array('' => '');
303 $actions[$i]->event
= QUESTION_EVENTOPEN
;
305 $actions[$i]->timestamp
= $timestamp;
306 question_process_responses($questions[$i], $states[$i], $actions[$i], $quiz, $attempt);
307 save_question_session($questions[$i], $states[$i]);
310 $attempt->timemodified
= $timestamp;
312 // We have now finished processing form data
315 // Finish attempt if requested
316 if ($finishattempt) {
318 // Set the attempt to be finished
319 $attempt->timefinish
= $timestamp;
321 // load all the questions
322 $closequestionlist = quiz_questions_in_quiz($attempt->layout
);
323 $sql = "SELECT q.*, i.grade AS maxgrade, i.id AS instance".
324 " FROM {$CFG->prefix}question q,".
325 " {$CFG->prefix}quiz_question_instances i".
326 " WHERE i.quiz = '$quiz->id' AND q.id = i.question".
327 " AND q.id IN ($closequestionlist)";
328 if (!$closequestions = get_records_sql($sql)) {
329 error('Questions missing');
332 // Load the question type specific information
333 if (!get_question_options($closequestions)) {
334 error('Could not load question options');
337 // Restore the question sessions
338 if (!$closestates = get_question_states($closequestions, $quiz, $attempt)) {
339 error('Could not restore question sessions');
342 foreach($closequestions as $key => $question) {
343 $action->event
= QUESTION_EVENTCLOSE
;
344 $action->responses
= $closestates[$key]->responses
;
345 $action->timestamp
= $closestates[$key]->timestamp
;
346 question_process_responses($question, $closestates[$key], $action, $quiz, $attempt);
347 save_question_session($question, $closestates[$key]);
350 add_to_log($course->id
, 'quiz', 'close attempt',
351 "review.php?attempt=$attempt->id",
352 "$quiz->id", $cm->id
);
355 // Update the quiz attempt and the overall grade for the quiz
356 if ($responses ||
$finishattempt) {
357 if (!update_record('quiz_attempts', $attempt)) {
358 error('Failed to save the current quiz attempt!');
360 if (($attempt->attempt
> 1 ||
$attempt->timefinish
> 0) and !$attempt->preview
) {
361 quiz_save_best_grade($quiz);
365 /// Send emails to those who have the capability set
366 if ($finishattempt && !$attempt->preview
) {
367 quiz_send_notification_emails($course, $quiz, $attempt, $context, $cm);
370 /// Check access to quiz page
372 // check the quiz times
373 if ($timestamp < $quiz->timeopen ||
($quiz->timeclose
and $timestamp > $quiz->timeclose
)) {
375 notify(get_string('notavailabletostudents', 'quiz'));
377 notice(get_string('notavailable', 'quiz'), "view.php?id={$cm->id}");
381 if ($finishattempt) {
382 redirect('review.php?attempt='.$attempt->id
, 0);
385 /// Print the quiz page ////////////////////////////////////////////////////////
387 // Print the page header
388 $pagequestions = explode(',', $pagelist);
389 $headtags = get_html_head_contributions($pagequestions, $questions, $states);
390 if (!empty($popup)) {
391 define('MESSAGE_WINDOW', true); // This prevents the message window coming up
392 print_header($course->shortname
.': '.format_string($quiz->name
), '', '', '', $headtags, false, '', '', false, '');
393 include('protect_js.php');
395 $strupdatemodule = has_capability('moodle/course:manageactivities', $coursecontext)
396 ?
update_module_button($cm->id
, $course->id
, get_string('modulename', 'quiz'))
399 $navlinks[] = array('name' => $strquizzes, 'link' => "index.php?id=$course->id", 'type' => 'activity');
400 $navlinks[] = array('name' => format_string($quiz->name
), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
401 $navlinks[] = array('name' => $strattemptnum, 'link' => '', 'type' => 'title');
403 $navigation = build_navigation($navlinks);
405 print_header_simple(format_string($quiz->name
), "", $navigation, "", $headtags, true, $strupdatemodule);
408 echo '<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>'; // for overlib
410 // Print the quiz name heading and tabs for teacher, etc.
412 $currenttab = 'preview';
415 print_heading(get_string('previewquiz', 'quiz', format_string($quiz->name
)));
416 unset($buttonoptions);
417 $buttonoptions['q'] = $quiz->id
;
418 $buttonoptions['forcenew'] = true;
419 print_single_button($CFG->wwwroot
.'/mod/quiz/attempt.php', $buttonoptions, get_string('startagain', 'quiz'));
421 notify(get_string('popupnotice', 'quiz'));
424 if ($quiz->attempts
!= 1) {
425 print_heading(format_string($quiz->name
).' - '.$strattemptnum);
427 print_heading(format_string($quiz->name
));
432 echo '<form id="responseform" method="post" action="attempt.php?q=', s($quiz->id
), '&page=', s($page),
433 '" enctype="multipart/form-data" onclick="this.autocomplete=\'off\'">', "\n";
434 if($quiz->timelimit
> 0) {
435 // Make sure javascript is enabled for time limited quizzes
437 <script type
="text/javascript">
442 <?php
print_heading(get_string('noscript', 'quiz')); ?
>
448 // Add a hidden field with the quiz id
451 // Print the navigation panel if required
452 $numpages = quiz_number_of_pages($attempt->layout
);
455 <script type
="text/javascript">
457 function navigate(page
) {
458 var ourForm
= document
.getElementById('responseform');
459 ourForm
.action
= ourForm
.action
.replace(/page
=.*/
, 'page=' + page
);
460 if (ourForm
.onsubmit
) {
468 quiz_print_navigation_panel($page, $numpages);
471 /// Print all the questions
473 // Add a hidden field with questionids
474 echo '<input type="hidden" name="questionids" value="'.$pagelist."\" />\n";
476 $number = quiz_first_questionnumber($attempt->layout
, $pagelist);
477 foreach ($pagequestions as $i) {
478 $options = quiz_get_renderoptions($quiz->review
, $states[$i]);
479 // Print the question
480 print_question($questions[$i], $states[$i], $number, $quiz, $options);
481 save_question_session($questions[$i], $states[$i]);
482 $number +
= $questions[$i]->length
;
485 // Print the submit buttons
486 $strconfirmattempt = addslashes(get_string("confirmclose", "quiz"));
487 $onclick = "return confirm('$strconfirmattempt')";
488 echo "<div class=\"submitbtns mdl-align\">\n";
490 echo "<input type=\"submit\" name=\"saveattempt\" value=\"".get_string("savenosubmit", "quiz")."\" />\n";
491 if ($quiz->optionflags
& QUESTION_ADAPTIVE
) {
492 echo "<input type=\"submit\" name=\"markall\" value=\"".get_string("markall", "quiz")."\" />\n";
494 echo "<input type=\"submit\" name=\"finishattempt\" value=\"".get_string("finishattempt", "quiz")."\" onclick=\"$onclick\" />\n";
495 echo '<input type="hidden" name="timeup" id="timeup" value="0" />';
499 // Print the navigation panel if required
501 quiz_print_navigation_panel($page, $numpages);
508 $secondsleft = ($quiz->timeclose ?
$quiz->timeclose
: 999999999999) - time();
510 // For teachers ignore the quiz closing time
511 $secondsleft = 999999999999;
514 // If time limit is set include floating timer.
515 // MDL-7495, no timer for users with disability
516 if ($quiz->timelimit
> 0 && !has_capability('mod/quiz:ignoretimelimits', $context, NULL, false)) {
517 $timesincestart = time() - $attempt->timestart
;
518 $timerstartvalue = min($quiz->timelimit
*60 - $timesincestart, $secondsleft);
519 if ($timerstartvalue <= 0) {
520 $timerstartvalue = 1;
523 require('jstimer.php');
525 // Add the javascript timer in the title bar if the closing time appears close
526 if ($secondsleft > 0 and $secondsleft < 24*3600) { // less than a day remaining
527 include('jsclock.php');
533 print_footer($course);