formslib / tags: MDL-17889 debugging message if you try to create a tags field when...
[moodle.git] / question / type / edit_question_form.php
CommitLineData
36703ed7 1<?php
2/**
3 * A base class for question editing forms.
4 *
5 * @copyright &copy; 2006 The Open University
6 * @author T.J.Hunt@open.ac.uk
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
4323d029 8 * @package questionbank
9 * @subpackage questiontypes
36703ed7 10 *//** */
11
12/**
13 * Form definition base class. This defines the common fields that
14 * all question types need. Question types should define their own
15 * class that inherits from this one, and implements the definition_inner()
16 * method.
271e6dec 17 *
4323d029 18 * @package questionbank
19 * @subpackage questiontypes
36703ed7 20 */
1d284fbd 21class question_edit_form extends moodleform {
271ffe3f 22 /**
23 * Question object with options and answers already loaded by get_question_options
24 * Be careful how you use this it is needed sometimes to set up the structure of the
32db0d42 25 * form in definition_inner but data is always loaded into the form with set_data.
271ffe3f 26 *
27 * @var object
28 */
f34488b2 29 public $question;
92186abc 30
f34488b2 31 public $contexts;
32 public $category;
33 public $categorycontext;
34 public $coursefilesid;
271e6dec 35
36 function question_edit_form($submiturl, $question, $category, $contexts, $formeditable = true){
37
271ffe3f 38 $this->question = $question;
271e6dec 39
40 $this->contexts = $contexts;
41
42 $this->category = $category;
43 $this->categorycontext = get_context_instance_by_id($category->contextid);
44
45 //course id or site id depending on question cat context
46 $this->coursefilesid = get_filesdir_from_context(get_context_instance_by_id($category->contextid));
47
48 parent::moodleform($submiturl, null, 'post', '', null, $formeditable);
49
271ffe3f 50 }
8e652f02 51
36703ed7 52 /**
53 * Build the form definition.
1d284fbd 54 *
295043c2 55 * This adds all the form fields that the default question type supports.
36703ed7 56 * If your question type does not support all these fields, then you can
57 * override this method and remove the ones you don't want with $mform->removeElement().
58 */
59 function definition() {
f34488b2 60 global $COURSE, $CFG, $DB;
1d284fbd 61
36703ed7 62 $qtype = $this->qtype();
63 $langfile = "qtype_$qtype";
1d284fbd 64
36703ed7 65 $mform =& $this->_form;
36703ed7 66
67 // Standard fields at the start of the form.
271ffe3f 68 $mform->addElement('header', 'generalheader', get_string("general", 'form'));
1d284fbd 69
271e6dec 70 if (!isset($this->question->id)){
71 //adding question
72 $mform->addElement('questioncategory', 'category', get_string('category', 'quiz'),
522b89d1 73 array('contexts' => $this->contexts->having_cap('moodle/question:add')));
271e6dec 74 } elseif (!($this->question->formoptions->canmove || $this->question->formoptions->cansaveasnew)){
75 //editing question with no permission to move from category.
76 $mform->addElement('questioncategory', 'category', get_string('category', 'quiz'),
77 array('contexts' => array($this->categorycontext)));
78 } elseif ($this->question->formoptions->movecontext){
79 //moving question to another context.
80 $mform->addElement('questioncategory', 'categorymoveto', get_string('category', 'quiz'),
81 array('contexts' => $this->contexts->having_cap('moodle/question:add')));
82
83 } else {
84 //editing question with permission to move from category or save as new q
85 $currentgrp = array();
86 $currentgrp[0] =& $mform->createElement('questioncategory', 'category', get_string('categorycurrent', 'question'),
87 array('contexts' => array($this->categorycontext)));
88 if ($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew){
89 //not move only form
90 $currentgrp[1] =& $mform->createElement('checkbox', 'usecurrentcat', '', get_string('categorycurrentuse', 'question'));
91 $mform->setDefault('usecurrentcat', 1);
92 }
93 $currentgrp[0]->freeze();
94 $currentgrp[0]->setPersistantFreeze(false);
95 $mform->addGroup($currentgrp, 'currentgrp', get_string('categorycurrent', 'question'), null, false);
96
97 $mform->addElement('questioncategory', 'categorymoveto', get_string('categorymoveto', 'question'),
98 array('contexts' => array($this->categorycontext)));
99 if ($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew){
100 //not move only form
101 $mform->disabledIf('categorymoveto', 'usecurrentcat', 'checked');
102 }
103 }
375ed78a 104
271e6dec 105 $mform->addElement('text', 'name', get_string('questionname', 'quiz'), array('size' => 50));
271ffe3f 106 $mform->setType('name', PARAM_TEXT);
36703ed7 107 $mform->addRule('name', null, 'required', null, 'client');
1d284fbd 108
36703ed7 109 $mform->addElement('htmleditor', 'questiontext', get_string('questiontext', 'quiz'),
271e6dec 110 array('rows' => 15, 'course' => $this->coursefilesid));
36703ed7 111 $mform->setType('questiontext', PARAM_RAW);
16851b22 112 $mform->setHelpButton('questiontext', array(array('questiontext', get_string('questiontext', 'quiz'), 'quiz'), 'richtext2'), false, 'editorhelpbutton');
36703ed7 113 $mform->addElement('format', 'questiontextformat', get_string('format'));
114
271e6dec 115 make_upload_directory($this->coursefilesid); // Just in case
116 $coursefiles = get_directory_list("$CFG->dataroot/$this->coursefilesid", $CFG->moddata);
271ffe3f 117 foreach ($coursefiles as $filename) {
118 if (mimeinfo("icon", $filename) == "image.gif") {
119 $images["$filename"] = $filename;
120 }
121 }
36703ed7 122 if (empty($images)) {
123 $mform->addElement('static', 'image', get_string('imagedisplay', 'quiz'), get_string('noimagesyet'));
124 } else {
9836d9c8 125 $mform->addElement('select', 'image', get_string('imagedisplay', 'quiz'), array_merge(array(''=>get_string('none')), $images));
36703ed7 126 }
1d284fbd 127
36703ed7 128 $mform->addElement('text', 'defaultgrade', get_string('defaultgrade', 'quiz'),
129 array('size' => 3));
130 $mform->setType('defaultgrade', PARAM_INT);
92186abc 131 $mform->setDefault('defaultgrade', 1);
36703ed7 132 $mform->addRule('defaultgrade', null, 'required', null, 'client');
133
134 $mform->addElement('text', 'penalty', get_string('penaltyfactor', 'quiz'),
135 array('size' => 3));
136 $mform->setType('penalty', PARAM_NUMBER);
137 $mform->addRule('penalty', null, 'required', null, 'client');
138 $mform->setHelpButton('penalty', array('penalty', get_string('penalty', 'quiz'), 'quiz'));
92186abc 139 $mform->setDefault('penalty', 0.1);
36703ed7 140
141 $mform->addElement('htmleditor', 'generalfeedback', get_string('generalfeedback', 'quiz'),
271e6dec 142 array('rows' => 10, 'course' => $this->coursefilesid));
36703ed7 143 $mform->setType('generalfeedback', PARAM_RAW);
144 $mform->setHelpButton('generalfeedback', array('generalfeedback', get_string('generalfeedback', 'quiz'), 'quiz'));
1d284fbd 145
36703ed7 146 // Any questiontype specific fields.
147 $this->definition_inner($mform);
148
271e6dec 149 if (!empty($this->question->id)){
150 $mform->addElement('header', 'createdmodifiedheader', get_string('createdmodifiedheader', 'question'));
151 $a = new object();
152 if (!empty($this->question->createdby)){
153 $a->time = userdate($this->question->timecreated);
f34488b2 154 $a->user = fullname($DB->get_record('user', array('id' => $this->question->createdby)));
271e6dec 155 } else {
156 $a->time = get_string('unknown', 'question');
157 $a->user = get_string('unknown', 'question');
158 }
159 $mform->addElement('static', 'created', get_string('created', 'question'), get_string('byandon', 'question', $a));
160 if (!empty($this->question->modifiedby)){
161 $a = new object();
162 $a->time = userdate($this->question->timemodified);
f34488b2 163 $a->user = fullname($DB->get_record('user', array('id' => $this->question->modifiedby)));
271e6dec 164 $mform->addElement('static', 'modified', get_string('modified', 'question'), get_string('byandon', 'question', $a));
165 }
166 }
167
36703ed7 168 // Standard fields at the end of the form.
169 $mform->addElement('hidden', 'id');
170 $mform->setType('id', PARAM_INT);
171
172 $mform->addElement('hidden', 'qtype');
173 $mform->setType('qtype', PARAM_ALPHA);
174
175 $mform->addElement('hidden', 'inpopup');
176 $mform->setType('inpopup', PARAM_INT);
177
178 $mform->addElement('hidden', 'versioning');
179 $mform->setType('versioning', PARAM_BOOL);
180
271e6dec 181 $mform->addElement('hidden', 'movecontext');
182 $mform->setType('movecontext', PARAM_BOOL);
183
9ab75b2b 184 $mform->addElement('hidden', 'cmid');
185 $mform->setType('cmid', PARAM_INT);
186 $mform->setDefault('cmid', 0);
187
271e6dec 188 $mform->addElement('hidden', 'courseid');
189 $mform->setType('courseid', PARAM_INT);
190 $mform->setDefault('courseid', 0);
191
7cd4fda6 192 $mform->addElement('hidden', 'returnurl');
193 $mform->setType('returnurl', PARAM_LOCALURL);
271e6dec 194 $mform->setDefault('returnurl', 0);
7cd4fda6 195
fa583f5f 196 $mform->addElement('hidden', 'appendqnumstring');
197 $mform->setType('appendqnumstring', PARAM_ALPHA);
198 $mform->setDefault('appendqnumstring', 0);
199
375ed78a 200 $buttonarray = array();
271e6dec 201 if (!empty($this->question->id)){
202 //editing / moving question
203 if ($this->question->formoptions->movecontext){
204 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('moveq', 'question'));
205 } elseif ($this->question->formoptions->canedit || $this->question->formoptions->canmove ||$this->question->formoptions->movecontext){
206 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges'));
207 }
208 if ($this->question->formoptions->cansaveasnew){
209 $buttonarray[] = &$mform->createElement('submit', 'makecopy', get_string('makecopy', 'quiz'));
210 }
211 $buttonarray[] = &$mform->createElement('cancel');
212 } else {
213 // adding new question
214 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges'));
215 $buttonarray[] = &$mform->createElement('cancel');
375ed78a 216 }
375ed78a 217 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
218 $mform->closeHeaderBefore('buttonar');
271e6dec 219
220 if ($this->question->formoptions->movecontext){
221 $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar'));
222 } elseif ((!empty($this->question->id)) && (!($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew))){
223 $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar', 'currentgrp'));
224 }
36703ed7 225 }
fa583f5f 226
3efbe6bc 227 function validation($fromform, $files) {
228 $errors= parent::validation($fromform, $files);
fa583f5f 229 if (empty($fromform->makecopy) && isset($this->question->id)
230 && ($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew)
3efbe6bc 231 && empty($fromform->usecurrentcat) && !$this->question->formoptions->canmove){
232 $errors['currentgrp'] = get_string('nopermissionmove', 'question');
233 }
234 return $errors;
235 }
1d284fbd 236
36703ed7 237 /**
238 * Add any question-type specific form fields.
1d284fbd 239 *
240 * @param object $mform the form being built.
36703ed7 241 */
242 function definition_inner(&$mform) {
243 // By default, do nothing.
244 }
1d284fbd 245
32db0d42 246 function set_data($question) {
36703ed7 247 global $QTYPES;
271ffe3f 248 if (empty($question->image)){
249 unset($question->image);
250 }
295043c2 251
9152f6a5 252 // Remove unnecessary trailing 0s form grade fields.
cfd24d98 253 if (isset($question->defaultgrade)) {
254 $question->defaultgrade = 0 + $question->defaultgrade;
255 }
256 if (isset($question->penalty)) {
257 $question->penalty = 0 + $question->penalty;
258 }
9152f6a5 259
295043c2 260 // Set any options.
261 $extra_question_fields = $QTYPES[$question->qtype]->extra_question_fields();
8e652f02 262 if (is_array($extra_question_fields) && !empty($question->options)) {
295043c2 263 array_shift($extra_question_fields);
264 foreach ($extra_question_fields as $field) {
8e652f02 265 if (!empty($question->options->$field)) {
266 $question->$field = $question->options->$field;
267 }
295043c2 268 }
269 }
8e652f02 270
32db0d42 271 parent::set_data($question);
36703ed7 272 }
1d284fbd 273
36703ed7 274 /**
275 * Override this in the subclass to question type name.
276 * @return the question type name, should be the same as the name() method in the question type class.
277 */
278 function qtype() {
279 return '';
280 }
281}
282
f34488b2 283?>