MDL-20636 Previewing a truefalse question in deferred feedback mode now works.
[moodle.git] / question / preview.php
1 <?php
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/>.
19 /**
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  *
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
32  */
34 require_once(dirname(__FILE__) . '/../config.php');
35 require_once($CFG->libdir . '/questionlib.php');
36 require_once($CFG->libdir . '/formslib.php');
37 require_once(dirname(__FILE__) . '/previewlib.php');
39 // Get and validate question id.
40 $id = required_param('id', PARAM_INT);
41 $question = question_bank::load_question($id);
42 require_login();
43 $category = $DB->get_record('question_categories', array('id' => $question->category), '*', MUST_EXIST);
44 question_require_capability_on($question, 'use');
45 $PAGE->set_pagelayout('popup');
46 $PAGE->set_context(get_context_instance_by_id($category->contextid));
48 // Get and validate display options.
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));
54 // Get and validate exitsing preview, or start a new one.
55 $previewid = optional_param('previewid', 0, PARAM_ALPHANUM);
56 if ($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);
62     } catch (Exception $e){
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;
74 } else {
75     $quba = question_engine::make_questions_usage_by_activity('core_question_preview',
76             get_context_instance_by_id($category->contextid));
77     $quba->set_preferred_behaviour($options->behaviour);
78     $slot = $quba->add_question($question, $options->maxmark);
79     $quba->start_all_questions();
81     $transaction = $DB->start_delegated_transaction();
82     question_engine::save_questions_usage_by_activity($quba);
83     $transaction->allow_commit();
85     $SESSION->question_previews[$quba->get_id()] = true;
86 }
87 $options->behaviour = $quba->get_preferred_behaviour();
88 $options->maxmark = $quba->get_question_max_mark($slot);
90 // Create the settings form, and initialise the fields.
91 $optionsform = new preview_options_form($CFG->wwwroot . '/question/preview.php?id=' . $question->id, $quba);
92 $optionsform->set_data($options);
94 // Process change of settings, if that was requested.
95 if ($newoptions = $optionsform->get_submitted_data()) {
96     // Set user preferences
97     $options->save_user_preview_options($newoptions);
98     restart_preview($previewid, $question->id, $newoptions);
99 }
101 // Prepare a URL that is used in various places.
102 $actionurl = question_preview_action_url($question->id, $quba->get_id(), $options);
104 // Process any actions from the buttons at the bottom of the form.
105 if (data_submitted() && confirm_sesskey()) {
106     if (optional_param('restart', false, PARAM_BOOL)) {
107         restart_preview($previewid, $question->id, $options);
109     } else if (optional_param('fill', null, PARAM_BOOL)) {
110         $correctresponse = $quba->get_correct_response($slot);
111         $quba->process_action($slot, $correctresponse);
113         $transaction = $DB->start_delegated_transaction();
114         question_engine::save_questions_usage_by_activity($quba);
115         $transaction->allow_commit();
117         redirect($actionurl);
119     } else if (optional_param('finish', null, PARAM_BOOL)) {
120         try {
121             $quba->process_all_actions();
122         } catch (question_out_of_sequence_exception $e){
123             print_error('submissionoutofsequencefriendlymessage', 'question', $actionurl);
124         }
125         $quba->finish_all_questions();
127         $transaction = $DB->start_delegated_transaction();
128         question_engine::save_questions_usage_by_activity($quba);
129         $transaction->allow_commit();
130         redirect($actionurl);
132     } else {
133         try {
134             $quba->process_all_actions();
135         } catch (question_out_of_sequence_exception $e){
136             print_error('submissionoutofsequencefriendlymessage', 'question', $actionurl);
137         }
139         $transaction = $DB->start_delegated_transaction();
140         question_engine::save_questions_usage_by_activity($quba);
141         $transaction->allow_commit();
143         $scrollpos = optional_param('scrollpos', '', PARAM_RAW);
144         if ($scrollpos !== '') {
145             $actionurl .= '&scrollpos=' . ((int) $scrollpos);
146         }
147         redirect($actionurl);
148     }
151 if ($question->length) {
152     $displaynumber = '1';
153 } else {
154     $displaynumber = 'i';
156 $restartdisabled = '';
157 $finishdisabled = '';
158 $filldisabled = '';
159 if ($quba->get_question_state($slot)->is_finished()) {
160     $finishdisabled = ' disabled="disabled"';
161     $filldisabled = ' disabled="disabled"';
163 if (!$previewid) {
164     $restartdisabled = ' disabled="disabled"';
167 $PAGE->requires->js_init_call('M.core_question_preview.init', null, array(
168         'name' => 'core_question_preview',
169         'fullpath' => '/question/preview.js',
170         'requires' => array('base', 'dom', 'event-delegate', 'event-key', 'core_question_engine'),
171         'strings' => array(
172             array('question', 'closepreview'),
173         )));
175 // Output
176 $title = get_string('previewquestion', 'question', format_string($question->name));
177 $headtags = question_engine::initialise_js() . $quba->render_question_head_html($slot);
178 $PAGE->set_title($title);
179 $PAGE->set_heading($title);
180 echo $OUTPUT->header();
182 // Start the question form.
183 echo '<form method="post" action="' . s($actionurl) .
184         '" enctype="multipart/form-data" id="responseform">', "\n";
185 echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />', "\n";
186 echo '<input type="hidden" name="slots" value="' . $slot . '" />', "\n";
188 // Output the question.
189 echo $quba->render_question($slot, $options, $displaynumber);
191 echo '<p class="notifytiny">' . get_string('behaviourbeingused', 'question',
192         question_engine::get_behaviour_name(
193         $quba->get_question_attempt($slot)->get_behaviour_name())) . '</p>';
194 // Finish the question form.
195 echo '<div id="previewcontrols" class="controls">';
196 echo '<input type="submit" name="restart"' . $restartdisabled .
197         ' value="' . get_string('restart', 'question') . '" />', "\n";
198 echo '<input type="submit" name="fill"' . $filldisabled .
199         ' value="' . get_string('fillincorrect', 'question') . '" />', "\n";
200 echo '<input type="submit" name="finish"' . $finishdisabled .
201         ' value="' . get_string('submitandfinish', 'question') . '" />', "\n";
202 echo '<input type="hidden" name="scrollpos" id="scrollpos" value="" />';
203 echo '</div>';
204 echo '</form>';
206 // Display the settings form.
207 $optionsform->display();
209 echo '<script type="text/javascript">question_preview_init("' .
210         get_string('closepreview', 'question') . '", "previewcontrols");</script>', "\n";
212 echo $OUTPUT->footer();