MDL-24408 extra_answer_fields : get answers even if they miss extra data
[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
2b7da645 113 parent::__construct($submiturl, null, 'post', '', null, $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();
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)));
271e6dec 149 } else {
c7218aef 150 // Editing question with permission to move from category or save as new q.
271e6dec 151 $currentgrp = array();
eaeb6b51
TH
152 $currentgrp[0] = $mform->createElement('questioncategory', 'category',
153 get_string('categorycurrent', 'question'),
271e6dec 154 array('contexts' => array($this->categorycontext)));
eaeb6b51
TH
155 if ($this->question->formoptions->canedit ||
156 $this->question->formoptions->cansaveasnew) {
c7218aef 157 // Not move only form.
eaeb6b51
TH
158 $currentgrp[1] = $mform->createElement('checkbox', 'usecurrentcat', '',
159 get_string('categorycurrentuse', 'question'));
271e6dec 160 $mform->setDefault('usecurrentcat', 1);
161 }
162 $currentgrp[0]->freeze();
163 $currentgrp[0]->setPersistantFreeze(false);
eaeb6b51
TH
164 $mform->addGroup($currentgrp, 'currentgrp',
165 get_string('categorycurrent', 'question'), null, false);
271e6dec 166
eaeb6b51
TH
167 $mform->addElement('questioncategory', 'categorymoveto',
168 get_string('categorymoveto', 'question'),
271e6dec 169 array('contexts' => array($this->categorycontext)));
eaeb6b51
TH
170 if ($this->question->formoptions->canedit ||
171 $this->question->formoptions->cansaveasnew) {
c7218aef 172 // Not move only form.
271e6dec 173 $mform->disabledIf('categorymoveto', 'usecurrentcat', 'checked');
174 }
175 }
375ed78a 176
eaeb6b51 177 $mform->addElement('text', 'name', get_string('questionname', 'question'),
b9b38ea7 178 array('size' => 50, 'maxlength' => 255));
271ffe3f 179 $mform->setType('name', PARAM_TEXT);
36703ed7 180 $mform->addRule('name', null, 'required', null, 'client');
1d284fbd 181
2b7da645 182 $mform->addElement('editor', 'questiontext', get_string('questiontext', 'question'),
0c0dfa8f 183 array('rows' => 15), $this->editoroptions);
36703ed7 184 $mform->setType('questiontext', PARAM_RAW);
363f8224 185 $mform->addRule('questiontext', null, 'required', null, 'client');
1d284fbd 186
2b7da645 187 $mform->addElement('text', 'defaultmark', get_string('defaultmark', 'question'),
d2acbd1a 188 array('size' => 7));
7664182d 189 $mform->setType('defaultmark', PARAM_FLOAT);
2b7da645
TH
190 $mform->setDefault('defaultmark', 1);
191 $mform->addRule('defaultmark', null, 'required', null, 'client');
36703ed7 192
2b7da645 193 $mform->addElement('editor', 'generalfeedback', get_string('generalfeedback', 'question'),
0c0dfa8f 194 array('rows' => 10), $this->editoroptions);
36703ed7 195 $mform->setType('generalfeedback', PARAM_RAW);
2b7da645 196 $mform->addHelpButton('generalfeedback', 'generalfeedback', 'question');
1d284fbd 197
36703ed7 198 // Any questiontype specific fields.
199 $this->definition_inner($mform);
200
c599a682 201 if (!empty($CFG->usetags)) {
202 $mform->addElement('header', 'tagsheader', get_string('tags'));
203 $mform->addElement('tags', 'tags', get_string('tags'));
204 }
205
2b7da645 206 if (!empty($this->question->id)) {
eaeb6b51
TH
207 $mform->addElement('header', 'createdmodifiedheader',
208 get_string('createdmodifiedheader', 'question'));
7f389342 209 $a = new stdClass();
2b7da645 210 if (!empty($this->question->createdby)) {
271e6dec 211 $a->time = userdate($this->question->timecreated);
eaeb6b51
TH
212 $a->user = fullname($DB->get_record(
213 'user', array('id' => $this->question->createdby)));
271e6dec 214 } else {
215 $a->time = get_string('unknown', 'question');
216 $a->user = get_string('unknown', 'question');
217 }
eaeb6b51
TH
218 $mform->addElement('static', 'created', get_string('created', 'question'),
219 get_string('byandon', 'question', $a));
2b7da645 220 if (!empty($this->question->modifiedby)) {
7f389342 221 $a = new stdClass();
271e6dec 222 $a->time = userdate($this->question->timemodified);
eaeb6b51
TH
223 $a->user = fullname($DB->get_record(
224 'user', array('id' => $this->question->modifiedby)));
225 $mform->addElement('static', 'modified', get_string('modified', 'question'),
226 get_string('byandon', 'question', $a));
271e6dec 227 }
228 }
229
72553162 230 $this->add_hidden_fields();
36703ed7 231
72553162
TH
232 $mform->addElement('hidden', 'qtype');
233 $mform->setType('qtype', PARAM_ALPHA);
fa583f5f 234
e0c41591
K
235 $mform->addElement('hidden', 'makecopy');
236 $mform->setType('makecopy', PARAM_INT);
237
375ed78a 238 $buttonarray = array();
8011be18 239 $buttonarray[] = $mform->createElement('submit', 'updatebutton',
7756e2bc 240 get_string('savechangesandcontinueediting', 'question'));
8011be18
TH
241 if ($this->can_preview()) {
242 $previewlink = $PAGE->get_renderer('core_question')->question_preview_link(
243 $this->question->id, $this->context, true);
7756e2bc 244 $buttonarray[] = $mform->createElement('static', 'previewlink', '', $previewlink);
375ed78a 245 }
7756e2bc 246
8011be18
TH
247 $mform->addGroup($buttonarray, 'updatebuttonar', '', array(' '), false);
248 $mform->closeHeaderBefore('updatebuttonar');
7756e2bc
K
249
250 $this->add_action_buttons(true, get_string('savechanges'));
271e6dec 251
435d3279 252 if ((!empty($this->question->id)) && (!($this->question->formoptions->canedit ||
eaeb6b51 253 $this->question->formoptions->cansaveasnew))) {
271e6dec 254 $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar', 'currentgrp'));
255 }
36703ed7 256 }
fa583f5f 257
36703ed7 258 /**
259 * Add any question-type specific form fields.
1d284fbd 260 *
261 * @param object $mform the form being built.
36703ed7 262 */
2b7da645 263 protected function definition_inner($mform) {
36703ed7 264 // By default, do nothing.
265 }
1d284fbd 266
8011be18
TH
267 /**
268 * Is the question being edited in a state where it can be previewed?
269 * @return bool whether to show the preview link.
270 */
271 protected function can_preview() {
272 return empty($this->question->beingcopied) && !empty($this->question->id) &&
273 $this->question->formoptions->canedit;
274 }
275
2aef1fe5 276 /**
277 * Get the list of form elements to repeat, one for each answer.
278 * @param object $mform the form being built.
279 * @param $label the label to use for each option.
280 * @param $gradeoptions the possible grades for each answer.
281 * @param $repeatedoptions reference to array of repeated options to fill
eaeb6b51
TH
282 * @param $answersoption reference to return the name of $question->options
283 * field holding an array of answers
2aef1fe5 284 * @return array of form fields.
285 */
2a6c5c52 286 protected function get_per_answer_fields($mform, $label, $gradeoptions,
eaeb6b51 287 &$repeatedoptions, &$answersoption) {
2aef1fe5 288 $repeated = array();
c7218aef
CC
289 $answeroptions = array();
290 $answeroptions[] = $mform->createElement('text', 'answer',
291 $label, array('size' => 40));
292 $answeroptions[] = $mform->createElement('select', 'fraction',
eaeb6b51 293 get_string('grade'), $gradeoptions);
c7218aef
CC
294 $repeated[] = $mform->createElement('group', 'answeroptions',
295 $label, $answeroptions, null, false);
eaeb6b51
TH
296 $repeated[] = $mform->createElement('editor', 'feedback',
297 get_string('feedback', 'question'), array('rows' => 5), $this->editoroptions);
2aef1fe5 298 $repeatedoptions['answer']['type'] = PARAM_RAW;
299 $repeatedoptions['fraction']['default'] = 0;
300 $answersoption = 'answers';
301 return $repeated;
302 }
303
304 /**
305 * Add a set of form fields, obtained from get_per_answer_fields, to the form,
306 * one for each existing answer, with some blanks for some new ones.
307 * @param object $mform the form being built.
308 * @param $label the label to use for each option.
309 * @param $gradeoptions the possible grades for each answer.
eaeb6b51
TH
310 * @param $minoptions the minimum number of answer blanks to display.
311 * Default QUESTION_NUMANS_START.
2aef1fe5 312 * @param $addoptions the number of answer blanks to add. Default QUESTION_NUMANS_ADD.
313 */
eaeb6b51
TH
314 protected function add_per_answer_fields(&$mform, $label, $gradeoptions,
315 $minoptions = QUESTION_NUMANS_START, $addoptions = QUESTION_NUMANS_ADD) {
c7218aef
CC
316 $mform->addElement('header', 'answerhdr',
317 get_string('answers', 'question'), '');
318 $mform->setExpanded('answerhdr', 1);
2aef1fe5 319 $answersoption = '';
320 $repeatedoptions = array();
eaeb6b51
TH
321 $repeated = $this->get_per_answer_fields($mform, $label, $gradeoptions,
322 $repeatedoptions, $answersoption);
2aef1fe5 323
2b7da645 324 if (isset($this->question->options)) {
c3c65f41 325 $repeatsatstart = count($this->question->options->$answersoption);
2aef1fe5 326 } else {
c3c65f41 327 $repeatsatstart = $minoptions;
2aef1fe5 328 }
329
eaeb6b51
TH
330 $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions,
331 'noanswers', 'addanswers', $addoptions,
c7218aef 332 $this->get_more_choices_string(), true);
55748620
TH
333 }
334
335 /**
336 * Language string to use for 'Add {no} more {whatever we call answers}'.
337 */
338 protected function get_more_choices_string() {
339 return get_string('addmorechoiceblanks', 'question');
2aef1fe5 340 }
341
2b7da645
TH
342 protected function add_combined_feedback_fields($withshownumpartscorrect = false) {
343 $mform = $this->_form;
344
eaeb6b51
TH
345 $mform->addElement('header', 'combinedfeedbackhdr',
346 get_string('combinedfeedback', 'question'));
2b7da645 347
eaeb6b51
TH
348 $fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
349 foreach ($fields as $feedbackname) {
086cced7
CC
350 $element = $mform->addElement('editor', $feedbackname,
351 get_string($feedbackname, 'question'),
2b7da645
TH
352 array('rows' => 5), $this->editoroptions);
353 $mform->setType($feedbackname, PARAM_RAW);
086cced7 354 // Using setValue() as setDefault() does not work for the editor class.
e0c41591 355 $element->setValue(array('text' => get_string($feedbackname.'default', 'question')));
2b7da645
TH
356
357 if ($withshownumpartscorrect && $feedbackname == 'partiallycorrectfeedback') {
f7094147 358 $mform->addElement('advcheckbox', 'shownumcorrect',
eaeb6b51 359 get_string('options', 'question'),
d5ffb789 360 get_string('shownumpartscorrectwhenfinished', 'question'));
f0a64f01 361 $mform->setDefault('shownumcorrect', true);
2b7da645
TH
362 }
363 }
364 }
365
6dbc3460
CC
366 /**
367 * Create the form elements required by one hint.
368 * @param string $withclearwrong whether this quesiton type uses the 'Clear wrong' option on hints.
369 * @param string $withshownumpartscorrect whether this quesiton type uses the 'Show num parts correct' option on hints.
370 * @return array form field elements for one hint.
371 */
2b7da645
TH
372 protected function get_hint_fields($withclearwrong = false, $withshownumpartscorrect = false) {
373 $mform = $this->_form;
374
6dbc3460 375 $repeatedoptions = array();
2b7da645 376 $repeated = array();
6dbc3460 377 $repeated[] = $mform->createElement('editor', 'hint', get_string('hintn', 'question'),
7a719748 378 array('rows' => 5), $this->editoroptions);
2b7da645
TH
379 $repeatedoptions['hint']['type'] = PARAM_RAW;
380
6dbc3460 381 $optionelements = array();
2b7da645 382 if ($withclearwrong) {
6dbc3460 383 $optionelements[] = $mform->createElement('advcheckbox', 'hintclearwrong',
eaeb6b51 384 get_string('options', 'question'), get_string('clearwrongparts', 'question'));
2b7da645
TH
385 }
386 if ($withshownumpartscorrect) {
6dbc3460 387 $optionelements[] = $mform->createElement('advcheckbox', 'hintshownumcorrect', '',
eaeb6b51 388 get_string('shownumpartscorrect', 'question'));
2b7da645
TH
389 }
390
6dbc3460
CC
391 if (count($optionelements)) {
392 $repeated[] = $mform->createElement('group', 'hintoptions',
393 get_string('hintnoptions', 'question'), $optionelements, null, false);
394 }
395
2b7da645
TH
396 return array($repeated, $repeatedoptions);
397 }
398
eaeb6b51
TH
399 protected function add_interactive_settings($withclearwrong = false,
400 $withshownumpartscorrect = false) {
2b7da645
TH
401 $mform = $this->_form;
402
eaeb6b51
TH
403 $mform->addElement('header', 'multitriesheader',
404 get_string('settingsformultipletries', 'question'));
2b7da645
TH
405
406 $penalties = array(
407 1.0000000,
408 0.5000000,
409 0.3333333,
410 0.2500000,
411 0.2000000,
412 0.1000000,
413 0.0000000
414 );
415 if (!empty($this->question->penalty) && !in_array($this->question->penalty, $penalties)) {
416 $penalties[] = $this->question->penalty;
417 sort($penalties);
418 }
419 $penaltyoptions = array();
420 foreach ($penalties as $penalty) {
421 $penaltyoptions["$penalty"] = (100 * $penalty) . '%';
422 }
eaeb6b51
TH
423 $mform->addElement('select', 'penalty',
424 get_string('penaltyforeachincorrecttry', 'question'), $penaltyoptions);
068b4594 425 $mform->addHelpButton('penalty', 'penaltyforeachincorrecttry', 'question');
2b7da645
TH
426 $mform->setDefault('penalty', 0.3333333);
427
428 if (isset($this->question->hints)) {
429 $counthints = count($this->question->hints);
430 } else {
431 $counthints = 0;
432 }
433
434 if ($this->question->formoptions->repeatelements) {
435 $repeatsatstart = max(self::DEFAULT_NUM_HINTS, $counthints);
436 } else {
437 $repeatsatstart = $counthints;
438 }
439
440 list($repeated, $repeatedoptions) = $this->get_hint_fields(
441 $withclearwrong, $withshownumpartscorrect);
442 $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions,
6dbc3460 443 'numhints', 'addhint', 1, get_string('addanotherhint', 'question'), true);
2b7da645
TH
444 }
445
446 public function set_data($question) {
f9b0500f 447 question_bank::get_qtype($question->qtype)->set_default_options($question);
2b7da645 448
c7218aef 449 // Prepare question text.
fe6ce234
DC
450 $draftid = file_get_submitted_draft_itemid('questiontext');
451
452 if (!empty($question->questiontext)) {
453 $questiontext = $question->questiontext;
454 } else {
1a2de1c7
TH
455 $questiontext = $this->_form->getElement('questiontext')->getValue();
456 $questiontext = $questiontext['text'];
271ffe3f 457 }
eaeb6b51
TH
458 $questiontext = file_prepare_draft_area($draftid, $this->context->id,
459 'question', 'questiontext', empty($question->id) ? null : (int) $question->id,
460 $this->fileoptions, $questiontext);
fe6ce234
DC
461
462 $question->questiontext = array();
463 $question->questiontext['text'] = $questiontext;
eaeb6b51
TH
464 $question->questiontext['format'] = empty($question->questiontextformat) ?
465 editors_get_preferred_format() : $question->questiontextformat;
fe6ce234
DC
466 $question->questiontext['itemid'] = $draftid;
467
c7218aef 468 // Prepare general feedback.
fe6ce234
DC
469 $draftid = file_get_submitted_draft_itemid('generalfeedback');
470
471 if (empty($question->generalfeedback)) {
1a2de1c7
TH
472 $generalfeedback = $this->_form->getElement('generalfeedback')->getValue();
473 $question->generalfeedback = $generalfeedback['text'];
fe6ce234
DC
474 }
475
eaeb6b51
TH
476 $feedback = file_prepare_draft_area($draftid, $this->context->id,
477 'question', 'generalfeedback', empty($question->id) ? null : (int) $question->id,
478 $this->fileoptions, $question->generalfeedback);
fe6ce234
DC
479 $question->generalfeedback = array();
480 $question->generalfeedback['text'] = $feedback;
eaeb6b51
TH
481 $question->generalfeedback['format'] = empty($question->generalfeedbackformat) ?
482 editors_get_preferred_format() : $question->generalfeedbackformat;
fe6ce234 483 $question->generalfeedback['itemid'] = $draftid;
295043c2 484
9152f6a5 485 // Remove unnecessary trailing 0s form grade fields.
cfd24d98 486 if (isset($question->defaultgrade)) {
487 $question->defaultgrade = 0 + $question->defaultgrade;
488 }
489 if (isset($question->penalty)) {
490 $question->penalty = 0 + $question->penalty;
491 }
9152f6a5 492
295043c2 493 // Set any options.
eaeb6b51
TH
494 $extraquestionfields = question_bank::get_qtype($question->qtype)->extra_question_fields();
495 if (is_array($extraquestionfields) && !empty($question->options)) {
496 array_shift($extraquestionfields);
497 foreach ($extraquestionfields as $field) {
a8e577de 498 if (property_exists($question->options, $field)) {
8e652f02 499 $question->$field = $question->options->$field;
500 }
295043c2 501 }
502 }
c9c989a0 503
c7218aef 504 // Subclass adds data_preprocessing code here.
fe6ce234 505 $question = $this->data_preprocessing($question);
2b7da645 506
32db0d42 507 parent::set_data($question);
36703ed7 508 }
1d284fbd 509
fe6ce234 510 /**
fdd015b7
TH
511 * Perform an preprocessing needed on the data passed to {@link set_data()}
512 * before it is used to initialise the form.
513 * @param object $question the data being passed to the form.
514 * @return object $question the modified data.
fe6ce234 515 */
c7df5006 516 protected function data_preprocessing($question) {
7a719748
TH
517 return $question;
518 }
519
fdd015b7
TH
520 /**
521 * Perform the necessary preprocessing for the fields added by
522 * {@link add_per_answer_fields()}.
523 * @param object $question the data being passed to the form.
524 * @return object $question the modified data.
525 */
2a6c5c52 526 protected function data_preprocessing_answers($question, $withanswerfiles = false) {
7a719748
TH
527 if (empty($question->options->answers)) {
528 return $question;
529 }
530
531 $key = 0;
eaeb6b51 532 foreach ($question->options->answers as $answer) {
2a6c5c52 533 if ($withanswerfiles) {
c7218aef 534 // Prepare the feedback editor to display files in draft area.
2a6c5c52 535 $draftitemid = file_get_submitted_draft_itemid('answer['.$key.']');
536 $question->answer[$key]['text'] = file_prepare_draft_area(
3d9645ae 537 $draftitemid, // Draftid
2a6c5c52 538 $this->context->id, // context
539 'question', // component
540 'answer', // filarea
541 !empty($answer->id) ? (int) $answer->id : null, // itemid
542 $this->fileoptions, // options
3d9645ae 543 $answer->answer // text.
2a6c5c52 544 );
545 $question->answer[$key]['itemid'] = $draftitemid;
546 $question->answer[$key]['format'] = $answer->answerformat;
547 } else {
548 $question->answer[$key] = $answer->answer;
549 }
550
7a719748
TH
551 $question->fraction[$key] = 0 + $answer->fraction;
552 $question->feedback[$key] = array();
553
554 // Evil hack alert. Formslib can store defaults in two ways for
eaeb6b51
TH
555 // repeat elements:
556 // ->_defaultValues['fraction[0]'] and
557 // ->_defaultValues['fraction'][0].
558 // The $repeatedoptions['fraction']['default'] = 0 bit above means
559 // that ->_defaultValues['fraction[0]'] has already been set, but we
560 // are using object notation here, so we will be setting
561 // ->_defaultValues['fraction'][0]. That does not work, so we have
c7218aef 562 // to unset ->_defaultValues['fraction[0]'].
7a719748
TH
563 unset($this->_form->_defaultValues["fraction[$key]"]);
564
c7218aef 565 // Prepare the feedback editor to display files in draft area.
7a719748
TH
566 $draftitemid = file_get_submitted_draft_itemid('feedback['.$key.']');
567 $question->feedback[$key]['text'] = file_prepare_draft_area(
3d9645ae 568 $draftitemid, // Draftid
7a719748
TH
569 $this->context->id, // context
570 'question', // component
571 'answerfeedback', // filarea
572 !empty($answer->id) ? (int) $answer->id : null, // itemid
573 $this->fileoptions, // options
3d9645ae 574 $answer->feedback // text.
7a719748
TH
575 );
576 $question->feedback[$key]['itemid'] = $draftitemid;
577 $question->feedback[$key]['format'] = $answer->feedbackformat;
578 $key++;
579 }
580 return $question;
581 }
582
fdd015b7
TH
583 /**
584 * Perform the necessary preprocessing for the fields added by
585 * {@link add_combined_feedback_fields()}.
586 * @param object $question the data being passed to the form.
587 * @return object $question the modified data.
588 */
eaeb6b51
TH
589 protected function data_preprocessing_combined_feedback($question,
590 $withshownumcorrect = false) {
c9c989a0
TH
591 if (empty($question->options)) {
592 return $question;
593 }
594
eaeb6b51
TH
595 $fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
596 foreach ($fields as $feedbackname) {
c9c989a0 597 $draftid = file_get_submitted_draft_itemid($feedbackname);
8b9dfc2b
TH
598 $feedback = array();
599 $feedback['text'] = file_prepare_draft_area(
3d9645ae 600 $draftid, // Draftid
c9c989a0 601 $this->context->id, // context
93cadb1e 602 'question', // component
c9c989a0
TH
603 $feedbackname, // filarea
604 !empty($question->id) ? (int) $question->id : null, // itemid
605 $this->fileoptions, // options
3d9645ae 606 $question->options->$feedbackname // text.
c9c989a0
TH
607 );
608 $feedbackformat = $feedbackname . 'format';
8b9dfc2b
TH
609 $feedback['format'] = $question->options->$feedbackformat;
610 $feedback['itemid'] = $draftid;
611
612 $question->$feedbackname = $feedback;
c9c989a0
TH
613 }
614
615 if ($withshownumcorrect) {
616 $question->shownumcorrect = $question->options->shownumcorrect;
617 }
618
619 return $question;
620 }
621
fdd015b7
TH
622 /**
623 * Perform the necessary preprocessing for the hint fields.
624 * @param object $question the data being passed to the form.
625 * @return object $question the modified data.
626 */
eaeb6b51
TH
627 protected function data_preprocessing_hints($question, $withclearwrong = false,
628 $withshownumpartscorrect = false) {
7a719748
TH
629 if (empty($question->hints)) {
630 return $question;
631 }
632
633 $key = 0;
eaeb6b51 634 foreach ($question->hints as $hint) {
7a719748
TH
635 $question->hint[$key] = array();
636
c7218aef 637 // Prepare feedback editor to display files in draft area.
7a719748
TH
638 $draftitemid = file_get_submitted_draft_itemid('hint['.$key.']');
639 $question->hint[$key]['text'] = file_prepare_draft_area(
3d9645ae 640 $draftitemid, // Draftid
7a719748
TH
641 $this->context->id, // context
642 'question', // component
643 'hint', // filarea
644 !empty($hint->id) ? (int) $hint->id : null, // itemid
645 $this->fileoptions, // options
3d9645ae 646 $hint->hint // text.
7a719748
TH
647 );
648 $question->hint[$key]['itemid'] = $draftitemid;
649 $question->hint[$key]['format'] = $hint->hintformat;
650 $key++;
7a719748 651
8b9dfc2b
TH
652 if ($withclearwrong) {
653 $question->hintclearwrong[] = $hint->clearwrong;
654 }
655 if ($withshownumpartscorrect) {
656 $question->hintshownumcorrect[] = $hint->shownumcorrect;
657 }
658 }
7a719748 659
fe6ce234
DC
660 return $question;
661 }
662
2b7da645
TH
663 public function validation($fromform, $files) {
664 $errors = parent::validation($fromform, $files);
51c5e605 665 if (empty($fromform['makecopy']) && isset($this->question->id)
eaeb6b51
TH
666 && ($this->question->formoptions->canedit ||
667 $this->question->formoptions->cansaveasnew)
51c5e605 668 && empty($fromform['usecurrentcat']) && !$this->question->formoptions->canmove) {
2b7da645
TH
669 $errors['currentgrp'] = get_string('nopermissionmove', 'question');
670 }
00cefd92
TH
671
672 // Default mark.
1aecaa3d 673 if (array_key_exists('defaultmark', $fromform) && $fromform['defaultmark'] < 0) {
00cefd92
TH
674 $errors['defaultmark'] = get_string('defaultmarkmustbepositive', 'question');
675 }
676
2b7da645
TH
677 return $errors;
678 }
679
36703ed7 680 /**
681 * Override this in the subclass to question type name.
eaeb6b51
TH
682 * @return the question type name, should be the same as the name() method
683 * in the question type class.
36703ed7 684 */
2b7da645 685 public abstract function qtype();
2a22be64 686
dff72cde
SH
687 /**
688 * Returns an array of editor options with collapsed options turned off.
0c0dfa8f 689 * @deprecated since 2.6
dff72cde
SH
690 * @return array
691 */
692 protected function get_non_collabsible_editor_options() {
0c0dfa8f
DW
693 debugging('get_non_collabsible_editor_options() is deprecated, use $this->editoroptions instead.', DEBUG_DEVELOPER);
694 return $this->editoroptions;
2a22be64 695 }
0c0dfa8f 696
36703ed7 697}