MDL-20636 Conversion of the description question type.
[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
2b7da645 18
36703ed7 19/**
20 * A base class for question editing forms.
21 *
2b7da645
TH
22 * @package moodlecore
23 * @subpackage questiontypes
36703ed7 24 * @copyright &copy; 2006 The Open University
36703ed7 25 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
fe6ce234 26 */
36703ed7 27
2b7da645 28
36703ed7 29/**
30 * Form definition base class. This defines the common fields that
31 * all question types need. Question types should define their own
32 * class that inherits from this one, and implements the definition_inner()
33 * method.
271e6dec 34 *
2b7da645
TH
35 * @copyright &copy; 2006 The Open University
36 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
36703ed7 37 */
2b7da645
TH
38abstract class question_edit_form extends moodleform {
39 const DEFAULT_NUM_HINTS = 2;
40
271ffe3f 41 /**
42 * Question object with options and answers already loaded by get_question_options
43 * Be careful how you use this it is needed sometimes to set up the structure of the
32db0d42 44 * form in definition_inner but data is always loaded into the form with set_data.
271ffe3f 45 * @var object
46 */
2b7da645
TH
47 protected $question;
48
49 protected $contexts;
50 protected $category;
51 protected $categorycontext;
271e6dec 52
fe6ce234
DC
53 /** @var object current context */
54 public $context;
55 /** @var array html editor options */
56 public $editoroptions;
57 /** @var array options to preapre draft area */
58 public $fileoptions;
59 /** @var object instance of question type */
60 public $instance;
61
2b7da645 62 public function __construct($submiturl, $question, $category, $contexts, $formeditable = true) {
fe6ce234 63 global $DB;
271e6dec 64
271ffe3f 65 $this->question = $question;
271e6dec 66 $this->contexts = $contexts;
67
5d548d3e 68 $record = $DB->get_record('question_categories', array('id' => $question->category), 'contextid');
fe6ce234
DC
69 $this->context = get_context_instance_by_id($record->contextid);
70
41dcc2a5
DC
71 $this->editoroptions = array('subdirs' => 1,'maxfiles' => EDITOR_UNLIMITED_FILES, 'context' => $this->context);
72 $this->fileoptions = array('subdirs' => 1, 'maxfiles' => -1, 'maxbytes' => -1);
fe6ce234 73
271e6dec 74 $this->category = $category;
75 $this->categorycontext = get_context_instance_by_id($category->contextid);
271e6dec 76
2b7da645 77 parent::__construct($submiturl, null, 'post', '', null, $formeditable);
271ffe3f 78 }
8e652f02 79
36703ed7 80 /**
81 * Build the form definition.
1d284fbd 82 *
295043c2 83 * This adds all the form fields that the default question type supports.
36703ed7 84 * If your question type does not support all these fields, then you can
85 * override this method and remove the ones you don't want with $mform->removeElement().
86 */
2b7da645 87 public function definition() {
f34488b2 88 global $COURSE, $CFG, $DB;
1d284fbd 89
36703ed7 90 $qtype = $this->qtype();
91 $langfile = "qtype_$qtype";
1d284fbd 92
2b7da645 93 $mform = $this->_form;
36703ed7 94
95 // Standard fields at the start of the form.
271ffe3f 96 $mform->addElement('header', 'generalheader', get_string("general", 'form'));
1d284fbd 97
2b7da645 98 if (!isset($this->question->id)) {
5d548d3e 99 // Adding question
2b7da645 100 $mform->addElement('questioncategory', 'category', get_string('category', 'question'),
522b89d1 101 array('contexts' => $this->contexts->having_cap('moodle/question:add')));
2b7da645 102 } elseif (!($this->question->formoptions->canmove || $this->question->formoptions->cansaveasnew)) {
5d548d3e 103 // Editing question with no permission to move from category.
2b7da645 104 $mform->addElement('questioncategory', 'category', get_string('category', 'question'),
271e6dec 105 array('contexts' => array($this->categorycontext)));
2b7da645 106 } elseif ($this->question->formoptions->movecontext) {
5d548d3e 107 // Moving question to another context.
2b7da645 108 $mform->addElement('questioncategory', 'categorymoveto', get_string('category', 'question'),
271e6dec 109 array('contexts' => $this->contexts->having_cap('moodle/question:add')));
110
111 } else {
5d548d3e 112 // Editing question with permission to move from category or save as new q
271e6dec 113 $currentgrp = array();
114 $currentgrp[0] =& $mform->createElement('questioncategory', 'category', get_string('categorycurrent', 'question'),
115 array('contexts' => array($this->categorycontext)));
2b7da645 116 if ($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew) {
271e6dec 117 //not move only form
118 $currentgrp[1] =& $mform->createElement('checkbox', 'usecurrentcat', '', get_string('categorycurrentuse', 'question'));
119 $mform->setDefault('usecurrentcat', 1);
120 }
121 $currentgrp[0]->freeze();
122 $currentgrp[0]->setPersistantFreeze(false);
123 $mform->addGroup($currentgrp, 'currentgrp', get_string('categorycurrent', 'question'), null, false);
124
125 $mform->addElement('questioncategory', 'categorymoveto', get_string('categorymoveto', 'question'),
126 array('contexts' => array($this->categorycontext)));
2b7da645 127 if ($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew) {
271e6dec 128 //not move only form
129 $mform->disabledIf('categorymoveto', 'usecurrentcat', 'checked');
130 }
131 }
375ed78a 132
2b7da645 133 $mform->addElement('text', 'name', get_string('questionname', 'question'), array('size' => 50));
271ffe3f 134 $mform->setType('name', PARAM_TEXT);
36703ed7 135 $mform->addRule('name', null, 'required', null, 'client');
1d284fbd 136
2b7da645 137 $mform->addElement('editor', 'questiontext', get_string('questiontext', 'question'),
5d548d3e 138 array('rows' => 15), $this->editoroptions);
36703ed7 139 $mform->setType('questiontext', PARAM_RAW);
1d284fbd 140
2b7da645 141 $mform->addElement('text', 'defaultmark', get_string('defaultmark', 'question'),
36703ed7 142 array('size' => 3));
2b7da645
TH
143 $mform->setType('defaultmark', PARAM_INT);
144 $mform->setDefault('defaultmark', 1);
145 $mform->addRule('defaultmark', null, 'required', null, 'client');
36703ed7 146
2b7da645 147 $mform->addElement('editor', 'generalfeedback', get_string('generalfeedback', 'question'),
5d548d3e 148 array('rows' => 10), $this->editoroptions);
36703ed7 149 $mform->setType('generalfeedback', PARAM_RAW);
2b7da645 150 $mform->addHelpButton('generalfeedback', 'generalfeedback', 'question');
1d284fbd 151
36703ed7 152 // Any questiontype specific fields.
153 $this->definition_inner($mform);
154
c599a682 155 if (!empty($CFG->usetags)) {
156 $mform->addElement('header', 'tagsheader', get_string('tags'));
157 $mform->addElement('tags', 'tags', get_string('tags'));
158 }
159
2b7da645 160 if (!empty($this->question->id)) {
271e6dec 161 $mform->addElement('header', 'createdmodifiedheader', get_string('createdmodifiedheader', 'question'));
7f389342 162 $a = new stdClass();
2b7da645 163 if (!empty($this->question->createdby)) {
271e6dec 164 $a->time = userdate($this->question->timecreated);
f34488b2 165 $a->user = fullname($DB->get_record('user', array('id' => $this->question->createdby)));
271e6dec 166 } else {
167 $a->time = get_string('unknown', 'question');
168 $a->user = get_string('unknown', 'question');
169 }
170 $mform->addElement('static', 'created', get_string('created', 'question'), get_string('byandon', 'question', $a));
2b7da645 171 if (!empty($this->question->modifiedby)) {
7f389342 172 $a = new stdClass();
271e6dec 173 $a->time = userdate($this->question->timemodified);
f34488b2 174 $a->user = fullname($DB->get_record('user', array('id' => $this->question->modifiedby)));
271e6dec 175 $mform->addElement('static', 'modified', get_string('modified', 'question'), get_string('byandon', 'question', $a));
176 }
177 }
178
36703ed7 179 // Standard fields at the end of the form.
180 $mform->addElement('hidden', 'id');
181 $mform->setType('id', PARAM_INT);
182
183 $mform->addElement('hidden', 'qtype');
184 $mform->setType('qtype', PARAM_ALPHA);
185
186 $mform->addElement('hidden', 'inpopup');
187 $mform->setType('inpopup', PARAM_INT);
188
189 $mform->addElement('hidden', 'versioning');
190 $mform->setType('versioning', PARAM_BOOL);
191
271e6dec 192 $mform->addElement('hidden', 'movecontext');
193 $mform->setType('movecontext', PARAM_BOOL);
194
9ab75b2b 195 $mform->addElement('hidden', 'cmid');
196 $mform->setType('cmid', PARAM_INT);
197 $mform->setDefault('cmid', 0);
198
271e6dec 199 $mform->addElement('hidden', 'courseid');
200 $mform->setType('courseid', PARAM_INT);
201 $mform->setDefault('courseid', 0);
202
7cd4fda6 203 $mform->addElement('hidden', 'returnurl');
204 $mform->setType('returnurl', PARAM_LOCALURL);
271e6dec 205 $mform->setDefault('returnurl', 0);
7cd4fda6 206
fa583f5f 207 $mform->addElement('hidden', 'appendqnumstring');
208 $mform->setType('appendqnumstring', PARAM_ALPHA);
209 $mform->setDefault('appendqnumstring', 0);
210
375ed78a 211 $buttonarray = array();
2b7da645 212 if (!empty($this->question->id)) {
271e6dec 213 //editing / moving question
2b7da645 214 if ($this->question->formoptions->movecontext) {
271e6dec 215 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('moveq', 'question'));
2b7da645 216 } elseif ($this->question->formoptions->canedit || $this->question->formoptions->canmove ||$this->question->formoptions->movecontext) {
271e6dec 217 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges'));
218 }
2b7da645
TH
219 if ($this->question->formoptions->cansaveasnew) {
220 $buttonarray[] = &$mform->createElement('submit', 'makecopy', get_string('makecopy', 'question'));
271e6dec 221 }
222 $buttonarray[] = &$mform->createElement('cancel');
223 } else {
224 // adding new question
225 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges'));
226 $buttonarray[] = &$mform->createElement('cancel');
375ed78a 227 }
375ed78a 228 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
229 $mform->closeHeaderBefore('buttonar');
271e6dec 230
5d548d3e 231 if ($this->question->formoptions->movecontext) {
271e6dec 232 $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar'));
2b7da645 233 } else if ((!empty($this->question->id)) && (!($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew))) {
271e6dec 234 $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar', 'currentgrp'));
235 }
36703ed7 236 }
fa583f5f 237
36703ed7 238 /**
239 * Add any question-type specific form fields.
1d284fbd 240 *
241 * @param object $mform the form being built.
36703ed7 242 */
2b7da645 243 protected function definition_inner($mform) {
36703ed7 244 // By default, do nothing.
245 }
1d284fbd 246
2aef1fe5 247 /**
248 * Get the list of form elements to repeat, one for each answer.
249 * @param object $mform the form being built.
250 * @param $label the label to use for each option.
251 * @param $gradeoptions the possible grades for each answer.
252 * @param $repeatedoptions reference to array of repeated options to fill
253 * @param $answersoption reference to return the name of $question->options field holding an array of answers
254 * @return array of form fields.
255 */
2b7da645 256 protected function get_per_answer_fields(&$mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
2aef1fe5 257 $repeated = array();
258 $repeated[] =& $mform->createElement('header', 'answerhdr', $label);
de58c9c4 259 $repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'), array('size' => 80));
2aef1fe5 260 $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
fe6ce234 261 $repeated[] =& $mform->createElement('editor', 'feedback', get_string('feedback', 'quiz'),
5d548d3e 262 array('rows' => 5), $this->editoroptions);
2aef1fe5 263 $repeatedoptions['answer']['type'] = PARAM_RAW;
264 $repeatedoptions['fraction']['default'] = 0;
265 $answersoption = 'answers';
266 return $repeated;
267 }
268
269 /**
270 * Add a set of form fields, obtained from get_per_answer_fields, to the form,
271 * one for each existing answer, with some blanks for some new ones.
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 $minoptions the minimum number of answer blanks to display. Default QUESTION_NUMANS_START.
276 * @param $addoptions the number of answer blanks to add. Default QUESTION_NUMANS_ADD.
277 */
2b7da645 278 protected function add_per_answer_fields(&$mform, $label, $gradeoptions, $minoptions = QUESTION_NUMANS_START, $addoptions = QUESTION_NUMANS_ADD) {
2aef1fe5 279 $answersoption = '';
280 $repeatedoptions = array();
281 $repeated = $this->get_per_answer_fields($mform, $label, $gradeoptions, $repeatedoptions, $answersoption);
282
2b7da645 283 if (isset($this->question->options)) {
2aef1fe5 284 $countanswers = count($this->question->options->$answersoption);
285 } else {
286 $countanswers = 0;
287 }
2b7da645 288 if ($this->question->formoptions->repeatelements) {
2aef1fe5 289 $repeatsatstart = max($minoptions, $countanswers + $addoptions);
290 } else {
291 $repeatsatstart = $countanswers;
292 }
293
294 $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', $addoptions, get_string('addmorechoiceblanks', 'qtype_multichoice'));
295 }
296
2b7da645
TH
297 protected function add_combined_feedback_fields($withshownumpartscorrect = false) {
298 $mform = $this->_form;
299
300 $mform->addElement('header', 'combinedfeedbackhdr', get_string('combinedfeedback', 'question'));
301
302 foreach (array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback') as $feedbackname) {
303 $mform->addElement('editor', $feedbackname, get_string($feedbackname, 'question'),
304 array('rows' => 5), $this->editoroptions);
305 $mform->setType($feedbackname, PARAM_RAW);
306
307 if ($withshownumpartscorrect && $feedbackname == 'partiallycorrectfeedback') {
308 $mform->addElement('checkbox', 'shownumcorrect', get_string('options', 'question'), get_string('shownumpartscorrect', 'question'));
309 }
310 }
311 }
312
313 protected function get_hint_fields($withclearwrong = false, $withshownumpartscorrect = false) {
314 $mform = $this->_form;
315
316 $repeated = array();
317 $repeated[] = $mform->createElement('header', 'answerhdr', get_string('hintn', 'question'));
318 $repeated[] = $mform->createElement('htmleditor', 'hint', get_string('hinttext', 'question'), array('size' => 50));
319 $repeatedoptions['hint']['type'] = PARAM_RAW;
320
321 if ($withclearwrong) {
322 $repeated[] = $mform->createElement('checkbox', 'hintclearwrong', get_string('options', 'question'), get_string('clearwrongparts', 'question'));
323 }
324 if ($withshownumpartscorrect) {
325 $repeated[] = $mform->createElement('checkbox', 'hintshownumcorrect', '', get_string('shownumpartscorrect', 'question'));
326 }
327
328 return array($repeated, $repeatedoptions);
329 }
330
331 protected function add_interactive_settings($withclearwrong = false, $withshownumpartscorrect = false) {
332 $mform = $this->_form;
333
334 $mform->addElement('header', 'multitriesheader', get_string('settingsformultipletries', 'question'));
335
336 $penalties = array(
337 1.0000000,
338 0.5000000,
339 0.3333333,
340 0.2500000,
341 0.2000000,
342 0.1000000,
343 0.0000000
344 );
345 if (!empty($this->question->penalty) && !in_array($this->question->penalty, $penalties)) {
346 $penalties[] = $this->question->penalty;
347 sort($penalties);
348 }
349 $penaltyoptions = array();
350 foreach ($penalties as $penalty) {
351 $penaltyoptions["$penalty"] = (100 * $penalty) . '%';
352 }
353 $mform->addElement('select', 'penalty', get_string('penaltyforeachincorrecttry', 'question'),
354 $penaltyoptions);
355 $mform->addRule('penalty', null, 'required', null, 'client');
068b4594 356 $mform->addHelpButton('penalty', 'penaltyforeachincorrecttry', 'question');
2b7da645
TH
357 $mform->setDefault('penalty', 0.3333333);
358
359 if (isset($this->question->hints)) {
360 $counthints = count($this->question->hints);
361 } else {
362 $counthints = 0;
363 }
364
365 if ($this->question->formoptions->repeatelements) {
366 $repeatsatstart = max(self::DEFAULT_NUM_HINTS, $counthints);
367 } else {
368 $repeatsatstart = $counthints;
369 }
370
371 list($repeated, $repeatedoptions) = $this->get_hint_fields(
372 $withclearwrong, $withshownumpartscorrect);
373 $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions,
374 'numhints', 'addhint', 1, get_string('addanotherhint', 'question'));
375 }
376
377 public function set_data($question) {
36703ed7 378 global $QTYPES;
2b7da645
TH
379 $QTYPES[$question->qtype]->set_default_options($question);
380
fe6ce234
DC
381 // prepare question text
382 $draftid = file_get_submitted_draft_itemid('questiontext');
383
384 if (!empty($question->questiontext)) {
385 $questiontext = $question->questiontext;
386 } else {
387 $questiontext = '';
271ffe3f 388 }
41dcc2a5 389 $questiontext = file_prepare_draft_area($draftid, $this->context->id, 'question', 'questiontext', empty($question->id)?null:(int)$question->id, $this->fileoptions, $questiontext);
fe6ce234
DC
390
391 $question->questiontext = array();
392 $question->questiontext['text'] = $questiontext;
393 $question->questiontext['format'] = empty($question->questiontextformat) ? editors_get_preferred_format() : $question->questiontextformat;
394 $question->questiontext['itemid'] = $draftid;
395
396 // prepare general feedback
397 $draftid = file_get_submitted_draft_itemid('generalfeedback');
398
399 if (empty($question->generalfeedback)) {
400 $question->generalfeedback = '';
401 }
402
41dcc2a5 403 $feedback = file_prepare_draft_area($draftid, $this->context->id, 'question', 'generalfeedback', empty($question->id)?null:(int)$question->id, $this->fileoptions, $question->generalfeedback);
fe6ce234
DC
404 $question->generalfeedback = array();
405 $question->generalfeedback['text'] = $feedback;
406 $question->generalfeedback['format'] = empty($question->generalfeedbackformat) ? editors_get_preferred_format() : $question->generalfeedbackformat;
407 $question->generalfeedback['itemid'] = $draftid;
295043c2 408
9152f6a5 409 // Remove unnecessary trailing 0s form grade fields.
cfd24d98 410 if (isset($question->defaultgrade)) {
411 $question->defaultgrade = 0 + $question->defaultgrade;
412 }
413 if (isset($question->penalty)) {
414 $question->penalty = 0 + $question->penalty;
415 }
9152f6a5 416
295043c2 417 // Set any options.
418 $extra_question_fields = $QTYPES[$question->qtype]->extra_question_fields();
8e652f02 419 if (is_array($extra_question_fields) && !empty($question->options)) {
295043c2 420 array_shift($extra_question_fields);
421 foreach ($extra_question_fields as $field) {
b555ce76 422 if (isset($question->options->$field)) {
8e652f02 423 $question->$field = $question->options->$field;
424 }
295043c2 425 }
426 }
fe6ce234
DC
427 // subclass adds data_preprocessing code here
428 $question = $this->data_preprocessing($question);
2b7da645
TH
429
430 if (!empty($question->hints)) {
431 $i = 0;
432 foreach ($question->hints as $hint) {
433 $question->hint[$i] = $hint->hint;
434 $question->hintclearwrong[$i] = !empty($hint->clearwrong);
435 $question->hintshownumcorrect[$i] = !empty($hint->shownumcorrect);
436 $i += 1;
437 }
438 }
439
32db0d42 440 parent::set_data($question);
36703ed7 441 }
1d284fbd 442
fe6ce234
DC
443 /**
444 * Any preprocessing needed for the settings form for the question type
445 *
446 * @param array $question - array to fill in with the default values
447 */
448 function data_preprocessing($question) {
2b7da645 449 // TODO is this really necessary?
fe6ce234
DC
450 return $question;
451 }
452
2b7da645
TH
453 public function validation($fromform, $files) {
454 $errors = parent::validation($fromform, $files);
455 if (empty($fromform->makecopy) && isset($this->question->id)
456 && ($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew)
457 && empty($fromform->usecurrentcat) && !$this->question->formoptions->canmove) {
458 $errors['currentgrp'] = get_string('nopermissionmove', 'question');
459 }
460 return $errors;
461 }
462
36703ed7 463 /**
464 * Override this in the subclass to question type name.
465 * @return the question type name, should be the same as the name() method in the question type class.
466 */
2b7da645 467 public abstract function qtype();
36703ed7 468}