MDL-27649 support question variants as a first-class concept in the question engine.
[moodle.git] / question / previewlib.php
CommitLineData
fe6ce234 1<?php
fe6ce234
DC
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
17/**
18 * Library functions used by question/preview.php.
19 *
06f8ed54 20 * @package moodlecore
fe6ce234
DC
21 * @subpackage questionengine
22 * @copyright 2010 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
06f8ed54 26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
7a719748
TH
29require_once($CFG->libdir . '/formslib.php');
30
31
06f8ed54
TH
32/**
33 * Settings form for the preview options.
34 *
f7970e3c
TH
35 * @copyright 2009 The Open University
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
06f8ed54
TH
37 */
38class preview_options_form extends moodleform {
39 public function definition() {
40 $mform = $this->_form;
41
42 $hiddenofvisible = array(
43 question_display_options::HIDDEN => get_string('notshown', 'question'),
44 question_display_options::VISIBLE => get_string('shown', 'question'),
45 );
46
47 $mform->addElement('header', 'optionsheader', get_string('changeoptions', 'question'));
48
49 $behaviours = question_engine::get_behaviour_options($this->_customdata->get_preferred_behaviour());
50 $mform->addElement('select', 'behaviour', get_string('howquestionsbehave', 'question'), $behaviours);
c76145d3 51 $mform->addHelpButton('behaviour', 'howquestionsbehave', 'question');
06f8ed54
TH
52
53 $mform->addElement('text', 'maxmark', get_string('markedoutof', 'question'), array('size' => '5'));
54 $mform->setType('maxmark', PARAM_NUMBER);
55
56 $mform->addElement('select', 'correctness', get_string('whethercorrect', 'question'), $hiddenofvisible);
57
58 $marksoptions = array(
59 question_display_options::HIDDEN => get_string('notshown', 'question'),
60 question_display_options::MAX_ONLY => get_string('showmaxmarkonly', 'question'),
61 question_display_options::MARK_AND_MAX => get_string('showmarkandmax', 'question'),
62 );
63 $mform->addElement('select', 'marks', get_string('marks', 'question'), $marksoptions);
64
65 $mform->addElement('select', 'markdp', get_string('decimalplacesingrades', 'question'),
66 question_engine::get_dp_options());
67
68 $mform->addElement('select', 'feedback', get_string('specificfeedback', 'question'), $hiddenofvisible);
69
70 $mform->addElement('select', 'generalfeedback', get_string('generalfeedback', 'question'), $hiddenofvisible);
71
72 $mform->addElement('select', 'rightanswer', get_string('rightanswer', 'question'), $hiddenofvisible);
73
74 $mform->addElement('select', 'history', get_string('responsehistory', 'question'), $hiddenofvisible);
75
76 $mform->addElement('submit', 'submit', get_string('restartwiththeseoptions', 'question'), $hiddenofvisible);
77 }
78}
79
80
81/**
82 * Displays question preview options as default and set the options
83 * Setting default, getting and setting user preferences in question preview options.
84 *
f7970e3c
TH
85 * @copyright 2010 The Open University
86 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
06f8ed54
TH
87 */
88class question_preview_options extends question_display_options {
89 /** @var string the behaviour to use for this preview. */
90 public $behaviour;
91
92 /** @var number the maximum mark to use for this preview. */
93 public $maxmark;
94
95 /** @var string prefix to append to field names to get user_preference names. */
96 const OPTIONPREFIX = 'question_preview_options_';
97
98 /**
99 * Constructor.
100 */
101 public function __construct($question) {
102 global $CFG;
103 $this->behaviour = 'deferredfeedback';
104 $this->maxmark = $question->defaultmark;
105 $this->correctness = self::VISIBLE;
106 $this->marks = self::MARK_AND_MAX;
c76145d3 107 $this->markdp = get_config('quiz', 'decimalpoints');
06f8ed54
TH
108 $this->feedback = self::VISIBLE;
109 $this->numpartscorrect = $this->feedback;
110 $this->generalfeedback = self::VISIBLE;
111 $this->rightanswer = self::VISIBLE;
112 $this->history = self::HIDDEN;
113 $this->flags = self::HIDDEN;
114 $this->manualcomment = self::HIDDEN;
115 }
116
117 /**
118 * @return array names of the options we store in the user preferences table.
119 */
120 protected function get_user_pref_fields() {
121 return array('behaviour', 'correctness', 'marks', 'markdp', 'feedback',
122 'generalfeedback', 'rightanswer', 'history');
123 }
124
125 /**
126 * @return array names and param types of the options we read from the request.
127 */
128 protected function get_field_types() {
129 return array(
130 'behaviour' => PARAM_ALPHA,
131 'maxmark' => PARAM_NUMBER,
132 'correctness' => PARAM_BOOL,
133 'marks' => PARAM_INT,
134 'markdp' => PARAM_INT,
135 'feedback' => PARAM_BOOL,
136 'generalfeedback' => PARAM_BOOL,
137 'rightanswer' => PARAM_BOOL,
138 'history' => PARAM_BOOL,
139 );
140 }
141
142 /**
143 * Load the value of the options from the user_preferences table.
144 */
145 public function load_user_defaults() {
146 foreach ($this->get_user_pref_fields() as $field) {
147 $this->$field = get_user_preferences(
148 self::OPTIONPREFIX . $field, $this->$field);
149 }
150 $this->numpartscorrect = $this->feedback;
151 }
152
153 /**
154 * Save a change to the user's preview options to the database.
155 * @param object $newoptions
156 */
157 public function save_user_preview_options($newoptions) {
158 foreach ($this->get_user_pref_fields() as $field) {
159 if (isset($newoptions->$field)) {
160 set_user_preference(self::OPTIONPREFIX . $field, $newoptions->$field);
161 }
162 }
163 }
164
165 /**
166 * Set the value of any fields included in the request.
167 */
168 public function set_from_request() {
169 foreach ($this->get_field_types() as $field => $type) {
170 $this->$field = optional_param($field, $this->$field, $type);
171 }
172 $this->numpartscorrect = $this->feedback;
173 }
174
175 /**
176 * @return string URL fragment. Parameters needed in the URL when continuing
177 * this preview.
178 */
179 public function get_query_string() {
180 $querystring = array();
181 foreach ($this->get_field_types() as $field => $notused) {
182 if ($field == 'behaviour' || $field == 'maxmark') {
183 continue;
184 }
185 $querystring[] = $field . '=' . $this->$field;
186 }
187 return implode('&', $querystring);
188 }
189}
190
191
fe6ce234
DC
192/**
193 * Called via pluginfile.php -> question_pluginfile to serve files belonging to
194 * a question in a question_attempt when that attempt is a preview.
195 *
196 * @param object $course course settings object
197 * @param object $context context object
198 * @param string $component the name of the component we are serving files for.
199 * @param string $filearea the name of the file area.
f7970e3c
TH
200 * @param int $qubaid the question_usage this image belongs to.
201 * @param int $slot the relevant slot within the usage.
fe6ce234
DC
202 * @param array $args the remaining bits of the file path.
203 * @param bool $forcedownload whether the user must be forced to download the file.
204 * @return bool false if file not found, does not return if found - justsend the file
205 */
206function question_preview_question_pluginfile($course, $context, $component,
7a719748
TH
207 $filearea, $qubaid, $slot, $args, $forcedownload) {
208 global $USER, $DB, $CFG;
fe6ce234 209
7a719748 210 $quba = question_engine::load_questions_usage_by_activity($qubaid);
fe6ce234 211
7a719748 212 if ($quba->get_owning_context()->id != $context->id) {
fe6ce234
DC
213 send_file_not_found();
214 }
215
7a719748 216 if (!question_has_capability_on($quba->get_question($slot), 'use')) {
fe6ce234
DC
217 send_file_not_found();
218 }
219
7a719748
TH
220 $options = new question_display_options();
221 $options->feedback = question_display_options::VISIBLE;
222 $options->numpartscorrect = question_display_options::VISIBLE;
223 $options->generalfeedback = question_display_options::VISIBLE;
224 $options->rightanswer = question_display_options::VISIBLE;
225 $options->manualcomment = question_display_options::VISIBLE;
226 $options->history = question_display_options::VISIBLE;
227 if (!$quba->check_file_access($slot, $options, $component,
fe6ce234
DC
228 $filearea, $args, $forcedownload)) {
229 send_file_not_found();
230 }
231
232 $fs = get_file_storage();
233 $relativepath = implode('/', $args);
234 $fullpath = "/$context->id/$component/$filearea/$relativepath";
235 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
236 send_file_not_found();
237 }
238
239 send_stored_file($file, 0, 0, $forcedownload);
240}
06f8ed54
TH
241
242/**
243 * The the URL to use for actions relating to this preview.
f7970e3c
TH
244 * @param int $questionid the question being previewed.
245 * @param int $qubaid the id of the question usage for this preview.
06f8ed54
TH
246 * @param question_preview_options $options the options in use.
247 */
248function question_preview_action_url($questionid, $qubaid,
249 question_preview_options $options) {
250 global $CFG;
251 $url = $CFG->wwwroot . '/question/preview.php?id=' . $questionid . '&previewid=' . $qubaid;
252 return $url . '&' . $options->get_query_string();
253}
254
255/**
256 * Delete the current preview, if any, and redirect to start a new preview.
f7970e3c
TH
257 * @param int $previewid
258 * @param int $questionid
06f8ed54
TH
259 * @param object $displayoptions
260 */
261function restart_preview($previewid, $questionid, $displayoptions) {
c76145d3
TH
262 global $DB;
263
06f8ed54 264 if ($previewid) {
c76145d3 265 $transaction = $DB->start_delegated_transaction();
06f8ed54 266 question_engine::delete_questions_usage_by_activity($previewid);
c76145d3 267 $transaction->allow_commit();
06f8ed54
TH
268 }
269 redirect(question_preview_url($questionid, $displayoptions->behaviour, $displayoptions->maxmark, $displayoptions));
270}