MDL-22547 course->legacyfiles switch implemented
[moodle.git] / course / edit_form.php
1 <?php
3 if (!defined('MOODLE_INTERNAL')) {
4     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
5 }
7 require_once($CFG->libdir.'/formslib.php');
9 class course_edit_form extends moodleform {
11     function definition() {
12         global $USER, $CFG, $DB;
14         $courseconfig = get_config('moodlecourse');
15         $mform    =& $this->_form;
17         $course   = $this->_customdata['course'];
18         $category = $this->_customdata['category'];
19         $editoroptions = $this->_customdata['editoroptions'];
21         $systemcontext = get_context_instance(CONTEXT_SYSTEM);
22         $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
24         $disable_meta = false; // basic meta course state protection; server-side security checks not needed
25         if (!empty($course->id)) {
26             $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
27             $context = $coursecontext;
29             if (course_in_meta($course)) {
30                 $disable_meta = get_string('metaalreadyinmeta');
32             } else if ($course->metacourse) {
33                 if ($DB->count_records('course_meta', array('parent_course'=>$course->id)) > 0) {
34                     $disable_meta = get_string('metaalreadyhascourses');
35                 }
37             } else {
38                 // if users already enrolled directly into coures, do not allow switching to meta,
39                 // users with metacourse manage permission are exception
40                 // please note that we do not need exact results - anything unexpected here prevents metacourse
41                 $managers = get_users_by_capability($coursecontext, 'moodle/course:managemetacourse', 'u.id');
42                 $enrolroles = get_roles_with_capability('moodle/course:participate', CAP_ALLOW, $coursecontext);
43                 if ($users = get_role_users(array_keys($enrolroles), $coursecontext, false, 'u.id', 'u.id ASC')) {
44                     foreach($users as $user) {
45                         if (!isset($managers[$user->id])) {
46                             $disable_meta = get_string('metaalreadyhasenrolments');
47                             break;
48                         }
49                     }
50                 }
51                 unset($managers);
52                 unset($users);
53                 unset($enrolroles);
54             }
55         } else {
56             $coursecontext = null;
57             $context = $categorycontext;
58         }
60 /// form definition with new course defaults
61 //--------------------------------------------------------------------------------
62         $mform->addElement('header','general', get_string('general', 'form'));
64         // Must have create course capability in both categories in order to move course
65         if (has_capability('moodle/course:create', $categorycontext)) {
66             $displaylist = array();
67             $parentlist = array();
68             make_categories_list($displaylist, $parentlist, 'moodle/course:create');
69             $mform->addElement('select', 'category', get_string('category'), $displaylist);
70         } else {
71             $mform->addElement('hidden', 'category', null);
72             $mform->setType('category', PARAM_INT);
73         }
74         $mform->addHelpButton('category', 'category');
75         $mform->setDefault('category', $category->id);
76         $mform->setType('category', PARAM_INT);
78         if (!empty($course->id) and !has_capability('moodle/course:changecategory', $coursecontext)) {
79             $mform->hardFreeze('category');
80             $mform->setConstant('category', $category->id);
81         }
83         $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
84         $mform->addHelpButton('fullname', 'fullnamecourse');
85         $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
86         $mform->setType('fullname', PARAM_MULTILANG);
87         if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
88             $mform->hardFreeze('fullname');
89             $mform->setConstant('fullname', $course->fullname);
90         }
92         $mform->addElement('text','shortname', get_string('shortnamecourse'),'maxlength="100" size="20"');
93         $mform->addHelpButton('shortname', 'shortnamecourse');
94         $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
95         $mform->setType('shortname', PARAM_MULTILANG);
96         if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
97             $mform->hardFreeze('shortname');
98             $mform->setConstant('shortname', $course->shortname);
99         }
101         $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100"  size="10"');
102         $mform->addHelpButton('idnumber', 'idnumbercourse');
103         $mform->setType('idnumber', PARAM_RAW);
104         if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
105             $mform->hardFreeze('idnumber');
106             $mform->setConstants('idnumber', $course->idnumber);
107         }
110         $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
111         $mform->addHelpButton('summary_editor', 'coursesummary');
112         $mform->setType('summary_editor', PARAM_RAW);
114         if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
115             $mform->hardFreeze('summary_editor');
116         }
118         $courseformats = get_plugin_list('format');
119         $formcourseformats = array();
120         foreach ($courseformats as $courseformat => $formatdir) {
121             $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
122         }
123         $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
124         $mform->addHelpButton('format', 'format');
125         $mform->setDefault('format', $courseconfig->format);
127         for ($i=1; $i<=52; $i++) {
128           $sectionmenu[$i] = "$i";
129         }
130         $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
131         $mform->setDefault('numsections', $courseconfig->numsections);
133         $mform->addElement('date_selector', 'startdate', get_string('startdate'));
134         $mform->addHelpButton('startdate', 'startdate');
135         $mform->setDefault('startdate', time() + 3600 * 24);
137         $choices = array();
138         $choices['0'] = get_string('hiddensectionscollapsed');
139         $choices['1'] = get_string('hiddensectionsinvisible');
140         $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
141         $mform->addHelpButton('hiddensections', 'hiddensections');
142         $mform->setDefault('hiddensections', $courseconfig->hiddensections);
144         $options = range(0, 10);
145         $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
146         $mform->addHelpButton('newsitems', 'newsitemsnumber');
147         $mform->setDefault('newsitems', $courseconfig->newsitems);
149         $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
150         $mform->addHelpButton('showgrades', 'showgrades');
151         $mform->setDefault('showgrades', $courseconfig->showgrades);
153         $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
154         $mform->addHelpButton('showreports', 'showreports');
155         $mform->setDefault('showreports', $courseconfig->showreports);
157         $choices = get_max_upload_sizes($CFG->maxbytes);
158         $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
159         $mform->addHelpButton('maxbytes', 'maximumupload');
160         $mform->setDefault('maxbytes', $courseconfig->maxbytes);
162         if (!empty($course->legacyfiles)) {
163             $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
164             $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
165             $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
166         }
168         if (!empty($CFG->allowcoursethemes)) {
169             $themeobjects = get_list_of_themes();
170             $themes=array();
171             $themes[''] = get_string('forceno');
172             foreach ($themeobjects as $key=>$theme) {
173                 $themes[$key] = $theme->name;
174             }
175             $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
176         }
178         $meta=array();
179         $meta[0] = get_string('no');
180         $meta[1] = get_string('yes');
181         if ($disable_meta === false) {
182             $mform->addElement('select', 'metacourse', get_string('managemeta'), $meta);
183             $mform->addHelpButton('metacourse', 'managemeta');
184             $mform->setDefault('metacourse', $courseconfig->metacourse);
185         } else {
186             // no metacourse element - we do not want to change it anyway!
187             $mform->addElement('static', 'nometacourse', get_string('managemeta'),
188                 ((empty($course->metacourse)) ? $meta[0] : $meta[1]) . " - $disable_meta ");
189             $mform->addHelpButton('nometacourse', 'managemeta');
190         }
192 //--------------------------------------------------------------------------------
193         $mform->addElement('header','enrolhdr', get_string('enrolments'));
195         $choices = array();
196         $modules = explode(',', $CFG->enrol_plugins_enabled);
197         foreach ($modules as $module) {
198             $name = get_string('enrolname', "enrol_$module");
199             $plugin = enrolment_factory::factory($module);
200             if (method_exists($plugin, 'print_entry')) {
201                 $choices[$name] = $module;
202             }
203         }
204         asort($choices);
205         $choices = array_flip($choices);
206         $choices = array_merge(array('' => get_string('sitedefault').' ('.get_string('enrolname', "enrol_$CFG->enrol").')'), $choices);
207         $mform->addElement('select', 'enrol', get_string('enrolmentplugins'), $choices);
208         $mform->addHelpButton('enrol', 'enrolmentplugins');
209         $mform->setDefault('enrol', $courseconfig->enrol);
212         $roles = get_assignable_roles($context);
213         if (!empty($course->id)) {
214             // add current default role, so that it is selectable even when user can not assign it
215             if ($current_role = $DB->get_record('role', array('id'=>$course->defaultrole))) {
216                 $roles[$current_role->id] = strip_tags(format_string($current_role->name, true));
217             }
218         }
219         $choices = array();
220         if ($sitedefaultrole = $DB->get_record('role', array('id'=>$CFG->defaultcourseroleid))) {
221             $choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name.')';
222         } else {
223             $choices[0] = get_string('sitedefault');
224         }
225         $choices = $choices + $roles;
227         // fix for MDL-9197
228         foreach ($choices as $choiceid => $choice) {
229             $choices[$choiceid] = format_string($choice);
230         }
232         $mform->addElement('select', 'defaultrole', get_string('defaultrole', 'role'), $choices);
233         $mform->setDefault('defaultrole', 0);
236         $radio = array();
237         $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('no'), 0);
238         $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('yes'), 1);
239         $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('enroldate'), 2);
240         $mform->addGroup($radio, 'enrollable', get_string('enrollable'), ' ', false);
241         $mform->addHelpButton('enrollable', 'enrollable');
242         $mform->setDefault('enrollable', $courseconfig->enrollable);
244         $mform->addElement('date_selector', 'enrolstartdate', get_string('enrolstartdate'), array('optional' => true));
245         $mform->setDefault('enrolstartdate', 0);
246         $mform->disabledIf('enrolstartdate', 'enrollable', 'neq', 2);
248         $mform->addElement('date_selector', 'enrolenddate', get_string('enrolenddate'), array('optional' => true));
249         $mform->setDefault('enrolenddate', 0);
250         $mform->disabledIf('enrolenddate', 'enrollable', 'neq', 2);
252         $mform->addElement('duration', 'enrolperiod', get_string('enrolperiod'), array('optional' => true, 'defaultunit' => 86400));
253         $mform->setDefault('enrolperiod', $courseconfig->enrolperiod);
256 //--------------------------------------------------------------------------------
257         $mform->addElement('header','expirynotifyhdr', get_string('expirynotify'));
259         $choices = array();
260         $choices['0'] = get_string('no');
261         $choices['1'] = get_string('yes');
262         $mform->addElement('select', 'expirynotify', get_string('notify'), $choices);
263         $mform->addHelpButton('expirynotify', 'notify');
264         $mform->setDefault('expirynotify', $courseconfig->expirynotify);
266         $mform->addElement('select', 'notifystudents', get_string('expirynotifystudents'), $choices);
267         $mform->addHelpButton('notifystudents', 'expirynotifystudents');
268         $mform->setDefault('notifystudents', $courseconfig->notifystudents);
270         $thresholdmenu=array();
271         for ($i=1; $i<=30; $i++) {
272             $seconds = $i * 86400;
273             $thresholdmenu[$seconds] = get_string('numdays', '', $i);
274         }
275         $mform->addElement('select', 'expirythreshold', get_string('expirythreshold'), $thresholdmenu);
276         $mform->addHelpButton('expirythreshold', 'expirythreshold');
277         $mform->setDefault('expirythreshold', $courseconfig->expirythreshold);
279 //--------------------------------------------------------------------------------
280         $mform->addElement('header','', get_string('groups', 'group'));
282         $choices = array();
283         $choices[NOGROUPS] = get_string('groupsnone', 'group');
284         $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
285         $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
286         $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
287         $mform->addHelpButton('groupmode', 'groupmode', 'group');
288         $mform->setDefault('groupmode', $courseconfig->groupmode);
290         $choices = array();
291         $choices['0'] = get_string('no');
292         $choices['1'] = get_string('yes');
293         $mform->addElement('select', 'groupmodeforce', get_string('groupmodeforce', 'group'), $choices);
294         $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
295         $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
297         //default groupings selector
298         $options = array();
299         $options[0] = get_string('none');
300         $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
302 //--------------------------------------------------------------------------------
303         $mform->addElement('header','', get_string('availability'));
305         $choices = array();
306         $choices['0'] = get_string('courseavailablenot');
307         $choices['1'] = get_string('courseavailable');
308         $mform->addElement('select', 'visible', get_string('availability'), $choices);
309         $mform->addHelpButton('visible', 'availability');
310         $mform->setDefault('visible', $courseconfig->visible);
311         if (!empty($course->id) and !has_capability('moodle/course:visibility', $coursecontext)) {
312             $mform->hardFreeze('visible');
313             $mform->setConstant('visible', $course->visible);
314         }
316         $mform->addElement('passwordunmask', 'enrolpassword', get_string('enrolmentkey'), 'size="25"');
317         $mform->setDefault('enrolpassword', '');
318         $mform->setDefault('enrolpassword', $courseconfig->enrolpassword);
319         $mform->setType('enrolpassword', PARAM_RAW);
321         if (empty($course->id) or ($course->password !== '' and $course->id != SITEID)) {
322             // do not require password in existing courses that do not have password yet - backwards compatibility ;-)
323             if (!empty($CFG->enrol_manual_requirekey)) {
324                 $mform->addRule('enrolpassword', get_string('required'), 'required', null, 'client');
325             }
326         }
328         $choices = array();
329         $choices['0'] = get_string('guestsno');
330         $choices['1'] = get_string('guestsyes');
331         $choices['2'] = get_string('guestskey');
332         $mform->addElement('select', 'guest', get_string('opentoguests'), $choices);
333         $mform->setDefault('guest', $courseconfig->guest);
335         // 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
336         $enrol_object = $CFG;
337         if (!empty($course->id)) {
338             $enrol_object = $course;
339         }
340         // If the print_entry method exists and the course enrol method isn't manual (both set or inherited from site), show cost
341         if (method_exists(enrolment_factory::factory($enrol_object->enrol), 'print_entry') && !($enrol_object->enrol == 'manual' || (empty($enrol_object->enrol) && $CFG->enrol == 'manual'))) {
342             $costgroup=array();
343             $currencies = get_string_manager()->get_list_of_currencies();
344             $costgroup[]= &MoodleQuickForm::createElement('text','cost', '', 'maxlength="6" size="6"');
345             $costgroup[]= &MoodleQuickForm::createElement('select', 'currency', '', $currencies);
346             $mform->addGroup($costgroup, 'costgrp', get_string('cost'), '&nbsp;', false);
347             //defining a rule for a form element within a group :
348             $costgrprules=array();
349             //set the message to null to tell Moodle to use a default message
350             //available for most rules, fetched from language pack (err_{rulename}).
351             $costgrprules['cost'][]=array(null, 'numeric', null, 'client');
352             $mform->addGroupRule('costgrp',$costgrprules);
353             $mform->setDefault('cost', '');
354             $mform->setDefault('currency', empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency);
356         }
358 //--------------------------------------------------------------------------------
359         $mform->addElement('header','', get_string('language'));
361         $languages=array();
362         $languages[''] = get_string('forceno');
363         $languages += get_string_manager()->get_list_of_translations();
364         $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
365         $mform->setDefault('lang', $courseconfig->lang);
367 //--------------------------------------------------------------------------------
368         require_once($CFG->libdir.'/completionlib.php');
369         if(completion_info::is_enabled_for_site()) {
370             $mform->addElement('header','', get_string('progress','completion'));
371             $mform->addElement('select', 'enablecompletion', get_string('completion','completion'),
372                 array(0=>get_string('completiondisabled','completion'), 1=>get_string('completionenabled','completion')));
373             $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
375             $mform->addElement('checkbox', 'completionstartonenrol', get_string('completionstartonenrol', 'completion'));
376             $mform->setDefault('completionstartonenrol', $courseconfig->completionstartonenrol);
377             $mform->disabledIf('completionstartonenrol', 'enablecompletion', 'eq', 0);
378         } else {
379             $mform->addElement('hidden', 'enablecompletion');
380             $mform->setType('enablecompletion', PARAM_INT);
381             $mform->setDefault('enablecompletion',0);
383             $mform->addElement('hidden', 'completionstartonenrol');
384             $mform->setType('completionstartonenrol', PARAM_INT);
385             $mform->setDefault('completionstartonenrol',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->addHelpButton('rolerenaming', 'rolerenaming');
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     }