merging MOODLE_19_QUESTIONS with HEAD
[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.
4323d029 17 *
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
271ffe3f 31 function question_edit_form($submiturl, $question){
32 $this->question = $question;
33 parent::moodleform($submiturl);
34 }
8e652f02 35
36703ed7 36 /**
37 * Build the form definition.
1d284fbd 38 *
295043c2 39 * This adds all the form fields that the default question type supports.
36703ed7 40 * If your question type does not support all these fields, then you can
41 * override this method and remove the ones you don't want with $mform->removeElement().
42 */
43 function definition() {
271ffe3f 44 global $COURSE, $CFG;
1d284fbd 45
36703ed7 46 $qtype = $this->qtype();
47 $langfile = "qtype_$qtype";
1d284fbd 48
36703ed7 49 $mform =& $this->_form;
36703ed7 50
51 // Standard fields at the start of the form.
271ffe3f 52 $mform->addElement('header', 'generalheader', get_string("general", 'form'));
1d284fbd 53
92186abc 54 $mform->addElement('questioncategory', 'category', get_string('category', 'quiz'), null,
36703ed7 55 array('courseid' => $COURSE->id, 'published' => true, 'only_editable' => true));
375ed78a 56
36703ed7 57 $mform->addElement('text', 'name', get_string('questionname', 'quiz'),
58 array('size' => 50));
271ffe3f 59 $mform->setType('name', PARAM_TEXT);
36703ed7 60 $mform->addRule('name', null, 'required', null, 'client');
1d284fbd 61
36703ed7 62 $mform->addElement('htmleditor', 'questiontext', get_string('questiontext', 'quiz'),
63 array('rows' => 15, 'course' => $COURSE->id));
64 $mform->setType('questiontext', PARAM_RAW);
271ffe3f 65 $mform->setHelpButton('questiontext', array(array('questiontext', get_string('questiontext', 'quiz'), 'quiz'), 'richtext'), false, 'editorhelpbutton');
36703ed7 66 $mform->addElement('format', 'questiontextformat', get_string('format'));
67
271ffe3f 68 make_upload_directory("$COURSE->id"); // Just in case
69 $coursefiles = get_directory_list("$CFG->dataroot/$COURSE->id", $CFG->moddata);
70 foreach ($coursefiles as $filename) {
71 if (mimeinfo("icon", $filename) == "image.gif") {
72 $images["$filename"] = $filename;
73 }
74 }
36703ed7 75 if (empty($images)) {
76 $mform->addElement('static', 'image', get_string('imagedisplay', 'quiz'), get_string('noimagesyet'));
77 } else {
9836d9c8 78 $mform->addElement('select', 'image', get_string('imagedisplay', 'quiz'), array_merge(array(''=>get_string('none')), $images));
36703ed7 79 }
1d284fbd 80
36703ed7 81 $mform->addElement('text', 'defaultgrade', get_string('defaultgrade', 'quiz'),
82 array('size' => 3));
83 $mform->setType('defaultgrade', PARAM_INT);
92186abc 84 $mform->setDefault('defaultgrade', 1);
36703ed7 85 $mform->addRule('defaultgrade', null, 'required', null, 'client');
86
87 $mform->addElement('text', 'penalty', get_string('penaltyfactor', 'quiz'),
88 array('size' => 3));
89 $mform->setType('penalty', PARAM_NUMBER);
90 $mform->addRule('penalty', null, 'required', null, 'client');
91 $mform->setHelpButton('penalty', array('penalty', get_string('penalty', 'quiz'), 'quiz'));
92186abc 92 $mform->setDefault('penalty', 0.1);
36703ed7 93
94 $mform->addElement('htmleditor', 'generalfeedback', get_string('generalfeedback', 'quiz'),
95 array('rows' => 10, 'course' => $COURSE->id));
96 $mform->setType('generalfeedback', PARAM_RAW);
97 $mform->setHelpButton('generalfeedback', array('generalfeedback', get_string('generalfeedback', 'quiz'), 'quiz'));
1d284fbd 98
36703ed7 99 // Any questiontype specific fields.
100 $this->definition_inner($mform);
101
102 // Standard fields at the end of the form.
103 $mform->addElement('hidden', 'id');
104 $mform->setType('id', PARAM_INT);
105
106 $mform->addElement('hidden', 'qtype');
107 $mform->setType('qtype', PARAM_ALPHA);
108
109 $mform->addElement('hidden', 'inpopup');
110 $mform->setType('inpopup', PARAM_INT);
111
112 $mform->addElement('hidden', 'versioning');
113 $mform->setType('versioning', PARAM_BOOL);
114
9ab75b2b 115 $mform->addElement('hidden', 'cmid');
116 $mform->setType('cmid', PARAM_INT);
117 $mform->setDefault('cmid', 0);
118
7cd4fda6 119 $mform->addElement('hidden', 'returnurl');
120 $mform->setType('returnurl', PARAM_LOCALURL);
121 $mform->setDefault('returnurl', '');
122
375ed78a 123 $buttonarray = array();
124 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges'));
125 if (!empty($this->question->id)) {
126 $buttonarray[] = &$mform->createElement('submit', 'makecopy', get_string('makecopy', 'quiz'));
127 }
128 $buttonarray[] = &$mform->createElement('cancel');
129 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
130 $mform->closeHeaderBefore('buttonar');
36703ed7 131 }
1d284fbd 132
36703ed7 133 /**
134 * Add any question-type specific form fields.
1d284fbd 135 *
136 * @param object $mform the form being built.
36703ed7 137 */
138 function definition_inner(&$mform) {
139 // By default, do nothing.
140 }
1d284fbd 141
32db0d42 142 function set_data($question) {
36703ed7 143 global $QTYPES;
92186abc 144 $QTYPES[$question->qtype]->set_default_options($question);
271ffe3f 145 if (empty($question->image)){
146 unset($question->image);
147 }
295043c2 148
149 // Set any options.
150 $extra_question_fields = $QTYPES[$question->qtype]->extra_question_fields();
8e652f02 151 if (is_array($extra_question_fields) && !empty($question->options)) {
295043c2 152 array_shift($extra_question_fields);
153 foreach ($extra_question_fields as $field) {
8e652f02 154 if (!empty($question->options->$field)) {
155 $question->$field = $question->options->$field;
156 }
295043c2 157 }
158 }
8e652f02 159
32db0d42 160 parent::set_data($question);
36703ed7 161 }
1d284fbd 162
36703ed7 163 /**
164 * Override this in the subclass to question type name.
165 * @return the question type name, should be the same as the name() method in the question type class.
166 */
167 function qtype() {
168 return '';
169 }
170}
171
172?>