MDL-11282 Implemented fix. Merging from MOODLE_19_STABLE
[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
6b467109 5 * add / update forms (mod/{modname}.mod_form.php replaces deprecated mod/{modname}/mod.html
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) {
60243313 124 global $COURSE;
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']) {
141 $cm = get_record('course_modules', 'id', $data['coursemodule']);
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 *
e04ff2d5 174 * @param mixed array or object describing supported features - groups, groupings, groupmembersonly
e24b7f85 175 */
e04ff2d5 176 function standard_coursemodule_elements($features=null){
75bde3cc 177 global $COURSE, $CFG;
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
57956368 216 $outcomesused = false;
4b86bb08 217 if (!empty($CFG->enableoutcomes) and $this->_features->outcomes) {
f3b783f4 218 if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
57956368 219 $outcomesused = true;
f3b783f4 220 $mform->addElement('header', 'modoutcomes', get_string('outcomes', 'grades'));
221 foreach($outcomes as $outcome) {
222 $mform->addElement('advcheckbox', 'outcome_'.$outcome->id, $outcome->get_name());
223 }
224 }
225 }
226
24e25bc1 227 $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
4b86bb08 228 if ($this->_features->groups) {
a78890d5 229 $options = array(NOGROUPS => get_string('groupsnone'),
230 SEPARATEGROUPS => get_string('groupsseparate'),
231 VISIBLEGROUPS => get_string('groupsvisible'));
232 $mform->addElement('select', 'groupmode', get_string('groupmode'), $options, NOGROUPS);
233 $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')));
e24b7f85 234 }
24f41672 235
236 if (!empty($CFG->enablegroupings)) {
4b86bb08 237 if ($this->_features->groupings or $this->_features->groupmembersonly) {
e04ff2d5 238 //groupings selector - used for normal grouping mode or also when restricting access with groupmembersonly
239 $options = array();
240 $options[0] = get_string('none');
241 if ($groupings = get_records('groupings', 'courseid', $COURSE->id)) {
242 foreach ($groupings as $grouping) {
243 $options[$grouping->id] = format_string($grouping->name);
244 }
24f41672 245 }
e04ff2d5 246 $mform->addElement('select', 'groupingid', get_string('grouping', 'group'), $options);
247 $mform->setAdvanced('groupingid');
24f41672 248 }
e04ff2d5 249
4b86bb08 250 if ($this->_features->groupmembersonly) {
f2fdc8eb 251 $mform->addElement('checkbox', 'groupmembersonly', get_string('groupmembersonly', 'group'));
a104debf 252 $mform->setAdvanced('groupmembersonly');
253 }
24f41672 254 }
255
e24b7f85 256 $mform->addElement('modvisible', 'visible', get_string('visible'));
d343bd21 257 $mform->addElement('text', 'cmidnumber', get_string('idnumbermod'));
258 $mform->setHelpButton('cmidnumber', array('cmidnumber', get_string('idnumbermod')), true);
71ee4471 259
4b86bb08 260 if ($this->_features->gradecat) {
57956368 261 $categories = grade_get_categories_menu($COURSE->id, $outcomesused);
4b86bb08 262 $mform->addElement('select', 'gradecat', get_string('gradecategory', 'grades'), $categories);
263 }
264
e24b7f85 265 $this->standard_hidden_coursemodule_elements();
266 }
267
268 function standard_hidden_coursemodule_elements(){
269 $mform =& $this->_form;
270 $mform->addElement('hidden', 'course', 0);
271 $mform->setType('course', PARAM_INT);
272
273 $mform->addElement('hidden', 'coursemodule', 0);
274 $mform->setType('coursemodule', PARAM_INT);
275
276 $mform->addElement('hidden', 'section', 0);
277 $mform->setType('section', PARAM_INT);
278
279 $mform->addElement('hidden', 'module', 0);
280 $mform->setType('module', PARAM_INT);
281
282 $mform->addElement('hidden', 'modulename', '');
283 $mform->setType('modulename', PARAM_SAFEDIR);
284
285 $mform->addElement('hidden', 'instance', 0);
286 $mform->setType('instance', PARAM_INT);
287
288 $mform->addElement('hidden', 'add', 0);
289 $mform->setType('add', PARAM_ALPHA);
290
291 $mform->addElement('hidden', 'update', 0);
292 $mform->setType('update', PARAM_INT);
19110c57 293
294 $mform->addElement('hidden', 'return', 0);
295 $mform->setType('return', PARAM_BOOL);
e24b7f85 296 }
297
6b467109 298 /**
299 * Overriding formslib's add_action_buttons() method, to add an extra submit "save changes and return" button.
300 *
301 * @param bool $cancel
302 * @param string $submitlabel
303 * @param string $submit2label
304 * @return void
305 */
306 function add_action_buttons($cancel=true, $submitlabel=null, $submit2label=null) {
307 if (is_null($submitlabel)) {
308 $submitlabel = get_string('savechangesanddisplay');
309 }
310
311 if (is_null($submit2label)) {
312 $submit2label = get_string('savechangesandreturntocourse');
313 }
314
315 $mform =& $this->_form;
316
317 //when two elements we need a group
318 $buttonarray=array();
6b467109 319 $buttonarray[] = &$mform->createElement('submit', 'submitbutton2', $submit2label);
eb6582a5 320 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
6b467109 321
322 if ($cancel) {
323 $buttonarray[] = &$mform->createElement('cancel');
324 }
325
326 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
de6f158f 327 $mform->setType('buttonar', PARAM_RAW);
6b467109 328 $mform->closeHeaderBefore('buttonar');
329 }
e24b7f85 330}
331
f3b783f4 332?>