MDL-20636 converstion of questionlib.php and base questiontype.php, plus other cheang...
[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 *
21 * @copyright &copy; 2006 The Open University
22 * @author T.J.Hunt@open.ac.uk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
4323d029 24 * @package questionbank
25 * @subpackage questiontypes
fe6ce234 26 */
36703ed7 27
28/**
29 * Form definition base class. This defines the common fields that
30 * all question types need. Question types should define their own
31 * class that inherits from this one, and implements the definition_inner()
32 * method.
271e6dec 33 *
4323d029 34 * @package questionbank
35 * @subpackage questiontypes
36703ed7 36 */
1d284fbd 37class question_edit_form extends moodleform {
271ffe3f 38 /**
39 * Question object with options and answers already loaded by get_question_options
40 * Be careful how you use this it is needed sometimes to set up the structure of the
32db0d42 41 * form in definition_inner but data is always loaded into the form with set_data.
271ffe3f 42 *
43 * @var object
44 */
f34488b2 45 public $question;
f34488b2 46 public $contexts;
47 public $category;
48 public $categorycontext;
271e6dec 49
fe6ce234
DC
50 /** @var object current context */
51 public $context;
52 /** @var array html editor options */
53 public $editoroptions;
54 /** @var array options to preapre draft area */
55 public $fileoptions;
56 /** @var object instance of question type */
57 public $instance;
58
271e6dec 59 function question_edit_form($submiturl, $question, $category, $contexts, $formeditable = true){
fe6ce234 60 global $DB;
271e6dec 61
271ffe3f 62 $this->question = $question;
271e6dec 63
64 $this->contexts = $contexts;
65
5d548d3e 66 $record = $DB->get_record('question_categories', array('id' => $question->category), 'contextid');
fe6ce234
DC
67 $this->context = get_context_instance_by_id($record->contextid);
68
41dcc2a5
DC
69 $this->editoroptions = array('subdirs' => 1,'maxfiles' => EDITOR_UNLIMITED_FILES, 'context' => $this->context);
70 $this->fileoptions = array('subdirs' => 1, 'maxfiles' => -1, 'maxbytes' => -1);
fe6ce234 71
271e6dec 72 $this->category = $category;
73 $this->categorycontext = get_context_instance_by_id($category->contextid);
271e6dec 74
fe6ce234 75 if (!empty($question->id)) {
5d548d3e 76 $question->id = (int) $question->id;
fe6ce234
DC
77 }
78
271e6dec 79 parent::moodleform($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 */
89 function definition() {
f34488b2 90 global $COURSE, $CFG, $DB;
1d284fbd 91
36703ed7 92 $qtype = $this->qtype();
93 $langfile = "qtype_$qtype";
1d284fbd 94
36703ed7 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
271e6dec 100 if (!isset($this->question->id)){
5d548d3e 101 // Adding question
271e6dec 102 $mform->addElement('questioncategory', 'category', get_string('category', 'quiz'),
522b89d1 103 array('contexts' => $this->contexts->having_cap('moodle/question:add')));
271e6dec 104 } elseif (!($this->question->formoptions->canmove || $this->question->formoptions->cansaveasnew)){
5d548d3e 105 // Editing question with no permission to move from category.
271e6dec 106 $mform->addElement('questioncategory', 'category', get_string('category', 'quiz'),
107 array('contexts' => array($this->categorycontext)));
108 } elseif ($this->question->formoptions->movecontext){
5d548d3e 109 // Moving question to another context.
271e6dec 110 $mform->addElement('questioncategory', 'categorymoveto', get_string('category', 'quiz'),
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)));
118 if ($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew){
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)));
129 if ($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew){
130 //not move only form
131 $mform->disabledIf('categorymoveto', 'usecurrentcat', 'checked');
132 }
133 }
375ed78a 134
271e6dec 135 $mform->addElement('text', 'name', get_string('questionname', 'quiz'), array('size' => 50));
271ffe3f 136 $mform->setType('name', PARAM_TEXT);
36703ed7 137 $mform->addRule('name', null, 'required', null, 'client');
1d284fbd 138
fe6ce234 139 $mform->addElement('editor', 'questiontext', get_string('questiontext', 'quiz'),
5d548d3e 140 array('rows' => 15), $this->editoroptions);
36703ed7 141 $mform->setType('questiontext', PARAM_RAW);
1d284fbd 142
36703ed7 143 $mform->addElement('text', 'defaultgrade', get_string('defaultgrade', 'quiz'),
144 array('size' => 3));
145 $mform->setType('defaultgrade', PARAM_INT);
92186abc 146 $mform->setDefault('defaultgrade', 1);
36703ed7 147 $mform->addRule('defaultgrade', null, 'required', null, 'client');
148
7292c11f 149 $mform->addElement('text', 'penalty', get_string('penaltyfactor', 'question'),
36703ed7 150 array('size' => 3));
151 $mform->setType('penalty', PARAM_NUMBER);
152 $mform->addRule('penalty', null, 'required', null, 'client');
7292c11f 153 $mform->addHelpButton('penalty', 'penaltyfactor', 'question');
92186abc 154 $mform->setDefault('penalty', 0.1);
36703ed7 155
fe6ce234 156 $mform->addElement('editor', 'generalfeedback', get_string('generalfeedback', 'quiz'),
5d548d3e 157 array('rows' => 10), $this->editoroptions);
36703ed7 158 $mform->setType('generalfeedback', PARAM_RAW);
5fcefc97 159 $mform->addHelpButton('generalfeedback', 'generalfeedback', 'quiz');
1d284fbd 160
36703ed7 161 // Any questiontype specific fields.
162 $this->definition_inner($mform);
163
c599a682 164 if (!empty($CFG->usetags)) {
165 $mform->addElement('header', 'tagsheader', get_string('tags'));
166 $mform->addElement('tags', 'tags', get_string('tags'));
167 }
168
271e6dec 169 if (!empty($this->question->id)){
170 $mform->addElement('header', 'createdmodifiedheader', get_string('createdmodifiedheader', 'question'));
7f389342 171 $a = new stdClass();
271e6dec 172 if (!empty($this->question->createdby)){
173 $a->time = userdate($this->question->timecreated);
f34488b2 174 $a->user = fullname($DB->get_record('user', array('id' => $this->question->createdby)));
271e6dec 175 } else {
176 $a->time = get_string('unknown', 'question');
177 $a->user = get_string('unknown', 'question');
178 }
179 $mform->addElement('static', 'created', get_string('created', 'question'), get_string('byandon', 'question', $a));
180 if (!empty($this->question->modifiedby)){
7f389342 181 $a = new stdClass();
271e6dec 182 $a->time = userdate($this->question->timemodified);
f34488b2 183 $a->user = fullname($DB->get_record('user', array('id' => $this->question->modifiedby)));
271e6dec 184 $mform->addElement('static', 'modified', get_string('modified', 'question'), get_string('byandon', 'question', $a));
185 }
186 }
187
36703ed7 188 // Standard fields at the end of the form.
189 $mform->addElement('hidden', 'id');
190 $mform->setType('id', PARAM_INT);
191
192 $mform->addElement('hidden', 'qtype');
193 $mform->setType('qtype', PARAM_ALPHA);
194
195 $mform->addElement('hidden', 'inpopup');
196 $mform->setType('inpopup', PARAM_INT);
197
198 $mform->addElement('hidden', 'versioning');
199 $mform->setType('versioning', PARAM_BOOL);
200
271e6dec 201 $mform->addElement('hidden', 'movecontext');
202 $mform->setType('movecontext', PARAM_BOOL);
203
9ab75b2b 204 $mform->addElement('hidden', 'cmid');
205 $mform->setType('cmid', PARAM_INT);
206 $mform->setDefault('cmid', 0);
207
271e6dec 208 $mform->addElement('hidden', 'courseid');
209 $mform->setType('courseid', PARAM_INT);
210 $mform->setDefault('courseid', 0);
211
7cd4fda6 212 $mform->addElement('hidden', 'returnurl');
213 $mform->setType('returnurl', PARAM_LOCALURL);
271e6dec 214 $mform->setDefault('returnurl', 0);
7cd4fda6 215
fa583f5f 216 $mform->addElement('hidden', 'appendqnumstring');
217 $mform->setType('appendqnumstring', PARAM_ALPHA);
218 $mform->setDefault('appendqnumstring', 0);
219
375ed78a 220 $buttonarray = array();
271e6dec 221 if (!empty($this->question->id)){
222 //editing / moving question
223 if ($this->question->formoptions->movecontext){
224 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('moveq', 'question'));
225 } elseif ($this->question->formoptions->canedit || $this->question->formoptions->canmove ||$this->question->formoptions->movecontext){
226 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges'));
227 }
228 if ($this->question->formoptions->cansaveasnew){
229 $buttonarray[] = &$mform->createElement('submit', 'makecopy', get_string('makecopy', 'quiz'));
230 }
231 $buttonarray[] = &$mform->createElement('cancel');
232 } else {
233 // adding new question
234 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges'));
235 $buttonarray[] = &$mform->createElement('cancel');
375ed78a 236 }
375ed78a 237 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
238 $mform->closeHeaderBefore('buttonar');
271e6dec 239
5d548d3e 240 if ($this->question->formoptions->movecontext) {
271e6dec 241 $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar'));
5d548d3e 242 } else if ((!empty($this->question->id)) && (!($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew))){
271e6dec 243 $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar', 'currentgrp'));
244 }
36703ed7 245 }
fa583f5f 246
3efbe6bc 247 function validation($fromform, $files) {
5d548d3e 248 $errors = parent::validation($fromform, $files);
fa583f5f 249 if (empty($fromform->makecopy) && isset($this->question->id)
250 && ($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew)
fe6ce234 251 && empty($fromform->usecurrentcat) && !$this->question->formoptions->canmove) {
3efbe6bc 252 $errors['currentgrp'] = get_string('nopermissionmove', 'question');
253 }
254 return $errors;
255 }
1d284fbd 256
36703ed7 257 /**
258 * Add any question-type specific form fields.
1d284fbd 259 *
260 * @param object $mform the form being built.
36703ed7 261 */
262 function definition_inner(&$mform) {
263 // By default, do nothing.
264 }
1d284fbd 265
2aef1fe5 266 /**
267 * Get the list of form elements to repeat, one for each answer.
268 * @param object $mform the form being built.
269 * @param $label the label to use for each option.
270 * @param $gradeoptions the possible grades for each answer.
271 * @param $repeatedoptions reference to array of repeated options to fill
272 * @param $answersoption reference to return the name of $question->options field holding an array of answers
273 * @return array of form fields.
274 */
275 function get_per_answer_fields(&$mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
276 $repeated = array();
277 $repeated[] =& $mform->createElement('header', 'answerhdr', $label);
de58c9c4 278 $repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'), array('size' => 80));
2aef1fe5 279 $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
fe6ce234 280 $repeated[] =& $mform->createElement('editor', 'feedback', get_string('feedback', 'quiz'),
5d548d3e 281 array('rows' => 5), $this->editoroptions);
2aef1fe5 282 $repeatedoptions['answer']['type'] = PARAM_RAW;
283 $repeatedoptions['fraction']['default'] = 0;
284 $answersoption = 'answers';
285 return $repeated;
286 }
287
288 /**
289 * Add a set of form fields, obtained from get_per_answer_fields, to the form,
290 * one for each existing answer, with some blanks for some new ones.
291 * @param object $mform the form being built.
292 * @param $label the label to use for each option.
293 * @param $gradeoptions the possible grades for each answer.
294 * @param $minoptions the minimum number of answer blanks to display. Default QUESTION_NUMANS_START.
295 * @param $addoptions the number of answer blanks to add. Default QUESTION_NUMANS_ADD.
296 */
297 function add_per_answer_fields(&$mform, $label, $gradeoptions, $minoptions = QUESTION_NUMANS_START, $addoptions = QUESTION_NUMANS_ADD) {
298 $answersoption = '';
299 $repeatedoptions = array();
300 $repeated = $this->get_per_answer_fields($mform, $label, $gradeoptions, $repeatedoptions, $answersoption);
301
302 if (isset($this->question->options)){
303 $countanswers = count($this->question->options->$answersoption);
304 } else {
305 $countanswers = 0;
306 }
307 if ($this->question->formoptions->repeatelements){
308 $repeatsatstart = max($minoptions, $countanswers + $addoptions);
309 } else {
310 $repeatsatstart = $countanswers;
311 }
312
313 $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', $addoptions, get_string('addmorechoiceblanks', 'qtype_multichoice'));
314 }
315
32db0d42 316 function set_data($question) {
36703ed7 317 global $QTYPES;
fe6ce234
DC
318 // prepare question text
319 $draftid = file_get_submitted_draft_itemid('questiontext');
320
321 if (!empty($question->questiontext)) {
322 $questiontext = $question->questiontext;
323 } else {
324 $questiontext = '';
271ffe3f 325 }
41dcc2a5 326 $questiontext = file_prepare_draft_area($draftid, $this->context->id, 'question', 'questiontext', empty($question->id)?null:(int)$question->id, $this->fileoptions, $questiontext);
fe6ce234
DC
327
328 $question->questiontext = array();
329 $question->questiontext['text'] = $questiontext;
330 $question->questiontext['format'] = empty($question->questiontextformat) ? editors_get_preferred_format() : $question->questiontextformat;
331 $question->questiontext['itemid'] = $draftid;
332
333 // prepare general feedback
334 $draftid = file_get_submitted_draft_itemid('generalfeedback');
335
336 if (empty($question->generalfeedback)) {
337 $question->generalfeedback = '';
338 }
339
41dcc2a5 340 $feedback = file_prepare_draft_area($draftid, $this->context->id, 'question', 'generalfeedback', empty($question->id)?null:(int)$question->id, $this->fileoptions, $question->generalfeedback);
fe6ce234
DC
341 $question->generalfeedback = array();
342 $question->generalfeedback['text'] = $feedback;
343 $question->generalfeedback['format'] = empty($question->generalfeedbackformat) ? editors_get_preferred_format() : $question->generalfeedbackformat;
344 $question->generalfeedback['itemid'] = $draftid;
295043c2 345
9152f6a5 346 // Remove unnecessary trailing 0s form grade fields.
cfd24d98 347 if (isset($question->defaultgrade)) {
348 $question->defaultgrade = 0 + $question->defaultgrade;
349 }
350 if (isset($question->penalty)) {
351 $question->penalty = 0 + $question->penalty;
352 }
9152f6a5 353
295043c2 354 // Set any options.
355 $extra_question_fields = $QTYPES[$question->qtype]->extra_question_fields();
8e652f02 356 if (is_array($extra_question_fields) && !empty($question->options)) {
295043c2 357 array_shift($extra_question_fields);
358 foreach ($extra_question_fields as $field) {
b555ce76 359 if (isset($question->options->$field)) {
8e652f02 360 $question->$field = $question->options->$field;
361 }
295043c2 362 }
363 }
fe6ce234
DC
364 // subclass adds data_preprocessing code here
365 $question = $this->data_preprocessing($question);
32db0d42 366 parent::set_data($question);
36703ed7 367 }
1d284fbd 368
fe6ce234
DC
369 /**
370 * Any preprocessing needed for the settings form for the question type
371 *
372 * @param array $question - array to fill in with the default values
373 */
374 function data_preprocessing($question) {
375 return $question;
376 }
377
36703ed7 378 /**
379 * Override this in the subclass to question type name.
380 * @return the question type name, should be the same as the name() method in the question type class.
381 */
382 function qtype() {
383 return '';
384 }
385}