MDL-37465 Combined feedback: Num correct responses ticked by default
[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;
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 if ($this->question->formoptions->movecontext) {
150             // Moving question to another context.
151             $mform->addElement('questioncategory', 'categorymoveto',
152                     get_string('category', 'question'),
153                     array('contexts' => $this->contexts->having_cap('moodle/question:add')));
155         } else {
156             // Editing question with permission to move from category or save as new q
157             $currentgrp = array();
158             $currentgrp[0] = $mform->createElement('questioncategory', 'category',
159                     get_string('categorycurrent', 'question'),
160                     array('contexts' => array($this->categorycontext)));
161             if ($this->question->formoptions->canedit ||
162                     $this->question->formoptions->cansaveasnew) {
163                 //not move only form
164                 $currentgrp[1] = $mform->createElement('checkbox', 'usecurrentcat', '',
165                         get_string('categorycurrentuse', 'question'));
166                 $mform->setDefault('usecurrentcat', 1);
167             }
168             $currentgrp[0]->freeze();
169             $currentgrp[0]->setPersistantFreeze(false);
170             $mform->addGroup($currentgrp, 'currentgrp',
171                     get_string('categorycurrent', 'question'), null, false);
173             $mform->addElement('questioncategory', 'categorymoveto',
174                     get_string('categorymoveto', 'question'),
175                     array('contexts' => array($this->categorycontext)));
176             if ($this->question->formoptions->canedit ||
177                     $this->question->formoptions->cansaveasnew) {
178                 //not move only form
179                 $mform->disabledIf('categorymoveto', 'usecurrentcat', 'checked');
180             }
181         }
183         $mform->addElement('text', 'name', get_string('questionname', 'question'),
184                 array('size' => 50));
185         $mform->setType('name', PARAM_TEXT);
186         $mform->addRule('name', null, 'required', null, 'client');
188         $mform->addElement('editor', 'questiontext', get_string('questiontext', 'question'),
189                 array('rows' => 15), $this->editoroptions);
190         $mform->setType('questiontext', PARAM_RAW);
192         $mform->addElement('text', 'defaultmark', get_string('defaultmark', 'question'),
193                 array('size' => 7));
194         $mform->setType('defaultmark', PARAM_FLOAT);
195         $mform->setDefault('defaultmark', 1);
196         $mform->addRule('defaultmark', null, 'required', null, 'client');
198         $mform->addElement('editor', 'generalfeedback', get_string('generalfeedback', 'question'),
199                 array('rows' => 10), $this->editoroptions);
200         $mform->setType('generalfeedback', PARAM_RAW);
201         $mform->addHelpButton('generalfeedback', 'generalfeedback', 'question');
203         // Any questiontype specific fields.
204         $this->definition_inner($mform);
206         if (!empty($CFG->usetags)) {
207             $mform->addElement('header', 'tagsheader', get_string('tags'));
208             $mform->addElement('tags', 'tags', get_string('tags'));
209         }
211         if (!empty($this->question->id)) {
212             $mform->addElement('header', 'createdmodifiedheader',
213                     get_string('createdmodifiedheader', 'question'));
214             $a = new stdClass();
215             if (!empty($this->question->createdby)) {
216                 $a->time = userdate($this->question->timecreated);
217                 $a->user = fullname($DB->get_record(
218                         'user', array('id' => $this->question->createdby)));
219             } else {
220                 $a->time = get_string('unknown', 'question');
221                 $a->user = get_string('unknown', 'question');
222             }
223             $mform->addElement('static', 'created', get_string('created', 'question'),
224                      get_string('byandon', 'question', $a));
225             if (!empty($this->question->modifiedby)) {
226                 $a = new stdClass();
227                 $a->time = userdate($this->question->timemodified);
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));
232             }
233         }
235         $this->add_hidden_fields();
237         $mform->addElement('hidden', 'movecontext');
238         $mform->setType('movecontext', PARAM_BOOL);
240         $mform->addElement('hidden', 'qtype');
241         $mform->setType('qtype', PARAM_ALPHA);
243         $buttonarray = array();
244         if (!empty($this->question->id)) {
245             // Editing / moving question
246             if ($this->question->formoptions->movecontext) {
247                 $buttonarray[] = $mform->createElement('submit', 'submitbutton',
248                         get_string('moveq', 'question'));
249             } else if ($this->question->formoptions->canedit) {
250                 $buttonarray[] = $mform->createElement('submit', 'submitbutton',
251                         get_string('savechanges'));
252             }
253             if ($this->question->formoptions->cansaveasnew) {
254                 $buttonarray[] = $mform->createElement('submit', 'makecopy',
255                         get_string('makecopy', 'question'));
256             }
257             $buttonarray[] = $mform->createElement('cancel');
258         } else {
259             // Adding new question
260             $buttonarray[] = $mform->createElement('submit', 'submitbutton',
261                     get_string('savechanges'));
262             $buttonarray[] = $mform->createElement('cancel');
263         }
264         $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
265         $mform->closeHeaderBefore('buttonar');
267         if ($this->question->formoptions->movecontext) {
268             $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar'));
269         } else if ((!empty($this->question->id)) && (!($this->question->formoptions->canedit ||
270                 $this->question->formoptions->cansaveasnew))) {
271             $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar', 'currentgrp'));
272         }
273     }
275     /**
276      * Add any question-type specific form fields.
277      *
278      * @param object $mform the form being built.
279      */
280     protected function definition_inner($mform) {
281         // By default, do nothing.
282     }
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
290      * @param $answersoption reference to return the name of $question->options
291      *      field holding an array of answers
292      * @return array of form fields.
293      */
294     protected function get_per_answer_fields($mform, $label, $gradeoptions,
295             &$repeatedoptions, &$answersoption) {
296         $repeated = array();
297         $repeated[] = $mform->createElement('header', 'answerhdr', $label);
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);
304         $repeatedoptions['answer']['type'] = PARAM_RAW;
305         $repeatedoptions['fraction']['default'] = 0;
306         $answersoption = 'answers';
307         return $repeated;
308     }
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.
316      * @param $minoptions the minimum number of answer blanks to display.
317      *      Default QUESTION_NUMANS_START.
318      * @param $addoptions the number of answer blanks to add. Default QUESTION_NUMANS_ADD.
319      */
320     protected function add_per_answer_fields(&$mform, $label, $gradeoptions,
321             $minoptions = QUESTION_NUMANS_START, $addoptions = QUESTION_NUMANS_ADD) {
322         $answersoption = '';
323         $repeatedoptions = array();
324         $repeated = $this->get_per_answer_fields($mform, $label, $gradeoptions,
325                 $repeatedoptions, $answersoption);
327         if (isset($this->question->options)) {
328             $countanswers = count($this->question->options->$answersoption);
329         } else {
330             $countanswers = 0;
331         }
332         if ($this->question->formoptions->repeatelements) {
333             $repeatsatstart = max($minoptions, $countanswers + $addoptions);
334         } else {
335             $repeatsatstart = $countanswers;
336         }
338         $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions,
339                 'noanswers', 'addanswers', $addoptions,
340                 $this->get_more_choices_string());
341     }
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');
348     }
350     protected function add_combined_feedback_fields($withshownumpartscorrect = false) {
351         $mform = $this->_form;
353         $mform->addElement('header', 'combinedfeedbackhdr',
354                 get_string('combinedfeedback', 'question'));
356         $fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
357         foreach ($fields as $feedbackname) {
358             $element = $mform->addElement('editor', $feedbackname,
359                                 get_string($feedbackname, 'question'),
360                                 array('rows' => 5), $this->editoroptions);
361             $mform->setType($feedbackname, PARAM_RAW);
362             // Using setValue() as setDefault() does not work for the editor class.
363             $element->setValue(array('text'=>get_string($feedbackname.'default', 'question')));
365             if ($withshownumpartscorrect && $feedbackname == 'partiallycorrectfeedback') {
366                 $mform->addElement('advcheckbox', 'shownumcorrect',
367                         get_string('options', 'question'),
368                         get_string('shownumpartscorrectwhenfinished', 'question'));
369                 $mform->setDefault('shownumcorrect', true);
370             }
371         }
372     }
374     protected function get_hint_fields($withclearwrong = false, $withshownumpartscorrect = false) {
375         $mform = $this->_form;
377         $repeated = array();
378         $repeated[] = $mform->createElement('header', 'hinthdr', get_string('hintn', 'question'));
379         $repeated[] = $mform->createElement('editor', 'hint', get_string('hinttext', 'question'),
380                 array('rows' => 5), $this->editoroptions);
381         $repeatedoptions['hint']['type'] = PARAM_RAW;
383         if ($withclearwrong) {
384             $repeated[] = $mform->createElement('advcheckbox', 'hintclearwrong',
385                     get_string('options', 'question'), get_string('clearwrongparts', 'question'));
386         }
387         if ($withshownumpartscorrect) {
388             $repeated[] = $mform->createElement('advcheckbox', 'hintshownumcorrect', '',
389                     get_string('shownumpartscorrect', 'question'));
390         }
392         return array($repeated, $repeatedoptions);
393     }
395     protected function add_interactive_settings($withclearwrong = false,
396             $withshownumpartscorrect = false) {
397         $mform = $this->_form;
399         $mform->addElement('header', 'multitriesheader',
400                 get_string('settingsformultipletries', 'question'));
402         $penalties = array(
403             1.0000000,
404             0.5000000,
405             0.3333333,
406             0.2500000,
407             0.2000000,
408             0.1000000,
409             0.0000000
410         );
411         if (!empty($this->question->penalty) && !in_array($this->question->penalty, $penalties)) {
412             $penalties[] = $this->question->penalty;
413             sort($penalties);
414         }
415         $penaltyoptions = array();
416         foreach ($penalties as $penalty) {
417             $penaltyoptions["$penalty"] = (100 * $penalty) . '%';
418         }
419         $mform->addElement('select', 'penalty',
420                 get_string('penaltyforeachincorrecttry', 'question'), $penaltyoptions);
421         $mform->addRule('penalty', null, 'required', null, 'client');
422         $mform->addHelpButton('penalty', 'penaltyforeachincorrecttry', 'question');
423         $mform->setDefault('penalty', 0.3333333);
425         if (isset($this->question->hints)) {
426             $counthints = count($this->question->hints);
427         } else {
428             $counthints = 0;
429         }
431         if ($this->question->formoptions->repeatelements) {
432             $repeatsatstart = max(self::DEFAULT_NUM_HINTS, $counthints);
433         } else {
434             $repeatsatstart = $counthints;
435         }
437         list($repeated, $repeatedoptions) = $this->get_hint_fields(
438                 $withclearwrong, $withshownumpartscorrect);
439         $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions,
440                 'numhints', 'addhint', 1, get_string('addanotherhint', 'question'));
441     }
443     public function set_data($question) {
444         question_bank::get_qtype($question->qtype)->set_default_options($question);
446         // prepare question text
447         $draftid = file_get_submitted_draft_itemid('questiontext');
449         if (!empty($question->questiontext)) {
450             $questiontext = $question->questiontext;
451         } else {
452             $questiontext = $this->_form->getElement('questiontext')->getValue();
453             $questiontext = $questiontext['text'];
454         }
455         $questiontext = file_prepare_draft_area($draftid, $this->context->id,
456                 'question', 'questiontext', empty($question->id) ? null : (int) $question->id,
457                 $this->fileoptions, $questiontext);
459         $question->questiontext = array();
460         $question->questiontext['text'] = $questiontext;
461         $question->questiontext['format'] = empty($question->questiontextformat) ?
462                 editors_get_preferred_format() : $question->questiontextformat;
463         $question->questiontext['itemid'] = $draftid;
465         // prepare general feedback
466         $draftid = file_get_submitted_draft_itemid('generalfeedback');
468         if (empty($question->generalfeedback)) {
469             $generalfeedback = $this->_form->getElement('generalfeedback')->getValue();
470             $question->generalfeedback = $generalfeedback['text'];
471         }
473         $feedback = file_prepare_draft_area($draftid, $this->context->id,
474                 'question', 'generalfeedback', empty($question->id) ? null : (int) $question->id,
475                 $this->fileoptions, $question->generalfeedback);
476         $question->generalfeedback = array();
477         $question->generalfeedback['text'] = $feedback;
478         $question->generalfeedback['format'] = empty($question->generalfeedbackformat) ?
479                 editors_get_preferred_format() : $question->generalfeedbackformat;
480         $question->generalfeedback['itemid'] = $draftid;
482         // Remove unnecessary trailing 0s form grade fields.
483         if (isset($question->defaultgrade)) {
484             $question->defaultgrade = 0 + $question->defaultgrade;
485         }
486         if (isset($question->penalty)) {
487             $question->penalty = 0 + $question->penalty;
488         }
490         // Set any options.
491         $extraquestionfields = question_bank::get_qtype($question->qtype)->extra_question_fields();
492         if (is_array($extraquestionfields) && !empty($question->options)) {
493             array_shift($extraquestionfields);
494             foreach ($extraquestionfields as $field) {
495                 if (property_exists($question->options, $field)) {
496                     $question->$field = $question->options->$field;
497                 }
498             }
499         }
501         // subclass adds data_preprocessing code here
502         $question = $this->data_preprocessing($question);
504         parent::set_data($question);
505     }
507     /**
508      * Perform an preprocessing needed on the data passed to {@link set_data()}
509      * before it is used to initialise the form.
510      * @param object $question the data being passed to the form.
511      * @return object $question the modified data.
512      */
513     protected function data_preprocessing($question) {
514         return $question;
515     }
517     /**
518      * Perform the necessary preprocessing for the fields added by
519      * {@link add_per_answer_fields()}.
520      * @param object $question the data being passed to the form.
521      * @return object $question the modified data.
522      */
523     protected function data_preprocessing_answers($question, $withanswerfiles = false) {
524         if (empty($question->options->answers)) {
525             return $question;
526         }
528         $key = 0;
529         foreach ($question->options->answers as $answer) {
530             if ($withanswerfiles) {
531                 // Prepare the feedback editor to display files in draft area
532                 $draftitemid = file_get_submitted_draft_itemid('answer['.$key.']');
533                 $question->answer[$key]['text'] = file_prepare_draft_area(
534                     $draftitemid,          // draftid
535                     $this->context->id,    // context
536                     'question',            // component
537                     'answer',              // filarea
538                     !empty($answer->id) ? (int) $answer->id : null, // itemid
539                     $this->fileoptions,    // options
540                     $answer->answer        // text
541                 );
542                 $question->answer[$key]['itemid'] = $draftitemid;
543                 $question->answer[$key]['format'] = $answer->answerformat;
544             } else {
545                 $question->answer[$key] = $answer->answer;
546             }
548             $question->fraction[$key] = 0 + $answer->fraction;
549             $question->feedback[$key] = array();
551             // Evil hack alert. Formslib can store defaults in two ways for
552             // repeat elements:
553             //   ->_defaultValues['fraction[0]'] and
554             //   ->_defaultValues['fraction'][0].
555             // The $repeatedoptions['fraction']['default'] = 0 bit above means
556             // that ->_defaultValues['fraction[0]'] has already been set, but we
557             // are using object notation here, so we will be setting
558             // ->_defaultValues['fraction'][0]. That does not work, so we have
559             // to unset ->_defaultValues['fraction[0]']
560             unset($this->_form->_defaultValues["fraction[$key]"]);
562             // Prepare the feedback editor to display files in draft area
563             $draftitemid = file_get_submitted_draft_itemid('feedback['.$key.']');
564             $question->feedback[$key]['text'] = file_prepare_draft_area(
565                 $draftitemid,          // draftid
566                 $this->context->id,    // context
567                 'question',            // component
568                 'answerfeedback',      // filarea
569                 !empty($answer->id) ? (int) $answer->id : null, // itemid
570                 $this->fileoptions,    // options
571                 $answer->feedback      // text
572             );
573             $question->feedback[$key]['itemid'] = $draftitemid;
574             $question->feedback[$key]['format'] = $answer->feedbackformat;
575             $key++;
576         }
577         return $question;
578     }
580     /**
581      * Perform the necessary preprocessing for the fields added by
582      * {@link add_combined_feedback_fields()}.
583      * @param object $question the data being passed to the form.
584      * @return object $question the modified data.
585      */
586     protected function data_preprocessing_combined_feedback($question,
587             $withshownumcorrect = false) {
588         if (empty($question->options)) {
589             return $question;
590         }
592         $fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
593         foreach ($fields as $feedbackname) {
594             $draftid = file_get_submitted_draft_itemid($feedbackname);
595             $feedback = array();
596             $feedback['text'] = file_prepare_draft_area(
597                 $draftid,              // draftid
598                 $this->context->id,    // context
599                 'question',            // component
600                 $feedbackname,         // filarea
601                 !empty($question->id) ? (int) $question->id : null, // itemid
602                 $this->fileoptions,    // options
603                 $question->options->$feedbackname // text
604             );
605             $feedbackformat = $feedbackname . 'format';
606             $feedback['format'] = $question->options->$feedbackformat;
607             $feedback['itemid'] = $draftid;
609             $question->$feedbackname = $feedback;
610         }
612         if ($withshownumcorrect) {
613             $question->shownumcorrect = $question->options->shownumcorrect;
614         }
616         return $question;
617     }
619     /**
620      * Perform the necessary preprocessing for the hint fields.
621      * @param object $question the data being passed to the form.
622      * @return object $question the modified data.
623      */
624     protected function data_preprocessing_hints($question, $withclearwrong = false,
625             $withshownumpartscorrect = false) {
626         if (empty($question->hints)) {
627             return $question;
628         }
630         $key = 0;
631         foreach ($question->hints as $hint) {
632             $question->hint[$key] = array();
634             // prepare feedback editor to display files in draft area
635             $draftitemid = file_get_submitted_draft_itemid('hint['.$key.']');
636             $question->hint[$key]['text'] = file_prepare_draft_area(
637                 $draftitemid,          // draftid
638                 $this->context->id,    // context
639                 'question',            // component
640                 'hint',                // filarea
641                 !empty($hint->id) ? (int) $hint->id : null, // itemid
642                 $this->fileoptions,    // options
643                 $hint->hint            // text
644             );
645             $question->hint[$key]['itemid'] = $draftitemid;
646             $question->hint[$key]['format'] = $hint->hintformat;
647             $key++;
649             if ($withclearwrong) {
650                 $question->hintclearwrong[] = $hint->clearwrong;
651             }
652             if ($withshownumpartscorrect) {
653                 $question->hintshownumcorrect[] = $hint->shownumcorrect;
654             }
655         }
657         return $question;
658     }
660     public function validation($fromform, $files) {
661         $errors = parent::validation($fromform, $files);
662         if (empty($fromform['makecopy']) && isset($this->question->id)
663                 && ($this->question->formoptions->canedit ||
664                         $this->question->formoptions->cansaveasnew)
665                 && empty($fromform['usecurrentcat']) && !$this->question->formoptions->canmove) {
666             $errors['currentgrp'] = get_string('nopermissionmove', 'question');
667         }
668         return $errors;
669     }
671     /**
672      * Override this in the subclass to question type name.
673      * @return the question type name, should be the same as the name() method
674      *      in the question type class.
675      */
676     public abstract function qtype();