41e773e3dfa819c0e95d93279d4e621a537ce067
[moodle.git] / question / type / edit_question_form.php
1 <?php
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/>.
17 /**
18  * A base class for question editing forms.
19  *
20  * @package    moodlecore
21  * @subpackage questiontypes
22  * @copyright  2006 The Open University
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
24  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
30 require_once($CFG->libdir.'/formslib.php');
33 abstract 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;
40         $mform->addElement('hidden', 'id');
41         $mform->setType('id', PARAM_INT);
43         $mform->addElement('hidden', 'inpopup');
44         $mform->setType('inpopup', PARAM_INT);
46         $mform->addElement('hidden', 'cmid');
47         $mform->setType('cmid', PARAM_INT);
49         $mform->addElement('hidden', 'courseid');
50         $mform->setType('courseid', PARAM_INT);
52         $mform->addElement('hidden', 'returnurl');
53         $mform->setType('returnurl', PARAM_LOCALURL);
55         $mform->addElement('hidden', 'scrollpos');
56         $mform->setType('scrollpos', PARAM_INT);
58         $mform->addElement('hidden', 'appendqnumstring');
59         $mform->setType('appendqnumstring', PARAM_ALPHA);
60     }
61 }
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.
68  *
69  * @copyright  2006 The Open University
70  * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
71  */
72 abstract class question_edit_form extends question_wizard_form {
73     const DEFAULT_NUM_HINTS = 2;
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
78      * form in definition_inner but data is always loaded into the form with set_data.
79      * @var object
80      */
81     protected $question;
83     protected $contexts;
84     protected $category;
85     protected $categorycontext;
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;
96     public function __construct($submiturl, $question, $category, $contexts, $formeditable = true) {
97         global $DB;
99         $this->question = $question;
100         $this->contexts = $contexts;
102         $record = $DB->get_record('question_categories',
103                 array('id' => $question->category), 'contextid');
104         $this->context = context::instance_by_id($record->contextid);
106         $this->editoroptions = array('subdirs' => 1, 'maxfiles' => EDITOR_UNLIMITED_FILES,
107                 'context' => $this->context);
108         $this->fileoptions = array('subdirs' => 1, 'maxfiles' => -1, 'maxbytes' => -1);
110         $this->category = $category;
111         $this->categorycontext = context::instance_by_id($category->contextid);
113         parent::__construct($submiturl, null, 'post', '', null, $formeditable);
114     }
116     /**
117      * Build the form definition.
118      *
119      * This adds all the form fields that the default question type supports.
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      */
123     protected function definition() {
124         global $COURSE, $CFG, $DB, $PAGE;
126         $qtype = $this->qtype();
127         $langfile = "qtype_$qtype";
129         $mform = $this->_form;
131         // Standard fields at the start of the form.
132         $mform->addElement('header', 'generalheader', get_string("general", 'form'));
134         if (!isset($this->question->id)) {
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             }
141             // Adding question.
142             $mform->addElement('questioncategory', 'category', get_string('category', 'question'),
143                     array('contexts' => $contexts));
144         } else if (!($this->question->formoptions->canmove ||
145                 $this->question->formoptions->cansaveasnew)) {
146             // Editing question with no permission to move from category.
147             $mform->addElement('questioncategory', 'category', get_string('category', 'question'),
148                     array('contexts' => array($this->categorycontext)));
149         } else {
150             // Editing question with permission to move from category or save as new q.
151             $currentgrp = array();
152             $currentgrp[0] = $mform->createElement('questioncategory', 'category',
153                     get_string('categorycurrent', 'question'),
154                     array('contexts' => array($this->categorycontext)));
155             if ($this->question->formoptions->canedit ||
156                     $this->question->formoptions->cansaveasnew) {
157                 // Not move only form.
158                 $currentgrp[1] = $mform->createElement('checkbox', 'usecurrentcat', '',
159                         get_string('categorycurrentuse', 'question'));
160                 $mform->setDefault('usecurrentcat', 1);
161             }
162             $currentgrp[0]->freeze();
163             $currentgrp[0]->setPersistantFreeze(false);
164             $mform->addGroup($currentgrp, 'currentgrp',
165                     get_string('categorycurrent', 'question'), null, false);
167             $mform->addElement('questioncategory', 'categorymoveto',
168                     get_string('categorymoveto', 'question'),
169                     array('contexts' => array($this->categorycontext)));
170             if ($this->question->formoptions->canedit ||
171                     $this->question->formoptions->cansaveasnew) {
172                 // Not move only form.
173                 $mform->disabledIf('categorymoveto', 'usecurrentcat', 'checked');
174             }
175         }
177         $mform->addElement('text', 'name', get_string('questionname', 'question'),
178                 array('size' => 50, 'maxlength' => 255));
179         $mform->setType('name', PARAM_TEXT);
180         $mform->addRule('name', null, 'required', null, 'client');
182         $mform->addElement('editor', 'questiontext', get_string('questiontext', 'question'),
183                 array('rows' => 15), $this->editoroptions);
184         $mform->setType('questiontext', PARAM_RAW);
185         $mform->addRule('questiontext', null, 'required', null, 'client');
187         $mform->addElement('text', 'defaultmark', get_string('defaultmark', 'question'),
188                 array('size' => 7));
189         $mform->setType('defaultmark', PARAM_FLOAT);
190         $mform->setDefault('defaultmark', 1);
191         $mform->addRule('defaultmark', null, 'required', null, 'client');
193         $mform->addElement('editor', 'generalfeedback', get_string('generalfeedback', 'question'),
194                 array('rows' => 10), $this->editoroptions);
195         $mform->setType('generalfeedback', PARAM_RAW);
196         $mform->addHelpButton('generalfeedback', 'generalfeedback', 'question');
198         // Any questiontype specific fields.
199         $this->definition_inner($mform);
201         if (!empty($CFG->usetags)) {
202             $mform->addElement('header', 'tagsheader', get_string('tags'));
203             $mform->addElement('tags', 'tags', get_string('tags'));
204         }
206         if (!empty($this->question->id)) {
207             $mform->addElement('header', 'createdmodifiedheader',
208                     get_string('createdmodifiedheader', 'question'));
209             $a = new stdClass();
210             if (!empty($this->question->createdby)) {
211                 $a->time = userdate($this->question->timecreated);
212                 $a->user = fullname($DB->get_record(
213                         'user', array('id' => $this->question->createdby)));
214             } else {
215                 $a->time = get_string('unknown', 'question');
216                 $a->user = get_string('unknown', 'question');
217             }
218             $mform->addElement('static', 'created', get_string('created', 'question'),
219                      get_string('byandon', 'question', $a));
220             if (!empty($this->question->modifiedby)) {
221                 $a = new stdClass();
222                 $a->time = userdate($this->question->timemodified);
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));
227             }
228         }
230         $this->add_hidden_fields();
232         $mform->addElement('hidden', 'qtype');
233         $mform->setType('qtype', PARAM_ALPHA);
235         $mform->addElement('hidden', 'makecopy');
236         $mform->setType('makecopy', PARAM_INT);
238         $buttonarray = array();
239         $buttonarray[] = $mform->createElement('submit', 'updatebutton',
240                              get_string('savechangesandcontinueediting', 'question'));
241         if ($this->can_preview()) {
242             $previewlink = $PAGE->get_renderer('core_question')->question_preview_link(
243                     $this->question->id, $this->context, true);
244             $buttonarray[] = $mform->createElement('static', 'previewlink', '', $previewlink);
245         }
247         $mform->addGroup($buttonarray, 'updatebuttonar', '', array(' '), false);
248         $mform->closeHeaderBefore('updatebuttonar');
250         $this->add_action_buttons(true, get_string('savechanges'));
252         if ((!empty($this->question->id)) && (!($this->question->formoptions->canedit ||
253                 $this->question->formoptions->cansaveasnew))) {
254             $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar', 'currentgrp'));
255         }
256     }
258     /**
259      * Add any question-type specific form fields.
260      *
261      * @param object $mform the form being built.
262      */
263     protected function definition_inner($mform) {
264         // By default, do nothing.
265     }
267     /**
268      * Is the question being edited in a state where it can be previewed?
269      * @return bool whether to show the preview link.
270      */
271     protected function can_preview() {
272         return empty($this->question->beingcopied) && !empty($this->question->id) &&
273                 $this->question->formoptions->canedit;
274     }
276     /**
277      * Get the list of form elements to repeat, one for each answer.
278      * @param object $mform the form being built.
279      * @param $label the label to use for each option.
280      * @param $gradeoptions the possible grades for each answer.
281      * @param $repeatedoptions reference to array of repeated options to fill
282      * @param $answersoption reference to return the name of $question->options
283      *      field holding an array of answers
284      * @return array of form fields.
285      */
286     protected function get_per_answer_fields($mform, $label, $gradeoptions,
287             &$repeatedoptions, &$answersoption) {
288         $repeated = array();
289         $answeroptions = array();
290         $answeroptions[] = $mform->createElement('text', 'answer',
291                 $label, array('size' => 40));
292         $answeroptions[] = $mform->createElement('select', 'fraction',
293                 get_string('grade'), $gradeoptions);
294         $repeated[] = $mform->createElement('group', 'answeroptions',
295                  $label, $answeroptions, null, false);
296         $repeated[] = $mform->createElement('editor', 'feedback',
297                 get_string('feedback', 'question'), array('rows' => 5), $this->editoroptions);
298         $repeatedoptions['answer']['type'] = PARAM_RAW;
299         $repeatedoptions['fraction']['default'] = 0;
300         $answersoption = 'answers';
301         return $repeated;
302     }
304     /**
305      * Add a set of form fields, obtained from get_per_answer_fields, to the form,
306      * one for each existing answer, with some blanks for some new ones.
307      * @param object $mform the form being built.
308      * @param $label the label to use for each option.
309      * @param $gradeoptions the possible grades for each answer.
310      * @param $minoptions the minimum number of answer blanks to display.
311      *      Default QUESTION_NUMANS_START.
312      * @param $addoptions the number of answer blanks to add. Default QUESTION_NUMANS_ADD.
313      */
314     protected function add_per_answer_fields(&$mform, $label, $gradeoptions,
315             $minoptions = QUESTION_NUMANS_START, $addoptions = QUESTION_NUMANS_ADD) {
316         $mform->addElement('header', 'answerhdr',
317                     get_string('answers', 'question'), '');
318         $mform->setExpanded('answerhdr', 1);
319         $answersoption = '';
320         $repeatedoptions = array();
321         $repeated = $this->get_per_answer_fields($mform, $label, $gradeoptions,
322                 $repeatedoptions, $answersoption);
324         if (isset($this->question->options)) {
325             $repeatsatstart = count($this->question->options->$answersoption);
326         } else {
327             $repeatsatstart = $minoptions;
328         }
330         $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions,
331                 'noanswers', 'addanswers', $addoptions,
332                 $this->get_more_choices_string(), true);
333     }
335     /**
336      * Language string to use for 'Add {no} more {whatever we call answers}'.
337      */
338     protected function get_more_choices_string() {
339         return get_string('addmorechoiceblanks', 'question');
340     }
342     protected function add_combined_feedback_fields($withshownumpartscorrect = false) {
343         $mform = $this->_form;
345         $mform->addElement('header', 'combinedfeedbackhdr',
346                 get_string('combinedfeedback', 'question'));
348         $fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
349         foreach ($fields as $feedbackname) {
350             $element = $mform->addElement('editor', $feedbackname,
351                                 get_string($feedbackname, 'question'),
352                                 array('rows' => 5), $this->editoroptions);
353             $mform->setType($feedbackname, PARAM_RAW);
354             // Using setValue() as setDefault() does not work for the editor class.
355             $element->setValue(array('text' => get_string($feedbackname.'default', 'question')));
357             if ($withshownumpartscorrect && $feedbackname == 'partiallycorrectfeedback') {
358                 $mform->addElement('advcheckbox', 'shownumcorrect',
359                         get_string('options', 'question'),
360                         get_string('shownumpartscorrectwhenfinished', 'question'));
361                 $mform->setDefault('shownumcorrect', true);
362             }
363         }
364     }
366     /**
367      * Create the form elements required by one hint.
368      * @param string $withclearwrong whether this quesiton type uses the 'Clear wrong' option on hints.
369      * @param string $withshownumpartscorrect whether this quesiton type uses the 'Show num parts correct' option on hints.
370      * @return array form field elements for one hint.
371      */
372     protected function get_hint_fields($withclearwrong = false, $withshownumpartscorrect = false) {
373         $mform = $this->_form;
375         $repeatedoptions = array();
376         $repeated = array();
377         $repeated[] = $mform->createElement('editor', 'hint', get_string('hintn', 'question'),
378                 array('rows' => 5), $this->editoroptions);
379         $repeatedoptions['hint']['type'] = PARAM_RAW;
381         $optionelements = array();
382         if ($withclearwrong) {
383             $optionelements[] = $mform->createElement('advcheckbox', 'hintclearwrong',
384                     get_string('options', 'question'), get_string('clearwrongparts', 'question'));
385         }
386         if ($withshownumpartscorrect) {
387             $optionelements[] = $mform->createElement('advcheckbox', 'hintshownumcorrect', '',
388                     get_string('shownumpartscorrect', 'question'));
389         }
391         if (count($optionelements)) {
392             $repeated[] = $mform->createElement('group', 'hintoptions',
393                  get_string('hintnoptions', 'question'), $optionelements, null, false);
394         }
396         return array($repeated, $repeatedoptions);
397     }
399     protected function add_interactive_settings($withclearwrong = false,
400             $withshownumpartscorrect = false) {
401         $mform = $this->_form;
403         $mform->addElement('header', 'multitriesheader',
404                 get_string('settingsformultipletries', 'question'));
406         $penalties = array(
407             1.0000000,
408             0.5000000,
409             0.3333333,
410             0.2500000,
411             0.2000000,
412             0.1000000,
413             0.0000000
414         );
415         if (!empty($this->question->penalty) && !in_array($this->question->penalty, $penalties)) {
416             $penalties[] = $this->question->penalty;
417             sort($penalties);
418         }
419         $penaltyoptions = array();
420         foreach ($penalties as $penalty) {
421             $penaltyoptions["$penalty"] = (100 * $penalty) . '%';
422         }
423         $mform->addElement('select', 'penalty',
424                 get_string('penaltyforeachincorrecttry', 'question'), $penaltyoptions);
425         $mform->addHelpButton('penalty', 'penaltyforeachincorrecttry', 'question');
426         $mform->setDefault('penalty', 0.3333333);
428         if (isset($this->question->hints)) {
429             $counthints = count($this->question->hints);
430         } else {
431             $counthints = 0;
432         }
434         if ($this->question->formoptions->repeatelements) {
435             $repeatsatstart = max(self::DEFAULT_NUM_HINTS, $counthints);
436         } else {
437             $repeatsatstart = $counthints;
438         }
440         list($repeated, $repeatedoptions) = $this->get_hint_fields(
441                 $withclearwrong, $withshownumpartscorrect);
442         $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions,
443                 'numhints', 'addhint', 1, get_string('addanotherhint', 'question'), true);
444     }
446     public function set_data($question) {
447         question_bank::get_qtype($question->qtype)->set_default_options($question);
449         // Prepare question text.
450         $draftid = file_get_submitted_draft_itemid('questiontext');
452         if (!empty($question->questiontext)) {
453             $questiontext = $question->questiontext;
454         } else {
455             $questiontext = $this->_form->getElement('questiontext')->getValue();
456             $questiontext = $questiontext['text'];
457         }
458         $questiontext = file_prepare_draft_area($draftid, $this->context->id,
459                 'question', 'questiontext', empty($question->id) ? null : (int) $question->id,
460                 $this->fileoptions, $questiontext);
462         $question->questiontext = array();
463         $question->questiontext['text'] = $questiontext;
464         $question->questiontext['format'] = empty($question->questiontextformat) ?
465                 editors_get_preferred_format() : $question->questiontextformat;
466         $question->questiontext['itemid'] = $draftid;
468         // Prepare general feedback.
469         $draftid = file_get_submitted_draft_itemid('generalfeedback');
471         if (empty($question->generalfeedback)) {
472             $generalfeedback = $this->_form->getElement('generalfeedback')->getValue();
473             $question->generalfeedback = $generalfeedback['text'];
474         }
476         $feedback = file_prepare_draft_area($draftid, $this->context->id,
477                 'question', 'generalfeedback', empty($question->id) ? null : (int) $question->id,
478                 $this->fileoptions, $question->generalfeedback);
479         $question->generalfeedback = array();
480         $question->generalfeedback['text'] = $feedback;
481         $question->generalfeedback['format'] = empty($question->generalfeedbackformat) ?
482                 editors_get_preferred_format() : $question->generalfeedbackformat;
483         $question->generalfeedback['itemid'] = $draftid;
485         // Remove unnecessary trailing 0s form grade fields.
486         if (isset($question->defaultgrade)) {
487             $question->defaultgrade = 0 + $question->defaultgrade;
488         }
489         if (isset($question->penalty)) {
490             $question->penalty = 0 + $question->penalty;
491         }
493         // Set any options.
494         $extraquestionfields = question_bank::get_qtype($question->qtype)->extra_question_fields();
495         if (is_array($extraquestionfields) && !empty($question->options)) {
496             array_shift($extraquestionfields);
497             foreach ($extraquestionfields as $field) {
498                 if (property_exists($question->options, $field)) {
499                     $question->$field = $question->options->$field;
500                 }
501             }
502         }
504         // Subclass adds data_preprocessing code here.
505         $question = $this->data_preprocessing($question);
507         parent::set_data($question);
508     }
510     /**
511      * Perform an preprocessing needed on the data passed to {@link set_data()}
512      * before it is used to initialise the form.
513      * @param object $question the data being passed to the form.
514      * @return object $question the modified data.
515      */
516     protected function data_preprocessing($question) {
517         return $question;
518     }
520     /**
521      * Perform the necessary preprocessing for the fields added by
522      * {@link add_per_answer_fields()}.
523      * @param object $question the data being passed to the form.
524      * @return object $question the modified data.
525      */
526     protected function data_preprocessing_answers($question, $withanswerfiles = false) {
527         if (empty($question->options->answers)) {
528             return $question;
529         }
531         $key = 0;
532         foreach ($question->options->answers as $answer) {
533             if ($withanswerfiles) {
534                 // Prepare the feedback editor to display files in draft area.
535                 $draftitemid = file_get_submitted_draft_itemid('answer['.$key.']');
536                 $question->answer[$key]['text'] = file_prepare_draft_area(
537                     $draftitemid,          // Draftid
538                     $this->context->id,    // context
539                     'question',            // component
540                     'answer',              // filarea
541                     !empty($answer->id) ? (int) $answer->id : null, // itemid
542                     $this->fileoptions,    // options
543                     $answer->answer        // text.
544                 );
545                 $question->answer[$key]['itemid'] = $draftitemid;
546                 $question->answer[$key]['format'] = $answer->answerformat;
547             } else {
548                 $question->answer[$key] = $answer->answer;
549             }
551             $question->fraction[$key] = 0 + $answer->fraction;
552             $question->feedback[$key] = array();
554             // Evil hack alert. Formslib can store defaults in two ways for
555             // repeat elements:
556             //   ->_defaultValues['fraction[0]'] and
557             //   ->_defaultValues['fraction'][0].
558             // The $repeatedoptions['fraction']['default'] = 0 bit above means
559             // that ->_defaultValues['fraction[0]'] has already been set, but we
560             // are using object notation here, so we will be setting
561             // ->_defaultValues['fraction'][0]. That does not work, so we have
562             // to unset ->_defaultValues['fraction[0]'].
563             unset($this->_form->_defaultValues["fraction[$key]"]);
565             // Prepare the feedback editor to display files in draft area.
566             $draftitemid = file_get_submitted_draft_itemid('feedback['.$key.']');
567             $question->feedback[$key]['text'] = file_prepare_draft_area(
568                 $draftitemid,          // Draftid
569                 $this->context->id,    // context
570                 'question',            // component
571                 'answerfeedback',      // filarea
572                 !empty($answer->id) ? (int) $answer->id : null, // itemid
573                 $this->fileoptions,    // options
574                 $answer->feedback      // text.
575             );
576             $question->feedback[$key]['itemid'] = $draftitemid;
577             $question->feedback[$key]['format'] = $answer->feedbackformat;
578             $key++;
579         }
580         return $question;
581     }
583     /**
584      * Perform the necessary preprocessing for the fields added by
585      * {@link add_combined_feedback_fields()}.
586      * @param object $question the data being passed to the form.
587      * @return object $question the modified data.
588      */
589     protected function data_preprocessing_combined_feedback($question,
590             $withshownumcorrect = false) {
591         if (empty($question->options)) {
592             return $question;
593         }
595         $fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
596         foreach ($fields as $feedbackname) {
597             $draftid = file_get_submitted_draft_itemid($feedbackname);
598             $feedback = array();
599             $feedback['text'] = file_prepare_draft_area(
600                 $draftid,              // Draftid
601                 $this->context->id,    // context
602                 'question',            // component
603                 $feedbackname,         // filarea
604                 !empty($question->id) ? (int) $question->id : null, // itemid
605                 $this->fileoptions,    // options
606                 $question->options->$feedbackname // text.
607             );
608             $feedbackformat = $feedbackname . 'format';
609             $feedback['format'] = $question->options->$feedbackformat;
610             $feedback['itemid'] = $draftid;
612             $question->$feedbackname = $feedback;
613         }
615         if ($withshownumcorrect) {
616             $question->shownumcorrect = $question->options->shownumcorrect;
617         }
619         return $question;
620     }
622     /**
623      * Perform the necessary preprocessing for the hint fields.
624      * @param object $question the data being passed to the form.
625      * @return object $question the modified data.
626      */
627     protected function data_preprocessing_hints($question, $withclearwrong = false,
628             $withshownumpartscorrect = false) {
629         if (empty($question->hints)) {
630             return $question;
631         }
633         $key = 0;
634         foreach ($question->hints as $hint) {
635             $question->hint[$key] = array();
637             // Prepare feedback editor to display files in draft area.
638             $draftitemid = file_get_submitted_draft_itemid('hint['.$key.']');
639             $question->hint[$key]['text'] = file_prepare_draft_area(
640                 $draftitemid,          // Draftid
641                 $this->context->id,    // context
642                 'question',            // component
643                 'hint',                // filarea
644                 !empty($hint->id) ? (int) $hint->id : null, // itemid
645                 $this->fileoptions,    // options
646                 $hint->hint            // text.
647             );
648             $question->hint[$key]['itemid'] = $draftitemid;
649             $question->hint[$key]['format'] = $hint->hintformat;
650             $key++;
652             if ($withclearwrong) {
653                 $question->hintclearwrong[] = $hint->clearwrong;
654             }
655             if ($withshownumpartscorrect) {
656                 $question->hintshownumcorrect[] = $hint->shownumcorrect;
657             }
658         }
660         return $question;
661     }
663     public function validation($fromform, $files) {
664         $errors = parent::validation($fromform, $files);
665         if (empty($fromform['makecopy']) && isset($this->question->id)
666                 && ($this->question->formoptions->canedit ||
667                         $this->question->formoptions->cansaveasnew)
668                 && empty($fromform['usecurrentcat']) && !$this->question->formoptions->canmove) {
669             $errors['currentgrp'] = get_string('nopermissionmove', 'question');
670         }
672         // Default mark.
673         if (array_key_exists('defaultmark', $fromform) && $fromform['defaultmark'] < 0) {
674             $errors['defaultmark'] = get_string('defaultmarkmustbepositive', 'question');
675         }
677         return $errors;
678     }
680     /**
681      * Override this in the subclass to question type name.
682      * @return the question type name, should be the same as the name() method
683      *      in the question type class.
684      */
685     public abstract function qtype();
687     /**
688      * Returns an array of editor options with collapsed options turned off.
689      * @deprecated since 2.6
690      * @return array
691      */
692     protected function get_non_collabsible_editor_options() {
693         debugging('get_non_collabsible_editor_options() is deprecated, use $this->editoroptions instead.', DEBUG_DEVELOPER);
694         return $this->editoroptions;
695     }