removed shorttag
[moodle.git] / course / moodleform_mod.php
CommitLineData
e24b7f85 1<?php
2require_once ($CFG->libdir.'/formslib.php');
3/**
4 * This class adds extra methods to form wrapper specific to be used for module
7cac0c4b 5 * add / update forms (mod/{modname}.mod_form.php replaces deprecated mod/{modname}/mod_form.php
e24b7f85 6 *
7 */
8class 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.
24f41672 19 * This is always the section number itself (column 'section' from 'course_sections' table).
e24b7f85 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;
4b86bb08 31 /**
32 * List of modform features
33 */
34 var $_features;
e24b7f85 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 }
71ee4471 42
e24b7f85 43 /**
a7f7e52f 44 * Only available on moodleform_mod.
e24b7f85 45 *
46 * @param array $default_values passed by reference
47 */
a7f7e52f 48 function data_preprocessing(&$default_values){
e24b7f85 49 }
71ee4471 50
4b86bb08 51 /**
52 * Each module which defines definition_after_data() must call this method using parent::definition_after_data();
53 */
71ee4471 54 function definition_after_data() {
4b86bb08 55 global $CFG, $COURSE;
71ee4471 56 $mform =& $this->_form;
57
58 if ($id = $mform->getElementValue('update')) {
59 $modulename = $mform->getElementValue('modulename');
60 $instance = $mform->getElementValue('instance');
61
4b86bb08 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;
71ee4471 83 }
4b86bb08 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');
71ee4471 97 }
98 }
99 }
100 }
24f41672 101
a78890d5 102 if ($COURSE->groupmodeforce) {
103 if ($mform->elementExists('groupmode')) {
24f41672 104 $mform->hardFreeze('groupmode'); // groupmode can not be changed if forced from course settings
105 }
106 }
a104debf 107
f2fdc8eb 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');
4b86bb08 113
f2fdc8eb 114 } else if (!$mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly')) {
115 // groupings have no use without groupmode or groupmembersonly
a104debf 116 if ($mform->elementExists('groupingid')) {
117 $mform->removeElement('groupingid');
118 }
119 }
71ee4471 120 }
121
60243313 122 // form verification
a78890d5 123 function validation($data, $files) {
32648682 124 global $COURSE, $DB;
a78890d5 125 $errors = parent::validation($data, $files);
60243313 126
273eb2f5 127 $mform =& $this->_form;
128
60243313 129 $errors = array();
130
273eb2f5 131 if ($mform->elementExists('name')) {
132 $name = trim($data['name']);
133 if ($name == '') {
134 $errors['name'] = get_string('required');
135 }
e04ff2d5 136 }
137
60243313 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']) {
32648682 141 $cm = $DB->get_record('course_modules', array('id'=>$data['coursemodule']));
60243313 142 } else {
143 $cm = null;
144 }
145
273eb2f5 146 if ($mform->elementExists('cmidnumber')) {
147 // verify the idnumber
204175c5 148 if (!grade_verify_idnumber($data['cmidnumber'], $COURSE->id, $grade_item, $cm)) {
273eb2f5 149 $errors['cmidnumber'] = get_string('idnumbertaken');
150 }
60243313 151 }
152
a78890d5 153 return $errors;
60243313 154 }
155
e24b7f85 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 */
32db0d42 163 function set_data($default_values) {
e24b7f85 164 if (is_object($default_values)) {
165 $default_values = (array)$default_values;
166 }
ab6803a9 167 $this->data_preprocessing($default_values);
24f41672 168 parent::set_data($default_values); //never slashed for moodleform_mod
e24b7f85 169 }
71ee4471 170
e24b7f85 171 /**
172 * Adds all the standard elements to a form to edit the settings for an activity module.
173 *
b5ab55dd 174 * @param mixed array or object describing supported features - groups, groupings, groupmembersonly, etc.
e24b7f85 175 */
e04ff2d5 176 function standard_coursemodule_elements($features=null){
c18269c7 177 global $COURSE, $CFG, $DB;
e24b7f85 178 $mform =& $this->_form;
f3b783f4 179
e04ff2d5 180 // deal with legacy $supportgroups param
181 if ($features === true or $features === false) {
182 $groupmode = $features;
4b86bb08 183 $this->_features = new object();
184 $this->_features->groups = $groupmode;
e04ff2d5 185
186 } else if (is_array($features)) {
4b86bb08 187 $this->_features = (object)$features;
e04ff2d5 188
189 } else if (empty($features)) {
4b86bb08 190 $this->_features = new object();
191
192 } else {
193 $this->_features = $features;
e04ff2d5 194 }
195
4b86bb08 196 if (!isset($this->_features->groups)) {
197 $this->_features->groups = true;
e04ff2d5 198 }
199
4b86bb08 200 if (!isset($this->_features->groupings)) {
201 $this->_features->groupings = false;
e04ff2d5 202 }
203
4b86bb08 204 if (!isset($this->_features->groupmembersonly)) {
205 $this->_features->groupmembersonly = false;
e04ff2d5 206 }
207
4b86bb08 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
b5ab55dd 216 if (!isset($this->_features->idnumber)) {
217 $this->_features->idnumber = true;
218 }
219
220 $outcomesused = false;
4b86bb08 221 if (!empty($CFG->enableoutcomes) and $this->_features->outcomes) {
f3b783f4 222 if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
b5ab55dd 223 $outcomesused = true;
f3b783f4 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
24e25bc1 231 $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
4b86bb08 232 if ($this->_features->groups) {
a78890d5 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')));
e24b7f85 238 }
24f41672 239
240 if (!empty($CFG->enablegroupings)) {
4b86bb08 241 if ($this->_features->groupings or $this->_features->groupmembersonly) {
e04ff2d5 242 //groupings selector - used for normal grouping mode or also when restricting access with groupmembersonly
243 $options = array();
244 $options[0] = get_string('none');
c18269c7 245 if ($groupings = $DB->get_records('groupings', array('courseid'=>$COURSE->id))) {
e04ff2d5 246 foreach ($groupings as $grouping) {
247 $options[$grouping->id] = format_string($grouping->name);
248 }
24f41672 249 }
e04ff2d5 250 $mform->addElement('select', 'groupingid', get_string('grouping', 'group'), $options);
251 $mform->setAdvanced('groupingid');
24f41672 252 }
e04ff2d5 253
4b86bb08 254 if ($this->_features->groupmembersonly) {
f2fdc8eb 255 $mform->addElement('checkbox', 'groupmembersonly', get_string('groupmembersonly', 'group'));
a104debf 256 $mform->setAdvanced('groupmembersonly');
257 }
24f41672 258 }
259
e24b7f85 260 $mform->addElement('modvisible', 'visible', get_string('visible'));
b5ab55dd 261
262 if ($this->_features->idnumber) {
263 $mform->addElement('text', 'cmidnumber', get_string('idnumbermod'));
264 $mform->setHelpButton('cmidnumber', array('cmidnumber', get_string('idnumbermod')), true);
265 }
71ee4471 266
4b86bb08 267 if ($this->_features->gradecat) {
57956368 268 $categories = grade_get_categories_menu($COURSE->id, $outcomesused);
4b86bb08 269 $mform->addElement('select', 'gradecat', get_string('gradecategory', 'grades'), $categories);
270 }
271
e24b7f85 272 $this->standard_hidden_coursemodule_elements();
273 }
274
275 function standard_hidden_coursemodule_elements(){
276 $mform =& $this->_form;
277 $mform->addElement('hidden', 'course', 0);
278 $mform->setType('course', PARAM_INT);
279
280 $mform->addElement('hidden', 'coursemodule', 0);
281 $mform->setType('coursemodule', PARAM_INT);
282
283 $mform->addElement('hidden', 'section', 0);
284 $mform->setType('section', PARAM_INT);
285
286 $mform->addElement('hidden', 'module', 0);
287 $mform->setType('module', PARAM_INT);
288
289 $mform->addElement('hidden', 'modulename', '');
290 $mform->setType('modulename', PARAM_SAFEDIR);
291
292 $mform->addElement('hidden', 'instance', 0);
293 $mform->setType('instance', PARAM_INT);
294
295 $mform->addElement('hidden', 'add', 0);
296 $mform->setType('add', PARAM_ALPHA);
297
298 $mform->addElement('hidden', 'update', 0);
299 $mform->setType('update', PARAM_INT);
19110c57 300
301 $mform->addElement('hidden', 'return', 0);
302 $mform->setType('return', PARAM_BOOL);
e24b7f85 303 }
304
6b467109 305 /**
306 * Overriding formslib's add_action_buttons() method, to add an extra submit "save changes and return" button.
b5ab55dd 307 *
308 * @param bool $cancel show cancel button
309 * @param string $submitlabel null means default, false means none, string is label text
310 * @param string $submit2label null means default, false means none, string is label text
6b467109 311 * @return void
b5ab55dd 312 */
6b467109 313 function add_action_buttons($cancel=true, $submitlabel=null, $submit2label=null) {
314 if (is_null($submitlabel)) {
315 $submitlabel = get_string('savechangesanddisplay');
316 }
b5ab55dd 317
6b467109 318 if (is_null($submit2label)) {
319 $submit2label = get_string('savechangesandreturntocourse');
320 }
b5ab55dd 321
6b467109 322 $mform =& $this->_form;
b5ab55dd 323
324 // elements in a row need a group
325 $buttonarray = array();
326
327 if ($submit2label !== false) {
328 $buttonarray[] = &$mform->createElement('submit', 'submitbutton2', $submit2label);
329 }
330
331 if ($submitlabel !== false) {
332 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
333 }
334
6b467109 335 if ($cancel) {
336 $buttonarray[] = &$mform->createElement('cancel');
337 }
338
339 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
de6f158f 340 $mform->setType('buttonar', PARAM_RAW);
6b467109 341 $mform->closeHeaderBefore('buttonar');
342 }
e24b7f85 343}
344
f3b783f4 345?>