On demand release 2.4beta+
[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
c014b989
TH
49 $behaviours = question_engine::get_behaviour_options(
50 $this->_customdata['quba']->get_preferred_behaviour());
51 $mform->addElement('select', 'behaviour',
52 get_string('howquestionsbehave', 'question'), $behaviours);
c76145d3 53 $mform->addHelpButton('behaviour', 'howquestionsbehave', 'question');
06f8ed54 54
c014b989
TH
55 $mform->addElement('text', 'maxmark', get_string('markedoutof', 'question'),
56 array('size' => '5'));
7664182d 57 $mform->setType('maxmark', PARAM_FLOAT);
06f8ed54 58
c014b989
TH
59 if ($this->_customdata['maxvariant'] > 1) {
60 $variants = range(1, $this->_customdata['maxvariant']);
61 $mform->addElement('select', 'variant', get_string('questionvariant', 'question'),
62 array_combine($variants, $variants));
63 }
7664182d 64 $mform->setType('variant', PARAM_INT);
c014b989
TH
65
66 $mform->addElement('select', 'correctness', get_string('whethercorrect', 'question'),
67 $hiddenofvisible);
06f8ed54
TH
68
69 $marksoptions = array(
70 question_display_options::HIDDEN => get_string('notshown', 'question'),
71 question_display_options::MAX_ONLY => get_string('showmaxmarkonly', 'question'),
72 question_display_options::MARK_AND_MAX => get_string('showmarkandmax', 'question'),
73 );
74 $mform->addElement('select', 'marks', get_string('marks', 'question'), $marksoptions);
75
76 $mform->addElement('select', 'markdp', get_string('decimalplacesingrades', 'question'),
77 question_engine::get_dp_options());
78
c014b989
TH
79 $mform->addElement('select', 'feedback',
80 get_string('specificfeedback', 'question'), $hiddenofvisible);
06f8ed54 81
c014b989
TH
82 $mform->addElement('select', 'generalfeedback',
83 get_string('generalfeedback', 'question'), $hiddenofvisible);
06f8ed54 84
c014b989
TH
85 $mform->addElement('select', 'rightanswer',
86 get_string('rightanswer', 'question'), $hiddenofvisible);
06f8ed54 87
c014b989
TH
88 $mform->addElement('select', 'history',
89 get_string('responsehistory', 'question'), $hiddenofvisible);
06f8ed54 90
c014b989 91 $mform->addElement('submit', 'submit',
079caafc 92 get_string('restartwiththeseoptions', 'question'));
06f8ed54
TH
93 }
94}
95
96
97/**
98 * Displays question preview options as default and set the options
99 * Setting default, getting and setting user preferences in question preview options.
100 *
f7970e3c
TH
101 * @copyright 2010 The Open University
102 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
06f8ed54
TH
103 */
104class question_preview_options extends question_display_options {
105 /** @var string the behaviour to use for this preview. */
106 public $behaviour;
107
108 /** @var number the maximum mark to use for this preview. */
109 public $maxmark;
110
c014b989
TH
111 /** @var int the variant of the question to preview. */
112 public $variant;
113
06f8ed54
TH
114 /** @var string prefix to append to field names to get user_preference names. */
115 const OPTIONPREFIX = 'question_preview_options_';
116
117 /**
118 * Constructor.
119 */
120 public function __construct($question) {
121 global $CFG;
122 $this->behaviour = 'deferredfeedback';
123 $this->maxmark = $question->defaultmark;
c014b989 124 $this->variant = null;
06f8ed54
TH
125 $this->correctness = self::VISIBLE;
126 $this->marks = self::MARK_AND_MAX;
c76145d3 127 $this->markdp = get_config('quiz', 'decimalpoints');
06f8ed54
TH
128 $this->feedback = self::VISIBLE;
129 $this->numpartscorrect = $this->feedback;
130 $this->generalfeedback = self::VISIBLE;
131 $this->rightanswer = self::VISIBLE;
132 $this->history = self::HIDDEN;
133 $this->flags = self::HIDDEN;
134 $this->manualcomment = self::HIDDEN;
135 }
136
137 /**
138 * @return array names of the options we store in the user preferences table.
139 */
140 protected function get_user_pref_fields() {
141 return array('behaviour', 'correctness', 'marks', 'markdp', 'feedback',
142 'generalfeedback', 'rightanswer', 'history');
143 }
144
145 /**
146 * @return array names and param types of the options we read from the request.
147 */
148 protected function get_field_types() {
149 return array(
150 'behaviour' => PARAM_ALPHA,
61cca0b7 151 'maxmark' => PARAM_FLOAT,
c014b989 152 'variant' => PARAM_INT,
06f8ed54
TH
153 'correctness' => PARAM_BOOL,
154 'marks' => PARAM_INT,
155 'markdp' => PARAM_INT,
156 'feedback' => PARAM_BOOL,
157 'generalfeedback' => PARAM_BOOL,
158 'rightanswer' => PARAM_BOOL,
159 'history' => PARAM_BOOL,
160 );
161 }
162
163 /**
164 * Load the value of the options from the user_preferences table.
165 */
166 public function load_user_defaults() {
167 foreach ($this->get_user_pref_fields() as $field) {
168 $this->$field = get_user_preferences(
169 self::OPTIONPREFIX . $field, $this->$field);
170 }
171 $this->numpartscorrect = $this->feedback;
172 }
173
174 /**
175 * Save a change to the user's preview options to the database.
176 * @param object $newoptions
177 */
178 public function save_user_preview_options($newoptions) {
179 foreach ($this->get_user_pref_fields() as $field) {
180 if (isset($newoptions->$field)) {
181 set_user_preference(self::OPTIONPREFIX . $field, $newoptions->$field);
182 }
183 }
184 }
185
186 /**
187 * Set the value of any fields included in the request.
188 */
189 public function set_from_request() {
190 foreach ($this->get_field_types() as $field => $type) {
191 $this->$field = optional_param($field, $this->$field, $type);
192 }
193 $this->numpartscorrect = $this->feedback;
194 }
195
196 /**
197 * @return string URL fragment. Parameters needed in the URL when continuing
198 * this preview.
199 */
612106b3
TH
200 public function get_url_params() {
201 $params = array();
06f8ed54 202 foreach ($this->get_field_types() as $field => $notused) {
c014b989 203 if ($field == 'behaviour' || $field == 'maxmark' || is_null($this->$field)) {
06f8ed54
TH
204 continue;
205 }
612106b3 206 $params[$field] = $this->$field;
06f8ed54 207 }
612106b3 208 return $params;
06f8ed54
TH
209 }
210}
211
212
fe6ce234
DC
213/**
214 * Called via pluginfile.php -> question_pluginfile to serve files belonging to
215 * a question in a question_attempt when that attempt is a preview.
216 *
d2b7803e
DC
217 * @package core_question
218 * @category files
219 * @param stdClass $course course settings object
220 * @param stdClass $context context object
fe6ce234
DC
221 * @param string $component the name of the component we are serving files for.
222 * @param string $filearea the name of the file area.
f7970e3c
TH
223 * @param int $qubaid the question_usage this image belongs to.
224 * @param int $slot the relevant slot within the usage.
fe6ce234
DC
225 * @param array $args the remaining bits of the file path.
226 * @param bool $forcedownload whether the user must be forced to download the file.
7a00d438 227 * @param array $options additional options affecting the file serving
fe6ce234
DC
228 * @return bool false if file not found, does not return if found - justsend the file
229 */
230function question_preview_question_pluginfile($course, $context, $component,
2aad62e9 231 $filearea, $qubaid, $slot, $args, $forcedownload, $fileoptions) {
7a719748 232 global $USER, $DB, $CFG;
fe6ce234 233
7a719748 234 $quba = question_engine::load_questions_usage_by_activity($qubaid);
fe6ce234 235
7a719748 236 if (!question_has_capability_on($quba->get_question($slot), 'use')) {
fe6ce234
DC
237 send_file_not_found();
238 }
239
7a719748
TH
240 $options = new question_display_options();
241 $options->feedback = question_display_options::VISIBLE;
242 $options->numpartscorrect = question_display_options::VISIBLE;
243 $options->generalfeedback = question_display_options::VISIBLE;
244 $options->rightanswer = question_display_options::VISIBLE;
245 $options->manualcomment = question_display_options::VISIBLE;
246 $options->history = question_display_options::VISIBLE;
247 if (!$quba->check_file_access($slot, $options, $component,
fe6ce234
DC
248 $filearea, $args, $forcedownload)) {
249 send_file_not_found();
250 }
251
252 $fs = get_file_storage();
253 $relativepath = implode('/', $args);
254 $fullpath = "/$context->id/$component/$filearea/$relativepath";
255 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
256 send_file_not_found();
257 }
258
2aad62e9 259 send_stored_file($file, 0, 0, $forcedownload, $fileoptions);
fe6ce234 260}
06f8ed54
TH
261
262/**
263 * The the URL to use for actions relating to this preview.
f7970e3c
TH
264 * @param int $questionid the question being previewed.
265 * @param int $qubaid the id of the question usage for this preview.
06f8ed54
TH
266 * @param question_preview_options $options the options in use.
267 */
268function question_preview_action_url($questionid, $qubaid,
56a4ae46 269 question_preview_options $options, $context) {
612106b3
TH
270 $params = array(
271 'id' => $questionid,
272 'previewid' => $qubaid,
273 );
56a4ae46
TH
274 if ($context->contextlevel == CONTEXT_MODULE) {
275 $params['cmid'] = $context->instanceid;
276 } else if ($context->contextlevel == CONTEXT_COURSE) {
277 $params['courseid'] = $context->instanceid;
278 }
612106b3
TH
279 $params = array_merge($params, $options->get_url_params());
280 return new moodle_url('/question/preview.php', $params);
06f8ed54
TH
281}
282
56a4ae46
TH
283/**
284 * The the URL to use for actions relating to this preview.
285 * @param int $questionid the question being previewed.
286 * @param int $qubaid the id of the question usage for this preview.
287 * @param question_preview_options $options the options in use.
288 */
289function question_preview_form_url($questionid, $context) {
290 $params = array(
291 'id' => $questionid,
292 );
293 if ($context->contextlevel == CONTEXT_MODULE) {
294 $params['cmid'] = $context->instanceid;
295 } else if ($context->contextlevel == CONTEXT_COURSE) {
296 $params['courseid'] = $context->instanceid;
297 }
298 return new moodle_url('/question/preview.php', $params);
299}
300
06f8ed54
TH
301/**
302 * Delete the current preview, if any, and redirect to start a new preview.
f7970e3c
TH
303 * @param int $previewid
304 * @param int $questionid
06f8ed54 305 * @param object $displayoptions
56a4ae46 306 * @param object $context
06f8ed54 307 */
56a4ae46 308function restart_preview($previewid, $questionid, $displayoptions, $context) {
c76145d3
TH
309 global $DB;
310
06f8ed54 311 if ($previewid) {
c76145d3 312 $transaction = $DB->start_delegated_transaction();
06f8ed54 313 question_engine::delete_questions_usage_by_activity($previewid);
c76145d3 314 $transaction->allow_commit();
06f8ed54 315 }
c014b989 316 redirect(question_preview_url($questionid, $displayoptions->behaviour,
56a4ae46 317 $displayoptions->maxmark, $displayoptions, $displayoptions->variant, $context));
06f8ed54 318}
2ec325c2
TH
319
320/**
321 * Scheduled tasks relating to question preview. Specifically, delete any old
322 * previews that are left over in the database.
323 */
324function question_preview_cron() {
325 $maxage = 24*60*60; // We delete previews that have not been touched for 24 hours.
326 $lastmodifiedcutoff = time() - $maxage;
327
328 mtrace("\n Cleaning up old question previews...", '');
329 $oldpreviews = new qubaid_join('{question_usages} quba', 'quba.id',
330 'quba.component = :qubacomponent
331 AND NOT EXISTS (
332 SELECT 1
333 FROM {question_attempts} qa
334 JOIN {question_attempt_steps} qas ON qas.questionattemptid = qa.id
335 WHERE qa.questionusageid = quba.id
336 AND (qa.timemodified > :qamodifiedcutoff
337 OR qas.timecreated > :stepcreatedcutoff)
338 )
339 ',
340 array('qubacomponent' => 'core_question_preview',
341 'qamodifiedcutoff' => $lastmodifiedcutoff, 'stepcreatedcutoff' => $lastmodifiedcutoff));
342
343 question_engine::delete_questions_usage_by_activities($oldpreviews);
344 mtrace('done.');
345}