Updated the 19 build version to 20100510
[moodle.git] / course / edit_form.php
blob2738e51695acbc292c8cf5465487b73c5286a61c
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 $courseconfig = get_config('moodlecourse');
11 $mform =& $this->_form;
13 $course = $this->_customdata['course'];
14 $category = $this->_customdata['category'];
16 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
17 $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
19 $disable_meta = false; // basic meta course state protection; server-side security checks not needed
21 if (!empty($course)) {
22 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
23 $context = $coursecontext;
25 if (course_in_meta($course)) {
26 $disable_meta = get_string('metaalreadyinmeta');
28 } else if ($course->metacourse) {
29 if (count_records('course_meta', 'parent_course', $course->id) > 0) {
30 $disable_meta = get_string('metaalreadyhascourses');
33 } else {
34 // if users already enrolled directly into coures, do not allow switching to meta,
35 // users with metacourse manage permission are exception
36 // please note that we do not need exact results - anything unexpected here prevents metacourse
37 $managers = get_users_by_capability($coursecontext, 'moodle/course:managemetacourse', 'u.id');
38 $enrolroles = get_roles_with_capability('moodle/course:view', CAP_ALLOW, $coursecontext);
39 if ($users = get_role_users(array_keys($enrolroles), $coursecontext, false, 'u.id', 'u.id ASC')) {
40 foreach($users as $user) {
41 if (!isset($managers[$user->id])) {
42 $disable_meta = get_string('metaalreadyhasenrolments');
43 break;
47 unset($managers);
48 unset($users);
49 unset($enrolroles);
51 } else {
52 $coursecontext = null;
53 $context = $categorycontext;
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);
70 $mform->setHelpButton('category', array('coursecategory', get_string('category')));
71 $mform->setDefault('category', $category->id);
72 $mform->setType('category', PARAM_INT);
74 if ($course and !has_capability('moodle/course:changecategory', $coursecontext)) {
75 $mform->hardFreeze('category');
76 $mform->setConstant('category', $category->id);
79 $mform->addElement('text','fullname', get_string('fullname'),'maxlength="254" size="50"');
80 $mform->setHelpButton('fullname', array('coursefullname', get_string('fullname')), true);
81 $mform->setDefault('fullname', get_string('defaultcoursefullname'));
82 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
83 $mform->setType('fullname', PARAM_MULTILANG);
84 if ($course and !has_capability('moodle/course:changefullname', $coursecontext)) {
85 $mform->hardFreeze('fullname');
86 $mform->setConstant('fullname', $course->fullname);
89 $mform->addElement('text','shortname', get_string('shortname'),'maxlength="100" size="20"');
90 $mform->setHelpButton('shortname', array('courseshortname', get_string('shortname')), true);
91 $mform->setDefault('shortname', get_string('defaultcourseshortname'));
92 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
93 $mform->setType('shortname', PARAM_MULTILANG);
94 if ($course and !has_capability('moodle/course:changeshortname', $coursecontext)) {
95 $mform->hardFreeze('shortname');
96 $mform->setConstant('shortname', $course->shortname);
99 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
100 $mform->setHelpButton('idnumber', array('courseidnumber', get_string('idnumbercourse')), true);
101 $mform->setType('idnumber', PARAM_RAW);
102 if ($course and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
103 $mform->hardFreeze('idnumber');
104 $mform->setConstants('idnumber', $course->idnumber);
107 $mform->addElement('htmleditor','summary', get_string('summary'), array('rows'=> '10', 'cols'=>'65'));
108 $mform->setHelpButton('summary', array('text', get_string('helptext')), true);
109 $mform->setType('summary', PARAM_RAW);
111 if ($course and !has_capability('moodle/course:changesummary', $coursecontext)) {
112 $mform->hardFreeze('summary');
115 $courseformats = get_list_of_plugins('course/format');
116 $formcourseformats = array();
117 foreach ($courseformats as $courseformat) {
118 $formcourseformats["$courseformat"] = get_string("format$courseformat","format_$courseformat");
119 if($formcourseformats["$courseformat"]=="[[format$courseformat]]") {
120 $formcourseformats["$courseformat"] = get_string("format$courseformat");
123 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
124 $mform->setHelpButton('format', array('courseformats', get_string('courseformats')), true);
125 $mform->setDefault('format', $courseconfig->format);
127 for ($i=1; $i<=52; $i++) {
128 $sectionmenu[$i] = "$i";
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->setHelpButton('startdate', array('coursestartdate', get_string('startdate')), true);
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->setHelpButton('hiddensections', array('coursehiddensections', get_string('hiddensections')), true);
142 $mform->setDefault('hiddensections', $courseconfig->hiddensections);
144 $options = range(0, 10);
145 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
146 $mform->setHelpButton('newsitems', array('coursenewsitems', get_string('newsitemsnumber')), true);
147 $mform->setDefault('newsitems', $courseconfig->newsitems);
149 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
150 $mform->setHelpButton('showgrades', array('coursegrades', get_string('grades')), true);
151 $mform->setDefault('showgrades', $courseconfig->showgrades);
153 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
154 $mform->setHelpButton('showreports', array('coursereports', get_string('activityreport')), true);
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->setHelpButton('maxbytes', array('courseuploadsize', get_string('maximumupload')), true);
160 $mform->setDefault('maxbytes', $courseconfig->maxbytes);
162 if (!empty($CFG->allowcoursethemes)) {
163 $themes=array();
164 $themes[''] = get_string('forceno');
165 $themes += get_list_of_themes();
166 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
169 $meta=array();
170 $meta[0] = get_string('no');
171 $meta[1] = get_string('yes');
172 if ($disable_meta === false) {
173 $mform->addElement('select', 'metacourse', get_string('managemeta'), $meta);
174 $mform->setHelpButton('metacourse', array('metacourse', get_string('metacourse')), true);
175 $mform->setDefault('metacourse', $courseconfig->metacourse);
176 } else {
177 // no metacourse element - we do not want to change it anyway!
178 $mform->addElement('static', 'nometacourse', get_string('managemeta'),
179 ((empty($course->metacourse)) ? $meta[0] : $meta[1]) . " - $disable_meta ");
180 $mform->setHelpButton('nometacourse', array('metacourse', get_string('metacourse')), true);
183 //--------------------------------------------------------------------------------
184 $mform->addElement('header','enrolhdr', get_string('enrolments'));
186 $choices = array();
187 $modules = explode(',', $CFG->enrol_plugins_enabled);
188 foreach ($modules as $module) {
189 $name = get_string('enrolname', "enrol_$module");
190 $plugin = enrolment_factory::factory($module);
191 if (method_exists($plugin, 'print_entry')) {
192 $choices[$name] = $module;
195 asort($choices);
196 $choices = array_flip($choices);
197 $choices = array_merge(array('' => get_string('sitedefault').' ('.get_string('enrolname', "enrol_$CFG->enrol").')'), $choices);
198 $mform->addElement('select', 'enrol', get_string('enrolmentplugins'), $choices);
199 $mform->setHelpButton('enrol', array('courseenrolmentplugins', get_string('enrolmentplugins')), true);
202 $roles = get_assignable_roles($context);
203 if (!empty($course)) {
204 // add current default role, so that it is selectable even when user can not assign it
205 if ($current_role = get_record('role', 'id', $course->defaultrole)) {
206 $roles[$current_role->id] = strip_tags(format_string($current_role->name, true));
209 $choices = array();
210 if ($sitedefaultrole = get_record('role', 'id', $CFG->defaultcourseroleid)) {
211 $choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name.')';
212 } else {
213 $choices[0] = get_string('sitedefault');
215 $choices = $choices + $roles;
217 // fix for MDL-9197
218 foreach ($choices as $choiceid => $choice) {
219 $choices[$choiceid] = format_string($choice);
222 $mform->addElement('select', 'defaultrole', get_string('defaultrole', 'role'), $choices);
223 $mform->setDefault('defaultrole', 0);
226 $radio = array();
227 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('no'), 0);
228 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('yes'), 1);
229 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('enroldate'), 2);
230 $mform->addGroup($radio, 'enrollable', get_string('enrollable'), ' ', false);
231 $mform->setHelpButton('enrollable', array('courseenrollable2', get_string('enrollable')), true);
232 $mform->setDefault('enrollable', 1);
234 $enroldatestartgrp = array();
235 $enroldatestartgrp[] = &MoodleQuickForm::createElement('date_selector', 'enrolstartdate');
236 $enroldatestartgrp[] = &MoodleQuickForm::createElement('checkbox', 'enrolstartdisabled', null, get_string('disable'));
237 $mform->addGroup($enroldatestartgrp, 'enrolstartdategrp', get_string('enrolstartdate'), ' ', false);
238 $mform->setDefault('enrolstartdate', 0);
239 $mform->setDefault('enrolstartdisabled', 1);
240 $mform->disabledIf('enrolstartdategrp', 'enrolstartdisabled', 'checked');
242 $enroldateendgrp = array();
243 $enroldateendgrp[] = &MoodleQuickForm::createElement('date_selector', 'enrolenddate');
244 $enroldateendgrp[] = &MoodleQuickForm::createElement('checkbox', 'enrolenddisabled', null, get_string('disable'));
245 $mform->addGroup($enroldateendgrp, 'enroldateendgrp', get_string('enrolenddate'), ' ', false);
246 $mform->setDefault('enrolenddate', 0);
247 $mform->setDefault('enrolenddisabled', 1);
248 $mform->disabledIf('enroldateendgrp', 'enrolenddisabled', 'checked');
250 $periodmenu=array();
251 $periodmenu[0] = get_string('unlimited');
252 for ($i=1; $i<=365; $i++) {
253 $seconds = $i * 86400;
254 $periodmenu[$seconds] = get_string('numdays', '', $i);
256 $mform->addElement('select', 'enrolperiod', get_string('enrolperiod'), $periodmenu);
257 $mform->setDefault('enrolperiod', 0);
260 //--------------------------------------------------------------------------------
261 $mform->addElement('header','expirynotifyhdr', get_string('expirynotify'));
263 $choices = array();
264 $choices['0'] = get_string('no');
265 $choices['1'] = get_string('yes');
266 $mform->addElement('select', 'expirynotify', get_string('notify'), $choices);
267 $mform->setHelpButton('expirynotify', array('expirynotify', get_string('expirynotify')), true);
268 $mform->setDefault('expirynotify', 0);
270 $mform->addElement('select', 'notifystudents', get_string('expirynotifystudents'), $choices);
271 $mform->setHelpButton('notifystudents', array('expirynotifystudents', get_string('expirynotifystudents')), true);
272 $mform->setDefault('notifystudents', 0);
274 $thresholdmenu=array();
275 for ($i=1; $i<=30; $i++) {
276 $seconds = $i * 86400;
277 $thresholdmenu[$seconds] = get_string('numdays', '', $i);
279 $mform->addElement('select', 'expirythreshold', get_string('expirythreshold'), $thresholdmenu);
280 $mform->setHelpButton('expirythreshold', array('expirythreshold', get_string('expirythreshold')), true);
281 $mform->setDefault('expirythreshold', 10 * 86400);
283 //--------------------------------------------------------------------------------
284 $mform->addElement('header','', get_string('groups', 'group'));
286 $choices = array();
287 $choices[NOGROUPS] = get_string('groupsnone', 'group');
288 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
289 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
290 $mform->addElement('select', 'groupmode', get_string('groupmode'), $choices);
291 $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')), true);
292 $mform->setDefault('groupmode', 0);
294 $choices = array();
295 $choices['0'] = get_string('no');
296 $choices['1'] = get_string('yes');
297 $mform->addElement('select', 'groupmodeforce', get_string('force'), $choices);
298 $mform->setHelpButton('groupmodeforce', array('groupmodeforce', get_string('groupmodeforce')), true);
299 $mform->setDefault('groupmodeforce', 0);
301 if (!empty($CFG->enablegroupings)) {
302 //default groupings selector
303 $options = array();
304 $options[0] = get_string('none');
305 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
308 //--------------------------------------------------------------------------------
309 $mform->addElement('header','', get_string('availability'));
311 $choices = array();
312 $choices['0'] = get_string('courseavailablenot');
313 $choices['1'] = get_string('courseavailable');
314 $mform->addElement('select', 'visible', get_string('availability'), $choices);
315 $mform->setHelpButton('visible', array('courseavailability', get_string('availability')), true);
316 $mform->setDefault('visible', 1);
317 if ($course and !has_capability('moodle/course:visibility', $coursecontext)) {
318 $mform->hardFreeze('visible');
319 $mform->setConstant('visible', $course->visible);
322 $mform->addElement('passwordunmask', 'enrolpassword', get_string('enrolmentkey'), 'size="25"');
323 $mform->setHelpButton('enrolpassword', array('enrolmentkey', get_string('enrolmentkey')), true);
324 $mform->setDefault('enrolpassword', '');
325 $mform->setType('enrolpassword', PARAM_RAW);
327 if (empty($course) 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');
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', 0);
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)) {
345 $enrol_object = $course;
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);
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);
374 //--------------------------------------------------------------------------------
375 if (has_capability('moodle/site:config', $systemcontext) && ((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
376 $mform->addElement('header', '', get_string('restrictmodules'));
378 $options = array();
379 $options['0'] = get_string('no');
380 $options['1'] = get_string('yes');
381 $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
382 $mods = array(0=>get_string('allownone'));
383 $mods += get_records_menu('modules', '','','','id, name');
386 $mform->addElement('select', 'allowedmods', get_string('to'), $mods,
387 array('multiple'=>'multiple', 'size'=>'10'));
388 $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
389 } else {
390 $mform->addElement('hidden', 'restrictmodules', null);
391 $mform->setType('restrictmodules', PARAM_INT);
393 if ($CFG->restrictmodulesfor == 'all') {
394 $mform->setDefault('allowedmods', explode(',',$CFG->defaultallowedmodules));
395 if (!empty($CFG->restrictbydefault)) {
396 $mform->setDefault('restrictmodules', 1);
399 $mform->setType('restrictmodules', PARAM_INT);
401 /// customizable role names in this course
402 //--------------------------------------------------------------------------------
403 $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
404 $mform->setHelpButton('rolerenaming', array('rolerenaming', get_string('rolerenaming')), true);
406 if ($roles = get_records('role')) {
407 foreach ($roles as $role) {
408 $mform->addElement('text', 'role_'.$role->id, $role->name);
409 $mform->setType('role_'.$role->id, PARAM_TEXT);
410 if ($coursecontext) {
411 if ($rolename = get_record('role_names', 'roleid', $role->id, 'contextid', $coursecontext->id)) {
412 $mform->setDefault('role_'.$role->id, $rolename->name);
418 //--------------------------------------------------------------------------------
419 $this->add_action_buttons();
420 //--------------------------------------------------------------------------------
421 $mform->addElement('hidden', 'id', null);
422 $mform->setType('id', PARAM_INT);
424 // fill in default teacher and student names to keep backwards compatibility for a while
425 $mform->addElement('hidden', 'teacher', get_string('defaultcourseteacher'));
426 $mform->setType('teacher', PARAM_RAW);
427 $mform->addElement('hidden', 'teachers', get_string('defaultcourseteachers'));
428 $mform->setType('teachers', PARAM_RAW);
429 $mform->addElement('hidden', 'student', get_string('defaultcoursestudent'));
430 $mform->setType('student', PARAM_RAW);
431 $mform->addElement('hidden', 'students', get_string('defaultcoursestudents'));
432 $mform->setType('students', PARAM_RAW);
435 function definition_after_data() {
436 global $CFG;
438 $mform =& $this->_form;
440 // add availabe groupings
441 if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
442 $options = array();
443 if ($groupings = get_records('groupings', 'courseid', $courseid)) {
444 foreach ($groupings as $grouping) {
445 $options[$grouping->id] = format_string($grouping->name);
448 $gr_el =& $mform->getElement('defaultgroupingid');
449 $gr_el->load($options);
454 /// perform some extra moodle validation
455 function validation($data, $files) {
456 global $CFG;
458 $errors = parent::validation($data, $files);
459 if ($foundcourses = get_records('course', 'shortname', $data['shortname'])) {
460 if (!empty($data['id'])) {
461 unset($foundcourses[$data['id']]);
463 if (!empty($foundcourses)) {
464 foreach ($foundcourses as $foundcourse) {
465 $foundcoursenames[] = $foundcourse->fullname;
467 $foundcoursenamestring = implode(',', $foundcoursenames);
468 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
472 if (empty($data['enrolenddisabled'])){
473 if ($data['enrolenddate'] <= $data['enrolstartdate']){
474 $errors['enroldateendgrp'] = get_string('enrolenddaterror');
478 if (!empty($CFG->enrol_manual_usepasswordpolicy) and isset($data['enrolpassword']) and $data['enrolpassword'] != '') {
479 $course = $this->_customdata['course'];
480 if ($course->password !== $data['enrolpassword']) {
481 // enforce password policy only if changing password - backwards compatibility
482 $errmsg = '';
483 if (!check_password_policy($data['enrolpassword'], $errmsg)) {
484 $errors['enrolpassword'] = $errmsg;
489 return $errors;