| [ Index ] |
PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008] |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * A base class for question editing forms. 4 * 5 * @copyright © 2006 The Open University 6 * @author T.J.Hunt@open.ac.uk 7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License 8 * @package questionbank 9 * @subpackage questiontypes 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. 17 * 18 * @package questionbank 19 * @subpackage questiontypes 20 */ 21 class question_edit_form extends moodleform { 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 25 * form in definition_inner but data is always loaded into the form with set_data. 26 * 27 * @var object 28 */ 29 var $question; 30 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 38 $this->question = $question; 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 50 } 51 52 /** 53 * Build the form definition. 54 * 55 * This adds all the form fields that the default question type supports. 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() { 60 global $COURSE, $CFG; 61 62 $qtype = $this->qtype(); 63 $langfile = "qtype_$qtype"; 64 65 $mform =& $this->_form; 66 67 // Standard fields at the start of the form. 68 $mform->addElement('header', 'generalheader', get_string("general", 'form')); 69 70 if (!isset($this->question->id)){ 71 //adding question 72 $mform->addElement('questioncategory', 'category', get_string('category', 'quiz'), 73 array('contexts' => $this->contexts->having_cap('moodle/question:add'))); 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 } 104 105 $mform->addElement('text', 'name', get_string('questionname', 'quiz'), array('size' => 50)); 106 $mform->setType('name', PARAM_TEXT); 107 $mform->addRule('name', null, 'required', null, 'client'); 108 109 $mform->addElement('htmleditor', 'questiontext', get_string('questiontext', 'quiz'), 110 array('rows' => 15, 'course' => $this->coursefilesid)); 111 $mform->setType('questiontext', PARAM_RAW); 112 $mform->setHelpButton('questiontext', array(array('questiontext', get_string('questiontext', 'quiz'), 'quiz'), 'richtext'), false, 'editorhelpbutton'); 113 $mform->addElement('format', 'questiontextformat', get_string('format')); 114 115 make_upload_directory($this->coursefilesid); // Just in case 116 $coursefiles = get_directory_list("$CFG->dataroot/$this->coursefilesid", $CFG->moddata); 117 foreach ($coursefiles as $filename) { 118 if (mimeinfo("icon", $filename) == "image.gif") { 119 $images["$filename"] = $filename; 120 } 121 } 122 if (empty($images)) { 123 $mform->addElement('static', 'image', get_string('imagedisplay', 'quiz'), get_string('noimagesyet')); 124 } else { 125 $mform->addElement('select', 'image', get_string('imagedisplay', 'quiz'), array_merge(array(''=>get_string('none')), $images)); 126 } 127 128 $mform->addElement('text', 'defaultgrade', get_string('defaultgrade', 'quiz'), 129 array('size' => 3)); 130 $mform->setType('defaultgrade', PARAM_INT); 131 $mform->setDefault('defaultgrade', 1); 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')); 139 $mform->setDefault('penalty', 0.1); 140 141 $mform->addElement('htmleditor', 'generalfeedback', get_string('generalfeedback', 'quiz'), 142 array('rows' => 10, 'course' => $this->coursefilesid)); 143 $mform->setType('generalfeedback', PARAM_RAW); 144 $mform->setHelpButton('generalfeedback', array('generalfeedback', get_string('generalfeedback', 'quiz'), 'quiz')); 145 146 // Any questiontype specific fields. 147 $this->definition_inner($mform); 148 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 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 181 $mform->addElement('hidden', 'movecontext'); 182 $mform->setType('movecontext', PARAM_BOOL); 183 184 $mform->addElement('hidden', 'cmid'); 185 $mform->setType('cmid', PARAM_INT); 186 $mform->setDefault('cmid', 0); 187 188 $mform->addElement('hidden', 'courseid'); 189 $mform->setType('courseid', PARAM_INT); 190 $mform->setDefault('courseid', 0); 191 192 $mform->addElement('hidden', 'returnurl'); 193 $mform->setType('returnurl', PARAM_LOCALURL); 194 $mform->setDefault('returnurl', 0); 195 196 $buttonarray = array(); 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'); 212 } 213 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false); 214 $mform->closeHeaderBefore('buttonar'); 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 } 221 } 222 223 function validation($fromform, $files) { 224 $errors= parent::validation($fromform, $files); 225 if (empty($fromform->makecopy) && isset($this->question->id) 226 && ($this->question->formoptions->canedit || $this->question->formoptions->cansaveasnew) 227 && empty($fromform->usecurrentcat) && !$this->question->formoptions->canmove){ 228 $errors['currentgrp'] = get_string('nopermissionmove', 'question'); 229 } 230 return $errors; 231 } 232 233 234 /** 235 * Add any question-type specific form fields. 236 * 237 * @param object $mform the form being built. 238 */ 239 function definition_inner(&$mform) { 240 // By default, do nothing. 241 } 242 243 function set_data($question) { 244 global $QTYPES; 245 $QTYPES[$question->qtype]->set_default_options($question); 246 if (empty($question->image)){ 247 unset($question->image); 248 } 249 250 // Set any options. 251 $extra_question_fields = $QTYPES[$question->qtype]->extra_question_fields(); 252 if (is_array($extra_question_fields) && !empty($question->options)) { 253 array_shift($extra_question_fields); 254 foreach ($extra_question_fields as $field) { 255 if (!empty($question->options->$field)) { 256 $question->$field = $question->options->$field; 257 } 258 } 259 } 260 261 parent::set_data($question); 262 } 263 264 /** 265 * Override this in the subclass to question type name. 266 * @return the question type name, should be the same as the name() method in the question type class. 267 */ 268 function qtype() { 269 return ''; 270 } 271 } 272 273 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Jan 14 11:33:29 2009 | Cross-referenced by PHPXref 0.7 |