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
|
|
|
*/
|
|
|
|
|
2022-12-19 21:14:51 +00:00
|
|
|
use mod_quiz\quiz_attempt;
|
|
|
|
|
2016-02-26 17:47:58 +11:00
|
|
|
require_once(__DIR__ . '/../../config.php');
|
2008-07-08 16:33:47 +00:00
|
|
|
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)) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('invalidcoursemodule');
|
2008-07-08 16:33:47 +00:00
|
|
|
}
|
|
|
|
if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception("coursemisconf");
|
2008-07-08 16:33:47 +00:00
|
|
|
}
|
|
|
|
|
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());
|
2021-01-25 18:16:14 +00:00
|
|
|
// During quiz attempts, the browser back/forwards buttons should force a reload.
|
|
|
|
$PAGE->set_cacheable(false);
|
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();
|
2013-11-05 18:52:24 +08:00
|
|
|
$PAGE->set_heading($quizobj->get_course()->fullname);
|
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 {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('cannotstartnoquestions', 'quiz', $quizobj->view_url());
|
2012-01-11 16:21:47 +00:00
|
|
|
}
|
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);
|
2012-01-16 13:41:12 +08:00
|
|
|
|
2016-02-08 15:39:41 +01:00
|
|
|
// Validate permissions for creating a new attempt and start a new preview attempt if required.
|
|
|
|
list($currentattemptid, $attemptnumber, $lastattempt, $messages, $page) =
|
|
|
|
quiz_validate_new_attempt($quizobj, $accessmanager, $forcenew, $page, true);
|
2008-07-08 16:33:47 +00:00
|
|
|
|
2011-02-04 17:41:49 +00:00
|
|
|
// Check access.
|
2008-07-08 16:33:47 +00:00
|
|
|
if (!$quizobj->is_preview_user() && $messages) {
|
2014-06-22 21:30:04 +01:00
|
|
|
$output = $PAGE->get_renderer('mod_quiz');
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('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()) {
|
2014-06-22 21:30:04 +01:00
|
|
|
$accessmanager->back_to_view_page($PAGE->get_renderer('mod_quiz'));
|
2011-10-07 17:43:15 +01:00
|
|
|
|
|
|
|
} else if (!$mform->get_data()) {
|
|
|
|
|
|
|
|
// Form not submitted successfully, re-display it and stop.
|
|
|
|
$PAGE->set_url($quizobj->start_attempt_url($page));
|
2014-02-05 14:47:23 +08:00
|
|
|
$PAGE->set_title($quizobj->get_quiz_name());
|
2011-10-07 17:43:15 +01:00
|
|
|
$accessmanager->setup_attempt_page($PAGE);
|
2014-06-22 21:30:04 +01:00
|
|
|
$output = $PAGE->get_renderer('mod_quiz');
|
2011-10-07 17:43:15 +01:00
|
|
|
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) {
|
2014-01-08 17:55:25 +00:00
|
|
|
if ($lastattempt->state == quiz_attempt::OVERDUE) {
|
|
|
|
redirect($quizobj->summary_url($lastattempt->id));
|
|
|
|
} else {
|
|
|
|
redirect($quizobj->attempt_url($currentattemptid, $page));
|
|
|
|
}
|
2011-10-07 17:43:15 +01:00
|
|
|
}
|
2008-07-08 16:33:47 +00:00
|
|
|
|
2016-02-08 15:39:41 +01:00
|
|
|
$attempt = quiz_prepare_and_start_new_attempt($quizobj, $attemptnumber, $lastattempt);
|
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));
|