curl now mandatory
[moodle.git] / question / type / edit_question_form.php
CommitLineData
aeb15530 1<?php
36703ed7 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
2e69d930 109 //TODO: MDL-16094 convert to new editor element
36703ed7 110 $mform->addElement('htmleditor', 'questiontext', get_string('questiontext', 'quiz'),
271e6dec 111 array('rows' => 15, 'course' => $this->coursefilesid));
36703ed7 112 $mform->setType('questiontext', PARAM_RAW);
2e69d930
PS
113 //$mform->addElement('format', 'questiontextformat', get_string('format'));
114 $mform->addElement('hidden', 'questiontextformat');
115 $mform->setType('questiontextformat', PARAM_INT);
36703ed7 116
271e6dec 117 make_upload_directory($this->coursefilesid); // Just in case
118 $coursefiles = get_directory_list("$CFG->dataroot/$this->coursefilesid", $CFG->moddata);
271ffe3f 119 foreach ($coursefiles as $filename) {
120 if (mimeinfo("icon", $filename) == "image.gif") {
121 $images["$filename"] = $filename;
122 }
123 }
36703ed7 124 if (empty($images)) {
125 $mform->addElement('static', 'image', get_string('imagedisplay', 'quiz'), get_string('noimagesyet'));
126 } else {
9836d9c8 127 $mform->addElement('select', 'image', get_string('imagedisplay', 'quiz'), array_merge(array(''=>get_string('none')), $images));
36703ed7 128 }
1d284fbd 129
36703ed7 130 $mform->addElement('text', 'defaultgrade', get_string('defaultgrade', 'quiz'),
131 array('size' => 3));
132 $mform->setType('defaultgrade', PARAM_INT);
92186abc 133 $mform->setDefault('defaultgrade', 1);
36703ed7 134 $mform->addRule('defaultgrade', null, 'required', null, 'client');
135
7292c11f 136 $mform->addElement('text', 'penalty', get_string('penaltyfactor', 'question'),
36703ed7 137 array('size' => 3));
138 $mform->setType('penalty', PARAM_NUMBER);
139 $mform->addRule('penalty', null, 'required', null, 'client');
7292c11f 140 $mform->addHelpButton('penalty', 'penaltyfactor', 'question');
92186abc 141 $mform->setDefault('penalty', 0.1);
36703ed7 142
143 $mform->addElement('htmleditor', 'generalfeedback', get_string('generalfeedback', 'quiz'),
271e6dec 144 array('rows' => 10, 'course' => $this->coursefilesid));
36703ed7 145 $mform->setType('generalfeedback', PARAM_RAW);
5fcefc97 146 $mform->addHelpButton('generalfeedback', 'generalfeedback', 'quiz');
1d284fbd 147
36703ed7 148 // Any questiontype specific fields.
149 $this->definition_inner($mform);
150
c599a682 151 if (!empty($CFG->usetags)) {
152 $mform->addElement('header', 'tagsheader', get_string('tags'));
153 $mform->addElement('tags', 'tags', get_string('tags'));
154 }
155
271e6dec 156 if (!empty($this->question->id)){
157 $mform->addElement('header', 'createdmodifiedheader', get_string('createdmodifiedheader', 'question'));
158 $a = new object();
159 if (!empty($this->question->createdby)){
160 $a->time = userdate($this->question->timecreated);
f34488b2 161 $a->user = fullname($DB->get_record('user', array('id' => $this->question->createdby)));
271e6dec 162 } else {
163 $a->time = get_string('unknown', 'question');
164 $a->user = get_string('unknown', 'question');
165 }
166 $mform->addElement('static', 'created', get_string('created', 'question'), get_string('byandon', 'question', $a));
167 if (!empty($this->question->modifiedby)){
168 $a = new object();
169 $a->time = userdate($this->question->timemodified);
f34488b2 170 $a->user = fullname($DB->get_record('user', array('id' => $this->question->modifiedby)));
271e6dec 171 $mform->addElement('static', 'modified', get_string('modified', 'question'), get_string('byandon', 'question', $a));
172 }
173 }
174
36703ed7 175 // Standard fields at the end of the form.
176 $mform->addElement('hidden', 'id');
177 $mform->setType('id', PARAM_INT);
178
179 $mform->addElement('hidden', 'qtype');
180 $mform->setType('qtype', PARAM_ALPHA);
181
182 $mform->addElement('hidden', 'inpopup');
183 $mform->setType('inpopup', PARAM_INT);
184
185 $mform->addElement('hidden', 'versioning');
186 $mform->setType('versioning', PARAM_BOOL);
187
271e6dec 188 $mform->addElement('hidden', 'movecontext');
189 $mform->setType('movecontext', PARAM_BOOL);
190
9ab75b2b 191 $mform->addElement('hidden', 'cmid');
192 $mform->setType('cmid', PARAM_INT);
193 $mform->setDefault('cmid', 0);
194
271e6dec 195 $mform->addElement('hidden', 'courseid');
196 $mform->setType('courseid', PARAM_INT);
197 $mform->setDefault('courseid', 0);
198
7cd4fda6 199 $mform->addElement('hidden', 'returnurl');
200 $mform->setType('returnurl', PARAM_LOCALURL);
271e6dec 201 $mform->setDefault('returnurl', 0);
7cd4fda6 202
fa583f5f 203 $mform->addElement('hidden', 'appendqnumstring');
204 $mform->setType('appendqnumstring', PARAM_ALPHA);
205 $mform->setDefault('appendqnumstring', 0);
206
375ed78a 207 $buttonarray = array();
271e6dec 208 if (!empty($this->question->id)){
209 //editing / moving question
210 if ($this->question->formoptions->movecontext){
211 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('moveq', 'question'));
212 } elseif ($this->question->formoptions->canedit || $this->question->formoptions->canmove ||$this->question->formoptions->movecontext){
213 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges'));
214 }
215 if ($this->question->formoptions->cansaveasnew){
216 $buttonarray[] = &$mform->createElement('submit', 'makecopy', get_string('makecopy', 'quiz'));
217 }
218 $buttonarray[] = &$mform->createElement('cancel');
219 } else {
220 // adding new question
221 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges'));
222 $buttonarray[] = &$mform->createElement('cancel');
375ed78a 223 }
375ed78a 224 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
225 $mform->closeHeaderBefore('buttonar');
271e6dec 226
227 if ($this->question->formoptions->movecontext){
228 $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar'));
229 } elseif ((!empty($this->question->id)) && (!($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew))){
230 $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar', 'currentgrp'));
231 }
36703ed7 232 }
fa583f5f 233
3efbe6bc 234 function validation($fromform, $files) {
235 $errors= parent::validation($fromform, $files);
fa583f5f 236 if (empty($fromform->makecopy) && isset($this->question->id)
237 && ($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew)
3efbe6bc 238 && empty($fromform->usecurrentcat) && !$this->question->formoptions->canmove){
239 $errors['currentgrp'] = get_string('nopermissionmove', 'question');
240 }
241 return $errors;
242 }
1d284fbd 243
36703ed7 244 /**
245 * Add any question-type specific form fields.
1d284fbd 246 *
247 * @param object $mform the form being built.
36703ed7 248 */
249 function definition_inner(&$mform) {
250 // By default, do nothing.
251 }
1d284fbd 252
2aef1fe5 253 /**
254 * Get the list of form elements to repeat, one for each answer.
255 * @param object $mform the form being built.
256 * @param $label the label to use for each option.
257 * @param $gradeoptions the possible grades for each answer.
258 * @param $repeatedoptions reference to array of repeated options to fill
259 * @param $answersoption reference to return the name of $question->options field holding an array of answers
260 * @return array of form fields.
261 */
262 function get_per_answer_fields(&$mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
263 $repeated = array();
264 $repeated[] =& $mform->createElement('header', 'answerhdr', $label);
265 $repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'), array('size' => 50));
266 $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
267 $repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'),
268 array('course' => $this->coursefilesid));
269 $repeatedoptions['answer']['type'] = PARAM_RAW;
270 $repeatedoptions['fraction']['default'] = 0;
271 $answersoption = 'answers';
272 return $repeated;
273 }
274
275 /**
276 * Add a set of form fields, obtained from get_per_answer_fields, to the form,
277 * one for each existing answer, with some blanks for some new ones.
278 * @param object $mform the form being built.
279 * @param $label the label to use for each option.
280 * @param $gradeoptions the possible grades for each answer.
281 * @param $minoptions the minimum number of answer blanks to display. Default QUESTION_NUMANS_START.
282 * @param $addoptions the number of answer blanks to add. Default QUESTION_NUMANS_ADD.
283 */
284 function add_per_answer_fields(&$mform, $label, $gradeoptions, $minoptions = QUESTION_NUMANS_START, $addoptions = QUESTION_NUMANS_ADD) {
285 $answersoption = '';
286 $repeatedoptions = array();
287 $repeated = $this->get_per_answer_fields($mform, $label, $gradeoptions, $repeatedoptions, $answersoption);
288
289 if (isset($this->question->options)){
290 $countanswers = count($this->question->options->$answersoption);
291 } else {
292 $countanswers = 0;
293 }
294 if ($this->question->formoptions->repeatelements){
295 $repeatsatstart = max($minoptions, $countanswers + $addoptions);
296 } else {
297 $repeatsatstart = $countanswers;
298 }
299
300 $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', $addoptions, get_string('addmorechoiceblanks', 'qtype_multichoice'));
301 }
302
32db0d42 303 function set_data($question) {
36703ed7 304 global $QTYPES;
271ffe3f 305 if (empty($question->image)){
306 unset($question->image);
307 }
295043c2 308
9152f6a5 309 // Remove unnecessary trailing 0s form grade fields.
cfd24d98 310 if (isset($question->defaultgrade)) {
311 $question->defaultgrade = 0 + $question->defaultgrade;
312 }
313 if (isset($question->penalty)) {
314 $question->penalty = 0 + $question->penalty;
315 }
9152f6a5 316
295043c2 317 // Set any options.
318 $extra_question_fields = $QTYPES[$question->qtype]->extra_question_fields();
8e652f02 319 if (is_array($extra_question_fields) && !empty($question->options)) {
295043c2 320 array_shift($extra_question_fields);
321 foreach ($extra_question_fields as $field) {
8e652f02 322 if (!empty($question->options->$field)) {
323 $question->$field = $question->options->$field;
324 }
295043c2 325 }
326 }
8e652f02 327
32db0d42 328 parent::set_data($question);
36703ed7 329 }
1d284fbd 330
36703ed7 331 /**
332 * Override this in the subclass to question type name.
333 * @return the question type name, should be the same as the name() method in the question type class.
334 */
335 function qtype() {
336 return '';
337 }
338}
339
aeb15530 340