2009-11-04 11:58:30 +00:00
|
|
|
<?php
|
2011-02-08 15:02:23 +00:00
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2008-07-15 16:46:24 +00:00
|
|
|
/**
|
|
|
|
* This page deals with processing responses during an attempt at a quiz.
|
|
|
|
*
|
|
|
|
* People will normally arrive here from a form submission on attempt.php or
|
|
|
|
* summary.php, and once the responses are processed, they will be redirected to
|
|
|
|
* attempt.php or summary.php.
|
|
|
|
*
|
|
|
|
* This code used to be near the top of attempt.php, if you are looking for CVS history.
|
|
|
|
*
|
2011-02-21 16:13:25 +00:00
|
|
|
* @package mod
|
2011-02-08 15:02:23 +00:00
|
|
|
* @subpackage quiz
|
2011-02-21 16:13:25 +00:00
|
|
|
* @copyright 2009 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2008-07-15 16:46:24 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
require_once(dirname(__FILE__) . '/../../config.php');
|
|
|
|
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
|
|
|
|
|
2011-02-25 15:36:07 +00:00
|
|
|
// Remember the current time as the time any responses were submitted
|
2012-05-04 12:40:21 +01:00
|
|
|
// (so as to make sure students don't get penalized for slow processing on this page).
|
2008-07-15 16:46:24 +00:00
|
|
|
$timenow = time();
|
|
|
|
|
2011-02-25 15:36:07 +00:00
|
|
|
// Get submitted parameters.
|
2012-07-27 11:42:03 +01:00
|
|
|
$attemptid = required_param('attempt', PARAM_INT);
|
2012-08-01 09:05:32 +08:00
|
|
|
$thispage = optional_param('thispage', 0, PARAM_INT);
|
|
|
|
$nextpage = optional_param('nextpage', 0, PARAM_INT);
|
2012-07-27 11:42:03 +01:00
|
|
|
$next = optional_param('next', false, PARAM_BOOL);
|
MDL-3030 quiz overdue handling: trigger automatic state transitions.
Here, we catch all the places where a student might be accessing their
own attempts, and make sure any automatic state transitions that
should happen, do happen, before the student sees the attempt.
The places where we need to check this are view.php, startattempt.php
and processattempt.php.
We do not really need to check attempt.php or summary.php, because if
the student is on one of those pages, the JavaScript timer will
auto-submit when time expires, taking them to processattempt.php,
which will do the acutal work.
We intentionally do not trigger state transition when a teacher is
looking at a student's quiz attemp. We will trigger state transitions
on cron, but that is still to do.
Also, the body of the process_... methods still needs to be written.
2012-04-18 19:18:55 +01:00
|
|
|
$finishattempt = optional_param('finishattempt', false, PARAM_BOOL);
|
2012-07-27 11:42:03 +01:00
|
|
|
$timeup = optional_param('timeup', 0, PARAM_BOOL); // True if form was submitted by timer.
|
|
|
|
$scrollpos = optional_param('scrollpos', '', PARAM_RAW);
|
2008-07-15 16:46:24 +00:00
|
|
|
|
2011-02-08 15:02:23 +00:00
|
|
|
$transaction = $DB->start_delegated_transaction();
|
2010-03-08 16:01:38 +00:00
|
|
|
$attemptobj = quiz_attempt::create($attemptid);
|
2008-07-15 16:46:24 +00:00
|
|
|
|
2011-02-25 15:36:07 +00:00
|
|
|
// Set $nexturl now.
|
2011-02-08 15:02:23 +00:00
|
|
|
if ($next) {
|
|
|
|
$page = $nextpage;
|
|
|
|
} else {
|
|
|
|
$page = $thispage;
|
|
|
|
}
|
|
|
|
if ($page == -1) {
|
2008-07-15 16:46:24 +00:00
|
|
|
$nexturl = $attemptobj->summary_url();
|
|
|
|
} else {
|
2011-08-17 14:40:05 +01:00
|
|
|
$nexturl = $attemptobj->attempt_url(null, $page);
|
2011-02-08 15:02:23 +00:00
|
|
|
if ($scrollpos !== '') {
|
2011-02-09 20:33:51 +00:00
|
|
|
$nexturl->param('scrollpos', $scrollpos);
|
2011-02-08 15:02:23 +00:00
|
|
|
}
|
2008-07-15 16:46:24 +00:00
|
|
|
}
|
|
|
|
|
2012-04-20 16:55:52 +01:00
|
|
|
// If there is only a very small amount of time left, there is no point trying
|
MDL-3030 quiz overdue handling: trigger automatic state transitions.
Here, we catch all the places where a student might be accessing their
own attempts, and make sure any automatic state transitions that
should happen, do happen, before the student sees the attempt.
The places where we need to check this are view.php, startattempt.php
and processattempt.php.
We do not really need to check attempt.php or summary.php, because if
the student is on one of those pages, the JavaScript timer will
auto-submit when time expires, taking them to processattempt.php,
which will do the acutal work.
We intentionally do not trigger state transition when a teacher is
looking at a student's quiz attemp. We will trigger state transitions
on cron, but that is still to do.
Also, the body of the process_... methods still needs to be written.
2012-04-18 19:18:55 +01:00
|
|
|
// to show the student another page of the quiz. Just finish now.
|
2012-06-07 19:17:07 +01:00
|
|
|
$graceperiodmin = null;
|
MDL-3030 quiz overdue handling: trigger automatic state transitions.
Here, we catch all the places where a student might be accessing their
own attempts, and make sure any automatic state transitions that
should happen, do happen, before the student sees the attempt.
The places where we need to check this are view.php, startattempt.php
and processattempt.php.
We do not really need to check attempt.php or summary.php, because if
the student is on one of those pages, the JavaScript timer will
auto-submit when time expires, taking them to processattempt.php,
which will do the acutal work.
We intentionally do not trigger state transition when a teacher is
looking at a student's quiz attemp. We will trigger state transitions
on cron, but that is still to do.
Also, the body of the process_... methods still needs to be written.
2012-04-18 19:18:55 +01:00
|
|
|
$accessmanager = $attemptobj->get_access_manager($timenow);
|
2012-10-09 13:48:30 -04:00
|
|
|
$timeclose = $accessmanager->get_end_time($attemptobj->get_attempt());
|
|
|
|
|
|
|
|
// Don't enforce timeclose for previews
|
|
|
|
if ($attemptobj->is_preview()) {
|
|
|
|
$timeclose = false;
|
|
|
|
}
|
2012-04-20 16:55:52 +01:00
|
|
|
$toolate = false;
|
2012-10-09 13:48:30 -04:00
|
|
|
if ($timeclose !== false && $timenow > $timeclose - QUIZ_MIN_TIME_TO_CONTINUE) {
|
MDL-3030 quiz overdue handling: trigger automatic state transitions.
Here, we catch all the places where a student might be accessing their
own attempts, and make sure any automatic state transitions that
should happen, do happen, before the student sees the attempt.
The places where we need to check this are view.php, startattempt.php
and processattempt.php.
We do not really need to check attempt.php or summary.php, because if
the student is on one of those pages, the JavaScript timer will
auto-submit when time expires, taking them to processattempt.php,
which will do the acutal work.
We intentionally do not trigger state transition when a teacher is
looking at a student's quiz attemp. We will trigger state transitions
on cron, but that is still to do.
Also, the body of the process_... methods still needs to be written.
2012-04-18 19:18:55 +01:00
|
|
|
$timeup = true;
|
2012-06-07 19:17:07 +01:00
|
|
|
$graceperiodmin = get_config('quiz', 'graceperiodmin');
|
2012-10-09 13:48:30 -04:00
|
|
|
if ($timenow > $timeclose + $graceperiodmin) {
|
2012-04-20 16:55:52 +01:00
|
|
|
$toolate = true;
|
|
|
|
}
|
2008-07-15 16:46:24 +00:00
|
|
|
}
|
|
|
|
|
2011-02-25 15:36:07 +00:00
|
|
|
// Check login.
|
2011-02-08 15:02:23 +00:00
|
|
|
require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
|
2009-11-02 17:10:31 +00:00
|
|
|
require_sesskey();
|
2008-07-15 16:46:24 +00:00
|
|
|
|
2011-02-25 15:36:07 +00:00
|
|
|
// Check that this attempt belongs to this user.
|
2008-07-15 16:46:24 +00:00
|
|
|
if ($attemptobj->get_userid() != $USER->id) {
|
2011-02-08 15:02:23 +00:00
|
|
|
throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'notyourattempt');
|
2008-07-15 16:46:24 +00:00
|
|
|
}
|
|
|
|
|
2011-02-25 15:36:07 +00:00
|
|
|
// Check capabilities.
|
2008-07-15 16:46:24 +00:00
|
|
|
if (!$attemptobj->is_preview_user()) {
|
|
|
|
$attemptobj->require_capability('mod/quiz:attempt');
|
|
|
|
}
|
|
|
|
|
2011-02-25 15:36:07 +00:00
|
|
|
// If the attempt is already closed, send them to the review page.
|
2008-07-15 16:46:24 +00:00
|
|
|
if ($attemptobj->is_finished()) {
|
2011-04-04 20:11:29 +01:00
|
|
|
throw new moodle_quiz_exception($attemptobj->get_quizobj(),
|
|
|
|
'attemptalreadyclosed', null, $attemptobj->review_url());
|
2008-07-15 16:46:24 +00:00
|
|
|
}
|
|
|
|
|
MDL-3030 quiz overdue handling: trigger automatic state transitions.
Here, we catch all the places where a student might be accessing their
own attempts, and make sure any automatic state transitions that
should happen, do happen, before the student sees the attempt.
The places where we need to check this are view.php, startattempt.php
and processattempt.php.
We do not really need to check attempt.php or summary.php, because if
the student is on one of those pages, the JavaScript timer will
auto-submit when time expires, taking them to processattempt.php,
which will do the acutal work.
We intentionally do not trigger state transition when a teacher is
looking at a student's quiz attemp. We will trigger state transitions
on cron, but that is still to do.
Also, the body of the process_... methods still needs to be written.
2012-04-18 19:18:55 +01:00
|
|
|
// If time is running out, trigger the appropriate action.
|
|
|
|
$becomingoverdue = false;
|
2012-06-07 19:17:07 +01:00
|
|
|
$becomingabandoned = false;
|
MDL-3030 quiz overdue handling: trigger automatic state transitions.
Here, we catch all the places where a student might be accessing their
own attempts, and make sure any automatic state transitions that
should happen, do happen, before the student sees the attempt.
The places where we need to check this are view.php, startattempt.php
and processattempt.php.
We do not really need to check attempt.php or summary.php, because if
the student is on one of those pages, the JavaScript timer will
auto-submit when time expires, taking them to processattempt.php,
which will do the acutal work.
We intentionally do not trigger state transition when a teacher is
looking at a student's quiz attemp. We will trigger state transitions
on cron, but that is still to do.
Also, the body of the process_... methods still needs to be written.
2012-04-18 19:18:55 +01:00
|
|
|
if ($timeup) {
|
2012-06-08 10:47:31 +08:00
|
|
|
if ($attemptobj->get_quiz()->overduehandling == 'graceperiod') {
|
2012-06-07 19:17:07 +01:00
|
|
|
if (is_null($graceperiodmin)) {
|
|
|
|
$graceperiodmin = get_config('quiz', 'graceperiodmin');
|
|
|
|
}
|
2012-10-09 13:48:30 -04:00
|
|
|
if ($timenow > $timeclose + $attemptobj->get_quiz()->graceperiod + $graceperiodmin) {
|
2012-06-07 19:17:07 +01:00
|
|
|
// Grace period has run out.
|
|
|
|
$finishattempt = true;
|
|
|
|
$becomingabandoned = true;
|
|
|
|
} else {
|
|
|
|
$becomingoverdue = true;
|
|
|
|
}
|
MDL-3030 quiz overdue handling: trigger automatic state transitions.
Here, we catch all the places where a student might be accessing their
own attempts, and make sure any automatic state transitions that
should happen, do happen, before the student sees the attempt.
The places where we need to check this are view.php, startattempt.php
and processattempt.php.
We do not really need to check attempt.php or summary.php, because if
the student is on one of those pages, the JavaScript timer will
auto-submit when time expires, taking them to processattempt.php,
which will do the acutal work.
We intentionally do not trigger state transition when a teacher is
looking at a student's quiz attemp. We will trigger state transitions
on cron, but that is still to do.
Also, the body of the process_... methods still needs to be written.
2012-04-18 19:18:55 +01:00
|
|
|
} else {
|
|
|
|
$finishattempt = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-25 15:36:07 +00:00
|
|
|
// Don't log - we will end with a redirect to a page that is logged.
|
2008-07-15 16:46:24 +00:00
|
|
|
|
|
|
|
if (!$finishattempt) {
|
2011-02-25 15:36:07 +00:00
|
|
|
// Just process the responses for this page and go to the next page.
|
2012-04-20 16:55:52 +01:00
|
|
|
if (!$toolate) {
|
|
|
|
try {
|
|
|
|
$attemptobj->process_submitted_actions($timenow, $becomingoverdue);
|
|
|
|
|
|
|
|
} catch (question_out_of_sequence_exception $e) {
|
|
|
|
print_error('submissionoutofsequencefriendlymessage', 'question',
|
|
|
|
$attemptobj->attempt_url(null, $thispage));
|
|
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// This sucks, if we display our own custom error message, there is no way
|
|
|
|
// to display the original stack trace.
|
|
|
|
$debuginfo = '';
|
|
|
|
if (!empty($e->debuginfo)) {
|
|
|
|
$debuginfo = $e->debuginfo;
|
|
|
|
}
|
|
|
|
print_error('errorprocessingresponses', 'question',
|
|
|
|
$attemptobj->attempt_url(null, $thispage), $e->getMessage(), $debuginfo);
|
2011-10-25 14:49:57 +01:00
|
|
|
}
|
2012-04-20 16:55:52 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// The student is too late.
|
|
|
|
$attemptobj->process_going_overdue($timenow, true);
|
2011-02-08 15:02:23 +00:00
|
|
|
}
|
2011-10-25 14:49:57 +01:00
|
|
|
|
2011-02-08 15:02:23 +00:00
|
|
|
$transaction->allow_commit();
|
MDL-3030 quiz overdue handling: trigger automatic state transitions.
Here, we catch all the places where a student might be accessing their
own attempts, and make sure any automatic state transitions that
should happen, do happen, before the student sees the attempt.
The places where we need to check this are view.php, startattempt.php
and processattempt.php.
We do not really need to check attempt.php or summary.php, because if
the student is on one of those pages, the JavaScript timer will
auto-submit when time expires, taking them to processattempt.php,
which will do the acutal work.
We intentionally do not trigger state transition when a teacher is
looking at a student's quiz attemp. We will trigger state transitions
on cron, but that is still to do.
Also, the body of the process_... methods still needs to be written.
2012-04-18 19:18:55 +01:00
|
|
|
if ($becomingoverdue) {
|
|
|
|
redirect($attemptobj->summary_url());
|
|
|
|
} else {
|
|
|
|
redirect($nexturl);
|
|
|
|
}
|
2008-07-15 16:46:24 +00:00
|
|
|
}
|
|
|
|
|
2011-02-25 15:36:07 +00:00
|
|
|
// Otherwise, we have been asked to finish attempt, so do that.
|
2008-07-15 16:46:24 +00:00
|
|
|
|
2011-02-25 15:36:07 +00:00
|
|
|
// Log the end of this attempt.
|
2008-07-15 16:46:24 +00:00
|
|
|
add_to_log($attemptobj->get_courseid(), 'quiz', 'close attempt',
|
|
|
|
'review.php?attempt=' . $attemptobj->get_attemptid(),
|
|
|
|
$attemptobj->get_quizid(), $attemptobj->get_cmid());
|
|
|
|
|
2011-02-25 15:36:07 +00:00
|
|
|
// Update the quiz attempt record.
|
2011-10-25 14:49:57 +01:00
|
|
|
try {
|
2012-06-07 19:17:07 +01:00
|
|
|
if ($becomingabandoned) {
|
|
|
|
$attemptobj->process_abandon($timenow, true);
|
|
|
|
} else {
|
|
|
|
$attemptobj->process_finish($timenow, !$toolate);
|
|
|
|
}
|
2011-10-25 14:49:57 +01:00
|
|
|
|
|
|
|
} catch (question_out_of_sequence_exception $e) {
|
|
|
|
print_error('submissionoutofsequencefriendlymessage', 'question',
|
|
|
|
$attemptobj->attempt_url(null, $thispage));
|
|
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// This sucks, if we display our own custom error message, there is no way
|
|
|
|
// to display the original stack trace.
|
|
|
|
$debuginfo = '';
|
|
|
|
if (!empty($e->debuginfo)) {
|
|
|
|
$debuginfo = $e->debuginfo;
|
|
|
|
}
|
|
|
|
print_error('errorprocessingresponses', 'question',
|
|
|
|
$attemptobj->attempt_url(null, $thispage), $e->getMessage(), $debuginfo);
|
|
|
|
}
|
2008-07-15 16:46:24 +00:00
|
|
|
|
2011-02-25 15:36:07 +00:00
|
|
|
// Send the user to the review page.
|
2011-02-08 15:02:23 +00:00
|
|
|
$transaction->allow_commit();
|
2008-07-15 16:46:24 +00:00
|
|
|
redirect($attemptobj->review_url());
|