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