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