category grades and course grades should not be dependent on restore_userdata_selectee
[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
86 $errors = array();
87
88 $grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$data['modulename'],
89 'iteminstance'=>$data['instance'], 'itemnumber'=>0, 'courseid'=>$COURSE->id));
90 if ($data['coursemodule']) {
91 $cm = get_record('course_modules', 'id', $data['coursemodule']);
92 } else {
93 $cm = null;
94 }
95
96 // verify the idnumber
97 if (!grade_verify_idnumber($data['cmidnumber'], $grade_item, $cm)) {
98 $errors['cmidnumber'] = get_string('idnumbertaken');
99 }
100
101 if (count($errors) == 0) {
102 return true;
103 } else {
104 return $errors;
105 }
106 }
107
e24b7f85 108 /**
109 * Load in existing data as form defaults. Usually new entry defaults are stored directly in
110 * form definition (new entry form); this function is used to load in data where values
111 * already exist and data is being edited (edit entry form).
112 *
113 * @param mixed $default_values object or array of default values
114 */
32db0d42 115 function set_data($default_values) {
e24b7f85 116 if (is_object($default_values)) {
117 $default_values = (array)$default_values;
118 }
ab6803a9 119 $this->data_preprocessing($default_values);
24f41672 120 parent::set_data($default_values); //never slashed for moodleform_mod
e24b7f85 121 }
71ee4471 122
e24b7f85 123 /**
124 * Adds all the standard elements to a form to edit the settings for an activity module.
125 *
126 * @param bool $supportsgroups does this module support groups?
a104debf 127 * @param bool $supportgroupmembersonly does this module support groupmembersonly access?
e24b7f85 128 */
a104debf 129 function standard_coursemodule_elements($supportsgroups=true, $supportgroupmembersonly=false){
75bde3cc 130 global $COURSE, $CFG;
e24b7f85 131 $mform =& $this->_form;
f3b783f4 132
133 if (!empty($CFG->enableoutcomes)) {
134 if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
135 $mform->addElement('header', 'modoutcomes', get_string('outcomes', 'grades'));
136 foreach($outcomes as $outcome) {
137 $mform->addElement('advcheckbox', 'outcome_'.$outcome->id, $outcome->get_name());
138 }
139 }
140 }
141
24e25bc1 142 $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
e24b7f85 143 if ($supportsgroups){
144 $mform->addElement('modgroupmode', 'groupmode', get_string('groupmode'));
145 }
24f41672 146
147 if (!empty($CFG->enablegroupings)) {
148 //groupings selector
149 $options = array();
150 $options[0] = get_string('none');
151 if ($groupings = get_records('groupings', 'courseid', $COURSE->id)) {
152 foreach ($groupings as $grouping) {
153 $options[$grouping->id] = format_string($grouping->name);
154 }
155 }
156 $mform->addElement('select', 'groupingid', get_string('grouping', 'group'), $options);
a104debf 157 $mform->setAdvanced('groupingid');
158
159 if ($supportgroupmembersonly) {
160 $mform->addElement('advcheckbox', 'groupmembersonly', get_string('groupmembersonly', 'group'));
161 $mform->setAdvanced('groupmembersonly');
162 }
24f41672 163 }
164
e24b7f85 165 $mform->addElement('modvisible', 'visible', get_string('visible'));
2d11be3b 166 $mform->addElement('text', 'cmidnumber', get_string('idnumber'));
71ee4471 167
e24b7f85 168 $this->standard_hidden_coursemodule_elements();
169 }
170
171 function standard_hidden_coursemodule_elements(){
172 $mform =& $this->_form;
173 $mform->addElement('hidden', 'course', 0);
174 $mform->setType('course', PARAM_INT);
175
176 $mform->addElement('hidden', 'coursemodule', 0);
177 $mform->setType('coursemodule', PARAM_INT);
178
179 $mform->addElement('hidden', 'section', 0);
180 $mform->setType('section', PARAM_INT);
181
182 $mform->addElement('hidden', 'module', 0);
183 $mform->setType('module', PARAM_INT);
184
185 $mform->addElement('hidden', 'modulename', '');
186 $mform->setType('modulename', PARAM_SAFEDIR);
187
188 $mform->addElement('hidden', 'instance', 0);
189 $mform->setType('instance', PARAM_INT);
190
191 $mform->addElement('hidden', 'add', 0);
192 $mform->setType('add', PARAM_ALPHA);
193
194 $mform->addElement('hidden', 'update', 0);
195 $mform->setType('update', PARAM_INT);
19110c57 196
197 $mform->addElement('hidden', 'return', 0);
198 $mform->setType('return', PARAM_BOOL);
e24b7f85 199 }
200
e24b7f85 201}
202
f3b783f4 203?>