Automatic installer.php lang files by installer_builder (20081123)
[moodle.git] / course / edit_form.php
blob2b6250cb809f30f20008e83ae7d726540b4892b1
1 <?php //$Id$
3 require_once($CFG->libdir.'/formslib.php');
5 class course_edit_form extends moodleform {
7 function definition() {
8 global $USER, $CFG;
10 $mform =& $this->_form;
12 $course = $this->_customdata['course'];
13 $category = $this->_customdata['category'];
15 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
16 $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
18 $disable_meta = false; // basic meta course state protection; server-side security checks not needed
20 if (!empty($course)) {
21 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
22 $context = $coursecontext;
24 if (course_in_meta($course)) {
25 $disable_meta = get_string('metaalreadyinmeta');
27 } else if ($course->metacourse) {
28 if (count_records('course_meta', 'parent_course', $course->id) > 0) {
29 $disable_meta = get_string('metaalreadyhascourses');
32 } else {
33 // if users already enrolled directly into coures, do not allow switching to meta,
34 // users with metacourse manage permission are exception
35 // please note that we do not need exact results - anything unexpected here prevents metacourse
36 $managers = get_users_by_capability($coursecontext, 'moodle/course:managemetacourse', 'u.id');
37 $enrolroles = get_roles_with_capability('moodle/course:view', CAP_ALLOW, $coursecontext);
38 if ($users = get_role_users(array_keys($enrolroles), $coursecontext, false, 'u.id', 'u.id ASC')) {
39 foreach($users as $user) {
40 if (!isset($managers[$user->id])) {
41 $disable_meta = get_string('metaalreadyhasenrolments');
42 break;
46 unset($managers);
47 unset($users);
48 unset($enrolroles);
50 } else {
51 $coursecontext = null;
52 $context = $categorycontext;
55 /// form definition with new course defaults
56 //--------------------------------------------------------------------------------
57 $mform->addElement('header','general', get_string('general', 'form'));
59 //must have create course capability in both categories in order to move course
60 if (has_capability('moodle/course:create', $categorycontext)) {
61 $displaylist = array();
62 $parentlist = array();
63 make_categories_list($displaylist, $parentlist);
64 foreach ($displaylist as $key=>$val) {
65 if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $key))) {
66 unset($displaylist[$key]);
69 $mform->addElement('select', 'category', get_string('category'), $displaylist);
70 } else {
71 $mform->addElement('hidden', 'category', null);
73 $mform->setHelpButton('category', array('coursecategory', get_string('category')));
74 $mform->setDefault('category', $category->id);
75 $mform->setType('category', PARAM_INT);
77 $mform->addElement('text','fullname', get_string('fullname'),'maxlength="254" size="50"');
78 $mform->setHelpButton('fullname', array('coursefullname', get_string('fullname')), true);
79 $mform->setDefault('fullname', get_string('defaultcoursefullname'));
80 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
81 $mform->setType('fullname', PARAM_MULTILANG);
82 if ($course and !has_capability('moodle/course:changefullname', $coursecontext)) {
83 $mform->hardFreeze('fullname');
84 $mform->setConstant('fullname', $course->fullname);
87 $mform->addElement('text','shortname', get_string('shortname'),'maxlength="100" size="20"');
88 $mform->setHelpButton('shortname', array('courseshortname', get_string('shortname')), true);
89 $mform->setDefault('shortname', get_string('defaultcourseshortname'));
90 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
91 $mform->setType('shortname', PARAM_MULTILANG);
92 if ($course and !has_capability('moodle/course:changeshortname', $coursecontext)) {
93 $mform->hardFreeze('shortname');
94 $mform->setConstant('shortname', $course->shortname);
97 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
98 $mform->setHelpButton('idnumber', array('courseidnumber', get_string('idnumbercourse')), true);
99 $mform->setType('idnumber', PARAM_RAW);
100 if ($course and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
101 $mform->hardFreeze('idnumber');
102 $mform->setConstants('idnumber', $course->idnumber);
105 $mform->addElement('htmleditor','summary', get_string('summary'), array('rows'=> '10', 'cols'=>'65'));
106 $mform->setHelpButton('summary', array('text', get_string('helptext')), true);
107 $mform->setType('summary', PARAM_RAW);
109 $courseformats = get_list_of_plugins('course/format');
110 $formcourseformats = array();
111 foreach ($courseformats as $courseformat) {
112 $formcourseformats["$courseformat"] = get_string("format$courseformat","format_$courseformat");
113 if($formcourseformats["$courseformat"]=="[[format$courseformat]]") {
114 $formcourseformats["$courseformat"] = get_string("format$courseformat");
117 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
118 $mform->setHelpButton('format', array('courseformats', get_string('courseformats')), true);
119 $mform->setDefault('format', 'weeks');
121 for ($i=1; $i<=52; $i++) {
122 $sectionmenu[$i] = "$i";
124 $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
125 $mform->setDefault('numsections', 10);
127 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
128 $mform->setHelpButton('startdate', array('coursestartdate', get_string('startdate')), true);
129 $mform->setDefault('startdate', time() + 3600 * 24);
131 $choices = array();
132 $choices['0'] = get_string('hiddensectionscollapsed');
133 $choices['1'] = get_string('hiddensectionsinvisible');
134 $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
135 $mform->setHelpButton('hiddensections', array('coursehiddensections', get_string('hiddensections')), true);
136 $mform->setDefault('hiddensections', 0);
138 $options = range(0, 10);
139 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
140 $mform->setHelpButton('newsitems', array('coursenewsitems', get_string('newsitemsnumber')), true);
141 $mform->setDefault('newsitems', 5);
143 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
144 $mform->setHelpButton('showgrades', array('coursegrades', get_string('grades')), true);
145 $mform->setDefault('showgrades', 1);
147 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
148 $mform->setHelpButton('showreports', array('coursereports', get_string('activityreport')), true);
149 $mform->setDefault('showreports', 0);
151 $choices = get_max_upload_sizes($CFG->maxbytes);
152 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
153 $mform->setHelpButton('maxbytes', array('courseuploadsize', get_string('maximumupload')), true);
155 if (!empty($CFG->allowcoursethemes)) {
156 $themes=array();
157 $themes[''] = get_string('forceno');
158 $themes += get_list_of_themes();
159 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
162 $meta=array();
163 $meta[0] = get_string('no');
164 $meta[1] = get_string('yes');
165 if ($disable_meta === false) {
166 $mform->addElement('select', 'metacourse', get_string('managemeta'), $meta);
167 $mform->setHelpButton('metacourse', array('metacourse', get_string('metacourse')), true);
168 $mform->setDefault('metacourse', 0);
169 } else {
170 // no metacourse element - we do not want to change it anyway!
171 $mform->addElement('static', 'nometacourse', get_string('managemeta'),
172 ((empty($course->metacourse)) ? $meta[0] : $meta[1]) . " - $disable_meta ");
173 $mform->setHelpButton('nometacourse', array('metacourse', get_string('metacourse')), true);
176 //--------------------------------------------------------------------------------
177 $mform->addElement('header','enrolhdr', get_string('enrolments'));
179 $choices = array();
180 $modules = explode(',', $CFG->enrol_plugins_enabled);
181 foreach ($modules as $module) {
182 $name = get_string('enrolname', "enrol_$module");
183 $plugin = enrolment_factory::factory($module);
184 if (method_exists($plugin, 'print_entry')) {
185 $choices[$name] = $module;
188 asort($choices);
189 $choices = array_flip($choices);
190 $choices = array_merge(array('' => get_string('sitedefault').' ('.get_string('enrolname', "enrol_$CFG->enrol").')'), $choices);
191 $mform->addElement('select', 'enrol', get_string('enrolmentplugins'), $choices);
192 $mform->setHelpButton('enrol', array('courseenrolmentplugins', get_string('enrolmentplugins')), true);
195 $roles = get_assignable_roles($context);
196 if (!empty($course)) {
197 // add current default role, so that it is selectable even when user can not assign it
198 if ($current_role = get_record('role', 'id', $course->defaultrole)) {
199 $roles[$current_role->id] = strip_tags(format_string($current_role->name, true));
202 $choices = array();
203 if ($sitedefaultrole = get_record('role', 'id', $CFG->defaultcourseroleid)) {
204 $choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name.')';
205 } else {
206 $choices[0] = get_string('sitedefault');
208 $choices = $choices + $roles;
210 // fix for MDL-9197
211 foreach ($choices as $choiceid => $choice) {
212 $choices[$choiceid] = format_string($choice);
215 $mform->addElement('select', 'defaultrole', get_string('defaultrole', 'role'), $choices);
216 $mform->setDefault('defaultrole', 0);
219 $radio = array();
220 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('no'), 0);
221 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('yes'), 1);
222 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('enroldate'), 2);
223 $mform->addGroup($radio, 'enrollable', get_string('enrollable'), ' ', false);
224 $mform->setHelpButton('enrollable', array('courseenrollable2', get_string('enrollable')), true);
225 $mform->setDefault('enrollable', 1);
227 $enroldatestartgrp = array();
228 $enroldatestartgrp[] = &MoodleQuickForm::createElement('date_selector', 'enrolstartdate');
229 $enroldatestartgrp[] = &MoodleQuickForm::createElement('checkbox', 'enrolstartdisabled', null, get_string('disable'));
230 $mform->addGroup($enroldatestartgrp, 'enrolstartdategrp', get_string('enrolstartdate'), ' ', false);
231 $mform->setDefault('enrolstartdate', 0);
232 $mform->setDefault('enrolstartdisabled', 1);
233 $mform->disabledIf('enrolstartdategrp', 'enrolstartdisabled', 'checked');
235 $enroldateendgrp = array();
236 $enroldateendgrp[] = &MoodleQuickForm::createElement('date_selector', 'enrolenddate');
237 $enroldateendgrp[] = &MoodleQuickForm::createElement('checkbox', 'enrolenddisabled', null, get_string('disable'));
238 $mform->addGroup($enroldateendgrp, 'enroldateendgrp', get_string('enrolenddate'), ' ', false);
239 $mform->setDefault('enrolenddate', 0);
240 $mform->setDefault('enrolenddisabled', 1);
241 $mform->disabledIf('enroldateendgrp', 'enrolenddisabled', 'checked');
243 $periodmenu=array();
244 $periodmenu[0] = get_string('unlimited');
245 for ($i=1; $i<=365; $i++) {
246 $seconds = $i * 86400;
247 $periodmenu[$seconds] = get_string('numdays', '', $i);
249 $mform->addElement('select', 'enrolperiod', get_string('enrolperiod'), $periodmenu);
250 $mform->setDefault('enrolperiod', 0);
253 //--------------------------------------------------------------------------------
254 $mform->addElement('header','expirynotifyhdr', get_string('expirynotify'));
256 $choices = array();
257 $choices['0'] = get_string('no');
258 $choices['1'] = get_string('yes');
259 $mform->addElement('select', 'expirynotify', get_string('notify'), $choices);
260 $mform->setHelpButton('expirynotify', array('expirynotify', get_string('expirynotify')), true);
261 $mform->setDefault('expirynotify', 0);
263 $mform->addElement('select', 'notifystudents', get_string('expirynotifystudents'), $choices);
264 $mform->setHelpButton('notifystudents', array('expirynotifystudents', get_string('expirynotifystudents')), true);
265 $mform->setDefault('notifystudents', 0);
267 $thresholdmenu=array();
268 for ($i=1; $i<=30; $i++) {
269 $seconds = $i * 86400;
270 $thresholdmenu[$seconds] = get_string('numdays', '', $i);
272 $mform->addElement('select', 'expirythreshold', get_string('expirythreshold'), $thresholdmenu);
273 $mform->setHelpButton('expirythreshold', array('expirythreshold', get_string('expirythreshold')), true);
274 $mform->setDefault('expirythreshold', 10 * 86400);
276 //--------------------------------------------------------------------------------
277 $mform->addElement('header','', get_string('groups', 'group'));
279 $choices = array();
280 $choices[NOGROUPS] = get_string('groupsnone', 'group');
281 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
282 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
283 $mform->addElement('select', 'groupmode', get_string('groupmode'), $choices);
284 $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')), true);
285 $mform->setDefault('groupmode', 0);
287 $choices = array();
288 $choices['0'] = get_string('no');
289 $choices['1'] = get_string('yes');
290 $mform->addElement('select', 'groupmodeforce', get_string('force'), $choices);
291 $mform->setHelpButton('groupmodeforce', array('groupmodeforce', get_string('groupmodeforce')), true);
292 $mform->setDefault('groupmodeforce', 0);
294 if (!empty($CFG->enablegroupings)) {
295 //default groupings selector
296 $options = array();
297 $options[0] = get_string('none');
298 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
301 //--------------------------------------------------------------------------------
302 $mform->addElement('header','', get_string('availability'));
304 $choices = array();
305 $choices['0'] = get_string('courseavailablenot');
306 $choices['1'] = get_string('courseavailable');
307 $mform->addElement('select', 'visible', get_string('availability'), $choices);
308 $mform->setHelpButton('visible', array('courseavailability', get_string('availability')), true);
309 $mform->setDefault('visible', 1);
311 $mform->addElement('passwordunmask', 'enrolpassword', get_string('enrolmentkey'), 'size="25"');
312 $mform->setHelpButton('enrolpassword', array('enrolmentkey', get_string('enrolmentkey')), true);
313 $mform->setDefault('enrolpassword', '');
314 $mform->setType('enrolpassword', PARAM_RAW);
316 $choices = array();
317 $choices['0'] = get_string('guestsno');
318 $choices['1'] = get_string('guestsyes');
319 $choices['2'] = get_string('guestskey');
320 $mform->addElement('select', 'guest', get_string('opentoguests'), $choices);
321 $mform->setHelpButton('guest', array('guestaccess', get_string('opentoguests')), true);
322 $mform->setDefault('guest', 0);
324 // 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
325 $enrol_object = $CFG;
326 if (!empty($course)) {
327 $enrol_object = $course;
329 // If the print_entry method exists and the course enrol method isn't manual (both set or inherited from site), show cost
330 if (method_exists(enrolment_factory::factory($enrol_object->enrol), 'print_entry') && !($enrol_object->enrol == 'manual' || (empty($enrol_object->enrol) && $CFG->enrol == 'manual'))) {
331 $costgroup=array();
332 $currencies = get_list_of_currencies();
333 $costgroup[]= &MoodleQuickForm::createElement('text','cost', '', 'maxlength="6" size="6"');
334 $costgroup[]= &MoodleQuickForm::createElement('select', 'currency', '', $currencies);
335 $mform->addGroup($costgroup, 'costgrp', get_string('cost'), '&nbsp;', false);
336 //defining a rule for a form element within a group :
337 $costgrprules=array();
338 //set the message to null to tell Moodle to use a default message
339 //available for most rules, fetched from language pack (err_{rulename}).
340 $costgrprules['cost'][]=array(null, 'numeric', null, 'client');
341 $mform->addGroupRule('costgrp',$costgrprules);
342 $mform->setHelpButton('costgrp', array('cost', get_string('cost')), true);
343 $mform->setDefault('cost', '');
344 $mform->setDefault('currency', empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency);
348 //--------------------------------------------------------------------------------
349 $mform->addElement('header','', get_string('language'));
351 $languages=array();
352 $languages[''] = get_string('forceno');
353 $languages += get_list_of_languages();
354 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
356 //--------------------------------------------------------------------------------
357 if (has_capability('moodle/site:config', $systemcontext) && ((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
358 $mform->addElement('header', '', get_string('restrictmodules'));
360 $options = array();
361 $options['0'] = get_string('no');
362 $options['1'] = get_string('yes');
363 $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
364 $mods = array(0=>get_string('allownone'));
365 $mods += get_records_menu('modules', '','','','id, name');
368 $mform->addElement('select', 'allowedmods', get_string('to'), $mods,
369 array('multiple'=>'multiple', 'size'=>'10'));
370 $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
371 } else {
372 $mform->addElement('hidden', 'restrictmodules', null);
374 if ($CFG->restrictmodulesfor == 'all') {
375 $mform->setDefault('allowedmods', explode(',',$CFG->defaultallowedmodules));
376 if (!empty($CFG->restrictbydefault)) {
377 $mform->setDefault('restrictmodules', 1);
380 $mform->setType('restrictmodules', PARAM_INT);
382 /// customizable role names in this course
383 //--------------------------------------------------------------------------------
384 $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
385 $mform->setHelpButton('rolerenaming', array('rolerenaming', get_string('rolerenaming')), true);
387 if ($roles = get_records('role')) {
388 foreach ($roles as $role) {
389 $mform->addElement('text', 'role_'.$role->id, $role->name);
390 $mform->setType('role_'.$role->id, PARAM_TEXT);
391 if ($coursecontext) {
392 if ($rolename = get_record('role_names', 'roleid', $role->id, 'contextid', $coursecontext->id)) {
393 $mform->setDefault('role_'.$role->id, $rolename->name);
399 //--------------------------------------------------------------------------------
400 $this->add_action_buttons();
401 //--------------------------------------------------------------------------------
402 $mform->addElement('hidden', 'id', null);
403 $mform->setType('id', PARAM_INT);
405 // fill in default teacher and student names to keep backwards compatibility for a while
406 $mform->addElement('hidden', 'teacher', get_string('defaultcourseteacher'));
407 $mform->addElement('hidden', 'teachers', get_string('defaultcourseteachers'));
408 $mform->addElement('hidden', 'student', get_string('defaultcoursestudent'));
409 $mform->addElement('hidden', 'students', get_string('defaultcoursestudents'));
412 function definition_after_data() {
413 global $CFG;
415 $mform =& $this->_form;
417 // add availabe groupings
418 if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
419 $options = array();
420 if ($groupings = get_records('groupings', 'courseid', $courseid)) {
421 foreach ($groupings as $grouping) {
422 $options[$grouping->id] = format_string($grouping->name);
425 $gr_el =& $mform->getElement('defaultgroupingid');
426 $gr_el->load($options);
431 /// perform some extra moodle validation
432 function validation($data, $files) {
433 $errors = parent::validation($data, $files);
434 if ($foundcourses = get_records('course', 'shortname', $data['shortname'])) {
435 if (!empty($data['id'])) {
436 unset($foundcourses[$data['id']]);
438 if (!empty($foundcourses)) {
439 foreach ($foundcourses as $foundcourse) {
440 $foundcoursenames[] = $foundcourse->fullname;
442 $foundcoursenamestring = implode(',', $foundcoursenames);
443 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
447 if (empty($data['enrolenddisabled'])){
448 if ($data['enrolenddate'] <= $data['enrolstartdate']){
449 $errors['enroldateendgrp'] = get_string('enrolenddaterror');
453 return $errors;