6837c7a46a8af2e3306b0b86f29919d858aad424
[moodle.git] / course / edit_form.php
1 <?php
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'];
15         $editoroptions = $this->_customdata['editoroptions'];
17         $systemcontext = get_context_instance(CONTEXT_SYSTEM);
18         $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
20         $disable_meta = false; // basic meta course state protection; server-side security checks not needed
21         if (!empty($course->id)) {
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:participate', 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         if (!empty($course->id) and !has_capability('moodle/course:changecategory', $coursecontext)) {
75             $mform->hardFreeze('category');
76             $mform->setConstant('category', $category->id);
77         }
79         $fullname  = get_string('defaultcoursefullname');
80         $shortname = get_string('defaultcourseshortname');
81         while ($DB->record_exists('course', array('fullname'=>$fullname))
82             or $DB->record_exists('course', array('fullname'=>$fullname))) {
83             $fullname++;
84             $shortname++;
85         }
87         $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
88         $mform->setHelpButton('fullname', array('coursefullname', get_string('fullnamecourse')), true);
89         $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
90         $mform->setType('fullname', PARAM_MULTILANG);
91         if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
92             $mform->hardFreeze('fullname');
93             $mform->setConstant('fullname', $course->fullname);
94         }
96         $mform->setDefault('fullname', $fullname);
98         $mform->addElement('text','shortname', get_string('shortnamecourse'),'maxlength="100" size="20"');
99         $mform->setHelpButton('shortname', array('courseshortname', get_string('shortnamecourse')), true);
100         $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
101         $mform->setType('shortname', PARAM_MULTILANG);
102         if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
103             $mform->hardFreeze('shortname');
104             $mform->setConstant('shortname', $course->shortname);
105         }
107         $mform->setDefault('shortname', $shortname);
109         $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100"  size="10"');
110         $mform->setHelpButton('idnumber', array('courseidnumber', get_string('idnumbercourse')), true);
111         $mform->setType('idnumber', PARAM_RAW);
112         if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
113             $mform->hardFreeze('idnumber');
114             $mform->setConstants('idnumber', $course->idnumber);
115         }
118         $mform->addElement('editor','summary_editor', get_string('summary'), null, $editoroptions);
119         $mform->setHelpButton('summary_editor', array('text2', get_string('helptext')), true);
120         $mform->setType('summary_editor', PARAM_RAW);
122         if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
123             $mform->hardFreeze('summary_editor');
124         }
126         $courseformats = get_plugin_list('format');
127         $formcourseformats = array();
128         foreach ($courseformats as $courseformat => $formatdir) {
129             $formcourseformats["$courseformat"] = get_string("format$courseformat", "format_$courseformat");
130             if($formcourseformats["$courseformat"]=="[[format$courseformat]]") {
131                 $formcourseformats["$courseformat"] = get_string("format$courseformat");
132             }
133         }
134         $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
135         $mform->setHelpButton('format', array('courseformats', get_string('courseformats')), true);
136         $mform->setDefault('format', $courseconfig->format);
138         for ($i=1; $i<=52; $i++) {
139           $sectionmenu[$i] = "$i";
140         }
141         $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
142         $mform->setDefault('numsections', $courseconfig->numsections);
144         $mform->addElement('date_selector', 'startdate', get_string('startdate'));
145         $mform->setHelpButton('startdate', array('coursestartdate', get_string('startdate')), true);
146         $mform->setDefault('startdate', time() + 3600 * 24);
148         $choices = array();
149         $choices['0'] = get_string('hiddensectionscollapsed');
150         $choices['1'] = get_string('hiddensectionsinvisible');
151         $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
152         $mform->setHelpButton('hiddensections', array('coursehiddensections', get_string('hiddensections')), true);
153         $mform->setDefault('hiddensections', $courseconfig->hiddensections);
155         $options = range(0, 10);
156         $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
157         $mform->setHelpButton('newsitems', array('coursenewsitems', get_string('newsitemsnumber')), true);
158         $mform->setDefault('newsitems', $courseconfig->newsitems);
160         $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
161         $mform->setHelpButton('showgrades', array('coursegrades', get_string('grades')), true);
162         $mform->setDefault('showgrades', $courseconfig->showgrades);
164         $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
165         $mform->setHelpButton('showreports', array('coursereports', get_string('activityreport')), true);
166         $mform->setDefault('showreports', $courseconfig->showreports);
168         $choices = get_max_upload_sizes($CFG->maxbytes);
169         $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
170         $mform->setHelpButton('maxbytes', array('courseuploadsize', get_string('maximumupload')), true);
171         $mform->setDefault('maxbytes', $courseconfig->maxbytes);
173         if (!empty($CFG->allowcoursethemes)) {
174             $themeobjects = get_list_of_themes();
175             $themes=array();
176             $themes[''] = get_string('forceno');
177             foreach ($themeobjects as $key=>$theme) {
178                 $themes[$key] = $theme->name;
179             }
180             $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
181         }
183         $meta=array();
184         $meta[0] = get_string('no');
185         $meta[1] = get_string('yes');
186         if ($disable_meta === false) {
187             $mform->addElement('select', 'metacourse', get_string('managemeta'), $meta);
188             $mform->setHelpButton('metacourse', array('metacourse', get_string('metacourse')), true);
189             $mform->setDefault('metacourse', $courseconfig->metacourse);
190         } else {
191             // no metacourse element - we do not want to change it anyway!
192             $mform->addElement('static', 'nometacourse', get_string('managemeta'),
193                 ((empty($course->metacourse)) ? $meta[0] : $meta[1]) . " - $disable_meta ");
194             $mform->setHelpButton('nometacourse', array('metacourse', get_string('metacourse')), true);
195         }
197 //--------------------------------------------------------------------------------
198         $mform->addElement('header','enrolhdr', get_string('enrolments'));
200         $choices = array();
201         $modules = explode(',', $CFG->enrol_plugins_enabled);
202         foreach ($modules as $module) {
203             $name = get_string('enrolname', "enrol_$module");
204             $plugin = enrolment_factory::factory($module);
205             if (method_exists($plugin, 'print_entry')) {
206                 $choices[$name] = $module;
207             }
208         }
209         asort($choices);
210         $choices = array_flip($choices);
211         $choices = array_merge(array('' => get_string('sitedefault').' ('.get_string('enrolname', "enrol_$CFG->enrol").')'), $choices);
212         $mform->addElement('select', 'enrol', get_string('enrolmentplugins'), $choices);
213         $mform->setHelpButton('enrol', array('courseenrolmentplugins', get_string('enrolmentplugins')), true);
214         $mform->setDefault('enrol', $courseconfig->enrol);
217         $roles = get_assignable_roles($context);
218         if (!empty($course->id)) {
219             // add current default role, so that it is selectable even when user can not assign it
220             if ($current_role = $DB->get_record('role', array('id'=>$course->defaultrole))) {
221                 $roles[$current_role->id] = strip_tags(format_string($current_role->name, true));
222             }
223         }
224         $choices = array();
225         if ($sitedefaultrole = $DB->get_record('role', array('id'=>$CFG->defaultcourseroleid))) {
226             $choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name.')';
227         } else {
228             $choices[0] = get_string('sitedefault');
229         }
230         $choices = $choices + $roles;
232         // fix for MDL-9197
233         foreach ($choices as $choiceid => $choice) {
234             $choices[$choiceid] = format_string($choice);
235         }
237         $mform->addElement('select', 'defaultrole', get_string('defaultrole', 'role'), $choices);
238         $mform->setDefault('defaultrole', 0);
241         $radio = array();
242         $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('no'), 0);
243         $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('yes'), 1);
244         $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('enroldate'), 2);
245         $mform->addGroup($radio, 'enrollable', get_string('enrollable'), ' ', false);
246         $mform->setHelpButton('enrollable', array('courseenrollable2', get_string('enrollable')), true);
247         $mform->setDefault('enrollable', $courseconfig->enrollable);
249         $mform->addElement('date_selector', 'enrolstartdate', get_string('enrolstartdate'), array('optional' => true));
250         $mform->setDefault('enrolstartdate', 0);
251         $mform->disabledIf('enrolstartdate', 'enrollable', 'neq', 2);
253         $mform->addElement('date_selector', 'enrolenddate', get_string('enrolenddate'), array('optional' => true));
254         $mform->setDefault('enrolenddate', 0);
255         $mform->disabledIf('enrolenddate', 'enrollable', 'neq', 2);
257         $mform->addElement('duration', 'enrolperiod', get_string('enrolperiod'), array('optional' => true, 'defaultunit' => 86400));
258         $mform->setDefault('enrolperiod', $courseconfig->enrolperiod);
261 //--------------------------------------------------------------------------------
262         $mform->addElement('header','expirynotifyhdr', get_string('expirynotify'));
264         $choices = array();
265         $choices['0'] = get_string('no');
266         $choices['1'] = get_string('yes');
267         $mform->addElement('select', 'expirynotify', get_string('notify'), $choices);
268         $mform->setHelpButton('expirynotify', array('expirynotify', get_string('expirynotify')), true);
269         $mform->setDefault('expirynotify', $courseconfig->expirynotify);
271         $mform->addElement('select', 'notifystudents', get_string('expirynotifystudents'), $choices);
272         $mform->setHelpButton('notifystudents', array('expirynotifystudents', get_string('expirynotifystudents')), true);
273         $mform->setDefault('notifystudents', $courseconfig->notifystudents);
275         $thresholdmenu=array();
276         for ($i=1; $i<=30; $i++) {
277             $seconds = $i * 86400;
278             $thresholdmenu[$seconds] = get_string('numdays', '', $i);
279         }
280         $mform->addElement('select', 'expirythreshold', get_string('expirythreshold'), $thresholdmenu);
281         $mform->setHelpButton('expirythreshold', array('expirythreshold', get_string('expirythreshold')), true);
282         $mform->setDefault('expirythreshold', $courseconfig->expirythreshold);
284 //--------------------------------------------------------------------------------
285         $mform->addElement('header','', get_string('groups', 'group'));
287         $choices = array();
288         $choices[NOGROUPS] = get_string('groupsnone', 'group');
289         $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
290         $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
291         $mform->addElement('select', 'groupmode', get_string('groupmode'), $choices);
292         $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')), true);
293         $mform->setDefault('groupmode', $courseconfig->groupmode);
295         $choices = array();
296         $choices['0'] = get_string('no');
297         $choices['1'] = get_string('yes');
298         $mform->addElement('select', 'groupmodeforce', get_string('force'), $choices);
299         $mform->setHelpButton('groupmodeforce', array('groupmodeforce', get_string('groupmodeforce')), true);
300         $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
302         //default groupings selector
303         $options = array();
304         $options[0] = get_string('none');
305         $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
307 //--------------------------------------------------------------------------------
308         $mform->addElement('header','', get_string('availability'));
310         $choices = array();
311         $choices['0'] = get_string('courseavailablenot');
312         $choices['1'] = get_string('courseavailable');
313         $mform->addElement('select', 'visible', get_string('availability'), $choices);
314         $mform->setHelpButton('visible', array('courseavailability', get_string('availability')), true);
315         $mform->setDefault('visible', $courseconfig->visible);
316         if (!empty($course->id) and !has_capability('moodle/course:visibility', $coursecontext)) {
317             $mform->hardFreeze('visible');
318             $mform->setConstant('visible', $course->visible);
319         }
321         $mform->addElement('passwordunmask', 'enrolpassword', get_string('enrolmentkey'), 'size="25"');
322         $mform->setHelpButton('enrolpassword', array('enrolmentkey', get_string('enrolmentkey')), true);
323         $mform->setDefault('enrolpassword', '');
324         $mform->setDefault('enrolpassword', $courseconfig->enrolpassword);
325         $mform->setType('enrolpassword', PARAM_RAW);
327         if (empty($course->id) or ($course->password !== '' and $course->id != SITEID)) {
328             // do not require password in existing courses that do not have password yet - backwards compatibility ;-)
329             if (!empty($CFG->enrol_manual_requirekey)) {
330                 $mform->addRule('enrolpassword', get_string('required'), 'required', null, 'client');
331             }
332         }
334         $choices = array();
335         $choices['0'] = get_string('guestsno');
336         $choices['1'] = get_string('guestsyes');
337         $choices['2'] = get_string('guestskey');
338         $mform->addElement('select', 'guest', get_string('opentoguests'), $choices);
339         $mform->setHelpButton('guest', array('guestaccess', get_string('opentoguests')), true);
340         $mform->setDefault('guest', $courseconfig->guest);
342         // 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
343         $enrol_object = $CFG;
344         if (!empty($course->id)) {
345             $enrol_object = $course;
346         }
347         // If the print_entry method exists and the course enrol method isn't manual (both set or inherited from site), show cost
348         if (method_exists(enrolment_factory::factory($enrol_object->enrol), 'print_entry') && !($enrol_object->enrol == 'manual' || (empty($enrol_object->enrol) && $CFG->enrol == 'manual'))) {
349             $costgroup=array();
350             $currencies = get_list_of_currencies();
351             $costgroup[]= &MoodleQuickForm::createElement('text','cost', '', 'maxlength="6" size="6"');
352             $costgroup[]= &MoodleQuickForm::createElement('select', 'currency', '', $currencies);
353             $mform->addGroup($costgroup, 'costgrp', get_string('cost'), '&nbsp;', false);
354             //defining a rule for a form element within a group :
355             $costgrprules=array();
356             //set the message to null to tell Moodle to use a default message
357             //available for most rules, fetched from language pack (err_{rulename}).
358             $costgrprules['cost'][]=array(null, 'numeric', null, 'client');
359             $mform->addGroupRule('costgrp',$costgrprules);
360             $mform->setHelpButton('costgrp', array('cost', get_string('cost')), true);
361             $mform->setDefault('cost', '');
362             $mform->setDefault('currency', empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency);
364         }
366 //--------------------------------------------------------------------------------
367         $mform->addElement('header','', get_string('language'));
369         $languages=array();
370         $languages[''] = get_string('forceno');
371         $languages += get_list_of_languages();
372         $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
373         $mform->setDefault('lang', $courseconfig->lang);
375 //--------------------------------------------------------------------------------
376         require_once($CFG->libdir.'/completionlib.php');
377         if(completion_info::is_enabled_for_site()) {
378             $mform->addElement('header','', get_string('progress','completion'));
379             $mform->addElement('select', 'enablecompletion', get_string('completion','completion'),
380                 array(0=>get_string('completiondisabled','completion'), 1=>get_string('completionenabled','completion')));
381             $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
382         } else {
383             $mform->addElement('hidden', 'enablecompletion');
384             $mform->setType('enablecompletion', PARAM_INT);
385             $mform->setDefault('enablecompletion',0);
386         }
388 //--------------------------------------------------------------------------------
389         if (has_capability('moodle/site:config', $systemcontext) && ((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
390             $mform->addElement('header', '', get_string('restrictmodules'));
392             $options = array();
393             $options['0'] = get_string('no');
394             $options['1'] = get_string('yes');
395             $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
396             $mods = array(0=>get_string('allownone'));
397             $mods += $DB->get_records_menu('modules', array(), 'name', 'id, name');
400             $mform->addElement('select', 'allowedmods', get_string('to'), $mods,
401                             array('multiple'=>'multiple', 'size'=>'10'));
402             $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
403         } else {
404             $mform->addElement('hidden', 'restrictmodules', null);
405             $mform->setType('restrictmodules', PARAM_INT);
406         }
407         if ($CFG->restrictmodulesfor == 'all') {
408             $mform->setDefault('allowedmods', explode(',',$CFG->defaultallowedmodules));
409             if (!empty($CFG->restrictbydefault)) {
410                 $mform->setDefault('restrictmodules', 1);
411             }
412         }
413         $mform->setType('restrictmodules', PARAM_INT);
415 /// customizable role names in this course
416 //--------------------------------------------------------------------------------
417         $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
418         $mform->setHelpButton('rolerenaming', array('rolerenaming', get_string('rolerenaming')), true);
420         if ($roles = get_all_roles()) {
421             if ($coursecontext) {
422                 $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS_RAW);
423             }
424             $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
425             foreach ($roles as $role) {
426                 $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->name));
427                 if (isset($role->localname)) {
428                     $mform->setDefault('role_'.$role->id, $role->localname);
429                 }
430                 $mform->setType('role_'.$role->id, PARAM_TEXT);
431                 if (!in_array($role->id, $assignableroles)) {
432                     $mform->setAdvanced('role_'.$role->id);
433                 }
434             }
435         }
437 //--------------------------------------------------------------------------------
438         $this->add_action_buttons();
439 //--------------------------------------------------------------------------------
440         $mform->addElement('hidden', 'id', null);
441         $mform->setType('id', PARAM_INT);
442     }
444     function definition_after_data() {
445         global $DB;
447         $mform =& $this->_form;
449         // add availabe groupings
450         if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
451             $options = array();
452             if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
453                 foreach ($groupings as $grouping) {
454                     $options[$grouping->id] = format_string($grouping->name);
455                 }
456             }
457             $gr_el =& $mform->getElement('defaultgroupingid');
458             $gr_el->load($options);
459         }
460     }
463 /// perform some extra moodle validation
464     function validation($data, $files) {
465         global $DB, $CFG;
467         $errors = parent::validation($data, $files);
468         if ($foundcourses = $DB->get_records('course', array('shortname'=>$data['shortname']))) {
469             if (!empty($data['id'])) {
470                 unset($foundcourses[$data['id']]);
471             }
472             if (!empty($foundcourses)) {
473                 foreach ($foundcourses as $foundcourse) {
474                     $foundcoursenames[] = $foundcourse->fullname;
475                 }
476                 $foundcoursenamestring = implode(',', $foundcoursenames);
477                 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
478             }
479         }
481         if (!empty($data['enrolstartdate']) && !empty($data['enrolenddate']) &&
482                 $data['enrolenddate'] <= $data['enrolstartdate']){
483             $errors['enrolenddate'] = get_string('enrolenddaterror');
484         }
486         if (!empty($CFG->enrol_manual_usepasswordpolicy) and isset($data['enrolpassword']) and $data['enrolpassword'] != '') {
487             $course = $this->_customdata['course'];
488             if ($course->password !== $data['enrolpassword']) {
489                 // enforce password policy only if changing password - backwards compatibility
490                 $errmsg = '';
491                 if (!check_password_policy($data['enrolpassword'], $errmsg)) {
492                     $errors['enrolpassword'] = $errmsg;
493                 }
494             }
495         }
497         return $errors;
498     }