MDL-22054 converting course formats to have own lang packs
[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('pluginname', "format_$courseformat");
130         }
131         $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
132         $mform->setHelpButton('format', array('courseformats', get_string('courseformats')), true);
133         $mform->setDefault('format', $courseconfig->format);
135         for ($i=1; $i<=52; $i++) {
136           $sectionmenu[$i] = "$i";
137         }
138         $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
139         $mform->setDefault('numsections', $courseconfig->numsections);
141         $mform->addElement('date_selector', 'startdate', get_string('startdate'));
142         $mform->setHelpButton('startdate', array('coursestartdate', get_string('startdate')), true);
143         $mform->setDefault('startdate', time() + 3600 * 24);
145         $choices = array();
146         $choices['0'] = get_string('hiddensectionscollapsed');
147         $choices['1'] = get_string('hiddensectionsinvisible');
148         $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
149         $mform->setHelpButton('hiddensections', array('coursehiddensections', get_string('hiddensections')), true);
150         $mform->setDefault('hiddensections', $courseconfig->hiddensections);
152         $options = range(0, 10);
153         $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
154         $mform->setHelpButton('newsitems', array('coursenewsitems', get_string('newsitemsnumber')), true);
155         $mform->setDefault('newsitems', $courseconfig->newsitems);
157         $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
158         $mform->setHelpButton('showgrades', array('coursegrades', get_string('grades')), true);
159         $mform->setDefault('showgrades', $courseconfig->showgrades);
161         $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
162         $mform->setHelpButton('showreports', array('coursereports', get_string('activityreport')), true);
163         $mform->setDefault('showreports', $courseconfig->showreports);
165         $choices = get_max_upload_sizes($CFG->maxbytes);
166         $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
167         $mform->setHelpButton('maxbytes', array('courseuploadsize', get_string('maximumupload')), true);
168         $mform->setDefault('maxbytes', $courseconfig->maxbytes);
170         if (!empty($CFG->allowcoursethemes)) {
171             $themeobjects = get_list_of_themes();
172             $themes=array();
173             $themes[''] = get_string('forceno');
174             foreach ($themeobjects as $key=>$theme) {
175                 $themes[$key] = $theme->name;
176             }
177             $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
178         }
180         $meta=array();
181         $meta[0] = get_string('no');
182         $meta[1] = get_string('yes');
183         if ($disable_meta === false) {
184             $mform->addElement('select', 'metacourse', get_string('managemeta'), $meta);
185             $mform->setHelpButton('metacourse', array('metacourse', get_string('metacourse')), true);
186             $mform->setDefault('metacourse', $courseconfig->metacourse);
187         } else {
188             // no metacourse element - we do not want to change it anyway!
189             $mform->addElement('static', 'nometacourse', get_string('managemeta'),
190                 ((empty($course->metacourse)) ? $meta[0] : $meta[1]) . " - $disable_meta ");
191             $mform->setHelpButton('nometacourse', array('metacourse', get_string('metacourse')), true);
192         }
194 //--------------------------------------------------------------------------------
195         $mform->addElement('header','enrolhdr', get_string('enrolments'));
197         $choices = array();
198         $modules = explode(',', $CFG->enrol_plugins_enabled);
199         foreach ($modules as $module) {
200             $name = get_string('enrolname', "enrol_$module");
201             $plugin = enrolment_factory::factory($module);
202             if (method_exists($plugin, 'print_entry')) {
203                 $choices[$name] = $module;
204             }
205         }
206         asort($choices);
207         $choices = array_flip($choices);
208         $choices = array_merge(array('' => get_string('sitedefault').' ('.get_string('enrolname', "enrol_$CFG->enrol").')'), $choices);
209         $mform->addElement('select', 'enrol', get_string('enrolmentplugins'), $choices);
210         $mform->setHelpButton('enrol', array('courseenrolmentplugins', get_string('enrolmentplugins')), true);
211         $mform->setDefault('enrol', $courseconfig->enrol);
214         $roles = get_assignable_roles($context);
215         if (!empty($course->id)) {
216             // add current default role, so that it is selectable even when user can not assign it
217             if ($current_role = $DB->get_record('role', array('id'=>$course->defaultrole))) {
218                 $roles[$current_role->id] = strip_tags(format_string($current_role->name, true));
219             }
220         }
221         $choices = array();
222         if ($sitedefaultrole = $DB->get_record('role', array('id'=>$CFG->defaultcourseroleid))) {
223             $choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name.')';
224         } else {
225             $choices[0] = get_string('sitedefault');
226         }
227         $choices = $choices + $roles;
229         // fix for MDL-9197
230         foreach ($choices as $choiceid => $choice) {
231             $choices[$choiceid] = format_string($choice);
232         }
234         $mform->addElement('select', 'defaultrole', get_string('defaultrole', 'role'), $choices);
235         $mform->setDefault('defaultrole', 0);
238         $radio = array();
239         $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('no'), 0);
240         $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('yes'), 1);
241         $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('enroldate'), 2);
242         $mform->addGroup($radio, 'enrollable', get_string('enrollable'), ' ', false);
243         $mform->setHelpButton('enrollable', array('courseenrollable2', get_string('enrollable')), true);
244         $mform->setDefault('enrollable', $courseconfig->enrollable);
246         $mform->addElement('date_selector', 'enrolstartdate', get_string('enrolstartdate'), array('optional' => true));
247         $mform->setDefault('enrolstartdate', 0);
248         $mform->disabledIf('enrolstartdate', 'enrollable', 'neq', 2);
250         $mform->addElement('date_selector', 'enrolenddate', get_string('enrolenddate'), array('optional' => true));
251         $mform->setDefault('enrolenddate', 0);
252         $mform->disabledIf('enrolenddate', 'enrollable', 'neq', 2);
254         $mform->addElement('duration', 'enrolperiod', get_string('enrolperiod'), array('optional' => true, 'defaultunit' => 86400));
255         $mform->setDefault('enrolperiod', $courseconfig->enrolperiod);
258 //--------------------------------------------------------------------------------
259         $mform->addElement('header','expirynotifyhdr', get_string('expirynotify'));
261         $choices = array();
262         $choices['0'] = get_string('no');
263         $choices['1'] = get_string('yes');
264         $mform->addElement('select', 'expirynotify', get_string('notify'), $choices);
265         $mform->setHelpButton('expirynotify', array('expirynotify', get_string('expirynotify')), true);
266         $mform->setDefault('expirynotify', $courseconfig->expirynotify);
268         $mform->addElement('select', 'notifystudents', get_string('expirynotifystudents'), $choices);
269         $mform->setHelpButton('notifystudents', array('expirynotifystudents', get_string('expirynotifystudents')), true);
270         $mform->setDefault('notifystudents', $courseconfig->notifystudents);
272         $thresholdmenu=array();
273         for ($i=1; $i<=30; $i++) {
274             $seconds = $i * 86400;
275             $thresholdmenu[$seconds] = get_string('numdays', '', $i);
276         }
277         $mform->addElement('select', 'expirythreshold', get_string('expirythreshold'), $thresholdmenu);
278         $mform->setHelpButton('expirythreshold', array('expirythreshold', get_string('expirythreshold')), true);
279         $mform->setDefault('expirythreshold', $courseconfig->expirythreshold);
281 //--------------------------------------------------------------------------------
282         $mform->addElement('header','', get_string('groups', 'group'));
284         $choices = array();
285         $choices[NOGROUPS] = get_string('groupsnone', 'group');
286         $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
287         $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
288         $mform->addElement('select', 'groupmode', get_string('groupmode'), $choices);
289         $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')), true);
290         $mform->setDefault('groupmode', $courseconfig->groupmode);
292         $choices = array();
293         $choices['0'] = get_string('no');
294         $choices['1'] = get_string('yes');
295         $mform->addElement('select', 'groupmodeforce', get_string('force'), $choices);
296         $mform->setHelpButton('groupmodeforce', array('groupmodeforce', get_string('groupmodeforce')), true);
297         $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
299         //default groupings selector
300         $options = array();
301         $options[0] = get_string('none');
302         $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
304 //--------------------------------------------------------------------------------
305         $mform->addElement('header','', get_string('availability'));
307         $choices = array();
308         $choices['0'] = get_string('courseavailablenot');
309         $choices['1'] = get_string('courseavailable');
310         $mform->addElement('select', 'visible', get_string('availability'), $choices);
311         $mform->setHelpButton('visible', array('courseavailability', get_string('availability')), true);
312         $mform->setDefault('visible', $courseconfig->visible);
313         if (!empty($course->id) and !has_capability('moodle/course:visibility', $coursecontext)) {
314             $mform->hardFreeze('visible');
315             $mform->setConstant('visible', $course->visible);
316         }
318         $mform->addElement('passwordunmask', 'enrolpassword', get_string('enrolmentkey'), 'size="25"');
319         $mform->setHelpButton('enrolpassword', array('enrolmentkey', get_string('enrolmentkey')), true);
320         $mform->setDefault('enrolpassword', '');
321         $mform->setDefault('enrolpassword', $courseconfig->enrolpassword);
322         $mform->setType('enrolpassword', PARAM_RAW);
324         if (empty($course->id) or ($course->password !== '' and $course->id != SITEID)) {
325             // do not require password in existing courses that do not have password yet - backwards compatibility ;-)
326             if (!empty($CFG->enrol_manual_requirekey)) {
327                 $mform->addRule('enrolpassword', get_string('required'), 'required', null, 'client');
328             }
329         }
331         $choices = array();
332         $choices['0'] = get_string('guestsno');
333         $choices['1'] = get_string('guestsyes');
334         $choices['2'] = get_string('guestskey');
335         $mform->addElement('select', 'guest', get_string('opentoguests'), $choices);
336         $mform->setHelpButton('guest', array('guestaccess', get_string('opentoguests')), true);
337         $mform->setDefault('guest', $courseconfig->guest);
339         // 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
340         $enrol_object = $CFG;
341         if (!empty($course->id)) {
342             $enrol_object = $course;
343         }
344         // If the print_entry method exists and the course enrol method isn't manual (both set or inherited from site), show cost
345         if (method_exists(enrolment_factory::factory($enrol_object->enrol), 'print_entry') && !($enrol_object->enrol == 'manual' || (empty($enrol_object->enrol) && $CFG->enrol == 'manual'))) {
346             $costgroup=array();
347             $currencies = get_list_of_currencies();
348             $costgroup[]= &MoodleQuickForm::createElement('text','cost', '', 'maxlength="6" size="6"');
349             $costgroup[]= &MoodleQuickForm::createElement('select', 'currency', '', $currencies);
350             $mform->addGroup($costgroup, 'costgrp', get_string('cost'), '&nbsp;', false);
351             //defining a rule for a form element within a group :
352             $costgrprules=array();
353             //set the message to null to tell Moodle to use a default message
354             //available for most rules, fetched from language pack (err_{rulename}).
355             $costgrprules['cost'][]=array(null, 'numeric', null, 'client');
356             $mform->addGroupRule('costgrp',$costgrprules);
357             $mform->setHelpButton('costgrp', array('cost', get_string('cost')), true);
358             $mform->setDefault('cost', '');
359             $mform->setDefault('currency', empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency);
361         }
363 //--------------------------------------------------------------------------------
364         $mform->addElement('header','', get_string('language'));
366         $languages=array();
367         $languages[''] = get_string('forceno');
368         $languages += get_list_of_languages();
369         $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
370         $mform->setDefault('lang', $courseconfig->lang);
372 //--------------------------------------------------------------------------------
373         require_once($CFG->libdir.'/completionlib.php');
374         if(completion_info::is_enabled_for_site()) {
375             $mform->addElement('header','', get_string('progress','completion'));
376             $mform->addElement('select', 'enablecompletion', get_string('completion','completion'),
377                 array(0=>get_string('completiondisabled','completion'), 1=>get_string('completionenabled','completion')));
378             $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
379         } else {
380             $mform->addElement('hidden', 'enablecompletion');
381             $mform->setType('enablecompletion', PARAM_INT);
382             $mform->setDefault('enablecompletion',0);
383         }
385 //--------------------------------------------------------------------------------
386         if (has_capability('moodle/site:config', $systemcontext) && ((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
387             $mform->addElement('header', '', get_string('restrictmodules'));
389             $options = array();
390             $options['0'] = get_string('no');
391             $options['1'] = get_string('yes');
392             $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
393             $mods = array(0=>get_string('allownone'));
394             $mods += $DB->get_records_menu('modules', array(), 'name', 'id, name');
397             $mform->addElement('select', 'allowedmods', get_string('to'), $mods,
398                             array('multiple'=>'multiple', 'size'=>'10'));
399             $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
400         } else {
401             $mform->addElement('hidden', 'restrictmodules', null);
402             $mform->setType('restrictmodules', PARAM_INT);
403         }
404         if ($CFG->restrictmodulesfor == 'all') {
405             $mform->setDefault('allowedmods', explode(',',$CFG->defaultallowedmodules));
406             if (!empty($CFG->restrictbydefault)) {
407                 $mform->setDefault('restrictmodules', 1);
408             }
409         }
410         $mform->setType('restrictmodules', PARAM_INT);
412 /// customizable role names in this course
413 //--------------------------------------------------------------------------------
414         $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
415         $mform->setHelpButton('rolerenaming', array('rolerenaming', get_string('rolerenaming')), true);
417         if ($roles = get_all_roles()) {
418             if ($coursecontext) {
419                 $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS_RAW);
420             }
421             $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
422             foreach ($roles as $role) {
423                 $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->name));
424                 if (isset($role->localname)) {
425                     $mform->setDefault('role_'.$role->id, $role->localname);
426                 }
427                 $mform->setType('role_'.$role->id, PARAM_TEXT);
428                 if (!in_array($role->id, $assignableroles)) {
429                     $mform->setAdvanced('role_'.$role->id);
430                 }
431             }
432         }
434 //--------------------------------------------------------------------------------
435         $this->add_action_buttons();
436 //--------------------------------------------------------------------------------
437         $mform->addElement('hidden', 'id', null);
438         $mform->setType('id', PARAM_INT);
439     }
441     function definition_after_data() {
442         global $DB;
444         $mform =& $this->_form;
446         // add availabe groupings
447         if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
448             $options = array();
449             if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
450                 foreach ($groupings as $grouping) {
451                     $options[$grouping->id] = format_string($grouping->name);
452                 }
453             }
454             $gr_el =& $mform->getElement('defaultgroupingid');
455             $gr_el->load($options);
456         }
457     }
460 /// perform some extra moodle validation
461     function validation($data, $files) {
462         global $DB, $CFG;
464         $errors = parent::validation($data, $files);
465         if ($foundcourses = $DB->get_records('course', array('shortname'=>$data['shortname']))) {
466             if (!empty($data['id'])) {
467                 unset($foundcourses[$data['id']]);
468             }
469             if (!empty($foundcourses)) {
470                 foreach ($foundcourses as $foundcourse) {
471                     $foundcoursenames[] = $foundcourse->fullname;
472                 }
473                 $foundcoursenamestring = implode(',', $foundcoursenames);
474                 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
475             }
476         }
478         if (!empty($data['enrolstartdate']) && !empty($data['enrolenddate']) &&
479                 $data['enrolenddate'] <= $data['enrolstartdate']){
480             $errors['enrolenddate'] = get_string('enrolenddaterror');
481         }
483         if (!empty($CFG->enrol_manual_usepasswordpolicy) and isset($data['enrolpassword']) and $data['enrolpassword'] != '') {
484             $course = $this->_customdata['course'];
485             if ($course->password !== $data['enrolpassword']) {
486                 // enforce password policy only if changing password - backwards compatibility
487                 $errmsg = '';
488                 if (!check_password_policy($data['enrolpassword'], $errmsg)) {
489                     $errors['enrolpassword'] = $errmsg;
490                 }
491             }
492         }
494         return $errors;
495     }