Related to MDL-10916 - saving Cloze questions in Moodle 1.9 generates Notices because...
[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
68 if ($mform->elementExists('groupmode')) {
69 if ($COURSE->groupmodeforce) {
70 $mform->hardFreeze('groupmode'); // groupmode can not be changed if forced from course settings
71 }
72 }
a104debf 73
74 // groupings have no use without groupmode or groupmembersonly
75 if (!$mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly')) {
76 if ($mform->elementExists('groupingid')) {
77 $mform->removeElement('groupingid');
78 }
79 }
71ee4471 80 }
81
60243313 82 // form verification
83 function validation($data) {
84 global $COURSE;
85
273eb2f5 86 $mform =& $this->_form;
87
60243313 88 $errors = array();
89
273eb2f5 90 if ($mform->elementExists('name')) {
91 $name = trim($data['name']);
92 if ($name == '') {
93 $errors['name'] = get_string('required');
94 }
e04ff2d5 95 }
96
60243313 97 $grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$data['modulename'],
98 'iteminstance'=>$data['instance'], 'itemnumber'=>0, 'courseid'=>$COURSE->id));
99 if ($data['coursemodule']) {
100 $cm = get_record('course_modules', 'id', $data['coursemodule']);
101 } else {
102 $cm = null;
103 }
104
273eb2f5 105 if ($mform->elementExists('cmidnumber')) {
106 // verify the idnumber
107 if (!grade_verify_idnumber($data['cmidnumber'], $grade_item, $cm)) {
108 $errors['cmidnumber'] = get_string('idnumbertaken');
109 }
60243313 110 }
111
112 if (count($errors) == 0) {
113 return true;
114 } else {
115 return $errors;
116 }
117 }
118
e24b7f85 119 /**
120 * Load in existing data as form defaults. Usually new entry defaults are stored directly in
121 * form definition (new entry form); this function is used to load in data where values
122 * already exist and data is being edited (edit entry form).
123 *
124 * @param mixed $default_values object or array of default values
125 */
32db0d42 126 function set_data($default_values) {
e24b7f85 127 if (is_object($default_values)) {
128 $default_values = (array)$default_values;
129 }
ab6803a9 130 $this->data_preprocessing($default_values);
24f41672 131 parent::set_data($default_values); //never slashed for moodleform_mod
e24b7f85 132 }
71ee4471 133
e24b7f85 134 /**
135 * Adds all the standard elements to a form to edit the settings for an activity module.
136 *
e04ff2d5 137 * @param mixed array or object describing supported features - groups, groupings, groupmembersonly
e24b7f85 138 */
e04ff2d5 139 function standard_coursemodule_elements($features=null){
75bde3cc 140 global $COURSE, $CFG;
e24b7f85 141 $mform =& $this->_form;
f3b783f4 142
e04ff2d5 143 // deal with legacy $supportgroups param
144 if ($features === true or $features === false) {
145 $groupmode = $features;
146 $features = new object();
147 $features->groups = $groupmode;
148
149 } else if (is_array($features)) {
150 $features = (object)$features;
151
152 } else if (empty($features)) {
153 $features = new object();
154 }
155
156 if (!isset($features->groups)) {
157 $features->groups = true;
158 }
159
160 if (!isset($features->groupings)) {
161 $features->groupings = false;
162 }
163
164 if (!isset($features->groupmembersonly)) {
165 $features->groupmembersonly = false;
166 }
167
f3b783f4 168 if (!empty($CFG->enableoutcomes)) {
169 if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
170 $mform->addElement('header', 'modoutcomes', get_string('outcomes', 'grades'));
171 foreach($outcomes as $outcome) {
172 $mform->addElement('advcheckbox', 'outcome_'.$outcome->id, $outcome->get_name());
173 }
174 }
175 }
176
24e25bc1 177 $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
e04ff2d5 178 if ($features->groups){
e24b7f85 179 $mform->addElement('modgroupmode', 'groupmode', get_string('groupmode'));
180 }
24f41672 181
182 if (!empty($CFG->enablegroupings)) {
e04ff2d5 183 if ($features->groupings or $features->groupmembersonly) {
184 //groupings selector - used for normal grouping mode or also when restricting access with groupmembersonly
185 $options = array();
186 $options[0] = get_string('none');
187 if ($groupings = get_records('groupings', 'courseid', $COURSE->id)) {
188 foreach ($groupings as $grouping) {
189 $options[$grouping->id] = format_string($grouping->name);
190 }
24f41672 191 }
e04ff2d5 192 $mform->addElement('select', 'groupingid', get_string('grouping', 'group'), $options);
193 $mform->setAdvanced('groupingid');
24f41672 194 }
e04ff2d5 195
196 if ($features->groupmembersonly) {
a104debf 197 $mform->addElement('advcheckbox', 'groupmembersonly', get_string('groupmembersonly', 'group'));
198 $mform->setAdvanced('groupmembersonly');
199 }
24f41672 200 }
201
e24b7f85 202 $mform->addElement('modvisible', 'visible', get_string('visible'));
2d11be3b 203 $mform->addElement('text', 'cmidnumber', get_string('idnumber'));
71ee4471 204
e24b7f85 205 $this->standard_hidden_coursemodule_elements();
206 }
207
208 function standard_hidden_coursemodule_elements(){
209 $mform =& $this->_form;
210 $mform->addElement('hidden', 'course', 0);
211 $mform->setType('course', PARAM_INT);
212
213 $mform->addElement('hidden', 'coursemodule', 0);
214 $mform->setType('coursemodule', PARAM_INT);
215
216 $mform->addElement('hidden', 'section', 0);
217 $mform->setType('section', PARAM_INT);
218
219 $mform->addElement('hidden', 'module', 0);
220 $mform->setType('module', PARAM_INT);
221
222 $mform->addElement('hidden', 'modulename', '');
223 $mform->setType('modulename', PARAM_SAFEDIR);
224
225 $mform->addElement('hidden', 'instance', 0);
226 $mform->setType('instance', PARAM_INT);
227
228 $mform->addElement('hidden', 'add', 0);
229 $mform->setType('add', PARAM_ALPHA);
230
231 $mform->addElement('hidden', 'update', 0);
232 $mform->setType('update', PARAM_INT);
19110c57 233
234 $mform->addElement('hidden', 'return', 0);
235 $mform->setType('return', PARAM_BOOL);
e24b7f85 236 }
237
e24b7f85 238}
239
f3b783f4 240?>