MDL-15094 fixed stats
[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 */
29 var $question;
92186abc 30
271e6dec 31 var $contexts;
32 var $category;
33 var $categorycontext;
34 var $coursefilesid;
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() {
271ffe3f 60 global $COURSE, $CFG;
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);
271ffe3f 112 $mform->setHelpButton('questiontext', array(array('questiontext', get_string('questiontext', 'quiz'), 'quiz'), 'richtext'), 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);
154 $a->user = fullname(get_record('user', 'id', $this->question->createdby));
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);
163 $a->user = fullname(get_record('user', 'id', $this->question->modifiedby));
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
375ed78a 196 $buttonarray = array();
271e6dec 197 if (!empty($this->question->id)){
198 //editing / moving question
199 if ($this->question->formoptions->movecontext){
200 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('moveq', 'question'));
201 } elseif ($this->question->formoptions->canedit || $this->question->formoptions->canmove ||$this->question->formoptions->movecontext){
202 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges'));
203 }
204 if ($this->question->formoptions->cansaveasnew){
205 $buttonarray[] = &$mform->createElement('submit', 'makecopy', get_string('makecopy', 'quiz'));
206 }
207 $buttonarray[] = &$mform->createElement('cancel');
208 } else {
209 // adding new question
210 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges'));
211 $buttonarray[] = &$mform->createElement('cancel');
375ed78a 212 }
375ed78a 213 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
214 $mform->closeHeaderBefore('buttonar');
271e6dec 215
216 if ($this->question->formoptions->movecontext){
217 $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar'));
218 } elseif ((!empty($this->question->id)) && (!($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew))){
219 $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar', 'currentgrp'));
220 }
36703ed7 221 }
1d284fbd 222
36703ed7 223 /**
224 * Add any question-type specific form fields.
1d284fbd 225 *
226 * @param object $mform the form being built.
36703ed7 227 */
228 function definition_inner(&$mform) {
229 // By default, do nothing.
230 }
1d284fbd 231
32db0d42 232 function set_data($question) {
36703ed7 233 global $QTYPES;
92186abc 234 $QTYPES[$question->qtype]->set_default_options($question);
271ffe3f 235 if (empty($question->image)){
236 unset($question->image);
237 }
295043c2 238
239 // Set any options.
240 $extra_question_fields = $QTYPES[$question->qtype]->extra_question_fields();
8e652f02 241 if (is_array($extra_question_fields) && !empty($question->options)) {
295043c2 242 array_shift($extra_question_fields);
243 foreach ($extra_question_fields as $field) {
8e652f02 244 if (!empty($question->options->$field)) {
245 $question->$field = $question->options->$field;
246 }
295043c2 247 }
248 }
8e652f02 249
32db0d42 250 parent::set_data($question);
36703ed7 251 }
1d284fbd 252
36703ed7 253 /**
254 * Override this in the subclass to question type name.
255 * @return the question type name, should be the same as the name() method in the question type class.
256 */
257 function qtype() {
258 return '';
259 }
260}
261
262?>