MDL-20636 Add @package and GPL boiler-plate to files in /question.
[moodle.git] / question / preview.php
CommitLineData
aeb15530 1<?php
c76145d3
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
18
516cf3eb 19/**
4323d029 20 * This page displays a preview of a question
21 *
22 * The preview uses the option settings from the activity within which the question
23 * is previewed or the default settings if no activity is specified. The question session
24 * information is stored in the session as an array of subsequent states rather
25 * than in the database.
26 *
c76145d3
TH
27 * @package moodlecore
28 * @subpackage questionbank
29 * @copyright Alex Smith {@link http://maths.york.ac.uk/serving_maths} and
30 * numerous contributors.
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4323d029 32 */
516cf3eb 33
c76145d3
TH
34require_once(dirname(__FILE__) . '/../config.php');
35require_once($CFG->libdir . '/questionlib.php');
c76145d3
TH
36require_once(dirname(__FILE__) . '/previewlib.php');
37
38// Get and validate question id.
39$id = required_param('id', PARAM_INT);
40$question = question_bank::load_question($id);
41require_login();
42$category = $DB->get_record('question_categories', array('id' => $question->category), '*', MUST_EXIST);
43question_require_capability_on($question, 'use');
44$PAGE->set_pagelayout('popup');
45$PAGE->set_context(get_context_instance_by_id($category->contextid));
46
47// Get and validate display options.
48$options = new question_preview_options($question);
49$options->load_user_defaults();
50$options->set_from_request();
51$PAGE->set_url(question_preview_url($id, $options->behaviour, $options->maxmark, $options));
52
53// Get and validate exitsing preview, or start a new one.
54$previewid = optional_param('previewid', 0, PARAM_ALPHANUM);
55if ($previewid) {
56 if (!isset($SESSION->question_previews[$previewid])) {
57 print_error('notyourpreview', 'question');
58 }
59 try {
60 $quba = question_engine::load_questions_usage_by_activity($previewid);
61 } catch (Exception $e){
62 print_error('submissionoutofsequencefriendlymessage', 'question',
63 question_preview_url($question->id, $options->behaviour,
64 $options->maxmark, $options), null, $e);
65 }
66 $slot = $quba->get_first_question_number();
67 $usedquestion = $quba->get_question($slot);
68 if ($usedquestion->id != $question->id) {
69 print_error('questionidmismatch', 'question');
70 }
71 $question = $usedquestion;
72
73} else {
74 $quba = question_engine::make_questions_usage_by_activity('core_question_preview',
75 get_context_instance_by_id($category->contextid));
76 $quba->set_preferred_behaviour($options->behaviour);
77 $slot = $quba->add_question($question, $options->maxmark);
78 $quba->start_all_questions();
79
80 $transaction = $DB->start_delegated_transaction();
81 question_engine::save_questions_usage_by_activity($quba);
82 $transaction->allow_commit();
83
84 $SESSION->question_previews[$quba->get_id()] = true;
85}
86$options->behaviour = $quba->get_preferred_behaviour();
87$options->maxmark = $quba->get_question_max_mark($slot);
88
89// Create the settings form, and initialise the fields.
90$optionsform = new preview_options_form($CFG->wwwroot . '/question/preview.php?id=' . $question->id, $quba);
91$optionsform->set_data($options);
92
93// Process change of settings, if that was requested.
94if ($newoptions = $optionsform->get_submitted_data()) {
95 // Set user preferences
96 $options->save_user_preview_options($newoptions);
97 restart_preview($previewid, $question->id, $newoptions);
98}
99
100// Prepare a URL that is used in various places.
101$actionurl = question_preview_action_url($question->id, $quba->get_id(), $options);
102
103// Process any actions from the buttons at the bottom of the form.
104if (data_submitted() && confirm_sesskey()) {
105 if (optional_param('restart', false, PARAM_BOOL)) {
106 restart_preview($previewid, $question->id, $options);
107
108 } else if (optional_param('fill', null, PARAM_BOOL)) {
109 $correctresponse = $quba->get_correct_response($slot);
110 $quba->process_action($slot, $correctresponse);
111
112 $transaction = $DB->start_delegated_transaction();
113 question_engine::save_questions_usage_by_activity($quba);
114 $transaction->allow_commit();
115
116 redirect($actionurl);
117
118 } else if (optional_param('finish', null, PARAM_BOOL)) {
119 try {
120 $quba->process_all_actions();
121 } catch (question_out_of_sequence_exception $e){
122 print_error('submissionoutofsequencefriendlymessage', 'question', $actionurl);
123 }
124 $quba->finish_all_questions();
516cf3eb 125
c76145d3
TH
126 $transaction = $DB->start_delegated_transaction();
127 question_engine::save_questions_usage_by_activity($quba);
128 $transaction->allow_commit();
129 redirect($actionurl);
516cf3eb 130
516cf3eb 131 } else {
c76145d3
TH
132 try {
133 $quba->process_all_actions();
134 } catch (question_out_of_sequence_exception $e){
135 print_error('submissionoutofsequencefriendlymessage', 'question', $actionurl);
516cf3eb 136 }
516cf3eb 137
c76145d3
TH
138 $transaction = $DB->start_delegated_transaction();
139 question_engine::save_questions_usage_by_activity($quba);
140 $transaction->allow_commit();
516cf3eb 141
c76145d3
TH
142 $scrollpos = optional_param('scrollpos', '', PARAM_RAW);
143 if ($scrollpos !== '') {
144 $actionurl .= '&scrollpos=' . ((int) $scrollpos);
145 }
146 redirect($actionurl);
147 }
148}
149
150if ($question->length) {
151 $displaynumber = '1';
152} else {
153 $displaynumber = 'i';
154}
155$restartdisabled = '';
156$finishdisabled = '';
157$filldisabled = '';
158if ($quba->get_question_state($slot)->is_finished()) {
159 $finishdisabled = ' disabled="disabled"';
160 $filldisabled = ' disabled="disabled"';
161}
162if (!$previewid) {
163 $restartdisabled = ' disabled="disabled"';
164}
165
c76145d3
TH
166// Output
167$title = get_string('previewquestion', 'question', format_string($question->name));
168$headtags = question_engine::initialise_js() . $quba->render_question_head_html($slot);
169$PAGE->set_title($title);
170$PAGE->set_heading($title);
171echo $OUTPUT->header();
172
173// Start the question form.
174echo '<form method="post" action="' . s($actionurl) .
175 '" enctype="multipart/form-data" id="responseform">', "\n";
176echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />', "\n";
177echo '<input type="hidden" name="slots" value="' . $slot . '" />', "\n";
178
179// Output the question.
180echo $quba->render_question($slot, $options, $displaynumber);
181
182echo '<p class="notifytiny">' . get_string('behaviourbeingused', 'question',
183 question_engine::get_behaviour_name(
184 $quba->get_question_attempt($slot)->get_behaviour_name())) . '</p>';
185// Finish the question form.
186echo '<div id="previewcontrols" class="controls">';
187echo '<input type="submit" name="restart"' . $restartdisabled .
188 ' value="' . get_string('restart', 'question') . '" />', "\n";
189echo '<input type="submit" name="fill"' . $filldisabled .
190 ' value="' . get_string('fillincorrect', 'question') . '" />', "\n";
191echo '<input type="submit" name="finish"' . $finishdisabled .
192 ' value="' . get_string('submitandfinish', 'question') . '" />', "\n";
193echo '<input type="hidden" name="scrollpos" id="scrollpos" value="" />';
194echo '</div>';
195echo '</form>';
196
197// Display the settings form.
198$optionsform->display();
199
cce1b655
TH
200$PAGE->requires->js_init_call('M.core_question_preview.init', null, false, array(
201 'name' => 'core_question_preview',
202 'fullpath' => '/question/preview.js',
203 'requires' => array('base', 'dom', 'event-delegate', 'event-key', 'core_question_engine'),
204 'strings' => array(
205 array('closepreview', 'question'),
206 )));
c76145d3 207echo $OUTPUT->footer();
aeb15530 208