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