MDL-10870 All files updated to new build_navigation() method.
[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.
19 * This is always the section number itself.
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 }
67 }
68
60243313 69 // form verification
70 function validation($data) {
71 global $COURSE;
72
73 $errors = array();
74
75 $grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$data['modulename'],
76 'iteminstance'=>$data['instance'], 'itemnumber'=>0, 'courseid'=>$COURSE->id));
77 if ($data['coursemodule']) {
78 $cm = get_record('course_modules', 'id', $data['coursemodule']);
79 } else {
80 $cm = null;
81 }
82
83 // verify the idnumber
84 if (!grade_verify_idnumber($data['cmidnumber'], $grade_item, $cm)) {
85 $errors['cmidnumber'] = get_string('idnumbertaken');
86 }
87
88 if (count($errors) == 0) {
89 return true;
90 } else {
91 return $errors;
92 }
93 }
94
e24b7f85 95 /**
96 * Load in existing data as form defaults. Usually new entry defaults are stored directly in
97 * form definition (new entry form); this function is used to load in data where values
98 * already exist and data is being edited (edit entry form).
99 *
100 * @param mixed $default_values object or array of default values
101 */
32db0d42 102 function set_data($default_values) {
e24b7f85 103 if (is_object($default_values)) {
104 $default_values = (array)$default_values;
105 }
ab6803a9 106 $this->data_preprocessing($default_values);
32db0d42 107 parent::set_data($default_values + $this->standard_coursemodule_elements_settings());//never slashed for moodleform_mod
e24b7f85 108 }
71ee4471 109
e24b7f85 110 /**
111 * Adds all the standard elements to a form to edit the settings for an activity module.
112 *
113 * @param bool $supportsgroups does this module support groups?
114 */
115 function standard_coursemodule_elements($supportsgroups=true){
75bde3cc 116 global $COURSE, $CFG;
e24b7f85 117 $mform =& $this->_form;
f3b783f4 118
119 if (!empty($CFG->enableoutcomes)) {
120 if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
121 $mform->addElement('header', 'modoutcomes', get_string('outcomes', 'grades'));
122 foreach($outcomes as $outcome) {
123 $mform->addElement('advcheckbox', 'outcome_'.$outcome->id, $outcome->get_name());
124 }
125 }
126 }
127
24e25bc1 128 $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
e24b7f85 129 if ($supportsgroups){
08103c93 130 // TODO: we must define this as mod property!
e24b7f85 131 $mform->addElement('modgroupmode', 'groupmode', get_string('groupmode'));
132 }
133 $mform->addElement('modvisible', 'visible', get_string('visible'));
2d11be3b 134 $mform->addElement('text', 'cmidnumber', get_string('idnumber'));
71ee4471 135
e24b7f85 136 $this->standard_hidden_coursemodule_elements();
137 }
138
139 function standard_hidden_coursemodule_elements(){
140 $mform =& $this->_form;
141 $mform->addElement('hidden', 'course', 0);
142 $mform->setType('course', PARAM_INT);
143
144 $mform->addElement('hidden', 'coursemodule', 0);
145 $mform->setType('coursemodule', PARAM_INT);
146
147 $mform->addElement('hidden', 'section', 0);
148 $mform->setType('section', PARAM_INT);
149
150 $mform->addElement('hidden', 'module', 0);
151 $mform->setType('module', PARAM_INT);
152
153 $mform->addElement('hidden', 'modulename', '');
154 $mform->setType('modulename', PARAM_SAFEDIR);
155
156 $mform->addElement('hidden', 'instance', 0);
157 $mform->setType('instance', PARAM_INT);
158
159 $mform->addElement('hidden', 'add', 0);
160 $mform->setType('add', PARAM_ALPHA);
161
162 $mform->addElement('hidden', 'update', 0);
163 $mform->setType('update', PARAM_INT);
19110c57 164
165 $mform->addElement('hidden', 'return', 0);
166 $mform->setType('return', PARAM_BOOL);
e24b7f85 167 }
168
169 /**
170 * This function is called by course/modedit.php to setup defaults for standard form
171 * elements.
172 *
173 * @param object $course
174 * @param object $cm
175 * @param integer $section
176 * @return unknown
177 */
178 function standard_coursemodule_elements_settings(){
2d11be3b 179 return ($this->modgroupmode_settings() + $this->modvisible_settings());
e24b7f85 180 }
181 /**
182 * This is called from modedit.php to load the default for the groupmode element.
183 *
184 * @param object $course
185 * @param object $cm
186 */
187 function modgroupmode_settings(){
188 global $COURSE;
189 return array('groupmode'=>groupmode($COURSE, $this->_cm));
190 }
191 /**
192 * This is called from modedit.php to set the default for modvisible form element.
193 *
194 * @param object $course
195 * @param object $cm
196 * @param integer $section section is a db id when updating a activity config
197 * or the section no when adding a new activity
198 */
199 function modvisible_settings(){
200 global $COURSE;
201 $cm=$this->_cm;
202 $section=$this->_section;
203 if ($cm) {
204 $visible = $cm->visible;
205 } else {
206 $visible = 1;
207 }
208
209 $hiddensection = !get_field('course_sections', 'visible', 'section', $section, 'course', $COURSE->id);
210 if ($hiddensection) {
211 $visible = 0;
212 }
213 return array('visible'=>$visible);
214 }
215
216}
217
f3b783f4 218?>