MDL-54594 mod_assign: Fix editpdf toolbar breakpoints
[moodle.git] / question / previewlib.php
blob00c8d2394a5e8c958e55349b2a9ff58f7c7ed0ce
1 <?php
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.
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/>.
17 /**
18 * Library functions used by question/preview.php.
20 * @package moodlecore
21 * @subpackage questionengine
22 * @copyright 2010 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir . '/formslib.php');
32 /**
33 * Settings form for the preview options.
35 * @copyright 2009 The Open University
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class preview_options_form extends moodleform {
39 public function definition() {
40 $mform = $this->_form;
42 $hiddenofvisible = array(
43 question_display_options::HIDDEN => get_string('notshown', 'question'),
44 question_display_options::VISIBLE => get_string('shown', 'question'),
47 $mform->addElement('header', 'optionsheader', get_string('attemptoptions', 'question'));
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);
53 $mform->addHelpButton('behaviour', 'howquestionsbehave', 'question');
55 $mform->addElement('text', 'maxmark', get_string('markedoutof', 'question'),
56 array('size' => '5'));
57 $mform->setType('maxmark', PARAM_FLOAT);
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));
64 $mform->setType('variant', PARAM_INT);
66 $mform->addElement('submit', 'saverestart',
67 get_string('restartwiththeseoptions', 'question'));
69 $mform->addElement('header', 'optionsheader', get_string('displayoptions', 'question'));
71 $mform->addElement('select', 'correctness', get_string('whethercorrect', 'question'),
72 $hiddenofvisible);
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'),
79 $mform->addElement('select', 'marks', get_string('marks', 'question'), $marksoptions);
81 $mform->addElement('select', 'markdp', get_string('decimalplacesingrades', 'question'),
82 question_engine::get_dp_options());
84 $mform->addElement('select', 'feedback',
85 get_string('specificfeedback', 'question'), $hiddenofvisible);
87 $mform->addElement('select', 'generalfeedback',
88 get_string('generalfeedback', 'question'), $hiddenofvisible);
90 $mform->addElement('select', 'rightanswer',
91 get_string('rightanswer', 'question'), $hiddenofvisible);
93 $mform->addElement('select', 'history',
94 get_string('responsehistory', 'question'), $hiddenofvisible);
96 $mform->addElement('submit', 'saveupdate',
97 get_string('updatedisplayoptions', 'question'));
103 * Displays question preview options as default and set the options
104 * Setting default, getting and setting user preferences in question preview options.
106 * @copyright 2010 The Open University
107 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
109 class question_preview_options extends question_display_options {
110 /** @var string the behaviour to use for this preview. */
111 public $behaviour;
113 /** @var number the maximum mark to use for this preview. */
114 public $maxmark;
116 /** @var int the variant of the question to preview. */
117 public $variant;
119 /** @var string prefix to append to field names to get user_preference names. */
120 const OPTIONPREFIX = 'question_preview_options_';
123 * Constructor.
125 public function __construct($question) {
126 $this->behaviour = 'deferredfeedback';
127 $this->maxmark = $question->defaultmark;
128 $this->variant = null;
129 $this->correctness = self::VISIBLE;
130 $this->marks = self::MARK_AND_MAX;
131 $this->markdp = get_config('quiz', 'decimalpoints');
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;
142 * @return array names of the options we store in the user preferences table.
144 protected function get_user_pref_fields() {
145 return array('behaviour', 'correctness', 'marks', 'markdp', 'feedback',
146 'generalfeedback', 'rightanswer', 'history');
150 * @return array names and param types of the options we read from the request.
152 protected function get_field_types() {
153 return array(
154 'behaviour' => PARAM_ALPHA,
155 'maxmark' => PARAM_FLOAT,
156 'variant' => PARAM_INT,
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,
168 * Load the value of the options from the user_preferences table.
170 public function load_user_defaults() {
171 $defaults = get_config('question_preview');
172 foreach ($this->get_user_pref_fields() as $field) {
173 $this->$field = get_user_preferences(
174 self::OPTIONPREFIX . $field, $defaults->$field);
176 $this->numpartscorrect = $this->feedback;
180 * Save a change to the user's preview options to the database.
181 * @param object $newoptions
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);
192 * Set the value of any fields included in the request.
194 public function set_from_request() {
195 foreach ($this->get_field_types() as $field => $type) {
196 $this->$field = optional_param($field, $this->$field, $type);
198 $this->numpartscorrect = $this->feedback;
202 * @return string URL fragment. Parameters needed in the URL when continuing
203 * this preview.
205 public function get_url_params() {
206 $params = array();
207 foreach ($this->get_field_types() as $field => $notused) {
208 if ($field == 'behaviour' || $field == 'maxmark' || is_null($this->$field)) {
209 continue;
211 $params[$field] = $this->$field;
213 return $params;
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.
222 * @package core_question
223 * @category files
224 * @param stdClass $course course settings object
225 * @param stdClass $context context object
226 * @param string $component the name of the component we are serving files for.
227 * @param string $filearea the name of the file area.
228 * @param int $qubaid the question_usage this image belongs to.
229 * @param int $slot the relevant slot within the usage.
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.
232 * @param array $options additional options affecting the file serving
233 * @return bool false if file not found, does not return if found - justsend the file
235 function question_preview_question_pluginfile($course, $context, $component,
236 $filearea, $qubaid, $slot, $args, $forcedownload, $fileoptions) {
237 global $USER, $DB, $CFG;
239 list($context, $course, $cm) = get_context_info_array($context->id);
240 require_login($course, false, $cm);
242 $quba = question_engine::load_questions_usage_by_activity($qubaid);
244 if (!question_has_capability_on($quba->get_question($slot), 'use')) {
245 send_file_not_found();
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,
256 $filearea, $args, $forcedownload)) {
257 send_file_not_found();
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();
267 send_stored_file($file, 0, 0, $forcedownload, $fileoptions);
271 * The the URL to use for actions relating to this preview.
272 * @param int $questionid the question being previewed.
273 * @param int $qubaid the id of the question usage for this preview.
274 * @param question_preview_options $options the options in use.
276 function question_preview_action_url($questionid, $qubaid,
277 question_preview_options $options, $context) {
278 $params = array(
279 'id' => $questionid,
280 'previewid' => $qubaid,
282 if ($context->contextlevel == CONTEXT_MODULE) {
283 $params['cmid'] = $context->instanceid;
284 } else if ($context->contextlevel == CONTEXT_COURSE) {
285 $params['courseid'] = $context->instanceid;
287 $params = array_merge($params, $options->get_url_params());
288 return new moodle_url('/question/preview.php', $params);
292 * The the URL to use for actions relating to this preview.
293 * @param int $questionid the question being previewed.
294 * @param context $context the current moodle context.
295 * @param int $previewid optional previewid to sign post saved previewed answers.
297 function question_preview_form_url($questionid, $context, $previewid = null) {
298 $params = array(
299 'id' => $questionid,
301 if ($context->contextlevel == CONTEXT_MODULE) {
302 $params['cmid'] = $context->instanceid;
303 } else if ($context->contextlevel == CONTEXT_COURSE) {
304 $params['courseid'] = $context->instanceid;
306 if ($previewid) {
307 $params['previewid'] = $previewid;
309 return new moodle_url('/question/preview.php', $params);
313 * Delete the current preview, if any, and redirect to start a new preview.
314 * @param int $previewid
315 * @param int $questionid
316 * @param object $displayoptions
317 * @param object $context
319 function restart_preview($previewid, $questionid, $displayoptions, $context) {
320 global $DB;
322 if ($previewid) {
323 $transaction = $DB->start_delegated_transaction();
324 question_engine::delete_questions_usage_by_activity($previewid);
325 $transaction->allow_commit();
327 redirect(question_preview_url($questionid, $displayoptions->behaviour,
328 $displayoptions->maxmark, $displayoptions, $displayoptions->variant, $context));
332 * Scheduled tasks relating to question preview. Specifically, delete any old
333 * previews that are left over in the database.
335 function question_preview_cron() {
336 $maxage = 24*60*60; // We delete previews that have not been touched for 24 hours.
337 $lastmodifiedcutoff = time() - $maxage;
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
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)
353 array('qubacomponent' => 'core_question_preview', 'qubacomponent2' => 'core_question_preview',
354 'qamodifiedcutoff' => $lastmodifiedcutoff, 'stepcreatedcutoff' => $lastmodifiedcutoff));
356 question_engine::delete_questions_usage_by_activities($oldpreviews);
357 mtrace('done.');