| [ Index ] |
PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008] |
[Summary view] [Print] [Text view]
1 <?php 2 require_once ($CFG->libdir.'/formslib.php'); 3 /** 4 * This class adds extra methods to form wrapper specific to be used for module 5 * add / update forms (mod/{modname}.mod_form.php replaces deprecated mod/{modname}/mod.html 6 * 7 */ 8 class moodleform_mod extends moodleform { 9 /** 10 * Instance of the module that is being updated. This is the id of the {prefix}{modulename} 11 * record. Can be used in form definition. Will be "" if this is an 'add' form and not an 12 * update one. 13 * 14 * @var mixed 15 */ 16 var $_instance; 17 /** 18 * Section of course that module instance will be put in or is in. 19 * This is always the section number itself (column 'section' from 'course_sections' table). 20 * 21 * @var mixed 22 */ 23 var $_section; 24 /** 25 * Coursemodle record of the module that is being updated. Will be null if this is an 'add' form and not an 26 * update one. 27 * 28 * @var mixed 29 */ 30 var $_cm; 31 /** 32 * List of modform features 33 */ 34 var $_features; 35 36 function moodleform_mod($instance, $section, $cm) { 37 $this->_instance = $instance; 38 $this->_section = $section; 39 $this->_cm = $cm; 40 parent::moodleform('modedit.php'); 41 } 42 43 /** 44 * Only available on moodleform_mod. 45 * 46 * @param array $default_values passed by reference 47 */ 48 function data_preprocessing(&$default_values){ 49 } 50 51 /** 52 * Each module which defines definition_after_data() must call this method using parent::definition_after_data(); 53 */ 54 function definition_after_data() { 55 global $CFG, $COURSE; 56 $mform =& $this->_form; 57 58 if ($id = $mform->getElementValue('update')) { 59 $modulename = $mform->getElementValue('modulename'); 60 $instance = $mform->getElementValue('instance'); 61 62 if ($this->_features->gradecat) { 63 $gradecat = false; 64 if (!empty($CFG->enableoutcomes) and $this->_features->outcomes) { 65 if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) { 66 $gradecat = true; 67 } 68 } 69 if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$modulename, 70 'iteminstance'=>$instance, 'courseid'=>$COURSE->id))) { 71 foreach ($items as $item) { 72 if (!empty($item->outcomeid)) { 73 $elname = 'outcome_'.$item->outcomeid; 74 if ($mform->elementExists($elname)) { 75 $mform->hardFreeze($elname); // prevent removing of existing outcomes 76 } 77 } 78 } 79 foreach ($items as $item) { 80 if (is_bool($gradecat)) { 81 $gradecat = $item->categoryid; 82 continue; 83 } 84 if ($gradecat != $item->categoryid) { 85 //mixed categories 86 $gradecat = false; 87 break; 88 } 89 } 90 } 91 92 if ($gradecat === false) { 93 // items and outcomes in different categories - remove the option 94 // TODO: it might be better to add a "Mixed categories" text instead 95 if ($mform->elementExists('gradecat')) { 96 $mform->removeElement('gradecat'); 97 } 98 } 99 } 100 } 101 102 if ($COURSE->groupmodeforce) { 103 if ($mform->elementExists('groupmode')) { 104 $mform->hardFreeze('groupmode'); // groupmode can not be changed if forced from course settings 105 } 106 } 107 108 if ($mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly') and empty($COURSE->groupmodeforce)) { 109 $mform->disabledIf('groupingid', 'groupmode', 'eq', NOGROUPS); 110 111 } else if (!$mform->elementExists('groupmode') and $mform->elementExists('groupmembersonly')) { 112 $mform->disabledIf('groupingid', 'groupmembersonly', 'notchecked'); 113 114 } else if (!$mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly')) { 115 // groupings have no use without groupmode or groupmembersonly 116 if ($mform->elementExists('groupingid')) { 117 $mform->removeElement('groupingid'); 118 } 119 } 120 } 121 122 // form verification 123 function validation($data, $files) { 124 global $COURSE; 125 $errors = parent::validation($data, $files); 126 127 $mform =& $this->_form; 128 129 $errors = array(); 130 131 if ($mform->elementExists('name')) { 132 $name = trim($data['name']); 133 if ($name == '') { 134 $errors['name'] = get_string('required'); 135 } 136 } 137 138 $grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$data['modulename'], 139 'iteminstance'=>$data['instance'], 'itemnumber'=>0, 'courseid'=>$COURSE->id)); 140 if ($data['coursemodule']) { 141 $cm = get_record('course_modules', 'id', $data['coursemodule']); 142 } else { 143 $cm = null; 144 } 145 146 if ($mform->elementExists('cmidnumber')) { 147 // verify the idnumber 148 if (!grade_verify_idnumber($data['cmidnumber'], $COURSE->id, $grade_item, $cm)) { 149 $errors['cmidnumber'] = get_string('idnumbertaken'); 150 } 151 } 152 153 return $errors; 154 } 155 156 /** 157 * Load in existing data as form defaults. Usually new entry defaults are stored directly in 158 * form definition (new entry form); this function is used to load in data where values 159 * already exist and data is being edited (edit entry form). 160 * 161 * @param mixed $default_values object or array of default values 162 */ 163 function set_data($default_values) { 164 if (is_object($default_values)) { 165 $default_values = (array)$default_values; 166 } 167 $this->data_preprocessing($default_values); 168 parent::set_data($default_values); //never slashed for moodleform_mod 169 } 170 171 /** 172 * Adds all the standard elements to a form to edit the settings for an activity module. 173 * 174 * @param mixed array or object describing supported features - groups, groupings, groupmembersonly, etc. 175 */ 176 function standard_coursemodule_elements($features=null){ 177 global $COURSE, $CFG; 178 $mform =& $this->_form; 179 180 // deal with legacy $supportgroups param 181 if ($features === true or $features === false) { 182 $groupmode = $features; 183 $this->_features = new object(); 184 $this->_features->groups = $groupmode; 185 186 } else if (is_array($features)) { 187 $this->_features = (object)$features; 188 189 } else if (empty($features)) { 190 $this->_features = new object(); 191 192 } else { 193 $this->_features = $features; 194 } 195 196 if (!isset($this->_features->groups)) { 197 $this->_features->groups = true; 198 } 199 200 if (!isset($this->_features->groupings)) { 201 $this->_features->groupings = false; 202 } 203 204 if (!isset($this->_features->groupmembersonly)) { 205 $this->_features->groupmembersonly = false; 206 } 207 208 if (!isset($this->_features->outcomes)) { 209 $this->_features->outcomes = true; 210 } 211 212 if (!isset($this->_features->gradecat)) { 213 $this->_features->gradecat = true; 214 } 215 216 if (!isset($this->_features->idnumber)) { 217 $this->_features->idnumber = true; 218 } 219 220 $outcomesused = false; 221 if (!empty($CFG->enableoutcomes) and $this->_features->outcomes) { 222 if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) { 223 $outcomesused = true; 224 $mform->addElement('header', 'modoutcomes', get_string('outcomes', 'grades')); 225 foreach($outcomes as $outcome) { 226 $mform->addElement('advcheckbox', 'outcome_'.$outcome->id, $outcome->get_name()); 227 } 228 } 229 } 230 231 $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form')); 232 if ($this->_features->groups) { 233 $options = array(NOGROUPS => get_string('groupsnone'), 234 SEPARATEGROUPS => get_string('groupsseparate'), 235 VISIBLEGROUPS => get_string('groupsvisible')); 236 $mform->addElement('select', 'groupmode', get_string('groupmode'), $options, NOGROUPS); 237 $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode'))); 238 } 239 240 if (!empty($CFG->enablegroupings)) { 241 if ($this->_features->groupings or $this->_features->groupmembersonly) { 242 //groupings selector - used for normal grouping mode or also when restricting access with groupmembersonly 243 $options = array(); 244 $options[0] = get_string('none'); 245 if ($groupings = get_records('groupings', 'courseid', $COURSE->id)) { 246 foreach ($groupings as $grouping) { 247 $options[$grouping->id] = format_string($grouping->name); 248 } 249 } 250 $mform->addElement('select', 'groupingid', get_string('grouping', 'group'), $options); 251 $mform->setHelpButton('groupingid', array('grouping', get_string('grouping', 'group'))); 252 $mform->setAdvanced('groupingid'); 253 } 254 255 if ($this->_features->groupmembersonly) { 256 $mform->addElement('checkbox', 'groupmembersonly', get_string('groupmembersonly', 'group')); 257 $mform->setHelpButton('groupmembersonly', array('groupmembersonly', get_string('groupmembersonly', 'group'))); 258 $mform->setAdvanced('groupmembersonly'); 259 } 260 } 261 262 $mform->addElement('modvisible', 'visible', get_string('visible')); 263 264 if ($this->_features->idnumber) { 265 $mform->addElement('text', 'cmidnumber', get_string('idnumbermod')); 266 $mform->setHelpButton('cmidnumber', array('cmidnumber', get_string('idnumbermod')), true); 267 } 268 269 if ($this->_features->gradecat) { 270 $categories = grade_get_categories_menu($COURSE->id, $outcomesused); 271 $mform->addElement('select', 'gradecat', get_string('gradecategory', 'grades'), $categories); 272 } 273 274 $this->standard_hidden_coursemodule_elements(); 275 } 276 277 function standard_hidden_coursemodule_elements(){ 278 $mform =& $this->_form; 279 $mform->addElement('hidden', 'course', 0); 280 $mform->setType('course', PARAM_INT); 281 282 $mform->addElement('hidden', 'coursemodule', 0); 283 $mform->setType('coursemodule', PARAM_INT); 284 285 $mform->addElement('hidden', 'section', 0); 286 $mform->setType('section', PARAM_INT); 287 288 $mform->addElement('hidden', 'module', 0); 289 $mform->setType('module', PARAM_INT); 290 291 $mform->addElement('hidden', 'modulename', ''); 292 $mform->setType('modulename', PARAM_SAFEDIR); 293 294 $mform->addElement('hidden', 'instance', 0); 295 $mform->setType('instance', PARAM_INT); 296 297 $mform->addElement('hidden', 'add', 0); 298 $mform->setType('add', PARAM_ALPHA); 299 300 $mform->addElement('hidden', 'update', 0); 301 $mform->setType('update', PARAM_INT); 302 303 $mform->addElement('hidden', 'return', 0); 304 $mform->setType('return', PARAM_BOOL); 305 } 306 307 /** 308 * Overriding formslib's add_action_buttons() method, to add an extra submit "save changes and return" button. 309 * 310 * @param bool $cancel show cancel button 311 * @param string $submitlabel null means default, false means none, string is label text 312 * @param string $submit2label null means default, false means none, string is label text 313 * @return void 314 */ 315 function add_action_buttons($cancel=true, $submitlabel=null, $submit2label=null) { 316 if (is_null($submitlabel)) { 317 $submitlabel = get_string('savechangesanddisplay'); 318 } 319 320 if (is_null($submit2label)) { 321 $submit2label = get_string('savechangesandreturntocourse'); 322 } 323 324 $mform =& $this->_form; 325 326 // elements in a row need a group 327 $buttonarray = array(); 328 329 if ($submit2label !== false) { 330 $buttonarray[] = &$mform->createElement('submit', 'submitbutton2', $submit2label); 331 } 332 333 if ($submitlabel !== false) { 334 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel); 335 } 336 337 if ($cancel) { 338 $buttonarray[] = &$mform->createElement('cancel'); 339 } 340 341 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false); 342 $mform->setType('buttonar', PARAM_RAW); 343 $mform->closeHeaderBefore('buttonar'); 344 } 345 } 346 347 ?>
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 |