2 require_once ($CFG->libdir
.'/formslib.php');
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
8 class moodleform_mod
extends moodleform
{
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
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).
25 * Coursemodle record of the module that is being updated. Will be null if this is an 'add' form and not an
32 * List of modform features
36 function moodleform_mod($instance, $section, $cm) {
37 $this->_instance
= $instance;
38 $this->_section
= $section;
40 parent
::moodleform('modedit.php');
44 * Only available on moodleform_mod.
46 * @param array $default_values passed by reference
48 function data_preprocessing(&$default_values){
52 * Each module which defines definition_after_data() must call this method using parent::definition_after_data();
54 function definition_after_data() {
56 $mform =& $this->_form
;
58 if ($id = $mform->getElementValue('update')) {
59 $modulename = $mform->getElementValue('modulename');
60 $instance = $mform->getElementValue('instance');
62 if ($this->_features
->gradecat
) {
64 if (!empty($CFG->enableoutcomes
) and $this->_features
->outcomes
) {
65 if ($outcomes = grade_outcome
::fetch_all_available($COURSE->id
)) {
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
79 foreach ($items as $item) {
80 if (is_bool($gradecat)) {
81 $gradecat = $item->categoryid
;
84 if ($gradecat != $item->categoryid
) {
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');
102 if ($COURSE->groupmodeforce
) {
103 if ($mform->elementExists('groupmode')) {
104 $mform->hardFreeze('groupmode'); // groupmode can not be changed if forced from course settings
108 if ($mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly') and empty($COURSE->groupmodeforce
)) {
109 $mform->disabledIf('groupingid', 'groupmode', 'eq', NOGROUPS
);
111 } else if (!$mform->elementExists('groupmode') and $mform->elementExists('groupmembersonly')) {
112 $mform->disabledIf('groupingid', 'groupmembersonly', 'notchecked');
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');
123 function validation($data, $files) {
125 $errors = parent
::validation($data, $files);
127 $mform =& $this->_form
;
131 if ($mform->elementExists('name')) {
132 $name = trim($data['name']);
134 $errors['name'] = get_string('required');
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']);
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');
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).
161 * @param mixed $default_values object or array of default values
163 function set_data($default_values) {
164 if (is_object($default_values)) {
165 $default_values = (array)$default_values;
167 $this->data_preprocessing($default_values);
168 parent
::set_data($default_values); //never slashed for moodleform_mod
172 * Adds all the standard elements to a form to edit the settings for an activity module.
174 * @param mixed array or object describing supported features - groups, groupings, groupmembersonly, etc.
176 function standard_coursemodule_elements($features=null){
177 global $COURSE, $CFG;
178 $mform =& $this->_form
;
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;
186 } else if (is_array($features)) {
187 $this->_features
= (object)$features;
189 } else if (empty($features)) {
190 $this->_features
= new object();
193 $this->_features
= $features;
196 if (!isset($this->_features
->groups
)) {
197 $this->_features
->groups
= true;
200 if (!isset($this->_features
->groupings
)) {
201 $this->_features
->groupings
= false;
204 if (!isset($this->_features
->groupmembersonly
)) {
205 $this->_features
->groupmembersonly
= false;
208 if (!isset($this->_features
->outcomes
)) {
209 $this->_features
->outcomes
= true;
212 if (!isset($this->_features
->gradecat
)) {
213 $this->_features
->gradecat
= true;
216 if (!isset($this->_features
->idnumber
)) {
217 $this->_features
->idnumber
= true;
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());
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')));
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
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
);
250 $mform->addElement('select', 'groupingid', get_string('grouping', 'group'), $options);
251 $mform->setAdvanced('groupingid');
254 if ($this->_features
->groupmembersonly
) {
255 $mform->addElement('checkbox', 'groupmembersonly', get_string('groupmembersonly', 'group'));
256 $mform->setAdvanced('groupmembersonly');
260 $mform->addElement('modvisible', 'visible', get_string('visible'));
262 if ($this->_features
->idnumber
) {
263 $mform->addElement('text', 'cmidnumber', get_string('idnumbermod'));
264 $mform->setHelpButton('cmidnumber', array('cmidnumber', get_string('idnumbermod')), true);
267 if ($this->_features
->gradecat
) {
268 $categories = grade_get_categories_menu($COURSE->id
, $outcomesused);
269 $mform->addElement('select', 'gradecat', get_string('gradecategory', 'grades'), $categories);
272 $this->standard_hidden_coursemodule_elements();
275 function standard_hidden_coursemodule_elements(){
276 $mform =& $this->_form
;
277 $mform->addElement('hidden', 'course', 0);
278 $mform->setType('course', PARAM_INT
);
280 $mform->addElement('hidden', 'coursemodule', 0);
281 $mform->setType('coursemodule', PARAM_INT
);
283 $mform->addElement('hidden', 'section', 0);
284 $mform->setType('section', PARAM_INT
);
286 $mform->addElement('hidden', 'module', 0);
287 $mform->setType('module', PARAM_INT
);
289 $mform->addElement('hidden', 'modulename', '');
290 $mform->setType('modulename', PARAM_SAFEDIR
);
292 $mform->addElement('hidden', 'instance', 0);
293 $mform->setType('instance', PARAM_INT
);
295 $mform->addElement('hidden', 'add', 0);
296 $mform->setType('add', PARAM_ALPHA
);
298 $mform->addElement('hidden', 'update', 0);
299 $mform->setType('update', PARAM_INT
);
301 $mform->addElement('hidden', 'return', 0);
302 $mform->setType('return', PARAM_BOOL
);
306 * Overriding formslib's add_action_buttons() method, to add an extra submit "save changes and return" button.
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
313 function add_action_buttons($cancel=true, $submitlabel=null, $submit2label=null) {
314 if (is_null($submitlabel)) {
315 $submitlabel = get_string('savechangesanddisplay');
318 if (is_null($submit2label)) {
319 $submit2label = get_string('savechangesandreturntocourse');
322 $mform =& $this->_form
;
324 // elements in a row need a group
325 $buttonarray = array();
327 if ($submit2label !== false) {
328 $buttonarray[] = &$mform->createElement('submit', 'submitbutton2', $submit2label);
331 if ($submitlabel !== false) {
332 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
336 $buttonarray[] = &$mform->createElement('cancel');
339 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
340 $mform->setType('buttonar', PARAM_RAW
);
341 $mform->closeHeaderBefore('buttonar');