MDL-70153 qtype_essay: Add behat test for attachments max size
[moodle.git] / question / type / edit_question_form.php
CommitLineData
aeb15530 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
36703ed7 17/**
18 * A base class for question editing forms.
19 *
7764183a 20 * @package moodlecore
2b7da645 21 * @subpackage questiontypes
b04a4319
TH
22 * @copyright 2006 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
fe6ce234 24 */
36703ed7 25
2b7da645 26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
603bd001
PS
29global $CFG;
30require_once($CFG->libdir.'/formslib.php');
31
a17b297d 32
72553162
TH
33abstract class question_wizard_form extends moodleform {
34 /**
35 * Add all the hidden form fields used by question/question.php.
36 */
37 protected function add_hidden_fields() {
38 $mform = $this->_form;
39
40 $mform->addElement('hidden', 'id');
41 $mform->setType('id', PARAM_INT);
42
43 $mform->addElement('hidden', 'inpopup');
44 $mform->setType('inpopup', PARAM_INT);
45
46 $mform->addElement('hidden', 'cmid');
47 $mform->setType('cmid', PARAM_INT);
48
49 $mform->addElement('hidden', 'courseid');
50 $mform->setType('courseid', PARAM_INT);
51
52 $mform->addElement('hidden', 'returnurl');
53 $mform->setType('returnurl', PARAM_LOCALURL);
54
55 $mform->addElement('hidden', 'scrollpos');
56 $mform->setType('scrollpos', PARAM_INT);
57
58 $mform->addElement('hidden', 'appendqnumstring');
59 $mform->setType('appendqnumstring', PARAM_ALPHA);
60 }
61}
62
36703ed7 63/**
64 * Form definition base class. This defines the common fields that
65 * all question types need. Question types should define their own
66 * class that inherits from this one, and implements the definition_inner()
67 * method.
271e6dec 68 *
7764183a
TH
69 * @copyright 2006 The Open University
70 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
36703ed7 71 */
72553162 72abstract class question_edit_form extends question_wizard_form {
2b7da645
TH
73 const DEFAULT_NUM_HINTS = 2;
74
271ffe3f 75 /**
76 * Question object with options and answers already loaded by get_question_options
77 * Be careful how you use this it is needed sometimes to set up the structure of the
32db0d42 78 * form in definition_inner but data is always loaded into the form with set_data.
271ffe3f 79 * @var object
80 */
2b7da645
TH
81 protected $question;
82
83 protected $contexts;
84 protected $category;
85 protected $categorycontext;
271e6dec 86
fe6ce234
DC
87 /** @var object current context */
88 public $context;
89 /** @var array html editor options */
90 public $editoroptions;
91 /** @var array options to preapre draft area */
92 public $fileoptions;
93 /** @var object instance of question type */
94 public $instance;
95
2b7da645 96 public function __construct($submiturl, $question, $category, $contexts, $formeditable = true) {
fe6ce234 97 global $DB;
271e6dec 98
271ffe3f 99 $this->question = $question;
271e6dec 100 $this->contexts = $contexts;
101
eaeb6b51
TH
102 $record = $DB->get_record('question_categories',
103 array('id' => $question->category), 'contextid');
d197ea43 104 $this->context = context::instance_by_id($record->contextid);
fe6ce234 105
eaeb6b51 106 $this->editoroptions = array('subdirs' => 1, 'maxfiles' => EDITOR_UNLIMITED_FILES,
0c0dfa8f 107 'context' => $this->context);
41dcc2a5 108 $this->fileoptions = array('subdirs' => 1, 'maxfiles' => -1, 'maxbytes' => -1);
fe6ce234 109
271e6dec 110 $this->category = $category;
d197ea43 111 $this->categorycontext = context::instance_by_id($category->contextid);
271e6dec 112
a8efb077 113 parent::__construct($submiturl, null, 'post', '', ['data-qtype' => $this->qtype()], $formeditable);
271ffe3f 114 }
8e652f02 115
36703ed7 116 /**
117 * Build the form definition.
1d284fbd 118 *
295043c2 119 * This adds all the form fields that the default question type supports.
36703ed7 120 * If your question type does not support all these fields, then you can
121 * override this method and remove the ones you don't want with $mform->removeElement().
122 */
c7df5006 123 protected function definition() {
8011be18 124 global $COURSE, $CFG, $DB, $PAGE;
1d284fbd 125
36703ed7 126 $qtype = $this->qtype();
f4fe3968 127 $langfile = "qtype_{$qtype}";
1d284fbd 128
f9b0500f 129 $mform = $this->_form;
36703ed7 130
131 // Standard fields at the start of the form.
271ffe3f 132 $mform->addElement('header', 'generalheader', get_string("general", 'form'));
1d284fbd 133
2b7da645 134 if (!isset($this->question->id)) {
76cf77e4
TH
135 if (!empty($this->question->formoptions->mustbeusable)) {
136 $contexts = $this->contexts->having_add_and_use();
137 } else {
138 $contexts = $this->contexts->having_cap('moodle/question:add');
139 }
140
c7218aef 141 // Adding question.
2b7da645 142 $mform->addElement('questioncategory', 'category', get_string('category', 'question'),
76cf77e4 143 array('contexts' => $contexts));
eaeb6b51
TH
144 } else if (!($this->question->formoptions->canmove ||
145 $this->question->formoptions->cansaveasnew)) {
5d548d3e 146 // Editing question with no permission to move from category.
2b7da645 147 $mform->addElement('questioncategory', 'category', get_string('category', 'question'),
271e6dec 148 array('contexts' => array($this->categorycontext)));
f276267d
TH
149 $mform->addElement('hidden', 'usecurrentcat', 1);
150 $mform->setType('usecurrentcat', PARAM_BOOL);
151 $mform->setConstant('usecurrentcat', 1);
271e6dec 152 } else {
c7218aef 153 // Editing question with permission to move from category or save as new q.
271e6dec 154 $currentgrp = array();
eaeb6b51
TH
155 $currentgrp[0] = $mform->createElement('questioncategory', 'category',
156 get_string('categorycurrent', 'question'),
271e6dec 157 array('contexts' => array($this->categorycontext)));
eaeb6b51
TH
158 if ($this->question->formoptions->canedit ||
159 $this->question->formoptions->cansaveasnew) {
c7218aef 160 // Not move only form.
eaeb6b51
TH
161 $currentgrp[1] = $mform->createElement('checkbox', 'usecurrentcat', '',
162 get_string('categorycurrentuse', 'question'));
271e6dec 163 $mform->setDefault('usecurrentcat', 1);
164 }
165 $currentgrp[0]->freeze();
166 $currentgrp[0]->setPersistantFreeze(false);
eaeb6b51
TH
167 $mform->addGroup($currentgrp, 'currentgrp',
168 get_string('categorycurrent', 'question'), null, false);
271e6dec 169
eaeb6b51
TH
170 $mform->addElement('questioncategory', 'categorymoveto',
171 get_string('categorymoveto', 'question'),
271e6dec 172 array('contexts' => array($this->categorycontext)));
eaeb6b51
TH
173 if ($this->question->formoptions->canedit ||
174 $this->question->formoptions->cansaveasnew) {
c7218aef 175 // Not move only form.
271e6dec 176 $mform->disabledIf('categorymoveto', 'usecurrentcat', 'checked');
177 }
178 }
375ed78a 179
eaeb6b51 180 $mform->addElement('text', 'name', get_string('questionname', 'question'),
b9b38ea7 181 array('size' => 50, 'maxlength' => 255));
271ffe3f 182 $mform->setType('name', PARAM_TEXT);
36703ed7 183 $mform->addRule('name', null, 'required', null, 'client');
1d284fbd 184
2b7da645 185 $mform->addElement('editor', 'questiontext', get_string('questiontext', 'question'),
0c0dfa8f 186 array('rows' => 15), $this->editoroptions);
36703ed7 187 $mform->setType('questiontext', PARAM_RAW);
363f8224 188 $mform->addRule('questiontext', null, 'required', null, 'client');
1d284fbd 189
bb407949 190 $mform->addElement('float', 'defaultmark', get_string('defaultmark', 'question'),
d2acbd1a 191 array('size' => 7));
2b7da645
TH
192 $mform->setDefault('defaultmark', 1);
193 $mform->addRule('defaultmark', null, 'required', null, 'client');
36703ed7 194
2b7da645 195 $mform->addElement('editor', 'generalfeedback', get_string('generalfeedback', 'question'),
0c0dfa8f 196 array('rows' => 10), $this->editoroptions);
36703ed7 197 $mform->setType('generalfeedback', PARAM_RAW);
2b7da645 198 $mform->addHelpButton('generalfeedback', 'generalfeedback', 'question');
1d284fbd 199
6189fda4
JB
200 $mform->addElement('text', 'idnumber', get_string('idnumber', 'question'), 'maxlength="100" size="10"');
201 $mform->addHelpButton('idnumber', 'idnumber', 'question');
202 $mform->setType('idnumber', PARAM_RAW);
203
36703ed7 204 // Any questiontype specific fields.
205 $this->definition_inner($mform);
206
4509be22 207 if (core_tag_tag::is_enabled('core_question', 'question')) {
081eb156 208 $this->add_tag_fields($mform);
4509be22
SL
209 }
210
2b7da645 211 if (!empty($this->question->id)) {
eaeb6b51
TH
212 $mform->addElement('header', 'createdmodifiedheader',
213 get_string('createdmodifiedheader', 'question'));
7f389342 214 $a = new stdClass();
2b7da645 215 if (!empty($this->question->createdby)) {
271e6dec 216 $a->time = userdate($this->question->timecreated);
eaeb6b51
TH
217 $a->user = fullname($DB->get_record(
218 'user', array('id' => $this->question->createdby)));
271e6dec 219 } else {
220 $a->time = get_string('unknown', 'question');
221 $a->user = get_string('unknown', 'question');
222 }
eaeb6b51
TH
223 $mform->addElement('static', 'created', get_string('created', 'question'),
224 get_string('byandon', 'question', $a));
2b7da645 225 if (!empty($this->question->modifiedby)) {
7f389342 226 $a = new stdClass();
271e6dec 227 $a->time = userdate($this->question->timemodified);
eaeb6b51
TH
228 $a->user = fullname($DB->get_record(
229 'user', array('id' => $this->question->modifiedby)));
230 $mform->addElement('static', 'modified', get_string('modified', 'question'),
231 get_string('byandon', 'question', $a));
271e6dec 232 }
233 }
234
72553162 235 $this->add_hidden_fields();
36703ed7 236
72553162
TH
237 $mform->addElement('hidden', 'qtype');
238 $mform->setType('qtype', PARAM_ALPHA);
fa583f5f 239
e0c41591
K
240 $mform->addElement('hidden', 'makecopy');
241 $mform->setType('makecopy', PARAM_INT);
242
375ed78a 243 $buttonarray = array();
8011be18 244 $buttonarray[] = $mform->createElement('submit', 'updatebutton',
7756e2bc 245 get_string('savechangesandcontinueediting', 'question'));
8011be18
TH
246 if ($this->can_preview()) {
247 $previewlink = $PAGE->get_renderer('core_question')->question_preview_link(
248 $this->question->id, $this->context, true);
7756e2bc 249 $buttonarray[] = $mform->createElement('static', 'previewlink', '', $previewlink);
375ed78a 250 }
7756e2bc 251
8011be18
TH
252 $mform->addGroup($buttonarray, 'updatebuttonar', '', array(' '), false);
253 $mform->closeHeaderBefore('updatebuttonar');
7756e2bc
K
254
255 $this->add_action_buttons(true, get_string('savechanges'));
271e6dec 256
435d3279 257 if ((!empty($this->question->id)) && (!($this->question->formoptions->canedit ||
eaeb6b51 258 $this->question->formoptions->cansaveasnew))) {
271e6dec 259 $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar', 'currentgrp'));
260 }
36703ed7 261 }
fa583f5f 262
36703ed7 263 /**
264 * Add any question-type specific form fields.
1d284fbd 265 *
266 * @param object $mform the form being built.
36703ed7 267 */
2b7da645 268 protected function definition_inner($mform) {
36703ed7 269 // By default, do nothing.
270 }
1d284fbd 271
8011be18
TH
272 /**
273 * Is the question being edited in a state where it can be previewed?
274 * @return bool whether to show the preview link.
275 */
276 protected function can_preview() {
277 return empty($this->question->beingcopied) && !empty($this->question->id) &&
278 $this->question->formoptions->canedit;
279 }
280
2aef1fe5 281 /**
282 * Get the list of form elements to repeat, one for each answer.
283 * @param object $mform the form being built.
284 * @param $label the label to use for each option.
285 * @param $gradeoptions the possible grades for each answer.
286 * @param $repeatedoptions reference to array of repeated options to fill
eaeb6b51
TH
287 * @param $answersoption reference to return the name of $question->options
288 * field holding an array of answers
2aef1fe5 289 * @return array of form fields.
290 */
2a6c5c52 291 protected function get_per_answer_fields($mform, $label, $gradeoptions,
eaeb6b51 292 &$repeatedoptions, &$answersoption) {
2aef1fe5 293 $repeated = array();
c7218aef
CC
294 $answeroptions = array();
295 $answeroptions[] = $mform->createElement('text', 'answer',
296 $label, array('size' => 40));
297 $answeroptions[] = $mform->createElement('select', 'fraction',
eaeb6b51 298 get_string('grade'), $gradeoptions);
c7218aef
CC
299 $repeated[] = $mform->createElement('group', 'answeroptions',
300 $label, $answeroptions, null, false);
eaeb6b51
TH
301 $repeated[] = $mform->createElement('editor', 'feedback',
302 get_string('feedback', 'question'), array('rows' => 5), $this->editoroptions);
2aef1fe5 303 $repeatedoptions['answer']['type'] = PARAM_RAW;
304 $repeatedoptions['fraction']['default'] = 0;
305 $answersoption = 'answers';
306 return $repeated;
307 }
308
081eb156
RW
309 /**
310 * Add the tag and course tag fields to the mform.
311 *
312 * If the form is being built in a course context then add the field
313 * for course tags.
314 *
315 * If the question category doesn't belong to a course context or we
316 * aren't editing in a course context then add the tags element to allow
317 * tags to be added to the question category context.
318 *
319 * @param object $mform The form being built
320 */
321 protected function add_tag_fields($mform) {
3722c7d6
SR
322 global $CFG, $DB;
323
081eb156
RW
324 $hastagcapability = question_has_capability_on($this->question, 'tag');
325 // Is the question category in a course context?
326 $qcontext = $this->categorycontext;
327 $qcoursecontext = $qcontext->get_course_context(false);
328 $iscourseoractivityquestion = !empty($qcoursecontext);
329 // Is the current context we're editing in a course context?
330 $editingcontext = $this->contexts->lowest();
331 $editingcoursecontext = $editingcontext->get_course_context(false);
332 $iseditingcontextcourseoractivity = !empty($editingcoursecontext);
333
334 $mform->addElement('header', 'tagsheader', get_string('tags'));
b71f21bd
SL
335 $tags = \core_tag_tag::get_tags_by_area_in_contexts('core_question', 'question', $this->contexts->all());
336 $tagstrings = [];
337 foreach ($tags as $tag) {
338 $tagstrings[$tag->name] = $tag->name;
339 }
3722c7d6
SR
340
341 $showstandard = core_tag_area::get_showstandard('core_question', 'question');
342 if ($showstandard != core_tag_tag::HIDE_STANDARD) {
343 $namefield = empty($CFG->keeptagnamecase) ? 'name' : 'rawname';
344 $standardtags = $DB->get_records('tag',
345 array('isstandard' => 1, 'tagcollid' => core_tag_area::get_collection('core', 'question')),
346 $namefield, 'id,' . $namefield);
347 foreach ($standardtags as $standardtag) {
348 $tagstrings[$standardtag->$namefield] = $standardtag->$namefield;
349 }
350 }
351
b71f21bd
SL
352 $options = [
353 'tags' => true,
354 'multiple' => true,
355 'noselectionstring' => get_string('anytags', 'quiz'),
356 ];
357 $mform->addElement('autocomplete', 'tags', get_string('tags'), $tagstrings, $options);
081eb156
RW
358
359 if (!$hastagcapability) {
360 $mform->hardFreeze('tags');
361 }
362
363 if ($iseditingcontextcourseoractivity && !$iscourseoractivityquestion) {
364 // If the question is being edited in a course or activity context
365 // and the question isn't a course or activity level question then
366 // allow course tags to be added to the course.
367 $coursetagheader = get_string('questionformtagheader', 'core_question',
368 $editingcoursecontext->get_context_name(true));
369 $mform->addElement('header', 'coursetagsheader', $coursetagheader);
b71f21bd 370 $mform->addElement('autocomplete', 'coursetags', get_string('tags'), $tagstrings, $options);
081eb156
RW
371
372 if (!$hastagcapability) {
373 $mform->hardFreeze('coursetags');
374 }
375 }
376 }
377
2aef1fe5 378 /**
379 * Add a set of form fields, obtained from get_per_answer_fields, to the form,
380 * one for each existing answer, with some blanks for some new ones.
381 * @param object $mform the form being built.
382 * @param $label the label to use for each option.
383 * @param $gradeoptions the possible grades for each answer.
eaeb6b51
TH
384 * @param $minoptions the minimum number of answer blanks to display.
385 * Default QUESTION_NUMANS_START.
2aef1fe5 386 * @param $addoptions the number of answer blanks to add. Default QUESTION_NUMANS_ADD.
387 */
eaeb6b51
TH
388 protected function add_per_answer_fields(&$mform, $label, $gradeoptions,
389 $minoptions = QUESTION_NUMANS_START, $addoptions = QUESTION_NUMANS_ADD) {
c7218aef
CC
390 $mform->addElement('header', 'answerhdr',
391 get_string('answers', 'question'), '');
392 $mform->setExpanded('answerhdr', 1);
2aef1fe5 393 $answersoption = '';
394 $repeatedoptions = array();
eaeb6b51
TH
395 $repeated = $this->get_per_answer_fields($mform, $label, $gradeoptions,
396 $repeatedoptions, $answersoption);
2aef1fe5 397
2b7da645 398 if (isset($this->question->options)) {
c3c65f41 399 $repeatsatstart = count($this->question->options->$answersoption);
2aef1fe5 400 } else {
c3c65f41 401 $repeatsatstart = $minoptions;
2aef1fe5 402 }
403
eaeb6b51
TH
404 $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions,
405 'noanswers', 'addanswers', $addoptions,
c7218aef 406 $this->get_more_choices_string(), true);
55748620
TH
407 }
408
409 /**
410 * Language string to use for 'Add {no} more {whatever we call answers}'.
411 */
412 protected function get_more_choices_string() {
413 return get_string('addmorechoiceblanks', 'question');
2aef1fe5 414 }
415
2b7da645
TH
416 protected function add_combined_feedback_fields($withshownumpartscorrect = false) {
417 $mform = $this->_form;
418
eaeb6b51
TH
419 $mform->addElement('header', 'combinedfeedbackhdr',
420 get_string('combinedfeedback', 'question'));
2b7da645 421
eaeb6b51
TH
422 $fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
423 foreach ($fields as $feedbackname) {
086cced7
CC
424 $element = $mform->addElement('editor', $feedbackname,
425 get_string($feedbackname, 'question'),
2b7da645
TH
426 array('rows' => 5), $this->editoroptions);
427 $mform->setType($feedbackname, PARAM_RAW);
086cced7 428 // Using setValue() as setDefault() does not work for the editor class.
e0c41591 429 $element->setValue(array('text' => get_string($feedbackname.'default', 'question')));
2b7da645
TH
430
431 if ($withshownumpartscorrect && $feedbackname == 'partiallycorrectfeedback') {
f7094147 432 $mform->addElement('advcheckbox', 'shownumcorrect',
eaeb6b51 433 get_string('options', 'question'),
d5ffb789 434 get_string('shownumpartscorrectwhenfinished', 'question'));
f0a64f01 435 $mform->setDefault('shownumcorrect', true);
2b7da645
TH
436 }
437 }
438 }
439
6dbc3460
CC
440 /**
441 * Create the form elements required by one hint.
442 * @param string $withclearwrong whether this quesiton type uses the 'Clear wrong' option on hints.
443 * @param string $withshownumpartscorrect whether this quesiton type uses the 'Show num parts correct' option on hints.
444 * @return array form field elements for one hint.
445 */
2b7da645
TH
446 protected function get_hint_fields($withclearwrong = false, $withshownumpartscorrect = false) {
447 $mform = $this->_form;
448
6dbc3460 449 $repeatedoptions = array();
2b7da645 450 $repeated = array();
6dbc3460 451 $repeated[] = $mform->createElement('editor', 'hint', get_string('hintn', 'question'),
7a719748 452 array('rows' => 5), $this->editoroptions);
2b7da645
TH
453 $repeatedoptions['hint']['type'] = PARAM_RAW;
454
6dbc3460 455 $optionelements = array();
2b7da645 456 if ($withclearwrong) {
6dbc3460 457 $optionelements[] = $mform->createElement('advcheckbox', 'hintclearwrong',
eaeb6b51 458 get_string('options', 'question'), get_string('clearwrongparts', 'question'));
2b7da645
TH
459 }
460 if ($withshownumpartscorrect) {
6dbc3460 461 $optionelements[] = $mform->createElement('advcheckbox', 'hintshownumcorrect', '',
eaeb6b51 462 get_string('shownumpartscorrect', 'question'));
2b7da645
TH
463 }
464
6dbc3460
CC
465 if (count($optionelements)) {
466 $repeated[] = $mform->createElement('group', 'hintoptions',
467 get_string('hintnoptions', 'question'), $optionelements, null, false);
468 }
469
2b7da645
TH
470 return array($repeated, $repeatedoptions);
471 }
472
eaeb6b51
TH
473 protected function add_interactive_settings($withclearwrong = false,
474 $withshownumpartscorrect = false) {
2b7da645
TH
475 $mform = $this->_form;
476
eaeb6b51
TH
477 $mform->addElement('header', 'multitriesheader',
478 get_string('settingsformultipletries', 'question'));
2b7da645
TH
479
480 $penalties = array(
481 1.0000000,
482 0.5000000,
483 0.3333333,
484 0.2500000,
485 0.2000000,
486 0.1000000,
487 0.0000000
488 );
489 if (!empty($this->question->penalty) && !in_array($this->question->penalty, $penalties)) {
490 $penalties[] = $this->question->penalty;
491 sort($penalties);
492 }
493 $penaltyoptions = array();
494 foreach ($penalties as $penalty) {
f4fe3968 495 $penaltyoptions["{$penalty}"] = (100 * $penalty) . '%';
2b7da645 496 }
eaeb6b51
TH
497 $mform->addElement('select', 'penalty',
498 get_string('penaltyforeachincorrecttry', 'question'), $penaltyoptions);
068b4594 499 $mform->addHelpButton('penalty', 'penaltyforeachincorrecttry', 'question');
2b7da645
TH
500 $mform->setDefault('penalty', 0.3333333);
501
502 if (isset($this->question->hints)) {
503 $counthints = count($this->question->hints);
504 } else {
505 $counthints = 0;
506 }
507
508 if ($this->question->formoptions->repeatelements) {
509 $repeatsatstart = max(self::DEFAULT_NUM_HINTS, $counthints);
510 } else {
511 $repeatsatstart = $counthints;
512 }
513
514 list($repeated, $repeatedoptions) = $this->get_hint_fields(
515 $withclearwrong, $withshownumpartscorrect);
516 $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions,
6dbc3460 517 'numhints', 'addhint', 1, get_string('addanotherhint', 'question'), true);
2b7da645
TH
518 }
519
520 public function set_data($question) {
f9b0500f 521 question_bank::get_qtype($question->qtype)->set_default_options($question);
2b7da645 522
c7218aef 523 // Prepare question text.
fe6ce234
DC
524 $draftid = file_get_submitted_draft_itemid('questiontext');
525
526 if (!empty($question->questiontext)) {
527 $questiontext = $question->questiontext;
528 } else {
1a2de1c7
TH
529 $questiontext = $this->_form->getElement('questiontext')->getValue();
530 $questiontext = $questiontext['text'];
271ffe3f 531 }
eaeb6b51
TH
532 $questiontext = file_prepare_draft_area($draftid, $this->context->id,
533 'question', 'questiontext', empty($question->id) ? null : (int) $question->id,
534 $this->fileoptions, $questiontext);
fe6ce234
DC
535
536 $question->questiontext = array();
537 $question->questiontext['text'] = $questiontext;
eaeb6b51
TH
538 $question->questiontext['format'] = empty($question->questiontextformat) ?
539 editors_get_preferred_format() : $question->questiontextformat;
fe6ce234
DC
540 $question->questiontext['itemid'] = $draftid;
541
c7218aef 542 // Prepare general feedback.
fe6ce234
DC
543 $draftid = file_get_submitted_draft_itemid('generalfeedback');
544
545 if (empty($question->generalfeedback)) {
1a2de1c7
TH
546 $generalfeedback = $this->_form->getElement('generalfeedback')->getValue();
547 $question->generalfeedback = $generalfeedback['text'];
fe6ce234
DC
548 }
549
eaeb6b51
TH
550 $feedback = file_prepare_draft_area($draftid, $this->context->id,
551 'question', 'generalfeedback', empty($question->id) ? null : (int) $question->id,
552 $this->fileoptions, $question->generalfeedback);
fe6ce234
DC
553 $question->generalfeedback = array();
554 $question->generalfeedback['text'] = $feedback;
eaeb6b51
TH
555 $question->generalfeedback['format'] = empty($question->generalfeedbackformat) ?
556 editors_get_preferred_format() : $question->generalfeedbackformat;
fe6ce234 557 $question->generalfeedback['itemid'] = $draftid;
295043c2 558
9152f6a5 559 // Remove unnecessary trailing 0s form grade fields.
cfd24d98 560 if (isset($question->defaultgrade)) {
561 $question->defaultgrade = 0 + $question->defaultgrade;
562 }
563 if (isset($question->penalty)) {
564 $question->penalty = 0 + $question->penalty;
565 }
9152f6a5 566
295043c2 567 // Set any options.
eaeb6b51
TH
568 $extraquestionfields = question_bank::get_qtype($question->qtype)->extra_question_fields();
569 if (is_array($extraquestionfields) && !empty($question->options)) {
570 array_shift($extraquestionfields);
571 foreach ($extraquestionfields as $field) {
a8e577de 572 if (property_exists($question->options, $field)) {
8e652f02 573 $question->$field = $question->options->$field;
574 }
295043c2 575 }
576 }
c9c989a0 577
c7218aef 578 // Subclass adds data_preprocessing code here.
fe6ce234 579 $question = $this->data_preprocessing($question);
2b7da645 580
32db0d42 581 parent::set_data($question);
36703ed7 582 }
1d284fbd 583
fe6ce234 584 /**
fdd015b7
TH
585 * Perform an preprocessing needed on the data passed to {@link set_data()}
586 * before it is used to initialise the form.
587 * @param object $question the data being passed to the form.
588 * @return object $question the modified data.
fe6ce234 589 */
c7df5006 590 protected function data_preprocessing($question) {
7a719748
TH
591 return $question;
592 }
593
fdd015b7
TH
594 /**
595 * Perform the necessary preprocessing for the fields added by
596 * {@link add_per_answer_fields()}.
597 * @param object $question the data being passed to the form.
598 * @return object $question the modified data.
599 */
2a6c5c52 600 protected function data_preprocessing_answers($question, $withanswerfiles = false) {
7a719748
TH
601 if (empty($question->options->answers)) {
602 return $question;
603 }
604
605 $key = 0;
eaeb6b51 606 foreach ($question->options->answers as $answer) {
2a6c5c52 607 if ($withanswerfiles) {
c7218aef 608 // Prepare the feedback editor to display files in draft area.
2a6c5c52 609 $draftitemid = file_get_submitted_draft_itemid('answer['.$key.']');
610 $question->answer[$key]['text'] = file_prepare_draft_area(
3d9645ae 611 $draftitemid, // Draftid
2a6c5c52 612 $this->context->id, // context
613 'question', // component
614 'answer', // filarea
615 !empty($answer->id) ? (int) $answer->id : null, // itemid
616 $this->fileoptions, // options
3d9645ae 617 $answer->answer // text.
2a6c5c52 618 );
619 $question->answer[$key]['itemid'] = $draftitemid;
620 $question->answer[$key]['format'] = $answer->answerformat;
621 } else {
622 $question->answer[$key] = $answer->answer;
623 }
624
7a719748
TH
625 $question->fraction[$key] = 0 + $answer->fraction;
626 $question->feedback[$key] = array();
627
628 // Evil hack alert. Formslib can store defaults in two ways for
eaeb6b51
TH
629 // repeat elements:
630 // ->_defaultValues['fraction[0]'] and
631 // ->_defaultValues['fraction'][0].
632 // The $repeatedoptions['fraction']['default'] = 0 bit above means
633 // that ->_defaultValues['fraction[0]'] has already been set, but we
634 // are using object notation here, so we will be setting
635 // ->_defaultValues['fraction'][0]. That does not work, so we have
c7218aef 636 // to unset ->_defaultValues['fraction[0]'].
f4fe3968 637 unset($this->_form->_defaultValues["fraction[{$key}]"]);
7a719748 638
c7218aef 639 // Prepare the feedback editor to display files in draft area.
7a719748
TH
640 $draftitemid = file_get_submitted_draft_itemid('feedback['.$key.']');
641 $question->feedback[$key]['text'] = file_prepare_draft_area(
3d9645ae 642 $draftitemid, // Draftid
7a719748
TH
643 $this->context->id, // context
644 'question', // component
645 'answerfeedback', // filarea
646 !empty($answer->id) ? (int) $answer->id : null, // itemid
647 $this->fileoptions, // options
3d9645ae 648 $answer->feedback // text.
7a719748
TH
649 );
650 $question->feedback[$key]['itemid'] = $draftitemid;
651 $question->feedback[$key]['format'] = $answer->feedbackformat;
652 $key++;
653 }
a68207b6
OS
654
655 // Now process extra answer fields.
656 $extraanswerfields = question_bank::get_qtype($question->qtype)->extra_answer_fields();
657 if (is_array($extraanswerfields)) {
658 // Omit table name.
659 array_shift($extraanswerfields);
660 $question = $this->data_preprocessing_extra_answer_fields($question, $extraanswerfields);
661 }
662
7a719748
TH
663 return $question;
664 }
665
a68207b6
OS
666 /**
667 * Perform the necessary preprocessing for the extra answer fields.
668 *
669 * Questions that do something not trivial when editing extra answer fields
670 * will want to override this.
671 * @param object $question the data being passed to the form.
672 * @param array $extraanswerfields extra answer fields (without table name).
673 * @return object $question the modified data.
674 */
675 protected function data_preprocessing_extra_answer_fields($question, $extraanswerfields) {
676 // Setting $question->$field[$key] won't work in PHP, so we need set an array of answer values to $question->$field.
677 // As we may have several extra fields with data for several answers in each, we use an array of arrays.
678 // Index in $extrafieldsdata is an extra answer field name, value - array of it's data for each answer.
679 $extrafieldsdata = array();
680 // First, prepare an array if empty arrays for each extra answer fields data.
681 foreach ($extraanswerfields as $field) {
682 $extrafieldsdata[$field] = array();
683 }
684
685 // Fill arrays with data from $question->options->answers.
686 $key = 0;
687 foreach ($question->options->answers as $answer) {
688 foreach ($extraanswerfields as $field) {
689 // See hack comment in {@link data_preprocessing_answers()}.
0e30442c 690 unset($this->_form->_defaultValues["{$field}[{$key}]"]);
a68207b6
OS
691 $extrafieldsdata[$field][$key] = $this->data_preprocessing_extra_answer_field($answer, $field);
692 }
693 $key++;
694 }
695
696 // Set this data in the $question object.
697 foreach ($extraanswerfields as $field) {
698 $question->$field = $extrafieldsdata[$field];
699 }
700 return $question;
701 }
702
703 /**
704 * Perfmorm preprocessing for particular extra answer field.
705 *
706 * Questions with non-trivial DB - form element relationship will
707 * want to override this.
708 * @param object $answer an answer object to get extra field from.
709 * @param string $field extra answer field name.
710 * @return field value to be set to the form.
711 */
712 protected function data_preprocessing_extra_answer_field($answer, $field) {
713 return $answer->$field;
714 }
715
fdd015b7
TH
716 /**
717 * Perform the necessary preprocessing for the fields added by
718 * {@link add_combined_feedback_fields()}.
719 * @param object $question the data being passed to the form.
720 * @return object $question the modified data.
721 */
eaeb6b51
TH
722 protected function data_preprocessing_combined_feedback($question,
723 $withshownumcorrect = false) {
c9c989a0
TH
724 if (empty($question->options)) {
725 return $question;
726 }
727
eaeb6b51
TH
728 $fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
729 foreach ($fields as $feedbackname) {
c9c989a0 730 $draftid = file_get_submitted_draft_itemid($feedbackname);
8b9dfc2b
TH
731 $feedback = array();
732 $feedback['text'] = file_prepare_draft_area(
3d9645ae 733 $draftid, // Draftid
c9c989a0 734 $this->context->id, // context
93cadb1e 735 'question', // component
c9c989a0
TH
736 $feedbackname, // filarea
737 !empty($question->id) ? (int) $question->id : null, // itemid
738 $this->fileoptions, // options
3d9645ae 739 $question->options->$feedbackname // text.
c9c989a0
TH
740 );
741 $feedbackformat = $feedbackname . 'format';
8b9dfc2b
TH
742 $feedback['format'] = $question->options->$feedbackformat;
743 $feedback['itemid'] = $draftid;
744
745 $question->$feedbackname = $feedback;
c9c989a0
TH
746 }
747
748 if ($withshownumcorrect) {
749 $question->shownumcorrect = $question->options->shownumcorrect;
750 }
751
752 return $question;
753 }
754
fdd015b7
TH
755 /**
756 * Perform the necessary preprocessing for the hint fields.
757 * @param object $question the data being passed to the form.
758 * @return object $question the modified data.
759 */
eaeb6b51
TH
760 protected function data_preprocessing_hints($question, $withclearwrong = false,
761 $withshownumpartscorrect = false) {
7a719748
TH
762 if (empty($question->hints)) {
763 return $question;
764 }
765
766 $key = 0;
eaeb6b51 767 foreach ($question->hints as $hint) {
7a719748
TH
768 $question->hint[$key] = array();
769
c7218aef 770 // Prepare feedback editor to display files in draft area.
7a719748
TH
771 $draftitemid = file_get_submitted_draft_itemid('hint['.$key.']');
772 $question->hint[$key]['text'] = file_prepare_draft_area(
3d9645ae 773 $draftitemid, // Draftid
7a719748
TH
774 $this->context->id, // context
775 'question', // component
776 'hint', // filarea
777 !empty($hint->id) ? (int) $hint->id : null, // itemid
778 $this->fileoptions, // options
3d9645ae 779 $hint->hint // text.
7a719748
TH
780 );
781 $question->hint[$key]['itemid'] = $draftitemid;
782 $question->hint[$key]['format'] = $hint->hintformat;
783 $key++;
7a719748 784
8b9dfc2b
TH
785 if ($withclearwrong) {
786 $question->hintclearwrong[] = $hint->clearwrong;
787 }
788 if ($withshownumpartscorrect) {
789 $question->hintshownumcorrect[] = $hint->shownumcorrect;
790 }
791 }
7a719748 792
fe6ce234
DC
793 return $question;
794 }
795
2b7da645 796 public function validation($fromform, $files) {
6189fda4
JB
797 global $DB;
798
2b7da645 799 $errors = parent::validation($fromform, $files);
51c5e605 800 if (empty($fromform['makecopy']) && isset($this->question->id)
eaeb6b51
TH
801 && ($this->question->formoptions->canedit ||
802 $this->question->formoptions->cansaveasnew)
51c5e605 803 && empty($fromform['usecurrentcat']) && !$this->question->formoptions->canmove) {
2b7da645
TH
804 $errors['currentgrp'] = get_string('nopermissionmove', 'question');
805 }
00cefd92 806
9275220d
SR
807 // Category.
808 if (empty($fromform['category'])) {
809 // User has provided an invalid category.
810 $errors['category'] = get_string('required');
811 }
812
00cefd92 813 // Default mark.
1aecaa3d 814 if (array_key_exists('defaultmark', $fromform) && $fromform['defaultmark'] < 0) {
00cefd92
TH
815 $errors['defaultmark'] = get_string('defaultmarkmustbepositive', 'question');
816 }
817
6189fda4
JB
818 // Can only have one idnumber per category.
819 if (strpos($fromform['category'], ',') !== false) {
820 list($category, $categorycontextid) = explode(',', $fromform['category']);
821 } else {
822 $category = $fromform['category'];
823 }
824 if (isset($fromform['idnumber']) && ((string) $fromform['idnumber'] !== '')) {
825 if (empty($fromform['usecurrentcat']) && !empty($fromform['categorymoveto'])) {
826 $categoryinfo = $fromform['categorymoveto'];
827 } else {
828 $categoryinfo = $fromform['category'];
829 }
830 list($categoryid, $notused) = explode(',', $categoryinfo);
831 $conditions = 'category = ? AND idnumber = ?';
832 $params = [$categoryid, $fromform['idnumber']];
833 if (!empty($this->question->id)) {
834 $conditions .= ' AND id <> ?';
835 $params[] = $this->question->id;
836 }
837 if ($DB->record_exists_select('question', $conditions, $params)) {
838 $errors['idnumber'] = get_string('idnumbertaken', 'error');
839 }
840 }
841
2b7da645
TH
842 return $errors;
843 }
844
36703ed7 845 /**
846 * Override this in the subclass to question type name.
eaeb6b51
TH
847 * @return the question type name, should be the same as the name() method
848 * in the question type class.
36703ed7 849 */
2b7da645 850 public abstract function qtype();
2a22be64 851
dff72cde
SH
852 /**
853 * Returns an array of editor options with collapsed options turned off.
0c0dfa8f 854 * @deprecated since 2.6
dff72cde
SH
855 * @return array
856 */
857 protected function get_non_collabsible_editor_options() {
0c0dfa8f
DW
858 debugging('get_non_collabsible_editor_options() is deprecated, use $this->editoroptions instead.', DEBUG_DEVELOPER);
859 return $this->editoroptions;
2a22be64 860 }
0c0dfa8f 861
36703ed7 862}