MDL-20636 Attempt as converting attempt.php over to renderer
[moodle.git] / mod / quiz / attempt.php
CommitLineData
83192608 1<?php
7c5bd5bf
TH
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
ee1fb969 18/**
7c5bd5bf 19 * This script displays a particular page of a quiz attempt that is in progress.
2b3b36d0 20 *
ba643847 21 * @package mod
7c5bd5bf 22 * @subpackage quiz
ba643847
TH
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2b3b36d0 25 */
55c0c75c 26
7c5bd5bf
TH
27require_once(dirname(__FILE__) . '/../../config.php');
28require_once($CFG->dirroot . '/mod/quiz/locallib.php');
ee1fb969 29
7c5bd5bf
TH
30// Look for old-style URLs, such as may be in the logs, and redirect them to startattemtp.php
31if ($id = optional_param('id', 0, PARAM_INTEGER)) {
32 redirect($CFG->wwwroot . '/mod/quiz/startattempt.php?cmid=' . $id . '&sesskey=' . sesskey());
33} else if ($qid = optional_param('q', 0, PARAM_INTEGER)) {
34 if (!$cm = get_coursemodule_from_instance('quiz', $qid)) {
35 print_error('invalidquizid', 'quiz');
55f599f0 36 }
55ca80ed
TH
37 redirect(new moodle_url('/mod/quiz/startattempt.php',
38 array('cmid' => $cm->id, 'sesskey' => sesskey())));
7c5bd5bf 39}
55f599f0 40
7c5bd5bf
TH
41// Get submitted parameters.
42$attemptid = required_param('attempt', PARAM_INT);
43$page = optional_param('page', 0, PARAM_INT);
55c0c75c 44
7c5bd5bf
TH
45$attemptobj = quiz_attempt::create($attemptid);
46$PAGE->set_url($attemptobj->attempt_url(0, $page));
e63faf2b 47
7c5bd5bf
TH
48// Check login.
49require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
c68287a9 50
7c5bd5bf
TH
51// Check that this attempt belongs to this user.
52if ($attemptobj->get_userid() != $USER->id) {
53 if ($attemptobj->has_capability('mod/quiz:viewreports')) {
9f9eec1e 54 redirect($attemptobj->review_url(0, $page));
50d7b53e 55 } else {
7c5bd5bf 56 throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'notyourattempt');
bb6f1955 57 }
7c5bd5bf 58}
a5e1f35c 59
7c5bd5bf
TH
60// Check capabilities and block settings
61if (!$attemptobj->is_preview_user()) {
62 $attemptobj->require_capability('mod/quiz:attempt');
63 if (empty($attemptobj->get_quiz()->showblocks)) {
64 $PAGE->blocks->show_only_fake_blocks();
586b2c82 65 }
ee1fb969 66
7c5bd5bf
TH
67} else {
68 navigation_node::override_active_url($attemptobj->start_attempt_url());
69}
70
71// If the attempt is already closed, send them to the review page.
72if ($attemptobj->is_finished()) {
73 redirect($attemptobj->review_url(0, $page));
74}
75
76// Check the access rules.
77$accessmanager = $attemptobj->get_access_manager(time());
78$messages = $accessmanager->prevent_access();
79if (!$attemptobj->is_preview_user() && $messages) {
80 print_error('attempterror', 'quiz', $attemptobj->view_url(),
81 $accessmanager->print_messages($messages, true));
82}
83$accessmanager->do_password_check($attemptobj->is_preview_user());
84
85add_to_log($attemptobj->get_courseid(), 'quiz', 'continue attempt',
86 'review.php?attempt=' . $attemptobj->get_attemptid(),
87 $attemptobj->get_quizid(), $attemptobj->get_cmid());
88
89// Get the list of questions needed by this page.
a1eb3a44 90$slots = $attemptobj->get_slots($page);
7c5bd5bf
TH
91
92// Check.
93if (empty($slots)) {
94 throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'noquestionsfound');
95}
96
97// Initialise the JavaScript.
98$headtags = $attemptobj->get_html_head_contributions($page);
99$PAGE->requires->js_init_call('M.mod_quiz.init_attempt_form', null, false, quiz_get_js_module());
100
101// Arrange for the navigation to be displayed.
102$navbc = $attemptobj->get_navigation_panel('quiz_attempt_nav_panel', $page);
103$firstregion = reset($PAGE->blocks->get_regions());
104$PAGE->blocks->add_fake_block($navbc, $firstregion);
105
606e07d5 106$output = $PAGE->get_renderer('mod_quiz');
7c5bd5bf 107
606e07d5 108$output->attempt_page($attemptobj, $page, $accessmanager, $messages, $slots, $id);