Updated the 19 build version to 20100610
[moodle.git] / course / moodleform_mod.php
blob21c1d9fc73beb605f87981f8c702a9a81a7333e5
1 <?php
3 if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
7 require_once ($CFG->libdir.'/formslib.php');
8 /**
9 * This class adds extra methods to form wrapper specific to be used for module
10 * add / update forms (mod/{modname}.mod_form.php replaces deprecated mod/{modname}/mod.html
13 class moodleform_mod extends moodleform {
14 /**
15 * Instance of the module that is being updated. This is the id of the {prefix}{modulename}
16 * record. Can be used in form definition. Will be "" if this is an 'add' form and not an
17 * update one.
19 * @var mixed
21 var $_instance;
22 /**
23 * Section of course that module instance will be put in or is in.
24 * This is always the section number itself (column 'section' from 'course_sections' table).
26 * @var mixed
28 var $_section;
29 /**
30 * Coursemodle record of the module that is being updated. Will be null if this is an 'add' form and not an
31 * update one.
33 * @var mixed
35 var $_cm;
36 /**
37 * List of modform features
39 var $_features;
41 function moodleform_mod($instance, $section, $cm) {
42 $this->_instance = $instance;
43 $this->_section = $section;
44 $this->_cm = $cm;
45 parent::moodleform('modedit.php');
48 /**
49 * Only available on moodleform_mod.
51 * @param array $default_values passed by reference
53 function data_preprocessing(&$default_values){
56 /**
57 * Each module which defines definition_after_data() must call this method using parent::definition_after_data();
59 function definition_after_data() {
60 global $CFG, $COURSE;
61 $mform =& $this->_form;
63 if ($id = $mform->getElementValue('update')) {
64 $modulename = $mform->getElementValue('modulename');
65 $instance = $mform->getElementValue('instance');
67 if ($this->_features->gradecat) {
68 $gradecat = false;
69 if (!empty($CFG->enableoutcomes) and $this->_features->outcomes) {
70 if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
71 $gradecat = true;
74 if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$modulename,
75 'iteminstance'=>$instance, 'courseid'=>$COURSE->id))) {
76 foreach ($items as $item) {
77 if (!empty($item->outcomeid)) {
78 $elname = 'outcome_'.$item->outcomeid;
79 if ($mform->elementExists($elname)) {
80 $mform->hardFreeze($elname); // prevent removing of existing outcomes
84 foreach ($items as $item) {
85 if (is_bool($gradecat)) {
86 $gradecat = $item->categoryid;
87 continue;
89 if ($gradecat != $item->categoryid) {
90 //mixed categories
91 $gradecat = false;
92 break;
97 if ($gradecat === false) {
98 // items and outcomes in different categories - remove the option
99 // TODO: it might be better to add a "Mixed categories" text instead
100 if ($mform->elementExists('gradecat')) {
101 $mform->removeElement('gradecat');
107 if ($COURSE->groupmodeforce) {
108 if ($mform->elementExists('groupmode')) {
109 $mform->hardFreeze('groupmode'); // groupmode can not be changed if forced from course settings
113 if ($mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly') and empty($COURSE->groupmodeforce)) {
114 $mform->disabledIf('groupingid', 'groupmode', 'eq', NOGROUPS);
116 } else if (!$mform->elementExists('groupmode') and $mform->elementExists('groupmembersonly')) {
117 $mform->disabledIf('groupingid', 'groupmembersonly', 'notchecked');
119 } else if (!$mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly')) {
120 // groupings have no use without groupmode or groupmembersonly
121 if ($mform->elementExists('groupingid')) {
122 $mform->removeElement('groupingid');
127 // form verification
128 function validation($data, $files) {
129 global $COURSE;
130 $errors = parent::validation($data, $files);
132 $mform =& $this->_form;
134 $errors = array();
136 if ($mform->elementExists('name')) {
137 $name = trim($data['name']);
138 if ($name == '') {
139 $errors['name'] = get_string('required');
143 $grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$data['modulename'],
144 'iteminstance'=>$data['instance'], 'itemnumber'=>0, 'courseid'=>$COURSE->id));
145 if ($data['coursemodule']) {
146 $cm = get_record('course_modules', 'id', $data['coursemodule']);
147 } else {
148 $cm = null;
151 if ($mform->elementExists('cmidnumber')) {
152 // verify the idnumber
153 if (!grade_verify_idnumber($data['cmidnumber'], $COURSE->id, $grade_item, $cm)) {
154 $errors['cmidnumber'] = get_string('idnumbertaken');
158 return $errors;
162 * Load in existing data as form defaults. Usually new entry defaults are stored directly in
163 * form definition (new entry form); this function is used to load in data where values
164 * already exist and data is being edited (edit entry form).
166 * @param mixed $default_values object or array of default values
168 function set_data($default_values) {
169 if (is_object($default_values)) {
170 $default_values = (array)$default_values;
172 $this->data_preprocessing($default_values);
173 parent::set_data($default_values); //never slashed for moodleform_mod
177 * Adds all the standard elements to a form to edit the settings for an activity module.
179 * @param mixed array or object describing supported features - groups, groupings, groupmembersonly, etc.
181 function standard_coursemodule_elements($features=null){
182 global $COURSE, $CFG;
183 $mform =& $this->_form;
185 // deal with legacy $supportgroups param
186 if ($features === true or $features === false) {
187 $groupmode = $features;
188 $this->_features = new object();
189 $this->_features->groups = $groupmode;
191 } else if (is_array($features)) {
192 $this->_features = (object)$features;
194 } else if (empty($features)) {
195 $this->_features = new object();
197 } else {
198 $this->_features = $features;
201 if (!isset($this->_features->groups)) {
202 $this->_features->groups = true;
205 if (!isset($this->_features->groupings)) {
206 $this->_features->groupings = false;
209 if (!isset($this->_features->groupmembersonly)) {
210 $this->_features->groupmembersonly = false;
213 if (!isset($this->_features->outcomes)) {
214 $this->_features->outcomes = true;
217 if (!isset($this->_features->gradecat)) {
218 $this->_features->gradecat = true;
221 if (!isset($this->_features->idnumber)) {
222 $this->_features->idnumber = true;
225 $outcomesused = false;
226 if (!empty($CFG->enableoutcomes) and $this->_features->outcomes) {
227 if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
228 $outcomesused = true;
229 $mform->addElement('header', 'modoutcomes', get_string('outcomes', 'grades'));
230 foreach($outcomes as $outcome) {
231 $mform->addElement('advcheckbox', 'outcome_'.$outcome->id, $outcome->get_name());
236 $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
237 if ($this->_features->groups) {
238 $options = array(NOGROUPS => get_string('groupsnone'),
239 SEPARATEGROUPS => get_string('groupsseparate'),
240 VISIBLEGROUPS => get_string('groupsvisible'));
241 $mform->addElement('select', 'groupmode', get_string('groupmode'), $options, NOGROUPS);
242 $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')));
245 if (!empty($CFG->enablegroupings)) {
246 if ($this->_features->groupings or $this->_features->groupmembersonly) {
247 //groupings selector - used for normal grouping mode or also when restricting access with groupmembersonly
248 $options = array();
249 $options[0] = get_string('none');
250 if ($groupings = get_records('groupings', 'courseid', $COURSE->id)) {
251 foreach ($groupings as $grouping) {
252 $options[$grouping->id] = format_string($grouping->name);
255 $mform->addElement('select', 'groupingid', get_string('grouping', 'group'), $options);
256 $mform->setHelpButton('groupingid', array('grouping', get_string('grouping', 'group')));
257 $mform->setAdvanced('groupingid');
260 if ($this->_features->groupmembersonly) {
261 $mform->addElement('checkbox', 'groupmembersonly', get_string('groupmembersonly', 'group'));
262 $mform->setHelpButton('groupmembersonly', array('groupmembersonly', get_string('groupmembersonly', 'group')));
263 $mform->setAdvanced('groupmembersonly');
267 $mform->addElement('modvisible', 'visible', get_string('visible'));
269 if ($this->_features->idnumber) {
270 $mform->addElement('text', 'cmidnumber', get_string('idnumbermod'));
271 $mform->setHelpButton('cmidnumber', array('cmidnumber', get_string('idnumbermod')), true);
274 if ($this->_features->gradecat) {
275 $categories = grade_get_categories_menu($COURSE->id, $outcomesused);
276 $mform->addElement('select', 'gradecat', get_string('gradecategory', 'grades'), $categories);
279 $this->standard_hidden_coursemodule_elements();
282 function standard_hidden_coursemodule_elements(){
283 $mform =& $this->_form;
284 $mform->addElement('hidden', 'course', 0);
285 $mform->setType('course', PARAM_INT);
287 $mform->addElement('hidden', 'coursemodule', 0);
288 $mform->setType('coursemodule', PARAM_INT);
290 $mform->addElement('hidden', 'section', 0);
291 $mform->setType('section', PARAM_INT);
293 $mform->addElement('hidden', 'module', 0);
294 $mform->setType('module', PARAM_INT);
296 $mform->addElement('hidden', 'modulename', '');
297 $mform->setType('modulename', PARAM_SAFEDIR);
299 $mform->addElement('hidden', 'instance', 0);
300 $mform->setType('instance', PARAM_INT);
302 $mform->addElement('hidden', 'add', 0);
303 $mform->setType('add', PARAM_ALPHA);
305 $mform->addElement('hidden', 'update', 0);
306 $mform->setType('update', PARAM_INT);
308 $mform->addElement('hidden', 'return', 0);
309 $mform->setType('return', PARAM_BOOL);
313 * Overriding formslib's add_action_buttons() method, to add an extra submit "save changes and return" button.
315 * @param bool $cancel show cancel button
316 * @param string $submitlabel null means default, false means none, string is label text
317 * @param string $submit2label null means default, false means none, string is label text
318 * @return void
320 function add_action_buttons($cancel=true, $submitlabel=null, $submit2label=null) {
321 if (is_null($submitlabel)) {
322 $submitlabel = get_string('savechangesanddisplay');
325 if (is_null($submit2label)) {
326 $submit2label = get_string('savechangesandreturntocourse');
329 $mform =& $this->_form;
331 // elements in a row need a group
332 $buttonarray = array();
334 if ($submit2label !== false) {
335 $buttonarray[] = &$mform->createElement('submit', 'submitbutton2', $submit2label);
338 if ($submitlabel !== false) {
339 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
342 if ($cancel) {
343 $buttonarray[] = &$mform->createElement('cancel');
346 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
347 $mform->setType('buttonar', PARAM_RAW);
348 $mform->closeHeaderBefore('buttonar');