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