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