MDL-20636 Fix codechecker issues in qtype_truefalse.
[moodle.git] / question / type / edit_question_form.php
CommitLineData
aeb15530 1<?php
fe6ce234
DC
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
36703ed7 18/**
19 * A base class for question editing forms.
20 *
7764183a 21 * @package moodlecore
2b7da645 22 * @subpackage questiontypes
b04a4319
TH
23 * @copyright 2006 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
fe6ce234 25 */
36703ed7 26
2b7da645 27
a17b297d
TH
28defined('MOODLE_INTERNAL') || die();
29
30
36703ed7 31/**
32 * Form definition base class. This defines the common fields that
33 * all question types need. Question types should define their own
34 * class that inherits from this one, and implements the definition_inner()
35 * method.
271e6dec 36 *
7764183a
TH
37 * @copyright 2006 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
36703ed7 39 */
2b7da645
TH
40abstract class question_edit_form extends moodleform {
41 const DEFAULT_NUM_HINTS = 2;
42
271ffe3f 43 /**
44 * Question object with options and answers already loaded by get_question_options
45 * Be careful how you use this it is needed sometimes to set up the structure of the
32db0d42 46 * form in definition_inner but data is always loaded into the form with set_data.
271ffe3f 47 * @var object
48 */
2b7da645
TH
49 protected $question;
50
51 protected $contexts;
52 protected $category;
53 protected $categorycontext;
271e6dec 54
fe6ce234
DC
55 /** @var object current context */
56 public $context;
57 /** @var array html editor options */
58 public $editoroptions;
59 /** @var array options to preapre draft area */
60 public $fileoptions;
61 /** @var object instance of question type */
62 public $instance;
63
2b7da645 64 public function __construct($submiturl, $question, $category, $contexts, $formeditable = true) {
fe6ce234 65 global $DB;
271e6dec 66
271ffe3f 67 $this->question = $question;
271e6dec 68 $this->contexts = $contexts;
69
5d548d3e 70 $record = $DB->get_record('question_categories', array('id' => $question->category), 'contextid');
fe6ce234
DC
71 $this->context = get_context_instance_by_id($record->contextid);
72
41dcc2a5
DC
73 $this->editoroptions = array('subdirs' => 1,'maxfiles' => EDITOR_UNLIMITED_FILES, 'context' => $this->context);
74 $this->fileoptions = array('subdirs' => 1, 'maxfiles' => -1, 'maxbytes' => -1);
fe6ce234 75
271e6dec 76 $this->category = $category;
77 $this->categorycontext = get_context_instance_by_id($category->contextid);
271e6dec 78
2b7da645 79 parent::__construct($submiturl, null, 'post', '', null, $formeditable);
271ffe3f 80 }
8e652f02 81
36703ed7 82 /**
83 * Build the form definition.
1d284fbd 84 *
295043c2 85 * This adds all the form fields that the default question type supports.
36703ed7 86 * If your question type does not support all these fields, then you can
87 * override this method and remove the ones you don't want with $mform->removeElement().
88 */
c7df5006 89 protected function definition() {
f34488b2 90 global $COURSE, $CFG, $DB;
1d284fbd 91
36703ed7 92 $qtype = $this->qtype();
93 $langfile = "qtype_$qtype";
1d284fbd 94
f9b0500f 95 $mform = $this->_form;
36703ed7 96
97 // Standard fields at the start of the form.
271ffe3f 98 $mform->addElement('header', 'generalheader', get_string("general", 'form'));
1d284fbd 99
2b7da645 100 if (!isset($this->question->id)) {
5d548d3e 101 // Adding question
2b7da645 102 $mform->addElement('questioncategory', 'category', get_string('category', 'question'),
522b89d1 103 array('contexts' => $this->contexts->having_cap('moodle/question:add')));
2b7da645 104 } elseif (!($this->question->formoptions->canmove || $this->question->formoptions->cansaveasnew)) {
5d548d3e 105 // Editing question with no permission to move from category.
2b7da645 106 $mform->addElement('questioncategory', 'category', get_string('category', 'question'),
271e6dec 107 array('contexts' => array($this->categorycontext)));
2b7da645 108 } elseif ($this->question->formoptions->movecontext) {
5d548d3e 109 // Moving question to another context.
2b7da645 110 $mform->addElement('questioncategory', 'categorymoveto', get_string('category', 'question'),
271e6dec 111 array('contexts' => $this->contexts->having_cap('moodle/question:add')));
112
113 } else {
5d548d3e 114 // Editing question with permission to move from category or save as new q
271e6dec 115 $currentgrp = array();
116 $currentgrp[0] =& $mform->createElement('questioncategory', 'category', get_string('categorycurrent', 'question'),
117 array('contexts' => array($this->categorycontext)));
2b7da645 118 if ($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew) {
271e6dec 119 //not move only form
120 $currentgrp[1] =& $mform->createElement('checkbox', 'usecurrentcat', '', get_string('categorycurrentuse', 'question'));
121 $mform->setDefault('usecurrentcat', 1);
122 }
123 $currentgrp[0]->freeze();
124 $currentgrp[0]->setPersistantFreeze(false);
125 $mform->addGroup($currentgrp, 'currentgrp', get_string('categorycurrent', 'question'), null, false);
126
127 $mform->addElement('questioncategory', 'categorymoveto', get_string('categorymoveto', 'question'),
128 array('contexts' => array($this->categorycontext)));
2b7da645 129 if ($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew) {
271e6dec 130 //not move only form
131 $mform->disabledIf('categorymoveto', 'usecurrentcat', 'checked');
132 }
133 }
375ed78a 134
2b7da645 135 $mform->addElement('text', 'name', get_string('questionname', 'question'), array('size' => 50));
271ffe3f 136 $mform->setType('name', PARAM_TEXT);
36703ed7 137 $mform->addRule('name', null, 'required', null, 'client');
1d284fbd 138
2b7da645 139 $mform->addElement('editor', 'questiontext', get_string('questiontext', 'question'),
5d548d3e 140 array('rows' => 15), $this->editoroptions);
36703ed7 141 $mform->setType('questiontext', PARAM_RAW);
1d284fbd 142
2b7da645 143 $mform->addElement('text', 'defaultmark', get_string('defaultmark', 'question'),
36703ed7 144 array('size' => 3));
2b7da645
TH
145 $mform->setType('defaultmark', PARAM_INT);
146 $mform->setDefault('defaultmark', 1);
147 $mform->addRule('defaultmark', null, 'required', null, 'client');
36703ed7 148
2b7da645 149 $mform->addElement('editor', 'generalfeedback', get_string('generalfeedback', 'question'),
5d548d3e 150 array('rows' => 10), $this->editoroptions);
36703ed7 151 $mform->setType('generalfeedback', PARAM_RAW);
2b7da645 152 $mform->addHelpButton('generalfeedback', 'generalfeedback', 'question');
1d284fbd 153
36703ed7 154 // Any questiontype specific fields.
155 $this->definition_inner($mform);
156
c599a682 157 if (!empty($CFG->usetags)) {
158 $mform->addElement('header', 'tagsheader', get_string('tags'));
159 $mform->addElement('tags', 'tags', get_string('tags'));
160 }
161
2b7da645 162 if (!empty($this->question->id)) {
271e6dec 163 $mform->addElement('header', 'createdmodifiedheader', get_string('createdmodifiedheader', 'question'));
7f389342 164 $a = new stdClass();
2b7da645 165 if (!empty($this->question->createdby)) {
271e6dec 166 $a->time = userdate($this->question->timecreated);
f34488b2 167 $a->user = fullname($DB->get_record('user', array('id' => $this->question->createdby)));
271e6dec 168 } else {
169 $a->time = get_string('unknown', 'question');
170 $a->user = get_string('unknown', 'question');
171 }
172 $mform->addElement('static', 'created', get_string('created', 'question'), get_string('byandon', 'question', $a));
2b7da645 173 if (!empty($this->question->modifiedby)) {
7f389342 174 $a = new stdClass();
271e6dec 175 $a->time = userdate($this->question->timemodified);
f34488b2 176 $a->user = fullname($DB->get_record('user', array('id' => $this->question->modifiedby)));
271e6dec 177 $mform->addElement('static', 'modified', get_string('modified', 'question'), get_string('byandon', 'question', $a));
178 }
179 }
180
36703ed7 181 // Standard fields at the end of the form.
182 $mform->addElement('hidden', 'id');
183 $mform->setType('id', PARAM_INT);
184
185 $mform->addElement('hidden', 'qtype');
186 $mform->setType('qtype', PARAM_ALPHA);
187
188 $mform->addElement('hidden', 'inpopup');
189 $mform->setType('inpopup', PARAM_INT);
190
191 $mform->addElement('hidden', 'versioning');
192 $mform->setType('versioning', PARAM_BOOL);
193
271e6dec 194 $mform->addElement('hidden', 'movecontext');
195 $mform->setType('movecontext', PARAM_BOOL);
196
9ab75b2b 197 $mform->addElement('hidden', 'cmid');
198 $mform->setType('cmid', PARAM_INT);
199 $mform->setDefault('cmid', 0);
200
271e6dec 201 $mform->addElement('hidden', 'courseid');
202 $mform->setType('courseid', PARAM_INT);
203 $mform->setDefault('courseid', 0);
204
7cd4fda6 205 $mform->addElement('hidden', 'returnurl');
206 $mform->setType('returnurl', PARAM_LOCALURL);
271e6dec 207 $mform->setDefault('returnurl', 0);
7cd4fda6 208
a13d4fbd
TH
209 $mform->addElement('hidden', 'scrollpos');
210 $mform->setType('scrollpos', PARAM_INT);
211 $mform->setDefault('scrollpos', 0);
212
fa583f5f 213 $mform->addElement('hidden', 'appendqnumstring');
214 $mform->setType('appendqnumstring', PARAM_ALPHA);
215 $mform->setDefault('appendqnumstring', 0);
216
375ed78a 217 $buttonarray = array();
2b7da645 218 if (!empty($this->question->id)) {
271e6dec 219 //editing / moving question
2b7da645 220 if ($this->question->formoptions->movecontext) {
271e6dec 221 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('moveq', 'question'));
2b7da645 222 } elseif ($this->question->formoptions->canedit || $this->question->formoptions->canmove ||$this->question->formoptions->movecontext) {
271e6dec 223 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges'));
224 }
2b7da645
TH
225 if ($this->question->formoptions->cansaveasnew) {
226 $buttonarray[] = &$mform->createElement('submit', 'makecopy', get_string('makecopy', 'question'));
271e6dec 227 }
228 $buttonarray[] = &$mform->createElement('cancel');
229 } else {
230 // adding new question
231 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges'));
232 $buttonarray[] = &$mform->createElement('cancel');
375ed78a 233 }
375ed78a 234 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
235 $mform->closeHeaderBefore('buttonar');
271e6dec 236
5d548d3e 237 if ($this->question->formoptions->movecontext) {
271e6dec 238 $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar'));
2b7da645 239 } else if ((!empty($this->question->id)) && (!($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew))) {
271e6dec 240 $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar', 'currentgrp'));
241 }
36703ed7 242 }
fa583f5f 243
36703ed7 244 /**
245 * Add any question-type specific form fields.
1d284fbd 246 *
247 * @param object $mform the form being built.
36703ed7 248 */
2b7da645 249 protected function definition_inner($mform) {
36703ed7 250 // By default, do nothing.
251 }
1d284fbd 252
2aef1fe5 253 /**
254 * Get the list of form elements to repeat, one for each answer.
255 * @param object $mform the form being built.
256 * @param $label the label to use for each option.
257 * @param $gradeoptions the possible grades for each answer.
258 * @param $repeatedoptions reference to array of repeated options to fill
259 * @param $answersoption reference to return the name of $question->options field holding an array of answers
260 * @return array of form fields.
261 */
2b7da645 262 protected function get_per_answer_fields(&$mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
2aef1fe5 263 $repeated = array();
1c2ed7c5 264 $repeated[] = $mform->createElement('header', 'answerhdr', $label);
5e8a85aa 265 $repeated[] = $mform->createElement('text', 'answer', get_string('answer', 'question'), array('size' => 80));
1c2ed7c5 266 $repeated[] = $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
5e8a85aa 267 $repeated[] = $mform->createElement('editor', 'feedback', get_string('feedback', 'question'),
5d548d3e 268 array('rows' => 5), $this->editoroptions);
2aef1fe5 269 $repeatedoptions['answer']['type'] = PARAM_RAW;
270 $repeatedoptions['fraction']['default'] = 0;
271 $answersoption = 'answers';
272 return $repeated;
273 }
274
275 /**
276 * Add a set of form fields, obtained from get_per_answer_fields, to the form,
277 * one for each existing answer, with some blanks for some new ones.
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 $minoptions the minimum number of answer blanks to display. Default QUESTION_NUMANS_START.
282 * @param $addoptions the number of answer blanks to add. Default QUESTION_NUMANS_ADD.
283 */
2b7da645 284 protected function add_per_answer_fields(&$mform, $label, $gradeoptions, $minoptions = QUESTION_NUMANS_START, $addoptions = QUESTION_NUMANS_ADD) {
2aef1fe5 285 $answersoption = '';
286 $repeatedoptions = array();
287 $repeated = $this->get_per_answer_fields($mform, $label, $gradeoptions, $repeatedoptions, $answersoption);
288
2b7da645 289 if (isset($this->question->options)) {
2aef1fe5 290 $countanswers = count($this->question->options->$answersoption);
291 } else {
292 $countanswers = 0;
293 }
2b7da645 294 if ($this->question->formoptions->repeatelements) {
2aef1fe5 295 $repeatsatstart = max($minoptions, $countanswers + $addoptions);
296 } else {
297 $repeatsatstart = $countanswers;
298 }
299
300 $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', $addoptions, get_string('addmorechoiceblanks', 'qtype_multichoice'));
301 }
302
2b7da645
TH
303 protected function add_combined_feedback_fields($withshownumpartscorrect = false) {
304 $mform = $this->_form;
305
306 $mform->addElement('header', 'combinedfeedbackhdr', get_string('combinedfeedback', 'question'));
307
308 foreach (array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback') as $feedbackname) {
309 $mform->addElement('editor', $feedbackname, get_string($feedbackname, 'question'),
310 array('rows' => 5), $this->editoroptions);
311 $mform->setType($feedbackname, PARAM_RAW);
312
313 if ($withshownumpartscorrect && $feedbackname == 'partiallycorrectfeedback') {
314 $mform->addElement('checkbox', 'shownumcorrect', get_string('options', 'question'), get_string('shownumpartscorrect', 'question'));
315 }
316 }
317 }
318
319 protected function get_hint_fields($withclearwrong = false, $withshownumpartscorrect = false) {
320 $mform = $this->_form;
321
322 $repeated = array();
323 $repeated[] = $mform->createElement('header', 'answerhdr', get_string('hintn', 'question'));
7a719748
TH
324 $repeated[] = $mform->createElement('editor', 'hint', get_string('hinttext', 'question'),
325 array('rows' => 5), $this->editoroptions);
2b7da645
TH
326 $repeatedoptions['hint']['type'] = PARAM_RAW;
327
328 if ($withclearwrong) {
329 $repeated[] = $mform->createElement('checkbox', 'hintclearwrong', get_string('options', 'question'), get_string('clearwrongparts', 'question'));
330 }
331 if ($withshownumpartscorrect) {
332 $repeated[] = $mform->createElement('checkbox', 'hintshownumcorrect', '', get_string('shownumpartscorrect', 'question'));
333 }
334
335 return array($repeated, $repeatedoptions);
336 }
337
338 protected function add_interactive_settings($withclearwrong = false, $withshownumpartscorrect = false) {
339 $mform = $this->_form;
340
341 $mform->addElement('header', 'multitriesheader', get_string('settingsformultipletries', 'question'));
342
343 $penalties = array(
344 1.0000000,
345 0.5000000,
346 0.3333333,
347 0.2500000,
348 0.2000000,
349 0.1000000,
350 0.0000000
351 );
352 if (!empty($this->question->penalty) && !in_array($this->question->penalty, $penalties)) {
353 $penalties[] = $this->question->penalty;
354 sort($penalties);
355 }
356 $penaltyoptions = array();
357 foreach ($penalties as $penalty) {
358 $penaltyoptions["$penalty"] = (100 * $penalty) . '%';
359 }
360 $mform->addElement('select', 'penalty', get_string('penaltyforeachincorrecttry', 'question'),
361 $penaltyoptions);
362 $mform->addRule('penalty', null, 'required', null, 'client');
068b4594 363 $mform->addHelpButton('penalty', 'penaltyforeachincorrecttry', 'question');
2b7da645
TH
364 $mform->setDefault('penalty', 0.3333333);
365
366 if (isset($this->question->hints)) {
367 $counthints = count($this->question->hints);
368 } else {
369 $counthints = 0;
370 }
371
372 if ($this->question->formoptions->repeatelements) {
373 $repeatsatstart = max(self::DEFAULT_NUM_HINTS, $counthints);
374 } else {
375 $repeatsatstart = $counthints;
376 }
377
378 list($repeated, $repeatedoptions) = $this->get_hint_fields(
379 $withclearwrong, $withshownumpartscorrect);
380 $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions,
381 'numhints', 'addhint', 1, get_string('addanotherhint', 'question'));
382 }
383
384 public function set_data($question) {
f9b0500f 385 question_bank::get_qtype($question->qtype)->set_default_options($question);
2b7da645 386
fe6ce234
DC
387 // prepare question text
388 $draftid = file_get_submitted_draft_itemid('questiontext');
389
390 if (!empty($question->questiontext)) {
391 $questiontext = $question->questiontext;
392 } else {
393 $questiontext = '';
271ffe3f 394 }
41dcc2a5 395 $questiontext = file_prepare_draft_area($draftid, $this->context->id, 'question', 'questiontext', empty($question->id)?null:(int)$question->id, $this->fileoptions, $questiontext);
fe6ce234
DC
396
397 $question->questiontext = array();
398 $question->questiontext['text'] = $questiontext;
399 $question->questiontext['format'] = empty($question->questiontextformat) ? editors_get_preferred_format() : $question->questiontextformat;
400 $question->questiontext['itemid'] = $draftid;
401
402 // prepare general feedback
403 $draftid = file_get_submitted_draft_itemid('generalfeedback');
404
405 if (empty($question->generalfeedback)) {
406 $question->generalfeedback = '';
407 }
408
41dcc2a5 409 $feedback = file_prepare_draft_area($draftid, $this->context->id, 'question', 'generalfeedback', empty($question->id)?null:(int)$question->id, $this->fileoptions, $question->generalfeedback);
fe6ce234
DC
410 $question->generalfeedback = array();
411 $question->generalfeedback['text'] = $feedback;
412 $question->generalfeedback['format'] = empty($question->generalfeedbackformat) ? editors_get_preferred_format() : $question->generalfeedbackformat;
413 $question->generalfeedback['itemid'] = $draftid;
295043c2 414
9152f6a5 415 // Remove unnecessary trailing 0s form grade fields.
cfd24d98 416 if (isset($question->defaultgrade)) {
417 $question->defaultgrade = 0 + $question->defaultgrade;
418 }
419 if (isset($question->penalty)) {
420 $question->penalty = 0 + $question->penalty;
421 }
9152f6a5 422
295043c2 423 // Set any options.
f9b0500f 424 $extra_question_fields = question_bank::get_qtype($question->qtype)->extra_question_fields();
8e652f02 425 if (is_array($extra_question_fields) && !empty($question->options)) {
295043c2 426 array_shift($extra_question_fields);
427 foreach ($extra_question_fields as $field) {
b555ce76 428 if (isset($question->options->$field)) {
8e652f02 429 $question->$field = $question->options->$field;
430 }
295043c2 431 }
432 }
c9c989a0 433
fe6ce234
DC
434 // subclass adds data_preprocessing code here
435 $question = $this->data_preprocessing($question);
2b7da645 436
32db0d42 437 parent::set_data($question);
36703ed7 438 }
1d284fbd 439
fe6ce234
DC
440 /**
441 * Any preprocessing needed for the settings form for the question type
442 *
443 * @param array $question - array to fill in with the default values
444 */
c7df5006 445 protected function data_preprocessing($question) {
7a719748
TH
446 return $question;
447 }
448
449 protected function data_preprocessing_answers($question) {
450 if (empty($question->options->answers)) {
451 return $question;
452 }
453
454 $key = 0;
455 foreach ($question->options->answers as $answer){
456 $question->answer[$key] = $answer->answer;
457 $question->fraction[$key] = 0 + $answer->fraction;
458 $question->feedback[$key] = array();
459
460 // Evil hack alert. Formslib can store defaults in two ways for
461 // repeat elements: ->_defaultValues['fraction[0]'] and
462 // ->_defaultValues['fraction'][0]. The $repeatedoptions['fraction']['default'] = 0;
463 // bit above means that ->_defaultValues['fraction[0]'] has already
464 // been set, but we are using object notation here, so we will be setting
465 // ->_defaultValues['fraction'][0]. That does not work, so we have to unset
466 // ->_defaultValues['fraction[0]']
467 unset($this->_form->_defaultValues["fraction[$key]"]);
468
469 // Prepare the feedback editor to display files in draft area
470 $draftitemid = file_get_submitted_draft_itemid('feedback['.$key.']');
471 $question->feedback[$key]['text'] = file_prepare_draft_area(
472 $draftitemid, // draftid
473 $this->context->id, // context
474 'question', // component
475 'answerfeedback', // filarea
476 !empty($answer->id) ? (int) $answer->id : null, // itemid
477 $this->fileoptions, // options
478 $answer->feedback // text
479 );
480 $question->feedback[$key]['itemid'] = $draftitemid;
481 $question->feedback[$key]['format'] = $answer->feedbackformat;
482 $key++;
483 }
484 return $question;
485 }
486
c9c989a0
TH
487 protected function data_preprocessing_combined_feedback($question, $withshownumcorrect = false) {
488 if (empty($question->options)) {
489 return $question;
490 }
491
492 foreach (array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback') as $feedbackname) {
493 $draftid = file_get_submitted_draft_itemid($feedbackname);
8b9dfc2b
TH
494 $feedback = array();
495 $feedback['text'] = file_prepare_draft_area(
c9c989a0
TH
496 $draftid, // draftid
497 $this->context->id, // context
93cadb1e 498 'question', // component
c9c989a0
TH
499 $feedbackname, // filarea
500 !empty($question->id) ? (int) $question->id : null, // itemid
501 $this->fileoptions, // options
502 $question->options->$feedbackname // text
503 );
504 $feedbackformat = $feedbackname . 'format';
8b9dfc2b
TH
505 $feedback['format'] = $question->options->$feedbackformat;
506 $feedback['itemid'] = $draftid;
507
508 $question->$feedbackname = $feedback;
c9c989a0
TH
509 }
510
511 if ($withshownumcorrect) {
512 $question->shownumcorrect = $question->options->shownumcorrect;
513 }
514
515 return $question;
516 }
517
7a719748
TH
518 protected function data_preprocessing_hints($question, $withclearwrong = false, $withshownumpartscorrect = false) {
519 if (empty($question->hints)) {
520 return $question;
521 }
522
523 $key = 0;
524 foreach ($question->hints as $hint){
525 $question->hint[$key] = array();
526
527 // prepare feedback editor to display files in draft area
528 $draftitemid = file_get_submitted_draft_itemid('hint['.$key.']');
529 $question->hint[$key]['text'] = file_prepare_draft_area(
530 $draftitemid, // draftid
531 $this->context->id, // context
532 'question', // component
533 'hint', // filarea
534 !empty($hint->id) ? (int) $hint->id : null, // itemid
535 $this->fileoptions, // options
536 $hint->hint // text
537 );
538 $question->hint[$key]['itemid'] = $draftitemid;
539 $question->hint[$key]['format'] = $hint->hintformat;
540 $key++;
7a719748 541
8b9dfc2b
TH
542 if ($withclearwrong) {
543 $question->hintclearwrong[] = $hint->clearwrong;
544 }
545 if ($withshownumpartscorrect) {
546 $question->hintshownumcorrect[] = $hint->shownumcorrect;
547 }
548 }
7a719748 549
fe6ce234
DC
550 return $question;
551 }
552
2b7da645
TH
553 public function validation($fromform, $files) {
554 $errors = parent::validation($fromform, $files);
555 if (empty($fromform->makecopy) && isset($this->question->id)
556 && ($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew)
557 && empty($fromform->usecurrentcat) && !$this->question->formoptions->canmove) {
558 $errors['currentgrp'] = get_string('nopermissionmove', 'question');
559 }
560 return $errors;
561 }
562
36703ed7 563 /**
564 * Override this in the subclass to question type name.
565 * @return the question type name, should be the same as the name() method in the question type class.
566 */
2b7da645 567 public abstract function qtype();
36703ed7 568}