3 require_once($CFG->libdir
.'/formslib.php');
5 class course_edit_form
extends moodleform
{
7 function definition() {
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');
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');
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);
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
);
83 $mform->addElement('text','shortname', get_string('shortname'),'maxlength="100" size="20"');
84 $mform->setHelpButton('shortname', array('courseshortname', get_string('shortname')), true);
85 $mform->setDefault('shortname', get_string('defaultcourseshortname'));
86 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
87 $mform->setType('shortname', PARAM_MULTILANG
);
89 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
90 $mform->setHelpButton('idnumber', array('courseidnumber', get_string('idnumbercourse')), true);
91 $mform->setType('idnumber', PARAM_RAW
);
93 $mform->addElement('htmleditor','summary', get_string('summary'), array('rows'=> '10', 'cols'=>'65'));
94 $mform->setHelpButton('summary', array('text', get_string('helptext')), true);
95 $mform->setType('summary', PARAM_RAW
);
97 $courseformats = get_list_of_plugins('course/format');
98 $formcourseformats = array();
99 foreach ($courseformats as $courseformat) {
100 $formcourseformats["$courseformat"] = get_string("format$courseformat","format_$courseformat");
101 if($formcourseformats["$courseformat"]=="[[format$courseformat]]") {
102 $formcourseformats["$courseformat"] = get_string("format$courseformat");
105 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
106 $mform->setHelpButton('format', array('courseformats', get_string('courseformats')), true);
107 $mform->setDefault('format', 'weeks');
109 for ($i=1; $i<=52; $i++
) {
110 $sectionmenu[$i] = "$i";
112 $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
113 $mform->setDefault('numsections', 10);
115 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
116 $mform->setHelpButton('startdate', array('coursestartdate', get_string('startdate')), true);
117 $mform->setDefault('startdate', time() +
3600 * 24);
120 $choices['0'] = get_string('hiddensectionscollapsed');
121 $choices['1'] = get_string('hiddensectionsinvisible');
122 $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
123 $mform->setHelpButton('hiddensections', array('coursehiddensections', get_string('hiddensections')), true);
124 $mform->setDefault('hiddensections', 0);
126 $options = range(0, 10);
127 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
128 $mform->setHelpButton('newsitems', array('coursenewsitems', get_string('newsitemsnumber')), true);
129 $mform->setDefault('newsitems', 5);
131 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
132 $mform->setHelpButton('showgrades', array('coursegrades', get_string('grades')), true);
133 $mform->setDefault('showgrades', 1);
135 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
136 $mform->setHelpButton('showreports', array('coursereports', get_string('activityreport')), true);
137 $mform->setDefault('showreports', 0);
139 $choices = get_max_upload_sizes($CFG->maxbytes
);
140 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
141 $mform->setHelpButton('maxbytes', array('courseuploadsize', get_string('maximumupload')), true);
143 if (!empty($CFG->allowcoursethemes
)) {
145 $themes[''] = get_string('forceno');
146 $themes +
= get_list_of_themes();
147 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
151 $meta[0] = get_string('no');
152 $meta[1] = get_string('yes');
153 if ($disable_meta === false) {
154 $mform->addElement('select', 'metacourse', get_string('managemeta'), $meta);
155 $mform->setHelpButton('metacourse', array('metacourse', get_string('metacourse')), true);
156 $mform->setDefault('metacourse', 0);
158 // no metacourse element - we do not want to change it anyway!
159 $mform->addElement('static', 'nometacourse', get_string('managemeta'),
160 ((empty($course->metacourse
)) ?
$meta[0] : $meta[1]) . " - $disable_meta ");
161 $mform->setHelpButton('nometacourse', array('metacourse', get_string('metacourse')), true);
164 //--------------------------------------------------------------------------------
165 $mform->addElement('header','enrolhdr', get_string('enrolments'));
168 $modules = explode(',', $CFG->enrol_plugins_enabled
);
169 foreach ($modules as $module) {
170 $name = get_string('enrolname', "enrol_$module");
171 $plugin = enrolment_factory
::factory($module);
172 if (method_exists($plugin, 'print_entry')) {
173 $choices[$name] = $module;
177 $choices = array_flip($choices);
178 $choices = array_merge(array('' => get_string('sitedefault').' ('.get_string('enrolname', "enrol_$CFG->enrol").')'), $choices);
179 $mform->addElement('select', 'enrol', get_string('enrolmentplugins'), $choices);
180 $mform->setHelpButton('enrol', array('courseenrolmentplugins', get_string('enrolmentplugins')), true);
183 $roles = get_assignable_roles($context);
184 if (!empty($course)) {
185 // add current default role, so that it is selectable even when user can not assign it
186 if ($current_role = get_record('role', 'id', $course->defaultrole
)) {
187 $roles[$current_role->id
] = strip_tags(format_string($current_role->name
, true));
191 if ($sitedefaultrole = get_record('role', 'id', $CFG->defaultcourseroleid
)) {
192 $choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name
.')';
194 $choices[0] = get_string('sitedefault');
196 $choices = $choices +
$roles;
199 foreach ($choices as $choiceid => $choice) {
200 $choices[$choiceid] = format_string($choice);
203 $mform->addElement('select', 'defaultrole', get_string('defaultrole', 'role'), $choices);
204 $mform->setDefault('defaultrole', 0);
208 $radio[] = &MoodleQuickForm
::createElement('radio', 'enrollable', null, get_string('no'), 0);
209 $radio[] = &MoodleQuickForm
::createElement('radio', 'enrollable', null, get_string('yes'), 1);
210 $radio[] = &MoodleQuickForm
::createElement('radio', 'enrollable', null, get_string('enroldate'), 2);
211 $mform->addGroup($radio, 'enrollable', get_string('enrollable'), ' ', false);
212 $mform->setHelpButton('enrollable', array('courseenrollable2', get_string('enrollable')), true);
213 $mform->setDefault('enrollable', 1);
215 $enroldatestartgrp = array();
216 $enroldatestartgrp[] = &MoodleQuickForm
::createElement('date_selector', 'enrolstartdate');
217 $enroldatestartgrp[] = &MoodleQuickForm
::createElement('checkbox', 'enrolstartdisabled', null, get_string('disable'));
218 $mform->addGroup($enroldatestartgrp, 'enrolstartdategrp', get_string('enrolstartdate'), ' ', false);
219 $mform->setDefault('enrolstartdate', 0);
220 $mform->setDefault('enrolstartdisabled', 1);
221 $mform->disabledIf('enrolstartdategrp', 'enrolstartdisabled', 'checked');
223 $enroldateendgrp = array();
224 $enroldateendgrp[] = &MoodleQuickForm
::createElement('date_selector', 'enrolenddate');
225 $enroldateendgrp[] = &MoodleQuickForm
::createElement('checkbox', 'enrolenddisabled', null, get_string('disable'));
226 $mform->addGroup($enroldateendgrp, 'enroldateendgrp', get_string('enrolenddate'), ' ', false);
227 $mform->setDefault('enrolenddate', 0);
228 $mform->setDefault('enrolenddisabled', 1);
229 $mform->disabledIf('enroldateendgrp', 'enrolenddisabled', 'checked');
232 $periodmenu[0] = get_string('unlimited');
233 for ($i=1; $i<=365; $i++
) {
234 $seconds = $i * 86400;
235 $periodmenu[$seconds] = get_string('numdays', '', $i);
237 $mform->addElement('select', 'enrolperiod', get_string('enrolperiod'), $periodmenu);
238 $mform->setDefault('enrolperiod', 0);
241 //--------------------------------------------------------------------------------
242 $mform->addElement('header','expirynotifyhdr', get_string('expirynotify'));
245 $choices['0'] = get_string('no');
246 $choices['1'] = get_string('yes');
247 $mform->addElement('select', 'expirynotify', get_string('notify'), $choices);
248 $mform->setHelpButton('expirynotify', array('expirynotify', get_string('expirynotify')), true);
249 $mform->setDefault('expirynotify', 0);
251 $mform->addElement('select', 'notifystudents', get_string('expirynotifystudents'), $choices);
252 $mform->setHelpButton('notifystudents', array('expirynotifystudents', get_string('expirynotifystudents')), true);
253 $mform->setDefault('notifystudents', 0);
255 $thresholdmenu=array();
256 for ($i=1; $i<=30; $i++
) {
257 $seconds = $i * 86400;
258 $thresholdmenu[$seconds] = get_string('numdays', '', $i);
260 $mform->addElement('select', 'expirythreshold', get_string('expirythreshold'), $thresholdmenu);
261 $mform->setHelpButton('expirythreshold', array('expirythreshold', get_string('expirythreshold')), true);
262 $mform->setDefault('expirythreshold', 10 * 86400);
264 //--------------------------------------------------------------------------------
265 $mform->addElement('header','', get_string('groups', 'group'));
268 $choices[NOGROUPS
] = get_string('groupsnone', 'group');
269 $choices[SEPARATEGROUPS
] = get_string('groupsseparate', 'group');
270 $choices[VISIBLEGROUPS
] = get_string('groupsvisible', 'group');
271 $mform->addElement('select', 'groupmode', get_string('groupmode'), $choices);
272 $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')), true);
273 $mform->setDefault('groupmode', 0);
276 $choices['0'] = get_string('no');
277 $choices['1'] = get_string('yes');
278 $mform->addElement('select', 'groupmodeforce', get_string('force'), $choices);
279 $mform->setHelpButton('groupmodeforce', array('groupmodeforce', get_string('groupmodeforce')), true);
280 $mform->setDefault('groupmodeforce', 0);
282 if (!empty($CFG->enablegroupings
)) {
283 //default groupings selector
285 $options[0] = get_string('none');
286 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
289 //--------------------------------------------------------------------------------
290 $mform->addElement('header','', get_string('availability'));
293 $choices['0'] = get_string('courseavailablenot');
294 $choices['1'] = get_string('courseavailable');
295 $mform->addElement('select', 'visible', get_string('availability'), $choices);
296 $mform->setHelpButton('visible', array('courseavailability', get_string('availability')), true);
297 $mform->setDefault('visible', 1);
299 $mform->addElement('passwordunmask', 'enrolpassword', get_string('enrolmentkey'), 'size="25"');
300 $mform->setHelpButton('enrolpassword', array('enrolmentkey', get_string('enrolmentkey')), true);
301 $mform->setDefault('enrolpassword', '');
302 $mform->setType('enrolpassword', PARAM_RAW
);
305 $choices['0'] = get_string('guestsno');
306 $choices['1'] = get_string('guestsyes');
307 $choices['2'] = get_string('guestskey');
308 $mform->addElement('select', 'guest', get_string('opentoguests'), $choices);
309 $mform->setHelpButton('guest', array('guestaccess', get_string('opentoguests')), true);
310 $mform->setDefault('guest', 0);
312 // 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
313 $enrol_object = $CFG;
314 if (!empty($course)) {
315 $enrol_object = $course;
317 // If the print_entry method exists and the course enrol method isn't manual (both set or inherited from site), show cost
318 if (method_exists(enrolment_factory
::factory($enrol_object->enrol
), 'print_entry') && !($enrol_object->enrol
== 'manual' ||
(empty($enrol_object->enrol
) && $CFG->enrol
== 'manual'))) {
320 $currencies = get_list_of_currencies();
321 $costgroup[]= &MoodleQuickForm
::createElement('text','cost', '', 'maxlength="6" size="6"');
322 $costgroup[]= &MoodleQuickForm
::createElement('select', 'currency', '', $currencies);
323 $mform->addGroup($costgroup, 'costgrp', get_string('cost'), ' ', false);
324 //defining a rule for a form element within a group :
325 $costgrprules=array();
326 //set the message to null to tell Moodle to use a default message
327 //available for most rules, fetched from language pack (err_{rulename}).
328 $costgrprules['cost'][]=array(null, 'numeric', null, 'client');
329 $mform->addGroupRule('costgrp',$costgrprules);
330 $mform->setHelpButton('costgrp', array('cost', get_string('cost')), true);
331 $mform->setDefault('cost', '');
332 $mform->setDefault('currency', empty($CFG->enrol_currency
) ?
'USD' : $CFG->enrol_currency
);
336 //--------------------------------------------------------------------------------
337 $mform->addElement('header','', get_string('language'));
340 $languages[''] = get_string('forceno');
341 $languages +
= get_list_of_languages();
342 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
344 //--------------------------------------------------------------------------------
345 if (has_capability('moodle/site:config', $systemcontext) && ((!empty($course->requested
) && $CFG->restrictmodulesfor
== 'requested') ||
$CFG->restrictmodulesfor
== 'all')) {
346 $mform->addElement('header', '', get_string('restrictmodules'));
349 $options['0'] = get_string('no');
350 $options['1'] = get_string('yes');
351 $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
352 $mods = array(0=>get_string('allownone'));
353 $mods +
= get_records_menu('modules', '','','','id, name');
356 $mform->addElement('select', 'allowedmods', get_string('to'), $mods,
357 array('multiple'=>'multiple', 'size'=>'10'));
358 $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
360 $mform->addElement('hidden', 'restrictmodules', null);
362 if ($CFG->restrictmodulesfor
== 'all') {
363 $mform->setDefault('allowedmods', explode(',',$CFG->defaultallowedmodules
));
364 if (!empty($CFG->restrictbydefault
)) {
365 $mform->setDefault('restrictmodules', 1);
368 $mform->setType('restrictmodules', PARAM_INT
);
370 /// customizable role names in this course
371 //--------------------------------------------------------------------------------
372 $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
373 $mform->setHelpButton('rolerenaming', array('rolerenaming', get_string('rolerenaming')), true);
375 if ($roles = get_records('role')) {
376 foreach ($roles as $role) {
377 $mform->addElement('text', 'role_'.$role->id
, $role->name
);
378 if ($coursecontext) {
379 if ($rolename = get_record('role_names', 'roleid', $role->id
, 'contextid', $coursecontext->id
)) {
380 $mform->setDefault('role_'.$role->id
, $rolename->name
);
386 //--------------------------------------------------------------------------------
387 $this->add_action_buttons();
388 //--------------------------------------------------------------------------------
389 $mform->addElement('hidden', 'id', null);
390 $mform->setType('id', PARAM_INT
);
392 // fill in default teacher and student names to keep backwards compatibility for a while
393 $mform->addElement('hidden', 'teacher', get_string('defaultcourseteacher'));
394 $mform->addElement('hidden', 'teachers', get_string('defaultcourseteachers'));
395 $mform->addElement('hidden', 'student', get_string('defaultcoursestudent'));
396 $mform->addElement('hidden', 'students', get_string('defaultcoursestudents'));
399 function definition_after_data() {
402 $mform =& $this->_form
;
404 // add availabe groupings
405 if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
407 if ($groupings = get_records('groupings', 'courseid', $courseid)) {
408 foreach ($groupings as $grouping) {
409 $options[$grouping->id
] = format_string($grouping->name
);
412 $gr_el =& $mform->getElement('defaultgroupingid');
413 $gr_el->load($options);
418 /// perform some extra moodle validation
419 function validation($data, $files) {
420 $errors = parent
::validation($data, $files);
421 if ($foundcourses = get_records('course', 'shortname', $data['shortname'])) {
422 if (!empty($data['id'])) {
423 unset($foundcourses[$data['id']]);
425 if (!empty($foundcourses)) {
426 foreach ($foundcourses as $foundcourse) {
427 $foundcoursenames[] = $foundcourse->fullname
;
429 $foundcoursenamestring = implode(',', $foundcoursenames);
430 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
434 if (empty($data['enrolenddisabled'])){
435 if ($data['enrolenddate'] <= $data['enrolstartdate']){
436 $errors['enroldateendgrp'] = get_string('enrolenddaterror');