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