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