MDL-13084 SORT_LOCALE_STRING not defined < PHP 4.4.0; merged 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
5 * add / update forms (mod/{modname}.mod_form.php replaces deprecared mod/{modname}/mod.html
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;
31
32 function moodleform_mod($instance, $section, $cm) {
33 $this->_instance = $instance;
34 $this->_section = $section;
35 $this->_cm = $cm;
36 parent::moodleform('modedit.php');
37 }
71ee4471 38
e24b7f85 39 /**
a7f7e52f 40 * Only available on moodleform_mod.
e24b7f85 41 *
42 * @param array $default_values passed by reference
43 */
a7f7e52f 44 function data_preprocessing(&$default_values){
e24b7f85 45 }
71ee4471 46
47 function definition_after_data() {
48 global $COURSE;
49 $mform =& $this->_form;
50
51 if ($id = $mform->getElementValue('update')) {
52 $modulename = $mform->getElementValue('modulename');
53 $instance = $mform->getElementValue('instance');
54
55 if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$modulename,
56 'iteminstance'=>$instance, 'courseid'=>$COURSE->id))) {
57 foreach ($items as $item) {
58 if (!empty($item->outcomeid)) {
59 $elname = 'outcome_'.$item->outcomeid;
60 if ($mform->elementExists($elname)) {
61 $mform->hardFreeze($elname); // prevent removing of existing outcomes
62 }
63 }
64 }
65 }
66 }
24f41672 67
a78890d5 68 if ($COURSE->groupmodeforce) {
69 if ($mform->elementExists('groupmode')) {
24f41672 70 $mform->hardFreeze('groupmode'); // groupmode can not be changed if forced from course settings
71 }
72 }
a104debf 73
f2fdc8eb 74 if ($mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly') and empty($COURSE->groupmodeforce)) {
75 $mform->disabledIf('groupingid', 'groupmode', 'eq', NOGROUPS);
76
77 } else if (!$mform->elementExists('groupmode') and $mform->elementExists('groupmembersonly')) {
78 $mform->disabledIf('groupingid', 'groupmembersonly', 'notchecked');
79
80 } else if (!$mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly')) {
81 // groupings have no use without groupmode or groupmembersonly
a104debf 82 if ($mform->elementExists('groupingid')) {
83 $mform->removeElement('groupingid');
84 }
85 }
71ee4471 86 }
87
60243313 88 // form verification
a78890d5 89 function validation($data, $files) {
60243313 90 global $COURSE;
a78890d5 91 $errors = parent::validation($data, $files);
60243313 92
273eb2f5 93 $mform =& $this->_form;
94
60243313 95 $errors = array();
96
273eb2f5 97 if ($mform->elementExists('name')) {
98 $name = trim($data['name']);
99 if ($name == '') {
100 $errors['name'] = get_string('required');
101 }
e04ff2d5 102 }
103
60243313 104 $grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$data['modulename'],
105 'iteminstance'=>$data['instance'], 'itemnumber'=>0, 'courseid'=>$COURSE->id));
106 if ($data['coursemodule']) {
107 $cm = get_record('course_modules', 'id', $data['coursemodule']);
108 } else {
109 $cm = null;
110 }
111
273eb2f5 112 if ($mform->elementExists('cmidnumber')) {
113 // verify the idnumber
204175c5 114 if (!grade_verify_idnumber($data['cmidnumber'], $COURSE->id, $grade_item, $cm)) {
273eb2f5 115 $errors['cmidnumber'] = get_string('idnumbertaken');
116 }
60243313 117 }
118
a78890d5 119 return $errors;
60243313 120 }
121
e24b7f85 122 /**
123 * Load in existing data as form defaults. Usually new entry defaults are stored directly in
124 * form definition (new entry form); this function is used to load in data where values
125 * already exist and data is being edited (edit entry form).
126 *
127 * @param mixed $default_values object or array of default values
128 */
32db0d42 129 function set_data($default_values) {
e24b7f85 130 if (is_object($default_values)) {
131 $default_values = (array)$default_values;
132 }
ab6803a9 133 $this->data_preprocessing($default_values);
24f41672 134 parent::set_data($default_values); //never slashed for moodleform_mod
e24b7f85 135 }
71ee4471 136
e24b7f85 137 /**
138 * Adds all the standard elements to a form to edit the settings for an activity module.
139 *
e04ff2d5 140 * @param mixed array or object describing supported features - groups, groupings, groupmembersonly
e24b7f85 141 */
e04ff2d5 142 function standard_coursemodule_elements($features=null){
75bde3cc 143 global $COURSE, $CFG;
e24b7f85 144 $mform =& $this->_form;
f3b783f4 145
e04ff2d5 146 // deal with legacy $supportgroups param
147 if ($features === true or $features === false) {
148 $groupmode = $features;
149 $features = new object();
150 $features->groups = $groupmode;
151
152 } else if (is_array($features)) {
153 $features = (object)$features;
154
155 } else if (empty($features)) {
156 $features = new object();
157 }
158
159 if (!isset($features->groups)) {
160 $features->groups = true;
161 }
162
163 if (!isset($features->groupings)) {
164 $features->groupings = false;
165 }
166
167 if (!isset($features->groupmembersonly)) {
168 $features->groupmembersonly = false;
169 }
170
f3b783f4 171 if (!empty($CFG->enableoutcomes)) {
172 if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
173 $mform->addElement('header', 'modoutcomes', get_string('outcomes', 'grades'));
174 foreach($outcomes as $outcome) {
175 $mform->addElement('advcheckbox', 'outcome_'.$outcome->id, $outcome->get_name());
176 }
177 }
178 }
179
24e25bc1 180 $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
a78890d5 181 if ($features->groups) {
182 $options = array(NOGROUPS => get_string('groupsnone'),
183 SEPARATEGROUPS => get_string('groupsseparate'),
184 VISIBLEGROUPS => get_string('groupsvisible'));
185 $mform->addElement('select', 'groupmode', get_string('groupmode'), $options, NOGROUPS);
186 $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')));
e24b7f85 187 }
24f41672 188
189 if (!empty($CFG->enablegroupings)) {
e04ff2d5 190 if ($features->groupings or $features->groupmembersonly) {
191 //groupings selector - used for normal grouping mode or also when restricting access with groupmembersonly
192 $options = array();
193 $options[0] = get_string('none');
194 if ($groupings = get_records('groupings', 'courseid', $COURSE->id)) {
195 foreach ($groupings as $grouping) {
196 $options[$grouping->id] = format_string($grouping->name);
197 }
24f41672 198 }
e04ff2d5 199 $mform->addElement('select', 'groupingid', get_string('grouping', 'group'), $options);
200 $mform->setAdvanced('groupingid');
24f41672 201 }
e04ff2d5 202
203 if ($features->groupmembersonly) {
f2fdc8eb 204 $mform->addElement('checkbox', 'groupmembersonly', get_string('groupmembersonly', 'group'));
a104debf 205 $mform->setAdvanced('groupmembersonly');
206 }
24f41672 207 }
208
e24b7f85 209 $mform->addElement('modvisible', 'visible', get_string('visible'));
2d11be3b 210 $mform->addElement('text', 'cmidnumber', get_string('idnumber'));
71ee4471 211
e24b7f85 212 $this->standard_hidden_coursemodule_elements();
213 }
214
215 function standard_hidden_coursemodule_elements(){
216 $mform =& $this->_form;
217 $mform->addElement('hidden', 'course', 0);
218 $mform->setType('course', PARAM_INT);
219
220 $mform->addElement('hidden', 'coursemodule', 0);
221 $mform->setType('coursemodule', PARAM_INT);
222
223 $mform->addElement('hidden', 'section', 0);
224 $mform->setType('section', PARAM_INT);
225
226 $mform->addElement('hidden', 'module', 0);
227 $mform->setType('module', PARAM_INT);
228
229 $mform->addElement('hidden', 'modulename', '');
230 $mform->setType('modulename', PARAM_SAFEDIR);
231
232 $mform->addElement('hidden', 'instance', 0);
233 $mform->setType('instance', PARAM_INT);
234
235 $mform->addElement('hidden', 'add', 0);
236 $mform->setType('add', PARAM_ALPHA);
237
238 $mform->addElement('hidden', 'update', 0);
239 $mform->setType('update', PARAM_INT);
19110c57 240
241 $mform->addElement('hidden', 'return', 0);
242 $mform->setType('return', PARAM_BOOL);
e24b7f85 243 }
244
e24b7f85 245}
246
f3b783f4 247?>