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