MDL-20353 adding missing data types of hidden form fields
[moodle.git] / course / edit_form.php
1 <?php  //$Id$
3 require_once($CFG->libdir.'/formslib.php');
5 class course_edit_form extends moodleform {
7     function definition() {
8         global $USER, $CFG, $DB;
10         $courseconfig = get_config('moodlecourse');
11         $mform    =& $this->_form;
13         $course   = $this->_customdata['course'];
14         $category = $this->_customdata['category'];
16         $systemcontext = get_context_instance(CONTEXT_SYSTEM);
17         $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
19         $disable_meta = false; // basic meta course state protection; server-side security checks not needed
21         if (!empty($course)) {
22             $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
23             $context = $coursecontext;
25             if (course_in_meta($course)) {
26                 $disable_meta = get_string('metaalreadyinmeta');
28             } else if ($course->metacourse) {
29                 if ($DB->count_records('course_meta', array('parent_course'=>$course->id)) > 0) {
30                     $disable_meta = get_string('metaalreadyhascourses');
31                 }
33             } else {
34                 // if users already enrolled directly into coures, do not allow switching to meta,
35                 // users with metacourse manage permission are exception
36                 // please note that we do not need exact results - anything unexpected here prevents metacourse
37                 $managers = get_users_by_capability($coursecontext, 'moodle/course:managemetacourse', 'u.id');
38                 $enrolroles = get_roles_with_capability('moodle/course:view', CAP_ALLOW, $coursecontext);
39                 if ($users = get_role_users(array_keys($enrolroles), $coursecontext, false, 'u.id', 'u.id ASC')) {
40                     foreach($users as $user) {
41                         if (!isset($managers[$user->id])) {
42                             $disable_meta = get_string('metaalreadyhasenrolments');
43                             break;
44                         }
45                     }
46                 }
47                 unset($managers);
48                 unset($users);
49                 unset($enrolroles);
50             }
51         } else {
52             $coursecontext = null;
53             $context = $categorycontext;
54         }
56 /// form definition with new course defaults
57 //--------------------------------------------------------------------------------
58         $mform->addElement('header','general', get_string('general', 'form'));
60         // Must have create course capability in both categories in order to move course
61         if (has_capability('moodle/course:create', $categorycontext)) {
62             $displaylist = array();
63             $parentlist = array();
64             make_categories_list($displaylist, $parentlist, 'moodle/course:create');
65             $mform->addElement('select', 'category', get_string('category'), $displaylist);
66         } else {
67             $mform->addElement('hidden', 'category', null);
68             $mform->setType('category', PARAM_INT);
69         }
70         $mform->setHelpButton('category', array('coursecategory', get_string('category')));
71         $mform->setDefault('category', $category->id);
72         $mform->setType('category', PARAM_INT);
74         $fullname  = get_string('defaultcoursefullname');
75         $shortname = get_string('defaultcourseshortname');
76         while ($DB->record_exists('course', array('fullname'=>$fullname))
77             or $DB->record_exists('course', array('fullname'=>$fullname))) {
78             $fullname++;
79             $shortname++;
80         }
82         $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
83         $mform->setHelpButton('fullname', array('coursefullname', get_string('fullnamecourse')), true);
84         $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
85         $mform->setType('fullname', PARAM_MULTILANG);
86         if ($course and !has_capability('moodle/course:changefullname', $coursecontext)) {
87             $mform->hardFreeze('fullname');
88             $mform->setConstant('fullname', $course->fullname);
89         }
91         $mform->setDefault('fullname', $fullname);
93         $mform->addElement('text','shortname', get_string('shortnamecourse'),'maxlength="100" size="20"');
94         $mform->setHelpButton('shortname', array('courseshortname', get_string('shortnamecourse')), true);
95         $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
96         $mform->setType('shortname', PARAM_MULTILANG);
97         if ($course and !has_capability('moodle/course:changeshortname', $coursecontext)) {
98             $mform->hardFreeze('shortname');
99             $mform->setConstant('shortname', $course->shortname);
100         }
102         $mform->setDefault('shortname', $shortname);
104         $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100"  size="10"');
105         $mform->setHelpButton('idnumber', array('courseidnumber', get_string('idnumbercourse')), true);
106         $mform->setType('idnumber', PARAM_RAW);
107         if ($course and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
108             $mform->hardFreeze('idnumber');
109             $mform->setConstants('idnumber', $course->idnumber);
110         }
112         $mform->addElement('htmleditor','summary', get_string('summary'), array('rows'=> '10', 'cols'=>'65'));
113         $mform->setHelpButton('summary', array('text2', get_string('helptext')), true);
114         $mform->setType('summary', PARAM_RAW);
116         $courseformats = get_plugin_list('format');
117         $formcourseformats = array();
118         foreach ($courseformats as $courseformat => $formatdir) {
119             $formcourseformats["$courseformat"] = get_string("format$courseformat", "format_$courseformat");
120             if($formcourseformats["$courseformat"]=="[[format$courseformat]]") {
121                 $formcourseformats["$courseformat"] = get_string("format$courseformat");
122             }
123         }
124         $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
125         $mform->setHelpButton('format', array('courseformats', get_string('courseformats')), true);
126         $mform->setDefault('format', $courseconfig->format);
128         for ($i=1; $i<=52; $i++) {
129           $sectionmenu[$i] = "$i";
130         }
131         $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
132         $mform->setDefault('numsections', $courseconfig->numsections);
134         $mform->addElement('date_selector', 'startdate', get_string('startdate'));
135         $mform->setHelpButton('startdate', array('coursestartdate', get_string('startdate')), true);
136         $mform->setDefault('startdate', time() + 3600 * 24);
138         $choices = array();
139         $choices['0'] = get_string('hiddensectionscollapsed');
140         $choices['1'] = get_string('hiddensectionsinvisible');
141         $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
142         $mform->setHelpButton('hiddensections', array('coursehiddensections', get_string('hiddensections')), true);
143         $mform->setDefault('hiddensections', $courseconfig->hiddensections);
145         $options = range(0, 10);
146         $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
147         $mform->setHelpButton('newsitems', array('coursenewsitems', get_string('newsitemsnumber')), true);
148         $mform->setDefault('newsitems', $courseconfig->newsitems);
150         $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
151         $mform->setHelpButton('showgrades', array('coursegrades', get_string('grades')), true);
152         $mform->setDefault('showgrades', $courseconfig->showgrades);
154         $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
155         $mform->setHelpButton('showreports', array('coursereports', get_string('activityreport')), true);
156         $mform->setDefault('showreports', $courseconfig->showreports);
158         $choices = get_max_upload_sizes($CFG->maxbytes);
159         $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
160         $mform->setHelpButton('maxbytes', array('courseuploadsize', get_string('maximumupload')), true);
161         $mform->setDefault('maxbytes', $courseconfig->maxbytes);
163         if (!empty($CFG->allowcoursethemes)) {
164             $themes=array();
165             $themes[''] = get_string('forceno');
166             $themes += get_list_of_themes();
167             $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
168         }
170         $meta=array();
171         $meta[0] = get_string('no');
172         $meta[1] = get_string('yes');
173         if ($disable_meta === false) {
174             $mform->addElement('select', 'metacourse', get_string('managemeta'), $meta);
175             $mform->setHelpButton('metacourse', array('metacourse', get_string('metacourse')), true);
176             $mform->setDefault('metacourse', $courseconfig->metacourse);
177         } else {
178             // no metacourse element - we do not want to change it anyway!
179             $mform->addElement('static', 'nometacourse', get_string('managemeta'),
180                 ((empty($course->metacourse)) ? $meta[0] : $meta[1]) . " - $disable_meta ");
181             $mform->setHelpButton('nometacourse', array('metacourse', get_string('metacourse')), true);
182         }
184 //--------------------------------------------------------------------------------
185         $mform->addElement('header','enrolhdr', get_string('enrolments'));
187         $choices = array();
188         $modules = explode(',', $CFG->enrol_plugins_enabled);
189         foreach ($modules as $module) {
190             $name = get_string('enrolname', "enrol_$module");
191             $plugin = enrolment_factory::factory($module);
192             if (method_exists($plugin, 'print_entry')) {
193                 $choices[$name] = $module;
194             }
195         }
196         asort($choices);
197         $choices = array_flip($choices);
198         $choices = array_merge(array('' => get_string('sitedefault').' ('.get_string('enrolname', "enrol_$CFG->enrol").')'), $choices);
199         $mform->addElement('select', 'enrol', get_string('enrolmentplugins'), $choices);
200         $mform->setHelpButton('enrol', array('courseenrolmentplugins', get_string('enrolmentplugins')), true);
201         $mform->setDefault('enrol', $courseconfig->enrol);
204         $roles = get_assignable_roles($context);
205         if (!empty($course)) {
206             // add current default role, so that it is selectable even when user can not assign it
207             if ($current_role = $DB->get_record('role', array('id'=>$course->defaultrole))) {
208                 $roles[$current_role->id] = strip_tags(format_string($current_role->name, true));
209             }
210         }
211         $choices = array();
212         if ($sitedefaultrole = $DB->get_record('role', array('id'=>$CFG->defaultcourseroleid))) {
213             $choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name.')';
214         } else {
215             $choices[0] = get_string('sitedefault');
216         }
217         $choices = $choices + $roles;
219         // fix for MDL-9197
220         foreach ($choices as $choiceid => $choice) {
221             $choices[$choiceid] = format_string($choice);
222         }
224         $mform->addElement('select', 'defaultrole', get_string('defaultrole', 'role'), $choices);
225         $mform->setDefault('defaultrole', 0);
228         $radio = array();
229         $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('no'), 0);
230         $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('yes'), 1);
231         $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('enroldate'), 2);
232         $mform->addGroup($radio, 'enrollable', get_string('enrollable'), ' ', false);
233         $mform->setHelpButton('enrollable', array('courseenrollable2', get_string('enrollable')), true);
234         $mform->setDefault('enrollable', $courseconfig->enrollable);
236         $mform->addElement('date_selector', 'enrolstartdate', get_string('enrolstartdate'), array('optional' => true));
237         $mform->setDefault('enrolstartdate', 0);
238         $mform->disabledIf('enrolstartdate', 'enrollable', 'neq', 2);
240         $mform->addElement('date_selector', 'enrolenddate', get_string('enrolenddate'), array('optional' => true));
241         $mform->setDefault('enrolenddate', 0);
242         $mform->disabledIf('enrolenddate', 'enrollable', 'neq', 2);
244         $mform->addElement('duration', 'enrolperiod', get_string('enrolperiod'), array('optional' => true, 'defaultunit' => 86400));
245         $mform->setDefault('enrolperiod', $courseconfig->enrolperiod);
248 //--------------------------------------------------------------------------------
249         $mform->addElement('header','expirynotifyhdr', get_string('expirynotify'));
251         $choices = array();
252         $choices['0'] = get_string('no');
253         $choices['1'] = get_string('yes');
254         $mform->addElement('select', 'expirynotify', get_string('notify'), $choices);
255         $mform->setHelpButton('expirynotify', array('expirynotify', get_string('expirynotify')), true);
256         $mform->setDefault('expirynotify', $courseconfig->expirynotify);
258         $mform->addElement('select', 'notifystudents', get_string('expirynotifystudents'), $choices);
259         $mform->setHelpButton('notifystudents', array('expirynotifystudents', get_string('expirynotifystudents')), true);
260         $mform->setDefault('notifystudents', $courseconfig->notifystudents);
262         $thresholdmenu=array();
263         for ($i=1; $i<=30; $i++) {
264             $seconds = $i * 86400;
265             $thresholdmenu[$seconds] = get_string('numdays', '', $i);
266         }
267         $mform->addElement('select', 'expirythreshold', get_string('expirythreshold'), $thresholdmenu);
268         $mform->setHelpButton('expirythreshold', array('expirythreshold', get_string('expirythreshold')), true);
269         $mform->setDefault('expirythreshold', $courseconfig->expirythreshold);
271 //--------------------------------------------------------------------------------
272         $mform->addElement('header','', get_string('groups', 'group'));
274         $choices = array();
275         $choices[NOGROUPS] = get_string('groupsnone', 'group');
276         $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
277         $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
278         $mform->addElement('select', 'groupmode', get_string('groupmode'), $choices);
279         $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')), true);
280         $mform->setDefault('groupmode', $courseconfig->groupmode);
282         $choices = array();
283         $choices['0'] = get_string('no');
284         $choices['1'] = get_string('yes');
285         $mform->addElement('select', 'groupmodeforce', get_string('force'), $choices);
286         $mform->setHelpButton('groupmodeforce', array('groupmodeforce', get_string('groupmodeforce')), true);
287         $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
289         if (!empty($CFG->enablegroupings)) {
290             //default groupings selector
291             $options = array();
292             $options[0] = get_string('none');
293             $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
294         }
296 //--------------------------------------------------------------------------------
297         $mform->addElement('header','', get_string('availability'));
299         $choices = array();
300         $choices['0'] = get_string('courseavailablenot');
301         $choices['1'] = get_string('courseavailable');
302         $mform->addElement('select', 'visible', get_string('availability'), $choices);
303         $mform->setHelpButton('visible', array('courseavailability', get_string('availability')), true);
304         $mform->setDefault('visible', $courseconfig->visible);
305         if ($course and !has_capability('moodle/course:visibility', $coursecontext)) {
306             $mform->hardFreeze('visible');
307             $mform->setConstant('visible', $course->visible);
308         }
310         $mform->addElement('passwordunmask', 'enrolpassword', get_string('enrolmentkey'), 'size="25"');
311         $mform->setHelpButton('enrolpassword', array('enrolmentkey', get_string('enrolmentkey')), true);
312         $mform->setDefault('enrolpassword', '');
313         $mform->setDefault('enrolpassword', $courseconfig->enrolpassword);
314         $mform->setType('enrolpassword', PARAM_RAW);
316         if (empty($course) or ($course->password !== '' and $course->id != SITEID)) {
317             // do not require password in existing courses that do not have password yet - backwards compatibility ;-)
318             if (!empty($CFG->enrol_manual_requirekey)) {
319                 $mform->addRule('enrolpassword', get_string('required'), 'required', null, 'client');
320             }
321         }
323         $choices = array();
324         $choices['0'] = get_string('guestsno');
325         $choices['1'] = get_string('guestsyes');
326         $choices['2'] = get_string('guestskey');
327         $mform->addElement('select', 'guest', get_string('opentoguests'), $choices);
328         $mform->setHelpButton('guest', array('guestaccess', get_string('opentoguests')), true);
329         $mform->setDefault('guest', $courseconfig->guest);
331         // 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
332         $enrol_object = $CFG;
333         if (!empty($course)) {
334             $enrol_object = $course;
335         }
336         // If the print_entry method exists and the course enrol method isn't manual (both set or inherited from site), show cost
337         if (method_exists(enrolment_factory::factory($enrol_object->enrol), 'print_entry') && !($enrol_object->enrol == 'manual' || (empty($enrol_object->enrol) && $CFG->enrol == 'manual'))) {
338             $costgroup=array();
339             $currencies = get_list_of_currencies();
340             $costgroup[]= &MoodleQuickForm::createElement('text','cost', '', 'maxlength="6" size="6"');
341             $costgroup[]= &MoodleQuickForm::createElement('select', 'currency', '', $currencies);
342             $mform->addGroup($costgroup, 'costgrp', get_string('cost'), '&nbsp;', false);
343             //defining a rule for a form element within a group :
344             $costgrprules=array();
345             //set the message to null to tell Moodle to use a default message
346             //available for most rules, fetched from language pack (err_{rulename}).
347             $costgrprules['cost'][]=array(null, 'numeric', null, 'client');
348             $mform->addGroupRule('costgrp',$costgrprules);
349             $mform->setHelpButton('costgrp', array('cost', get_string('cost')), true);
350             $mform->setDefault('cost', '');
351             $mform->setDefault('currency', empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency);
353         }
355 //--------------------------------------------------------------------------------
356         $mform->addElement('header','', get_string('language'));
358         $languages=array();
359         $languages[''] = get_string('forceno');
360         $languages += get_list_of_languages();
361         $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
362         $mform->setDefault('lang', $courseconfig->lang);
364 //--------------------------------------------------------------------------------
365         require_once($CFG->libdir.'/completionlib.php');
366         if(completion_info::is_enabled_for_site()) {
367             $mform->addElement('header','', get_string('progress','completion'));
368             $mform->addElement('select', 'enablecompletion', get_string('completion','completion'),
369                 array(0=>get_string('completiondisabled','completion'), 1=>get_string('completionenabled','completion')));
370             $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
371         } else {
372             $mform->addElement('hidden', 'enablecompletion');
373             $mform->setType('enablecompletion', PARAM_INT);
374             $mform->setDefault('enablecompletion',0);
375         }
377 //--------------------------------------------------------------------------------
378         if (has_capability('moodle/site:config', $systemcontext) && ((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
379             $mform->addElement('header', '', get_string('restrictmodules'));
381             $options = array();
382             $options['0'] = get_string('no');
383             $options['1'] = get_string('yes');
384             $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
385             $mods = array(0=>get_string('allownone'));
386             $mods += $DB->get_records_menu('modules', array(), 'name', 'id, name');
389             $mform->addElement('select', 'allowedmods', get_string('to'), $mods,
390                             array('multiple'=>'multiple', 'size'=>'10'));
391             $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
392         } else {
393             $mform->addElement('hidden', 'restrictmodules', null);
394             $mform->setType('restrictmodules', PARAM_INT);
395         }
396         if ($CFG->restrictmodulesfor == 'all') {
397             $mform->setDefault('allowedmods', explode(',',$CFG->defaultallowedmodules));
398             if (!empty($CFG->restrictbydefault)) {
399                 $mform->setDefault('restrictmodules', 1);
400             }
401         }
402         $mform->setType('restrictmodules', PARAM_INT);
404 /// customizable role names in this course
405 //--------------------------------------------------------------------------------
406         $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
407         $mform->setHelpButton('rolerenaming', array('rolerenaming', get_string('rolerenaming')), true);
409         if ($roles = get_all_roles()) {
410             if ($coursecontext) {
411                 $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS_RAW);
412             }
413             $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
414             foreach ($roles as $role) {
415                 $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->name));
416                 if (isset($role->localname)) {
417                     $mform->setDefault('role_'.$role->id, $role->localname);
418                 }
419                 $mform->setType('role_'.$role->id, PARAM_TEXT);
420                 if (!in_array($role->id, $assignableroles)) {
421                     $mform->setAdvanced('role_'.$role->id);
422                 }
423             }
424         }
426 //--------------------------------------------------------------------------------
427         $this->add_action_buttons();
428 //--------------------------------------------------------------------------------
429         $mform->addElement('hidden', 'id', null);
430         $mform->setType('id', PARAM_INT);
431     }
433     function definition_after_data() {
434         global $DB;
436         $mform =& $this->_form;
438         // add availabe groupings
439         if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
440             $options = array();
441             if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
442                 foreach ($groupings as $grouping) {
443                     $options[$grouping->id] = format_string($grouping->name);
444                 }
445             }
446             $gr_el =& $mform->getElement('defaultgroupingid');
447             $gr_el->load($options);
448         }
449     }
452 /// perform some extra moodle validation
453     function validation($data, $files) {
454         global $DB, $CFG;
456         $errors = parent::validation($data, $files);
457         if ($foundcourses = $DB->get_records('course', array('shortname'=>$data['shortname']))) {
458             if (!empty($data['id'])) {
459                 unset($foundcourses[$data['id']]);
460             }
461             if (!empty($foundcourses)) {
462                 foreach ($foundcourses as $foundcourse) {
463                     $foundcoursenames[] = $foundcourse->fullname;
464                 }
465                 $foundcoursenamestring = implode(',', $foundcoursenames);
466                 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
467             }
468         }
470         if (!empty($data['enrolstartdate']) && !empty($data['enrolenddate']) &&
471                 $data['enrolenddate'] <= $data['enrolstartdate']){
472             $errors['enrolenddate'] = get_string('enrolenddaterror');
473         }
475         if (!empty($CFG->enrol_manual_usepasswordpolicy) and isset($data['enrolpassword']) and $data['enrolpassword'] != '') {
476             $course = $this->_customdata['course'];
477             if ($course->password !== $data['enrolpassword']) {
478                 // enforce password policy only if changing password - backwards compatibility
479                 $errmsg = '';
480                 if (!check_password_policy($data['enrolpassword'], $errmsg)) {
481                     $errors['enrolpassword'] = $errmsg;
482                 }
483             }
484         }
486         return $errors;
487     }
489 ?>