weekly release 2.2dev
[moodle.git] / question / preview.php
CommitLineData
aeb15530 1<?php
c76145d3
TH
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
516cf3eb 17/**
4323d029 18 * This page displays a preview of a question
19 *
20 * The preview uses the option settings from the activity within which the question
21 * is previewed or the default settings if no activity is specified. The question session
22 * information is stored in the session as an array of subsequent states rather
23 * than in the database.
24 *
017bc1d9 25 * @package moodlecore
7764183a 26 * @subpackage questionengine
017bc1d9 27 * @copyright Alex Smith {@link http://maths.york.ac.uk/serving_maths} and
c76145d3 28 * numerous contributors.
7764183a 29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4323d029 30 */
516cf3eb 31
017bc1d9 32
c76145d3
TH
33require_once(dirname(__FILE__) . '/../config.php');
34require_once($CFG->libdir . '/questionlib.php');
c76145d3
TH
35require_once(dirname(__FILE__) . '/previewlib.php');
36
37// Get and validate question id.
38$id = required_param('id', PARAM_INT);
39$question = question_bank::load_question($id);
c76145d3 40$PAGE->set_pagelayout('popup');
56a4ae46
TH
41
42// Were we given a particular context to run the question in?
43// This affects things like filter settings, or forced theme or language.
44if ($cmid = optional_param('cmid', 0, PARAM_INT)) {
45 $cm = get_coursemodule_from_id(false, $cmid);
46 require_login($cm->course, false, $cm);
47 $context = get_context_instance(CONTEXT_MODULE, $cmid);
48
49} else if ($courseid = optional_param('courseid', 0, PARAM_INT)) {
50 require_login($courseid);
51 $context = get_context_instance(CONTEXT_COURSE, $courseid);
52
53} else {
54 require_login();
55 $category = $DB->get_record('question_categories',
56 array('id' => $question->category), '*', MUST_EXIST);
57 $context = get_context_instance_by_id($category->contextid);
58 $PAGE->set_context($context);
59 // Note that in the other cases, require_login will set the correct page context.
60}
61question_require_capability_on($question, 'use');
c76145d3
TH
62
63// Get and validate display options.
c014b989 64$maxvariant = $question->get_num_variants();
c76145d3
TH
65$options = new question_preview_options($question);
66$options->load_user_defaults();
67$options->set_from_request();
56a4ae46
TH
68$PAGE->set_url(question_preview_url($id, $options->behaviour, $options->maxmark,
69 $options, $options->variant, $context));
c76145d3
TH
70
71// Get and validate exitsing preview, or start a new one.
612106b3
TH
72$previewid = optional_param('previewid', 0, PARAM_INT);
73
c76145d3
TH
74if ($previewid) {
75 if (!isset($SESSION->question_previews[$previewid])) {
76 print_error('notyourpreview', 'question');
77 }
78 try {
79 $quba = question_engine::load_questions_usage_by_activity($previewid);
c014b989 80 } catch (Exception $e) {
c76145d3
TH
81 print_error('submissionoutofsequencefriendlymessage', 'question',
82 question_preview_url($question->id, $options->behaviour,
56a4ae46 83 $options->maxmark, $options, $options->variant, $context), null, $e);
c76145d3
TH
84 }
85 $slot = $quba->get_first_question_number();
86 $usedquestion = $quba->get_question($slot);
87 if ($usedquestion->id != $question->id) {
88 print_error('questionidmismatch', 'question');
89 }
90 $question = $usedquestion;
c014b989 91 $options->variant = $quba->get_variant($slot);
c76145d3
TH
92
93} else {
56a4ae46
TH
94 $quba = question_engine::make_questions_usage_by_activity(
95 'core_question_preview', $context);
c76145d3
TH
96 $quba->set_preferred_behaviour($options->behaviour);
97 $slot = $quba->add_question($question, $options->maxmark);
c014b989
TH
98
99 if ($options->variant) {
100 $options->variant = min($maxvariant, max(1, $options->variant));
101 } else {
102 $options->variant = rand(1, $maxvariant);
103 }
104
105 $quba->start_question($slot, $options->variant);
c76145d3
TH
106
107 $transaction = $DB->start_delegated_transaction();
108 question_engine::save_questions_usage_by_activity($quba);
109 $transaction->allow_commit();
110
111 $SESSION->question_previews[$quba->get_id()] = true;
112}
113$options->behaviour = $quba->get_preferred_behaviour();
114$options->maxmark = $quba->get_question_max_mark($slot);
115
116// Create the settings form, and initialise the fields.
56a4ae46
TH
117$optionsform = new preview_options_form(question_preview_form_url($question->id, $context),
118 array('quba' => $quba, 'maxvariant' => $maxvariant));
c76145d3
TH
119$optionsform->set_data($options);
120
121// Process change of settings, if that was requested.
122if ($newoptions = $optionsform->get_submitted_data()) {
123 // Set user preferences
124 $options->save_user_preview_options($newoptions);
612106b3
TH
125 if (!isset($newoptions->variant)) {
126 $newoptions->variant = $options->variant;
127 }
56a4ae46 128 restart_preview($previewid, $question->id, $newoptions, $context);
c76145d3
TH
129}
130
131// Prepare a URL that is used in various places.
56a4ae46 132$actionurl = question_preview_action_url($question->id, $quba->get_id(), $options, $context);
c76145d3
TH
133
134// Process any actions from the buttons at the bottom of the form.
135if (data_submitted() && confirm_sesskey()) {
136 if (optional_param('restart', false, PARAM_BOOL)) {
56a4ae46 137 restart_preview($previewid, $question->id, $options, $context);
c76145d3
TH
138
139 } else if (optional_param('fill', null, PARAM_BOOL)) {
140 $correctresponse = $quba->get_correct_response($slot);
141 $quba->process_action($slot, $correctresponse);
142
143 $transaction = $DB->start_delegated_transaction();
144 question_engine::save_questions_usage_by_activity($quba);
145 $transaction->allow_commit();
146
147 redirect($actionurl);
148
149 } else if (optional_param('finish', null, PARAM_BOOL)) {
150 try {
151 $quba->process_all_actions();
c014b989 152 } catch (question_out_of_sequence_exception $e) {
c76145d3
TH
153 print_error('submissionoutofsequencefriendlymessage', 'question', $actionurl);
154 }
155 $quba->finish_all_questions();
516cf3eb 156
c76145d3
TH
157 $transaction = $DB->start_delegated_transaction();
158 question_engine::save_questions_usage_by_activity($quba);
159 $transaction->allow_commit();
160 redirect($actionurl);
516cf3eb 161
516cf3eb 162 } else {
c76145d3
TH
163 try {
164 $quba->process_all_actions();
c014b989 165 } catch (question_out_of_sequence_exception $e) {
c76145d3 166 print_error('submissionoutofsequencefriendlymessage', 'question', $actionurl);
516cf3eb 167 }
516cf3eb 168
c76145d3
TH
169 $transaction = $DB->start_delegated_transaction();
170 question_engine::save_questions_usage_by_activity($quba);
171 $transaction->allow_commit();
516cf3eb 172
c76145d3
TH
173 $scrollpos = optional_param('scrollpos', '', PARAM_RAW);
174 if ($scrollpos !== '') {
612106b3 175 $actionurl->param('scrollpos', (int) $scrollpos);
c76145d3
TH
176 }
177 redirect($actionurl);
178 }
179}
180
181if ($question->length) {
182 $displaynumber = '1';
183} else {
184 $displaynumber = 'i';
185}
186$restartdisabled = '';
187$finishdisabled = '';
188$filldisabled = '';
189if ($quba->get_question_state($slot)->is_finished()) {
190 $finishdisabled = ' disabled="disabled"';
191 $filldisabled = ' disabled="disabled"';
192}
193if (!$previewid) {
194 $restartdisabled = ' disabled="disabled"';
195}
196
c76145d3
TH
197// Output
198$title = get_string('previewquestion', 'question', format_string($question->name));
199$headtags = question_engine::initialise_js() . $quba->render_question_head_html($slot);
200$PAGE->set_title($title);
201$PAGE->set_heading($title);
202echo $OUTPUT->header();
203
204// Start the question form.
612106b3 205echo '<form method="post" action="' . $actionurl .
c76145d3
TH
206 '" enctype="multipart/form-data" id="responseform">', "\n";
207echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />', "\n";
208echo '<input type="hidden" name="slots" value="' . $slot . '" />', "\n";
209
210// Output the question.
211echo $quba->render_question($slot, $options, $displaynumber);
212
213echo '<p class="notifytiny">' . get_string('behaviourbeingused', 'question',
214 question_engine::get_behaviour_name(
215 $quba->get_question_attempt($slot)->get_behaviour_name())) . '</p>';
216// Finish the question form.
217echo '<div id="previewcontrols" class="controls">';
218echo '<input type="submit" name="restart"' . $restartdisabled .
219 ' value="' . get_string('restart', 'question') . '" />', "\n";
220echo '<input type="submit" name="fill"' . $filldisabled .
221 ' value="' . get_string('fillincorrect', 'question') . '" />', "\n";
222echo '<input type="submit" name="finish"' . $finishdisabled .
223 ' value="' . get_string('submitandfinish', 'question') . '" />', "\n";
224echo '<input type="hidden" name="scrollpos" id="scrollpos" value="" />';
225echo '</div>';
226echo '</form>';
227
228// Display the settings form.
229$optionsform->display();
230
cce1b655
TH
231$PAGE->requires->js_init_call('M.core_question_preview.init', null, false, array(
232 'name' => 'core_question_preview',
233 'fullpath' => '/question/preview.js',
234 'requires' => array('base', 'dom', 'event-delegate', 'event-key', 'core_question_engine'),
235 'strings' => array(
236 array('closepreview', 'question'),
237 )));
c76145d3 238echo $OUTPUT->footer();
aeb15530 239