2009-11-04 11:58:30 +00:00
|
|
|
<?php
|
2011-02-04 17:41:49 +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-08 16:33:47 +00:00
|
|
|
/**
|
2011-02-04 17:41:49 +00:00
|
|
|
* This script deals with starting a new attempt at a quiz.
|
2008-07-08 16:33:47 +00:00
|
|
|
*
|
|
|
|
* Normally, it will end up redirecting to attempt.php - unless a password form is displayed.
|
|
|
|
*
|
2008-07-15 16:31:22 +00:00
|
|
|
* This code used to be at the top of attempt.php, if you are looking for CVS history.
|
|
|
|
*
|
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
|
|
|
* @package mod_quiz
|
|
|
|
* @copyright 2009 The Open University
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2008-07-08 16:33:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
require_once(dirname(__FILE__) . '/../../config.php');
|
|
|
|
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
|
|
|
|
|
2011-02-04 17:41:49 +00:00
|
|
|
// Get submitted parameters.
|
2011-06-22 21:07:40 +01:00
|
|
|
$id = required_param('cmid', PARAM_INT); // Course module id
|
2008-07-08 16:33:47 +00:00
|
|
|
$forcenew = optional_param('forcenew', false, PARAM_BOOL); // Used to force a new preview
|
2012-01-08 14:14:09 -08:00
|
|
|
$page = optional_param('page', -1, PARAM_INT); // Page to jump to in the attempt.
|
2008-07-08 16:33:47 +00:00
|
|
|
|
|
|
|
if (!$cm = get_coursemodule_from_id('quiz', $id)) {
|
|
|
|
print_error('invalidcoursemodule');
|
|
|
|
}
|
|
|
|
if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
|
|
|
|
print_error("coursemisconf");
|
|
|
|
}
|
|
|
|
|
2011-10-05 15:54:57 +01:00
|
|
|
$quizobj = quiz::create($cm->instance, $USER->id);
|
2010-07-28 15:06:28 +00:00
|
|
|
// This script should only ever be posted to, so set page URL to the view page.
|
|
|
|
$PAGE->set_url($quizobj->view_url());
|
2008-07-08 16:33:47 +00:00
|
|
|
|
2011-02-04 17:41:49 +00:00
|
|
|
// Check login and sesskey.
|
2012-04-22 17:41:47 +02:00
|
|
|
require_login($quizobj->get_course(), false, $quizobj->get_cm());
|
2011-02-23 16:50:09 +00:00
|
|
|
require_sesskey();
|
2008-07-08 16:33:47 +00:00
|
|
|
|
2012-05-04 12:40:21 +01:00
|
|
|
// If no questions have been set up yet redirect to edit.php or display an error.
|
2012-01-11 16:21:47 +00:00
|
|
|
if (!$quizobj->has_questions()) {
|
|
|
|
if ($quizobj->has_capability('mod/quiz:manage')) {
|
|
|
|
redirect($quizobj->edit_url());
|
|
|
|
} else {
|
|
|
|
print_error('cannotstartnoquestions', 'quiz', $quizobj->view_url());
|
|
|
|
}
|
2008-07-08 16:33:47 +00:00
|
|
|
}
|
|
|
|
|
2011-02-04 17:41:49 +00:00
|
|
|
// Create an object to manage all the other (non-roles) access rules.
|
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
|
|
|
$timenow = time();
|
|
|
|
$accessmanager = $quizobj->get_access_manager($timenow);
|
2008-07-08 16:33:47 +00:00
|
|
|
if ($quizobj->is_preview_user() && $forcenew) {
|
2011-10-07 17:43:15 +01:00
|
|
|
$accessmanager->current_attempt_finished();
|
2008-07-08 16:33:47 +00:00
|
|
|
}
|
|
|
|
|
2011-02-04 17:41:49 +00:00
|
|
|
// Check capabilities.
|
2008-07-08 16:33:47 +00:00
|
|
|
if (!$quizobj->is_preview_user()) {
|
|
|
|
$quizobj->require_capability('mod/quiz:attempt');
|
|
|
|
}
|
|
|
|
|
2011-02-04 17:41:49 +00:00
|
|
|
// Check to see if a new preview was requested.
|
2008-07-08 16:33:47 +00:00
|
|
|
if ($quizobj->is_preview_user() && $forcenew) {
|
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 force the creation of a new preview, we mark the current attempt (if any)
|
2012-05-04 12:40:21 +01:00
|
|
|
// as finished. It will then automatically be deleted below.
|
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
|
|
|
$DB->set_field('quiz_attempts', 'state', quiz_attempt::FINISHED,
|
2011-10-05 15:54:57 +01:00
|
|
|
array('quiz' => $quizobj->get_quizid(), 'userid' => $USER->id));
|
2008-07-08 16:33:47 +00:00
|
|
|
}
|
|
|
|
|
2011-02-04 17:41:49 +00:00
|
|
|
// Look for an existing attempt.
|
2011-10-07 17:43:15 +01:00
|
|
|
$attempts = quiz_get_user_attempts($quizobj->get_quizid(), $USER->id, 'all', true);
|
2011-06-18 18:23:49 +01:00
|
|
|
$lastattempt = end($attempts);
|
2008-07-08 16:33:47 +00:00
|
|
|
|
2011-06-18 18:23:49 +01:00
|
|
|
// If an in-progress attempt exists, check password then redirect to it.
|
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 ($lastattempt && ($lastattempt->state == quiz_attempt::IN_PROGRESS ||
|
|
|
|
$lastattempt->state == quiz_attempt::OVERDUE)) {
|
2011-10-07 17:43:15 +01:00
|
|
|
$currentattemptid = $lastattempt->id;
|
|
|
|
$messages = $accessmanager->prevent_access();
|
2012-01-16 13:41:12 +08: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 the attempt is now overdue, deal with that.
|
2012-04-20 14:51:15 +01:00
|
|
|
$quizobj->create_attempt_object($lastattempt)->handle_if_time_expired($timenow, 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
|
|
|
|
|
|
|
// And, if the attempt is now no longer in progress, redirect to the appropriate place.
|
|
|
|
if ($lastattempt->state == quiz_attempt::OVERDUE) {
|
2013-05-31 20:24:50 +01:00
|
|
|
redirect($quizobj->summary_url($lastattempt->id));
|
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 if ($lastattempt->state != quiz_attempt::IN_PROGRESS) {
|
|
|
|
redirect($quizobj->review_url($lastattempt->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the page number was not explicitly in the URL, go to the current page.
|
2012-01-08 14:14:09 -08:00
|
|
|
if ($page == -1) {
|
|
|
|
$page = $lastattempt->currentpage;
|
|
|
|
}
|
2008-07-08 16:33:47 +00:00
|
|
|
|
|
|
|
} else {
|
2013-05-31 20:24:50 +01:00
|
|
|
while ($lastattempt && $lastattempt->preview) {
|
|
|
|
$lastattempt = array_pop($attempts);
|
|
|
|
}
|
|
|
|
|
2012-05-04 12:40:21 +01:00
|
|
|
// Get number for the next or unfinished attempt.
|
2013-03-28 11:15:23 +10:30
|
|
|
if ($lastattempt) {
|
2011-10-07 17:43:15 +01:00
|
|
|
$attemptnumber = $lastattempt->attempt + 1;
|
|
|
|
} else {
|
|
|
|
$lastattempt = false;
|
|
|
|
$attemptnumber = 1;
|
|
|
|
}
|
|
|
|
$currentattemptid = null;
|
|
|
|
|
|
|
|
$messages = $accessmanager->prevent_access() +
|
|
|
|
$accessmanager->prevent_new_attempt(count($attempts), $lastattempt);
|
2012-01-08 14:14:09 -08:00
|
|
|
|
|
|
|
if ($page == -1) {
|
2012-01-16 13:41:12 +08:00
|
|
|
$page = 0;
|
2012-01-08 14:14:09 -08:00
|
|
|
}
|
2008-07-08 16:33:47 +00:00
|
|
|
}
|
|
|
|
|
2011-02-04 17:41:49 +00:00
|
|
|
// Check access.
|
2011-10-07 17:43:15 +01:00
|
|
|
$output = $PAGE->get_renderer('mod_quiz');
|
2008-07-08 16:33:47 +00:00
|
|
|
if (!$quizobj->is_preview_user() && $messages) {
|
|
|
|
print_error('attempterror', 'quiz', $quizobj->view_url(),
|
2011-11-10 12:51:24 +00:00
|
|
|
$output->access_messages($messages));
|
2008-07-08 16:33:47 +00:00
|
|
|
}
|
2011-10-05 20:58:38 +01:00
|
|
|
|
2011-10-07 17:43:15 +01:00
|
|
|
if ($accessmanager->is_preflight_check_required($currentattemptid)) {
|
|
|
|
// Need to do some checks before allowing the user to continue.
|
|
|
|
$mform = $accessmanager->get_preflight_check_form(
|
|
|
|
$quizobj->start_attempt_url($page), $currentattemptid);
|
|
|
|
|
|
|
|
if ($mform->is_cancelled()) {
|
|
|
|
$accessmanager->back_to_view_page($output);
|
|
|
|
|
|
|
|
} else if (!$mform->get_data()) {
|
|
|
|
|
|
|
|
// Form not submitted successfully, re-display it and stop.
|
|
|
|
$PAGE->set_url($quizobj->start_attempt_url($page));
|
|
|
|
$PAGE->set_title(format_string($quizobj->get_quiz_name()));
|
|
|
|
$accessmanager->setup_attempt_page($PAGE);
|
|
|
|
if (empty($quizobj->get_quiz()->showblocks)) {
|
|
|
|
$PAGE->blocks->show_only_fake_blocks();
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $output->start_attempt_page($quizobj, $mform);
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pre-flight check passed.
|
|
|
|
$accessmanager->notify_preflight_check_passed($currentattemptid);
|
|
|
|
}
|
|
|
|
if ($currentattemptid) {
|
|
|
|
redirect($quizobj->attempt_url($currentattemptid, $page));
|
|
|
|
}
|
2008-07-08 16:33:47 +00:00
|
|
|
|
2011-02-04 17:41:49 +00:00
|
|
|
// Delete any previous preview attempts belonging to this user.
|
2011-10-05 15:54:57 +01:00
|
|
|
quiz_delete_previews($quizobj->get_quiz(), $USER->id);
|
2008-07-08 16:33:47 +00:00
|
|
|
|
2011-02-04 17:41:49 +00:00
|
|
|
$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
2011-10-05 15:54:57 +01:00
|
|
|
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
2011-02-04 17:41:49 +00:00
|
|
|
|
|
|
|
// Create the new attempt and initialize the question sessions
|
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
|
|
|
$timenow = time(); // Update time now, in case the server is running really slowly.
|
2013-07-06 13:22:44 +07:00
|
|
|
$attempt = quiz_create_attempt($quizobj, $attemptnumber, $lastattempt, $timenow, $quizobj->is_preview_user());
|
2008-07-08 16:33:47 +00:00
|
|
|
|
2011-10-05 15:54:57 +01:00
|
|
|
if (!($quizobj->get_quiz()->attemptonlast && $lastattempt)) {
|
2013-07-06 13:22:44 +07:00
|
|
|
$attempt = quiz_start_new_attempt($quizobj, $quba, $attempt, $attemptnumber, $timenow);
|
2011-02-04 17:41:49 +00:00
|
|
|
} else {
|
2013-07-06 13:22:44 +07:00
|
|
|
$attempt = quiz_start_attempt_built_on_last($quba, $attempt, $lastattempt);
|
2011-02-04 17:41:49 +00:00
|
|
|
}
|
|
|
|
|
2011-02-08 14:19:23 +00:00
|
|
|
$transaction = $DB->start_delegated_transaction();
|
2008-07-08 16:33:47 +00:00
|
|
|
|
2013-07-06 13:22:44 +07:00
|
|
|
$attempt = quiz_attempt_save_started($quizobj, $quba, $attempt);
|
|
|
|
quiz_fire_attempt_started_event($attempt, $quizobj);
|
2008-07-08 16:33:47 +00:00
|
|
|
|
2011-02-08 14:19:23 +00:00
|
|
|
$transaction->allow_commit();
|
2011-02-04 17:41:49 +00:00
|
|
|
|
|
|
|
// Redirect to the attempt page.
|
2011-06-22 21:07:40 +01:00
|
|
|
redirect($quizobj->attempt_url($attempt->id, $page));
|