"REPOSITORY/MDL-13766, add thumbnail_alt, thumbnail_title and shorttitle parameters...
[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
c599a682 149 if (!empty($CFG->usetags)) {
150 $mform->addElement('header', 'tagsheader', get_string('tags'));
151 $mform->addElement('tags', 'tags', get_string('tags'));
152 }
153
271e6dec 154 if (!empty($this->question->id)){
155 $mform->addElement('header', 'createdmodifiedheader', get_string('createdmodifiedheader', 'question'));
156 $a = new object();
157 if (!empty($this->question->createdby)){
158 $a->time = userdate($this->question->timecreated);
f34488b2 159 $a->user = fullname($DB->get_record('user', array('id' => $this->question->createdby)));
271e6dec 160 } else {
161 $a->time = get_string('unknown', 'question');
162 $a->user = get_string('unknown', 'question');
163 }
164 $mform->addElement('static', 'created', get_string('created', 'question'), get_string('byandon', 'question', $a));
165 if (!empty($this->question->modifiedby)){
166 $a = new object();
167 $a->time = userdate($this->question->timemodified);
f34488b2 168 $a->user = fullname($DB->get_record('user', array('id' => $this->question->modifiedby)));
271e6dec 169 $mform->addElement('static', 'modified', get_string('modified', 'question'), get_string('byandon', 'question', $a));
170 }
171 }
172
36703ed7 173 // Standard fields at the end of the form.
174 $mform->addElement('hidden', 'id');
175 $mform->setType('id', PARAM_INT);
176
177 $mform->addElement('hidden', 'qtype');
178 $mform->setType('qtype', PARAM_ALPHA);
179
180 $mform->addElement('hidden', 'inpopup');
181 $mform->setType('inpopup', PARAM_INT);
182
183 $mform->addElement('hidden', 'versioning');
184 $mform->setType('versioning', PARAM_BOOL);
185
271e6dec 186 $mform->addElement('hidden', 'movecontext');
187 $mform->setType('movecontext', PARAM_BOOL);
188
9ab75b2b 189 $mform->addElement('hidden', 'cmid');
190 $mform->setType('cmid', PARAM_INT);
191 $mform->setDefault('cmid', 0);
192
271e6dec 193 $mform->addElement('hidden', 'courseid');
194 $mform->setType('courseid', PARAM_INT);
195 $mform->setDefault('courseid', 0);
196
7cd4fda6 197 $mform->addElement('hidden', 'returnurl');
198 $mform->setType('returnurl', PARAM_LOCALURL);
271e6dec 199 $mform->setDefault('returnurl', 0);
7cd4fda6 200
fa583f5f 201 $mform->addElement('hidden', 'appendqnumstring');
202 $mform->setType('appendqnumstring', PARAM_ALPHA);
203 $mform->setDefault('appendqnumstring', 0);
204
375ed78a 205 $buttonarray = array();
271e6dec 206 if (!empty($this->question->id)){
207 //editing / moving question
208 if ($this->question->formoptions->movecontext){
209 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('moveq', 'question'));
210 } elseif ($this->question->formoptions->canedit || $this->question->formoptions->canmove ||$this->question->formoptions->movecontext){
211 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges'));
212 }
213 if ($this->question->formoptions->cansaveasnew){
214 $buttonarray[] = &$mform->createElement('submit', 'makecopy', get_string('makecopy', 'quiz'));
215 }
216 $buttonarray[] = &$mform->createElement('cancel');
217 } else {
218 // adding new question
219 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges'));
220 $buttonarray[] = &$mform->createElement('cancel');
375ed78a 221 }
375ed78a 222 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
223 $mform->closeHeaderBefore('buttonar');
271e6dec 224
225 if ($this->question->formoptions->movecontext){
226 $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar'));
227 } elseif ((!empty($this->question->id)) && (!($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew))){
228 $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar', 'currentgrp'));
229 }
36703ed7 230 }
fa583f5f 231
3efbe6bc 232 function validation($fromform, $files) {
233 $errors= parent::validation($fromform, $files);
fa583f5f 234 if (empty($fromform->makecopy) && isset($this->question->id)
235 && ($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew)
3efbe6bc 236 && empty($fromform->usecurrentcat) && !$this->question->formoptions->canmove){
237 $errors['currentgrp'] = get_string('nopermissionmove', 'question');
238 }
239 return $errors;
240 }
1d284fbd 241
36703ed7 242 /**
243 * Add any question-type specific form fields.
1d284fbd 244 *
245 * @param object $mform the form being built.
36703ed7 246 */
247 function definition_inner(&$mform) {
248 // By default, do nothing.
249 }
1d284fbd 250
32db0d42 251 function set_data($question) {
36703ed7 252 global $QTYPES;
271ffe3f 253 if (empty($question->image)){
254 unset($question->image);
255 }
295043c2 256
9152f6a5 257 // Remove unnecessary trailing 0s form grade fields.
cfd24d98 258 if (isset($question->defaultgrade)) {
259 $question->defaultgrade = 0 + $question->defaultgrade;
260 }
261 if (isset($question->penalty)) {
262 $question->penalty = 0 + $question->penalty;
263 }
9152f6a5 264
295043c2 265 // Set any options.
266 $extra_question_fields = $QTYPES[$question->qtype]->extra_question_fields();
8e652f02 267 if (is_array($extra_question_fields) && !empty($question->options)) {
295043c2 268 array_shift($extra_question_fields);
269 foreach ($extra_question_fields as $field) {
8e652f02 270 if (!empty($question->options->$field)) {
271 $question->$field = $question->options->$field;
272 }
295043c2 273 }
274 }
8e652f02 275
32db0d42 276 parent::set_data($question);
36703ed7 277 }
1d284fbd 278
36703ed7 279 /**
280 * Override this in the subclass to question type name.
281 * @return the question type name, should be the same as the name() method in the question type class.
282 */
283 function qtype() {
284 return '';
285 }
286}
287
f34488b2 288?>