MDL-10439 yet another attempt to fix definition_after_data() - see the bug report...
[moodle.git] / course / edit_form.php
CommitLineData
a81bf9c3 1<?php //$Id$
2
39625498 3require_once($CFG->libdir.'/formslib.php');
4
a81bf9c3 5class course_edit_form extends moodleform {
6
7 function definition() {
8 global $USER, $CFG;
9
10 $mform =& $this->_form;
a81bf9c3 11
12 $course = $this->_customdata['course'];
13 $category = $this->_customdata['category'];
14
15 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
16 $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
17
18 $disable_meta = false; // basic meta course state protection; server-side security checks not needed
19
20 if (!empty($course)) {
21 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
22 $context = $coursecontext;
23
24 if (course_in_meta($course)) {
25 $disable_meta = get_string('metaalreadyinmeta');
26
27 } else if ($course->metacourse) {
3d397ee0 28 if (count_records('course_meta', 'parent_course', $course->id) > 0) {
a81bf9c3 29 $disable_meta = get_string('metaalreadyhascourses');
30 }
31
32 } else {
33 $managers = count(get_users_by_capability($coursecontext, 'moodle/course:managemetacourse'));
34 $participants = count(get_users_by_capability($coursecontext, 'moodle/course:view'));
35 if ($participants > $managers) {
36 $disable_meta = get_string('metaalreadyhasenrolments');
37 }
461f5127 38 }
39 } else {
a81bf9c3 40 $coursecontext = null;
41 $context = $categorycontext;
42 }
43
44/// form definition with new course defaults
45//--------------------------------------------------------------------------------
2b63df96 46 $mform->addElement('header','general', get_string('general', 'form'));
a81bf9c3 47
48 //must have create course capability in both categories in order to move course
49 if (has_capability('moodle/course:create', $categorycontext)) {
50 $displaylist = array();
51 $parentlist = array();
52 make_categories_list($displaylist, $parentlist);
53 foreach ($displaylist as $key=>$val) {
54 if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $key))) {
55 unset($displaylist[$key]);
56 }
461f5127 57 }
a81bf9c3 58 $mform->addElement('select', 'category', get_string('category'), $displaylist);
59 } else {
60 $mform->addElement('hidden', 'category', null);
461f5127 61 }
a81bf9c3 62 $mform->setHelpButton('category', array('coursecategory', get_string('category')));
63 $mform->setDefault('category', $category->id);
64 $mform->setType('category', PARAM_INT);
461f5127 65
a81bf9c3 66 $mform->addElement('text','fullname', get_string('fullname'),'maxlength="254" size="50"');
67 $mform->setHelpButton('fullname', array('coursefullname', get_string('fullname')), true);
68 $mform->setDefault('fullname', get_string('defaultcoursefullname'));
69 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
70 $mform->setType('fullname', PARAM_MULTILANG);
461f5127 71
a81bf9c3 72 $mform->addElement('text','shortname', get_string('shortname'),'maxlength="15" size="10"');
73 $mform->setHelpButton('shortname', array('courseshortname', get_string('shortname')), true);
74 $mform->setDefault('shortname', get_string('defaultcourseshortname'));
75 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
76 $mform->setType('shortname', PARAM_MULTILANG);
01963b95 77
a81bf9c3 78 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
79 $mform->setHelpButton('idnumber', array('courseidnumber', get_string('idnumbercourse')), true);
80 $mform->setType('idnumber', PARAM_RAW);
01963b95 81
a81bf9c3 82 $mform->addElement('htmleditor','summary', get_string('summary'), array('rows'=> '10', 'cols'=>'65'));
83 $mform->setHelpButton('summary', array('text', get_string('helptext')), true);
84 $mform->setDefault('summary', get_string('defaultcoursesummary'));
85 $mform->addRule('summary', get_string('missingsummary'), 'required', null, 'client');
86 $mform->setType('summary', PARAM_RAW);
87
88 $courseformats = get_list_of_plugins('course/format');
89 $formcourseformats = array();
90 foreach ($courseformats as $courseformat) {
ae628043 91 $formcourseformats["$courseformat"] = get_string("format$courseformat","format_$courseformat");
92 if($formcourseformats["$courseformat"]=="[[format$courseformat]]") {
93 $formcourseformats["$courseformat"] = get_string("format$courseformat");
a23f0aaf 94 }
a81bf9c3 95 }
96 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
97 $mform->setHelpButton('format', array('courseformats', get_string('courseformats')), true);
98 $mform->setDefault('format', 'weeks');
99
100 for ($i=1; $i<=52; $i++) {
101 $sectionmenu[$i] = "$i";
102 }
103 $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
104 $mform->setDefault('numsections', 10);
105
106 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
107 $mform->setHelpButton('startdate', array('coursestartdate', get_string('startdate')), true);
108 $mform->setDefault('startdate', time() + 3600 * 24);
109
110 $choices = array();
111 $choices['0'] = get_string('hiddensectionscollapsed');
112 $choices['1'] = get_string('hiddensectionsinvisible');
113 $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
114 $mform->setHelpButton('hiddensections', array('coursehiddensections', get_string('hiddensections')), true);
115 $mform->setDefault('hiddensections', 0);
116
65877218 117 $options = range(0, 10);
a81bf9c3 118 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
119 $mform->setHelpButton('newsitems', array('coursenewsitems', get_string('newsitemsnumber')), true);
120 $mform->setDefault('newsitems', 5);
121
65877218 122 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
a81bf9c3 123 $mform->setHelpButton('showgrades', array('coursegrades', get_string('grades')), true);
124 $mform->setDefault('showgrades', 1);
125
65877218 126 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
a81bf9c3 127 $mform->setHelpButton('showreports', array('coursereports', get_string('activityreport')), true);
128 $mform->setDefault('showreports', 0);
129
130 $choices = get_max_upload_sizes($CFG->maxbytes);
131 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
132 $mform->setHelpButton('maxbytes', array('courseuploadsize', get_string('maximumupload')), true);
133
134 if (!empty($CFG->allowcoursethemes)) {
135 $themes=array();
136 $themes[''] = get_string('forceno');
137 $themes += get_list_of_themes();
138 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
139 }
461f5127 140
01963b95 141 $meta=array();
142 $meta[0] = get_string('no');
143 $meta[1] = get_string('yes');
a81bf9c3 144 if ($disable_meta === false) {
145 $mform->addElement('select', 'metacourse', get_string('managemeta'), $meta);
3d397ee0 146 $mform->setHelpButton('metacourse', array('metacourse', get_string('metacourse')), true);
147 $mform->setDefault('metacourse', 0);
a81bf9c3 148 } else {
3d397ee0 149 // no metacourse element - we do not want to change it anyway!
150 $mform->addElement('static', 'nometacourse', get_string('managemeta'),
a81bf9c3 151 ((empty($course->metacourse)) ? $meta[0] : $meta[1]) . " - $disable_meta ");
3d397ee0 152 $mform->setHelpButton('nometacourse', array('metacourse', get_string('metacourse')), true);
a81bf9c3 153 }
01963b95 154
a81bf9c3 155 $roles = get_assignable_roles($context);
156 if (!empty($course)) {
157 // add current default role, so that it is selectable even when user can not assign it
158 if ($current_role = get_record('role', 'id', $course->defaultrole)) {
159 $roles[$current_role->id] = strip_tags(format_string($current_role->name, true));
160 }
161 }
162 $choices = array();
163 if ($sitedefaultrole = get_record('role', 'id', $CFG->defaultcourseroleid)) {
164 $choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name.')';
01963b95 165 } else {
a81bf9c3 166 $choices[0] = get_string('sitedefault');
01963b95 167 }
a81bf9c3 168 $choices = $choices + $roles;
f20d7c41 169
170 // fix for MDL-9197
171 foreach ($choices as $choiceid => $choice) {
172 $choices[$choiceid] = format_string($choice);
173 }
174
a81bf9c3 175 $mform->addElement('select', 'defaultrole', get_string('defaultrole', 'role'), $choices);
176 $mform->setDefault('defaultrole', 0);
177
178//--------------------------------------------------------------------------------
179 $mform->addElement('header','enrolhdr', get_string('enrolments'));
180
e517b6da 181 $choices = array();
a81bf9c3 182 $modules = explode(',', $CFG->enrol_plugins_enabled);
183 foreach ($modules as $module) {
184 $name = get_string('enrolname', "enrol_$module");
185 $plugin = enrolment_factory::factory($module);
186 if (method_exists($plugin, 'print_entry')) {
187 $choices[$name] = $module;
188 }
01963b95 189 }
a81bf9c3 190 asort($choices);
191 $choices = array_flip($choices);
192 $choices = array_merge(array('' => get_string('sitedefault').' ('.get_string('enrolname', "enrol_$CFG->enrol").')'), $choices);
193 $mform->addElement('select', 'enrol', get_string('enrolmentplugins'), $choices);
194 $mform->setHelpButton('enrol', array('courseenrolmentplugins', get_string('enrolmentplugins')), true);
01963b95 195
a81bf9c3 196 $radio = array();
197 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('no'), 0);
198 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('yes'), 1);
199 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('enroldate'), 2);
200 $mform->addGroup($radio, 'enrollable', get_string('enrollable'), ' ', false);
6be58b1c 201 $mform->setHelpButton('enrollable', array('courseenrollable2', get_string('enrollable')), true);
a81bf9c3 202 $mform->setDefault('enrollable', 1);
203
204 $enroldatestartgrp = array();
205 $enroldatestartgrp[] = &MoodleQuickForm::createElement('date_selector', 'enrolstartdate');
206 $enroldatestartgrp[] = &MoodleQuickForm::createElement('checkbox', 'enrolstartdisabled', null, get_string('disable'));
2dbd6409 207 $mform->addGroup($enroldatestartgrp, 'enrolstartdategrp', get_string('enrolstartdate'), ' ', false);
a81bf9c3 208 $mform->setDefault('enrolstartdate', 0);
209 $mform->setDefault('enrolstartdisabled', 1);
2dbd6409 210 $mform->disabledIf('enrolstartdategrp', 'enrolstartdisabled', 'checked');
a81bf9c3 211
212 $enroldateendgrp = array();
213 $enroldateendgrp[] = &MoodleQuickForm::createElement('date_selector', 'enrolenddate');
214 $enroldateendgrp[] = &MoodleQuickForm::createElement('checkbox', 'enrolenddisabled', null, get_string('disable'));
215 $mform->addGroup($enroldateendgrp, 'enroldateendgrp', get_string('enrolenddate'), ' ', false);
216 $mform->setDefault('enrolenddate', 0);
217 $mform->setDefault('enrolenddisabled', 1);
2dbd6409 218 $mform->disabledIf('enroldateendgrp', 'enrolenddisabled', 'checked');
a81bf9c3 219
220 $periodmenu=array();
221 $periodmenu[0] = get_string('unlimited');
222 for ($i=1; $i<=365; $i++) {
223 $seconds = $i * 86400;
224 $periodmenu[$seconds] = get_string('numdays', '', $i);
225 }
226 $mform->addElement('select', 'enrolperiod', get_string('enrolperiod'), $periodmenu);
227 $mform->setDefault('enrolperiod', 0);
228
229//--------------------------------------------------------------------------------
230 $mform->addElement('header','expirynotifyhdr', get_string('expirynotify'));
231
232 $choices = array();
233 $choices['0'] = get_string('no');
234 $choices['1'] = get_string('yes');
ec3dac98 235 $mform->addElement('select', 'expirynotify', get_string('notify'), $choices);
a81bf9c3 236 $mform->setHelpButton('expirynotify', array('expirynotify', get_string('expirynotify')), true);
237 $mform->setDefault('expirynotify', 0);
238
239 $mform->addElement('select', 'notifystudents', get_string('expirynotifystudents'), $choices);
240 $mform->setHelpButton('notifystudents', array('expirynotifystudents', get_string('expirynotifystudents')), true);
241 $mform->setDefault('notifystudents', 0);
242
243 $thresholdmenu=array();
244 for ($i=1; $i<=30; $i++) {
245 $seconds = $i * 86400;
246 $thresholdmenu[$seconds] = get_string('numdays', '', $i);
247 }
248 $mform->addElement('select', 'expirythreshold', get_string('expirythreshold'), $thresholdmenu);
249 $mform->setHelpButton('expirythreshold', array('expirythreshold', get_string('expirythreshold')), true);
250 $mform->setDefault('expirythreshold', 10 * 86400);
251
252//--------------------------------------------------------------------------------
ec3dac98 253 $mform->addElement('header','', get_string('groups', 'group'));
a81bf9c3 254
255 $choices = array();
ec3dac98 256 $choices[NOGROUPS] = get_string('no');
257 $choices[SEPARATEGROUPS] = get_string('separate');
258 $choices[VISIBLEGROUPS] = get_string('visible');
a81bf9c3 259 $mform->addElement('select', 'groupmode', get_string('groupmode'), $choices);
260 $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')), true);
261 $mform->setDefault('groupmode', 0);
262
263 $choices = array();
264 $choices['0'] = get_string('no');
265 $choices['1'] = get_string('yes');
266 $mform->addElement('select', 'groupmodeforce', get_string('force'), $choices);
267 $mform->setHelpButton('groupmodeforce', array('groupmodeforce', get_string('groupmodeforce')), true);
268 $mform->setDefault('groupmodeforce', 0);
269
270//--------------------------------------------------------------------------------
271 $mform->addElement('header','', get_string('availability'));
272
273 $choices = array();
274 $choices['0'] = get_string('courseavailablenot');
275 $choices['1'] = get_string('courseavailable');
276 $mform->addElement('select', 'visible', get_string('availability'), $choices);
277 $mform->setHelpButton('visible', array('courseavailability', get_string('availability')), true);
278 $mform->setDefault('visible', 1);
279
1898ccac 280 $mform->addElement('passwordunmask', 'enrolpassword', get_string('enrolmentkey'), 'size="25"');
281 $mform->setHelpButton('enrolpassword', array('enrolmentkey', get_string('enrolmentkey')), true);
282 $mform->setDefault('enrolpassword', '');
283 $mform->setType('enrolpassword', PARAM_RAW);
a81bf9c3 284
285 $choices = array();
286 $choices['0'] = get_string('guestsno');
287 $choices['1'] = get_string('guestsyes');
288 $choices['2'] = get_string('guestskey');
289 $mform->addElement('select', 'guest', get_string('opentoguests'), $choices);
290 $mform->setHelpButton('guest', array('guestaccess', get_string('opentoguests')), true);
291 $mform->setDefault('guest', 0);
a937b164 292
293 // If we are creating a course, its enrol method isn't yet chosen, BUT the site has a default enrol method which we can use here
294 $enrol_object = $CFG;
295 if (!empty($course)) {
82d3334a 296 $enrol_object = $course;
a937b164 297 }
298 if (method_exists(enrolment_factory::factory($enrol_object->enrol), 'print_entry') && $enrol_object->enrol != 'manual'){
a81bf9c3 299 $costgroup=array();
300 $currencies = get_list_of_currencies();
301 $costgroup[]= &MoodleQuickForm::createElement('text','cost', '', 'maxlength="6" size="6"');
302 $costgroup[]= &MoodleQuickForm::createElement('select', 'currency', '', $currencies);
303 $mform->addGroup($costgroup, 'costgrp', get_string('cost'), '&nbsp;', false);
5bc97c98 304 //defining a rule for a form element within a group :
305 $costgrprules=array();
306 //set the message to null to tell Moodle to use a default message
307 //available for most rules, fetched from language pack (err_{rulename}).
308 $costgrprules['cost'][]=array(null, 'numeric', null, 'client');
309 $mform->addGroupRule('costgrp',$costgrprules);
a81bf9c3 310 $mform->setHelpButton('costgrp', array('cost', get_string('cost')), true);
311 $mform->setDefault('cost', '');
312 $mform->setDefault('currency', empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency);
e995c46e 313
a81bf9c3 314 }
315
316//--------------------------------------------------------------------------------
317 $mform->addElement('header','', get_string('language'));
318
319 $languages=array();
320 $languages[''] = get_string('forceno');
321 $languages += get_list_of_languages();
322 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
323
324//--------------------------------------------------------------------------------
325 if (has_capability('moodle/site:config', $systemcontext) && ((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
326 $mform->addElement('header', '', get_string('restrictmodules'));
327
328 $options = array();
329 $options['0'] = get_string('no');
330 $options['1'] = get_string('yes');
2dbd6409 331 $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
a81bf9c3 332 $mods = array(0=>get_string('allownone'));
333 $mods += get_records_menu('modules', '','','','id, name');
a81bf9c3 334
335
2dbd6409 336 $mform->addElement('select', 'allowedmods', get_string('to'), $mods,
337 array('multiple'=>'multiple', 'size'=>'10'));
338 $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
339 } else {
a81bf9c3 340 $mform->addElement('hidden', 'restrictmodules', null);
341 }
342 if ($CFG->restrictmodulesfor == 'all') {
343 $mform->setDefault('allowedmods', explode(',',$CFG->defaultallowedmodules));
344 if (!empty($CFG->restrictbydefault)) {
345 $mform->setDefault('restrictmodules', 1);
346 }
347 }
348 $mform->setType('restrictmodules', PARAM_INT);
349
0150c561 350/// customizable role names in this course
351//--------------------------------------------------------------------------------
352 $mform->addElement('header','', get_string('roles'));
353
354 if ($roles = get_records('role')) {
355 foreach ($roles as $role) {
356 $mform->addElement('text', 'role_'.$role->id, $role->name);
357 if ($coursecontext) {
358 if ($rolename = get_record('role_names', 'roleid', $role->id, 'contextid', $coursecontext->id)) {
359 $mform->setDefault('role_'.$role->id, $rolename->text);
360 }
361 }
362 }
363 }
364
a81bf9c3 365//--------------------------------------------------------------------------------
a23f0aaf 366 $this->add_action_buttons();
a81bf9c3 367//--------------------------------------------------------------------------------
368 $mform->addElement('hidden', 'id', null);
369 $mform->setType('id', PARAM_INT);
370
371 // fill in default teacher and student names to keep backwards compatibility for a while
372 $mform->addElement('hidden', 'teacher', get_string('defaultcourseteacher'));
373 $mform->addElement('hidden', 'teachers', get_string('defaultcourseteachers'));
374 $mform->addElement('hidden', 'student', get_string('defaultcoursestudent'));
375 $mform->addElement('hidden', 'students', get_string('defaultcoursestudents'));
c08ac016 376 }
2dbd6409 377
a81bf9c3 378
379/// perform some extra moodle validation
380 function validation($data){
01963b95 381 $errors= array();
a81bf9c3 382 if ($foundcourses = get_records('course', 'shortname', $data['shortname'])) {
383 if (!empty($data['id'])) {
384 unset($foundcourses[$data['id']]);
01963b95 385 }
386 if (!empty($foundcourses)) {
387 foreach ($foundcourses as $foundcourse) {
388 $foundcoursenames[] = $foundcourse->fullname;
389 }
a81bf9c3 390 $foundcoursenamestring = implode(',', $foundcoursenames);
391 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
01963b95 392 }
01963b95 393 }
a81bf9c3 394
395 if (empty($data['enrolenddisabled'])){
396 if ($data['enrolenddate'] <= $data['enrolstartdate']){
397 $errors['enroldateendgrp'] = get_string('enrolenddaterror');
01963b95 398 }
399 }
a81bf9c3 400
401 if (0 == count($errors)){
01963b95 402 return true;
a81bf9c3 403 } else {
01963b95 404 return $errors;
405 }
406 }
a81bf9c3 407}
a937b164 408?>