|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00032 require_once(dirname(__FILE__) . '/../../config.php'); 00033 require_once($CFG->dirroot . '/mod/quiz/locallib.php'); 00034 00035 // Remember the current time as the time any responses were submitted 00036 // (so as to make sure students don't get penalized for slow processing on this page) 00037 $timenow = time(); 00038 00039 // Get submitted parameters. 00040 $attemptid = required_param('attempt', PARAM_INT); 00041 $next = optional_param('next', false, PARAM_BOOL); 00042 $thispage = optional_param('thispage', 0, PARAM_INT); 00043 $nextpage = optional_param('nextpage', 0, PARAM_INT); 00044 $finishattempt = optional_param('finishattempt', 0, PARAM_BOOL); 00045 $timeup = optional_param('timeup', 0, PARAM_BOOL); // True if form was submitted by timer. 00046 $scrollpos = optional_param('scrollpos', '', PARAM_RAW); 00047 00048 $transaction = $DB->start_delegated_transaction(); 00049 $attemptobj = quiz_attempt::create($attemptid); 00050 00051 // Set $nexturl now. 00052 if ($next) { 00053 $page = $nextpage; 00054 } else { 00055 $page = $thispage; 00056 } 00057 if ($page == -1) { 00058 $nexturl = $attemptobj->summary_url(); 00059 } else { 00060 $nexturl = $attemptobj->attempt_url(null, $page); 00061 if ($scrollpos !== '') { 00062 $nexturl->param('scrollpos', $scrollpos); 00063 } 00064 } 00065 00066 // We treat automatically closed attempts just like normally closed attempts 00067 if ($timeup) { 00068 $finishattempt = 1; 00069 } 00070 00071 // Check login. 00072 require_login($attemptobj->get_course(), false, $attemptobj->get_cm()); 00073 require_sesskey(); 00074 00075 // Check that this attempt belongs to this user. 00076 if ($attemptobj->get_userid() != $USER->id) { 00077 throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'notyourattempt'); 00078 } 00079 00080 // Check capabilities. 00081 if (!$attemptobj->is_preview_user()) { 00082 $attemptobj->require_capability('mod/quiz:attempt'); 00083 } 00084 00085 // If the attempt is already closed, send them to the review page. 00086 if ($attemptobj->is_finished()) { 00087 throw new moodle_quiz_exception($attemptobj->get_quizobj(), 00088 'attemptalreadyclosed', null, $attemptobj->review_url()); 00089 } 00090 00091 // Don't log - we will end with a redirect to a page that is logged. 00092 00093 if (!$finishattempt) { 00094 // Just process the responses for this page and go to the next page. 00095 try { 00096 $attemptobj->process_all_actions($timenow); 00097 00098 } catch (question_out_of_sequence_exception $e) { 00099 print_error('submissionoutofsequencefriendlymessage', 'question', 00100 $attemptobj->attempt_url(null, $thispage)); 00101 00102 } catch (Exception $e) { 00103 // This sucks, if we display our own custom error message, there is no way 00104 // to display the original stack trace. 00105 $debuginfo = ''; 00106 if (!empty($e->debuginfo)) { 00107 $debuginfo = $e->debuginfo; 00108 } 00109 print_error('errorprocessingresponses', 'question', 00110 $attemptobj->attempt_url(null, $thispage), $e->getMessage(), $debuginfo); 00111 } 00112 00113 $transaction->allow_commit(); 00114 redirect($nexturl); 00115 } 00116 00117 // Otherwise, we have been asked to finish attempt, so do that. 00118 00119 // Log the end of this attempt. 00120 add_to_log($attemptobj->get_courseid(), 'quiz', 'close attempt', 00121 'review.php?attempt=' . $attemptobj->get_attemptid(), 00122 $attemptobj->get_quizid(), $attemptobj->get_cmid()); 00123 00124 // Update the quiz attempt record. 00125 try { 00126 $attemptobj->finish_attempt($timenow); 00127 00128 } catch (question_out_of_sequence_exception $e) { 00129 print_error('submissionoutofsequencefriendlymessage', 'question', 00130 $attemptobj->attempt_url(null, $thispage)); 00131 00132 } catch (Exception $e) { 00133 // This sucks, if we display our own custom error message, there is no way 00134 // to display the original stack trace. 00135 $debuginfo = ''; 00136 if (!empty($e->debuginfo)) { 00137 $debuginfo = $e->debuginfo; 00138 } 00139 print_error('errorprocessingresponses', 'question', 00140 $attemptobj->attempt_url(null, $thispage), $e->getMessage(), $debuginfo); 00141 } 00142 00143 // Send the user to the review page. 00144 $transaction->allow_commit(); 00145 redirect($attemptobj->review_url());