Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / quiz / attempt.php
blob1bd5de98f911617fc5c4d8ca9523a1da4f4c2376
1 <?php // $Id$
2 /**
3 * This page prints a particular instance of quiz
5 * @author Martin Dougiamas and many others. This has recently been completely
6 * rewritten by Alex Smith, Julian Sedding and Gustav Delius as part of
7 * the Serving Mathematics project
8 * {@link http://maths.york.ac.uk/serving_maths}
9 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
10 * @package quiz
13 require_once("../../config.php");
14 require_once("locallib.php");
16 // remember the current time as the time any responses were submitted
17 // (so as to make sure students don't get penalized for slow processing on this page)
18 $timestamp = time();
20 // Get submitted parameters.
21 $id = optional_param('id', 0, PARAM_INT); // Course Module ID
22 $q = optional_param('q', 0, PARAM_INT); // or quiz ID
23 $page = optional_param('page', 0, PARAM_INT);
24 $questionids = optional_param('questionids', '');
25 $finishattempt = optional_param('finishattempt', 0, PARAM_BOOL);
26 $timeup = optional_param('timeup', 0, PARAM_BOOL); // True if form was submitted by timer.
27 $forcenew = optional_param('forcenew', false, PARAM_BOOL); // Teacher has requested new preview
29 if ($id) {
30 if (! $cm = get_coursemodule_from_id('quiz', $id)) {
31 error("There is no coursemodule with id $id");
33 if (! $course = get_record("course", "id", $cm->course)) {
34 error("Course is misconfigured");
36 if (! $quiz = get_record("quiz", "id", $cm->instance)) {
37 error("The quiz with id $cm->instance corresponding to this coursemodule $id is missing");
39 } else {
40 if (! $quiz = get_record("quiz", "id", $q)) {
41 error("There is no quiz with id $q");
43 if (! $course = get_record("course", "id", $quiz->course)) {
44 error("The course with id $quiz->course that the quiz with id $q belongs to is missing");
46 if (! $cm = get_coursemodule_from_instance("quiz", $quiz->id, $course->id)) {
47 error("The course module for the quiz with id $q is missing");
51 // We treat automatically closed attempts just like normally closed attempts
52 if ($timeup) {
53 $finishattempt = 1;
56 require_login($course->id, false, $cm);
58 $coursecontext = get_context_instance(CONTEXT_COURSE, $cm->course); // course context
59 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
60 $ispreviewing = has_capability('mod/quiz:preview', $context);
62 // if no questions have been set up yet redirect to edit.php
63 if (!$quiz->questions and has_capability('mod/quiz:manage', $context)) {
64 redirect($CFG->wwwroot . '/mod/quiz/edit.php?cmid=' . $cm->id);
67 if (!$ispreviewing) {
68 require_capability('mod/quiz:attempt', $context);
71 /// Get number for the next or unfinished attempt
72 if(!$attemptnumber = (int)get_field_sql('SELECT MAX(attempt)+1 FROM ' .
73 "{$CFG->prefix}quiz_attempts WHERE quiz = '{$quiz->id}' AND " .
74 "userid = '{$USER->id}' AND timefinish > 0 AND preview != 1")) {
75 $attemptnumber = 1;
78 $strattemptnum = get_string('attempt', 'quiz', $attemptnumber);
79 $strquizzes = get_string("modulenameplural", "quiz");
80 $popup = $quiz->popup && !$ispreviewing; // Controls whether this is shown in a javascript-protected window.
82 /// We intentionally do not check open and close times here. Instead we do it lower down.
83 /// This is to deal with what happens when someone submits close to the exact moment when the quiz closes.
85 /// Check number of attempts
86 $numberofpreviousattempts = count_records_select('quiz_attempts', "quiz = '{$quiz->id}' AND " .
87 "userid = '{$USER->id}' AND timefinish > 0 AND preview != 1");
88 if ($quiz->attempts and $numberofpreviousattempts >= $quiz->attempts) {
89 print_error('nomoreattempts', 'quiz', "view.php?id={$cm->id}");
92 /// Check subnet access
93 if (!$ispreviewing && $quiz->subnet && !address_in_subnet(getremoteaddr(), $quiz->subnet)) {
94 print_error("subneterror", "quiz", "view.php?id=$cm->id");
97 /// Check password access
98 if ($ispreviewing && $forcenew) {
99 unset($SESSION->passwordcheckedquizzes[$quiz->id]);
102 if ($quiz->password and empty($SESSION->passwordcheckedquizzes[$quiz->id])) {
103 $enteredpassword = optional_param('quizpassword', '', PARAM_RAW);
104 if (optional_param('cancelpassword', false)) {
105 // User clicked cancel in the password form.
106 redirect($CFG->wwwroot . '/mod/quiz/view.php?q=' . $quiz->id);
107 } else if (strcmp($quiz->password, $enteredpassword) === 0) {
108 // User entered the correct password.
109 $SESSION->passwordcheckedquizzes[$quiz->id] = true;
110 } else {
111 // User entered the wrong password, or has not entered one yet.
112 $url = $CFG->wwwroot . '/mod/quiz/attempt.php?q=' . $quiz->id;
114 if (empty($popup)) {
115 print_header('', '', '', 'quizpassword');
118 if (trim(strip_tags($quiz->intro))) {
119 $formatoptions->noclean = true;
120 print_box(format_text($quiz->intro, FORMAT_MOODLE, $formatoptions), 'generalbox', 'intro');
122 print_box_start('generalbox', 'passwordbox');
123 if (!empty($enteredpassword)) {
124 echo '<p class="notifyproblem">', get_string('passworderror', 'quiz'), '</p>';
127 <p><?php print_string('requirepasswordmessage', 'quiz'); ?></p>
128 <form id="passwordform" method="post" action="<?php echo $url; ?>" onclick="this.autocomplete='off'">
129 <div>
130 <label for="quizpassword"><?php print_string('password'); ?></label>
131 <input name="quizpassword" id="quizpassword" type="password" value=""/>
132 <input type="submit" value="<?php print_string('ok'); ?>" />
133 <input type="submit" name="cancelpassword" value="<?php print_string('cancel'); ?>" />
134 </div>
135 </form>
136 <?php
137 print_box_end();
138 if (empty($popup)) {
139 print_footer();
141 exit;
145 if ($quiz->delay1 or $quiz->delay2) {
146 //quiz enforced time delay
147 if ($attempts = quiz_get_user_attempts($quiz->id, $USER->id)) {
148 $numattempts = count($attempts);
149 } else {
150 $numattempts = 0;
152 $timenow = time();
153 $lastattempt_obj = get_record_select('quiz_attempts', "quiz = $quiz->id AND attempt = $numattempts AND userid = $USER->id", 'timefinish');
154 if ($lastattempt_obj) {
155 $lastattempt = $lastattempt_obj->timefinish;
157 if ($numattempts == 1 && $quiz->delay1) {
158 if ($timenow - $quiz->delay1 < $lastattempt) {
159 print_error('timedelay', 'quiz', 'view.php?q='.$quiz->id);
161 } else if($numattempts > 1 && $quiz->delay2) {
162 if ($timenow - $quiz->delay2 < $lastattempt) {
163 print_error('timedelay', 'quiz', 'view.php?q='.$quiz->id);
168 /// Load attempt or create a new attempt if there is no unfinished one
170 if ($ispreviewing and $forcenew) { // teacher wants a new preview
171 // so we set a finish time on the current attempt (if any).
172 // It will then automatically be deleted below
173 set_field('quiz_attempts', 'timefinish', $timestamp, 'quiz', $quiz->id, 'userid', $USER->id);
176 $attempt = quiz_get_user_attempt_unfinished($quiz->id, $USER->id);
178 $newattempt = false;
179 if (!$attempt) {
180 // Delete any previous preview attempts belonging to this user.
181 if ($oldattempts = get_records_select('quiz_attempts', "quiz = '$quiz->id'
182 AND userid = '$USER->id' AND preview = 1")) {
183 foreach ($oldattempts as $oldattempt) {
184 quiz_delete_attempt($oldattempt, $quiz);
187 $newattempt = true;
188 // Start a new attempt and initialize the question sessions
189 $attempt = quiz_create_attempt($quiz, $attemptnumber);
190 // If this is an attempt by a teacher mark it as a preview
191 if ($ispreviewing) {
192 $attempt->preview = 1;
194 // Save the attempt
195 if (!$attempt->id = insert_record('quiz_attempts', $attempt)) {
196 error('Could not create new attempt');
198 // make log entries
199 if ($ispreviewing) {
200 add_to_log($course->id, 'quiz', 'preview',
201 "attempt.php?id=$cm->id",
202 "$quiz->id", $cm->id);
203 } else {
204 add_to_log($course->id, 'quiz', 'attempt',
205 "review.php?attempt=$attempt->id",
206 "$quiz->id", $cm->id);
208 } else {
209 // log continuation of attempt only if some time has lapsed
210 if (($timestamp - $attempt->timemodified) > 600) { // 10 minutes have elapsed
211 add_to_log($course->id, 'quiz', 'continue attemp', // this action used to be called 'continue attempt' but the database field has only 15 characters
212 "review.php?attempt=$attempt->id",
213 "$quiz->id", $cm->id);
216 if (!$attempt->timestart) { // shouldn't really happen, just for robustness
217 debugging('timestart was not set for this attempt. That should be impossible.', DEBUG_DEVELOPER);
218 $attempt->timestart = $timestamp - 1;
221 /// Load all the questions and states needed by this script
223 // list of questions needed by page
224 $pagelist = quiz_questions_on_page($attempt->layout, $page);
226 if ($newattempt) {
227 $questionlist = quiz_questions_in_quiz($attempt->layout);
228 } else {
229 $questionlist = $pagelist;
232 // add all questions that are on the submitted form
233 if ($questionids) {
234 $questionlist .= ','.$questionids;
237 if (!$questionlist) {
238 print_error('noquestionsfound', 'quiz', 'view.php?q='.$quiz->id);
241 $sql = "SELECT q.*, i.grade AS maxgrade, i.id AS instance".
242 " FROM {$CFG->prefix}question q,".
243 " {$CFG->prefix}quiz_question_instances i".
244 " WHERE i.quiz = '$quiz->id' AND q.id = i.question".
245 " AND q.id IN ($questionlist)";
247 // Load the questions
248 if (!$questions = get_records_sql($sql)) {
249 print_error('noquestionsfound', 'quiz', 'view.php?q='.$quiz->id);
252 // Load the question type specific information
253 if (!get_question_options($questions)) {
254 error('Could not load question options');
257 // If the new attempt is to be based on a previous attempt find its id
258 $lastattemptid = false;
259 if ($newattempt and $attempt->attempt > 1 and $quiz->attemptonlast and !$attempt->preview) {
260 // Find the previous attempt
261 if (!$lastattemptid = get_field('quiz_attempts', 'uniqueid', 'quiz', $attempt->quiz, 'userid', $attempt->userid, 'attempt', $attempt->attempt-1)) {
262 error('Could not find previous attempt to build on');
266 // Restore the question sessions to their most recent states
267 // creating new sessions where required
268 if (!$states = get_question_states($questions, $quiz, $attempt, $lastattemptid)) {
269 error('Could not restore question sessions');
272 // Save all the newly created states
273 if ($newattempt) {
274 foreach ($questions as $i => $question) {
275 save_question_session($questions[$i], $states[$i]);
279 /// Process form data /////////////////////////////////////////////////
281 if ($responses = data_submitted() and empty($responses->quizpassword)) {
283 // set the default event. This can be overruled by individual buttons.
284 $event = (array_key_exists('markall', $responses)) ? QUESTION_EVENTSUBMIT :
285 ($finishattempt ? QUESTION_EVENTCLOSE : QUESTION_EVENTSAVE);
287 // Unset any variables we know are not responses
288 unset($responses->id);
289 unset($responses->q);
290 unset($responses->oldpage);
291 unset($responses->newpage);
292 unset($responses->review);
293 unset($responses->questionids);
294 unset($responses->saveattempt); // responses get saved anway
295 unset($responses->finishattempt); // same as $finishattempt
296 unset($responses->markall);
297 unset($responses->forcenewattempt);
299 // extract responses
300 // $actions is an array indexed by the questions ids
301 $actions = question_extract_responses($questions, $responses, $event);
303 // Process each question in turn
305 $questionidarray = explode(',', $questionids);
306 $success = true;
307 foreach($questionidarray as $i) {
308 if (!isset($actions[$i])) {
309 $actions[$i]->responses = array('' => '');
310 $actions[$i]->event = QUESTION_EVENTOPEN;
312 $actions[$i]->timestamp = $timestamp;
313 if (question_process_responses($questions[$i], $states[$i], $actions[$i], $quiz, $attempt)) {
314 save_question_session($questions[$i], $states[$i]);
315 } else {
316 $success = false;
320 if (!$success) {
321 $pagebit = '';
322 if ($page) {
323 $pagebit = '&amp;page=' . $page;
325 print_error('errorprocessingresponses', 'question',
326 $CFG->wwwroot . '/mod/quiz/attempt.php?q=' . $quiz->id . $pagebit);
329 $attempt->timemodified = $timestamp;
331 // We have now finished processing form data
334 /// Finish attempt if requested
335 if ($finishattempt) {
337 // Set the attempt to be finished
338 $attempt->timefinish = $timestamp;
340 // load all the questions
341 $closequestionlist = quiz_questions_in_quiz($attempt->layout);
342 $sql = "SELECT q.*, i.grade AS maxgrade, i.id AS instance".
343 " FROM {$CFG->prefix}question q,".
344 " {$CFG->prefix}quiz_question_instances i".
345 " WHERE i.quiz = '$quiz->id' AND q.id = i.question".
346 " AND q.id IN ($closequestionlist)";
347 if (!$closequestions = get_records_sql($sql)) {
348 error('Questions missing');
351 // Load the question type specific information
352 if (!get_question_options($closequestions)) {
353 error('Could not load question options');
356 // Restore the question sessions
357 if (!$closestates = get_question_states($closequestions, $quiz, $attempt)) {
358 error('Could not restore question sessions');
361 $success = true;
362 foreach($closequestions as $key => $question) {
363 $action->event = QUESTION_EVENTCLOSE;
364 $action->responses = $closestates[$key]->responses;
365 $action->timestamp = $closestates[$key]->timestamp;
367 if (question_process_responses($question, $closestates[$key], $action, $quiz, $attempt)) {
368 save_question_session($question, $closestates[$key]);
369 } else {
370 $success = false;
374 if (!$success) {
375 $pagebit = '';
376 if ($page) {
377 $pagebit = '&amp;page=' . $page;
379 print_error('errorprocessingresponses', 'question',
380 $CFG->wwwroot . '/mod/quiz/attempt.php?q=' . $quiz->id . $pagebit);
383 add_to_log($course->id, 'quiz', 'close attempt',
384 "review.php?attempt=$attempt->id",
385 "$quiz->id", $cm->id);
388 /// Update the quiz attempt and the overall grade for the quiz
389 if ($responses || $finishattempt) {
390 if (!update_record('quiz_attempts', $attempt)) {
391 error('Failed to save the current quiz attempt!');
393 if (($attempt->attempt > 1 || $attempt->timefinish > 0) and !$attempt->preview) {
394 quiz_save_best_grade($quiz);
398 /// Send emails to those who have the capability set
399 if ($finishattempt && !$attempt->preview) {
400 quiz_send_notification_emails($course, $quiz, $attempt, $context, $cm);
403 if ($finishattempt) {
404 if (!empty($SESSION->passwordcheckedquizzes[$quiz->id])) {
405 unset($SESSION->passwordcheckedquizzes[$quiz->id]);
407 redirect($CFG->wwwroot . '/mod/quiz/review.php?attempt='.$attempt->id, 0);
410 // Now is the right time to check the open and close times.
411 if (!$ispreviewing && ($timestamp < $quiz->timeopen || ($quiz->timeclose && $timestamp > $quiz->timeclose))) {
412 print_error('notavailable', 'quiz', "view.php?id={$cm->id}");
415 /// Print the quiz page ////////////////////////////////////////////////////////
417 // Print the page header
418 require_js($CFG->wwwroot . '/mod/quiz/quiz.js');
419 $pagequestions = explode(',', $pagelist);
420 $headtags = get_html_head_contributions($pagequestions, $questions, $states);
421 if (!empty($popup)) {
422 define('MESSAGE_WINDOW', true); // This prevents the message window coming up
423 print_header($course->shortname.': '.format_string($quiz->name), '', '', '', $headtags, false, '', '', false, '');
424 include('protect_js.php');
425 } else {
426 $strupdatemodule = has_capability('moodle/course:manageactivities', $coursecontext)
427 ? update_module_button($cm->id, $course->id, get_string('modulename', 'quiz'))
428 : "";
429 $navigation = build_navigation($strattemptnum, $cm);
430 print_header_simple(format_string($quiz->name), "", $navigation, "", $headtags, true, $strupdatemodule);
433 echo '<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>'; // for overlib
435 // Print the quiz name heading and tabs for teacher, etc.
436 if ($ispreviewing) {
437 $currenttab = 'preview';
438 include('tabs.php');
440 print_heading(get_string('previewquiz', 'quiz', format_string($quiz->name)));
441 unset($buttonoptions);
442 $buttonoptions['q'] = $quiz->id;
443 $buttonoptions['forcenew'] = true;
444 echo '<div class="controls">';
445 print_single_button($CFG->wwwroot.'/mod/quiz/attempt.php', $buttonoptions, get_string('startagain', 'quiz'));
446 echo '</div>';
447 /// Notices about restrictions that would affect students.
448 if ($quiz->popup) {
449 notify(get_string('popupnotice', 'quiz'));
451 if ($timestamp < $quiz->timeopen || ($quiz->timeclose && $timestamp > $quiz->timeclose)) {
452 notify(get_string('notavailabletostudents', 'quiz'));
454 if ($quiz->subnet && !address_in_subnet(getremoteaddr(), $quiz->subnet)) {
455 notify(get_string('subnetnotice', 'quiz'));
457 } else {
458 if ($quiz->attempts != 1) {
459 print_heading(format_string($quiz->name).' - '.$strattemptnum);
460 } else {
461 print_heading(format_string($quiz->name));
465 // Start the form
466 echo '<form id="responseform" method="post" action="attempt.php?q=', s($quiz->id), '&amp;page=', s($page),
467 '" enctype="multipart/form-data"' .
468 ' onclick="this.autocomplete=\'off\'" onkeypress="return check_enter(event);">', "\n";
469 if($quiz->timelimit > 0) {
470 // Make sure javascript is enabled for time limited quizzes
472 <script type="text/javascript">
473 // Do nothing, but you have to have a script tag before a noscript tag.
474 </script>
475 <noscript>
476 <div>
477 <?php print_heading(get_string('noscript', 'quiz')); ?>
478 </div>
479 </noscript>
480 <?php
482 echo '<div>';
484 /// Print the navigation panel if required
485 $numpages = quiz_number_of_pages($attempt->layout);
486 if ($numpages > 1) {
487 quiz_print_navigation_panel($page, $numpages);
490 /// Print all the questions
491 $number = quiz_first_questionnumber($attempt->layout, $pagelist);
492 foreach ($pagequestions as $i) {
493 $options = quiz_get_renderoptions($quiz->review, $states[$i]);
494 // Print the question
495 print_question($questions[$i], $states[$i], $number, $quiz, $options);
496 save_question_session($questions[$i], $states[$i]);
497 $number += $questions[$i]->length;
500 /// Print the submit buttons
501 $strconfirmattempt = addslashes(get_string("confirmclose", "quiz"));
502 $onclick = "return confirm('$strconfirmattempt')";
503 echo "<div class=\"submitbtns mdl-align\">\n";
505 echo "<input type=\"submit\" name=\"saveattempt\" value=\"".get_string("savenosubmit", "quiz")."\" />\n";
506 if ($quiz->optionflags & QUESTION_ADAPTIVE) {
507 echo "<input type=\"submit\" name=\"markall\" value=\"".get_string("markall", "quiz")."\" />\n";
509 echo "<input type=\"submit\" name=\"finishattempt\" value=\"".get_string("finishattempt", "quiz")."\" onclick=\"$onclick\" />\n";
511 echo "</div>";
513 // Print the navigation panel if required
514 if ($numpages > 1) {
515 quiz_print_navigation_panel($page, $numpages);
518 // Finish the form
519 echo '</div>';
520 echo '<input type="hidden" name="timeup" id="timeup" value="0" />';
522 // Add a hidden field with questionids. Do this at the end of the form, so
523 // if you navigate before the form has finished loading, it does not wipe all
524 // the student's answers.
525 echo '<input type="hidden" name="questionids" value="'.$pagelist."\" />\n";
527 echo "</form>\n";
529 // If the quiz has a time limit, or if we are close to the close time, include a floating timer.
530 $showtimer = false;
531 $timerstartvalue = 999999999999;
532 if ($quiz->timeclose) {
533 $timerstartvalue = min($timerstartvalue, $quiz->timeclose - time());
534 $showtimer = $timerstartvalue < 60*60; // Show the timer if we are less than 60 mins from the deadline.
536 if ($quiz->timelimit > 0 && !has_capability('mod/quiz:ignoretimelimits', $context, NULL, false)) {
537 $timerstartvalue = min($timerstartvalue, $attempt->timestart + $quiz->timelimit*60- time());
538 $showtimer = true;
540 if ($showtimer && (!$ispreviewing || $timerstartvalue > 0)) {
541 $timerstartvalue = max($timerstartvalue, 1); // Make sure it starts just above zero.
542 require('jstimer.php');
545 // Finish the page
546 if (empty($popup)) {
547 print_footer($course);