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