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 $managers = count(get_users_by_capability($coursecontext, 'moodle/course:managemetacourse'));
34 $participants = count(get_users_by_capability($coursecontext, 'moodle/course:view'));
35 if ($participants > $managers) {
36 $disable_meta = get_string('metaalreadyhasenrolments');
40 $coursecontext = null;
41 $context = $categorycontext;
44 /// form definition with new course defaults
45 //--------------------------------------------------------------------------------
46 $mform->addElement('header','general', get_string('general', 'form'));
48 //must have create course capability in both categories in order to move course
49 if (has_capability('moodle/course:create', $categorycontext)) {
50 $displaylist = array();
51 $parentlist = array();
52 make_categories_list($displaylist, $parentlist);
53 foreach ($displaylist as $key=>$val) {
54 if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT
, $key))) {
55 unset($displaylist[$key]);
58 $mform->addElement('select', 'category', get_string('category'), $displaylist);
60 $mform->addElement('hidden', 'category', null);
62 $mform->setHelpButton('category', array('coursecategory', get_string('category')));
63 $mform->setDefault('category', $category->id
);
64 $mform->setType('category', PARAM_INT
);
66 $mform->addElement('text','fullname', get_string('fullname'),'maxlength="254" size="50"');
67 $mform->setHelpButton('fullname', array('coursefullname', get_string('fullname')), true);
68 $mform->setDefault('fullname', get_string('defaultcoursefullname'));
69 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
70 $mform->setType('fullname', PARAM_MULTILANG
);
72 $mform->addElement('text','shortname', get_string('shortname'),'maxlength="100" size="20"');
73 $mform->setHelpButton('shortname', array('courseshortname', get_string('shortname')), true);
74 $mform->setDefault('shortname', get_string('defaultcourseshortname'));
75 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
76 $mform->setType('shortname', PARAM_MULTILANG
);
78 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
79 $mform->setHelpButton('idnumber', array('courseidnumber', get_string('idnumbercourse')), true);
80 $mform->setType('idnumber', PARAM_RAW
);
82 $mform->addElement('htmleditor','summary', get_string('summary'), array('rows'=> '10', 'cols'=>'65'));
83 $mform->setHelpButton('summary', array('text', get_string('helptext')), true);
84 $mform->setDefault('summary', get_string('defaultcoursesummary'));
85 $mform->addRule('summary', get_string('missingsummary'), 'required', null, 'client');
86 $mform->setType('summary', PARAM_RAW
);
88 $courseformats = get_list_of_plugins('course/format');
89 $formcourseformats = array();
90 foreach ($courseformats as $courseformat) {
91 $formcourseformats["$courseformat"] = get_string("format$courseformat","format_$courseformat");
92 if($formcourseformats["$courseformat"]=="[[format$courseformat]]") {
93 $formcourseformats["$courseformat"] = get_string("format$courseformat");
96 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
97 $mform->setHelpButton('format', array('courseformats', get_string('courseformats')), true);
98 $mform->setDefault('format', 'weeks');
100 for ($i=1; $i<=52; $i++
) {
101 $sectionmenu[$i] = "$i";
103 $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
104 $mform->setDefault('numsections', 10);
106 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
107 $mform->setHelpButton('startdate', array('coursestartdate', get_string('startdate')), true);
108 $mform->setDefault('startdate', time() +
3600 * 24);
111 $choices['0'] = get_string('hiddensectionscollapsed');
112 $choices['1'] = get_string('hiddensectionsinvisible');
113 $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
114 $mform->setHelpButton('hiddensections', array('coursehiddensections', get_string('hiddensections')), true);
115 $mform->setDefault('hiddensections', 0);
117 $options = range(0, 10);
118 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
119 $mform->setHelpButton('newsitems', array('coursenewsitems', get_string('newsitemsnumber')), true);
120 $mform->setDefault('newsitems', 5);
122 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
123 $mform->setHelpButton('showgrades', array('coursegrades', get_string('grades')), true);
124 $mform->setDefault('showgrades', 1);
126 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
127 $mform->setHelpButton('showreports', array('coursereports', get_string('activityreport')), true);
128 $mform->setDefault('showreports', 0);
130 $choices = get_max_upload_sizes($CFG->maxbytes
);
131 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
132 $mform->setHelpButton('maxbytes', array('courseuploadsize', get_string('maximumupload')), true);
134 if (!empty($CFG->allowcoursethemes
)) {
136 $themes[''] = get_string('forceno');
137 $themes +
= get_list_of_themes();
138 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
142 $meta[0] = get_string('no');
143 $meta[1] = get_string('yes');
144 if ($disable_meta === false) {
145 $mform->addElement('select', 'metacourse', get_string('managemeta'), $meta);
146 $mform->setHelpButton('metacourse', array('metacourse', get_string('metacourse')), true);
147 $mform->setDefault('metacourse', 0);
149 // no metacourse element - we do not want to change it anyway!
150 $mform->addElement('static', 'nometacourse', get_string('managemeta'),
151 ((empty($course->metacourse
)) ?
$meta[0] : $meta[1]) . " - $disable_meta ");
152 $mform->setHelpButton('nometacourse', array('metacourse', get_string('metacourse')), true);
155 //--------------------------------------------------------------------------------
156 $mform->addElement('header','enrolhdr', get_string('enrolments'));
159 $modules = explode(',', $CFG->enrol_plugins_enabled
);
160 foreach ($modules as $module) {
161 $name = get_string('enrolname', "enrol_$module");
162 $plugin = enrolment_factory
::factory($module);
163 if (method_exists($plugin, 'print_entry')) {
164 $choices[$name] = $module;
168 $choices = array_flip($choices);
169 $choices = array_merge(array('' => get_string('sitedefault').' ('.get_string('enrolname', "enrol_$CFG->enrol").')'), $choices);
170 $mform->addElement('select', 'enrol', get_string('enrolmentplugins'), $choices);
171 $mform->setHelpButton('enrol', array('courseenrolmentplugins', get_string('enrolmentplugins')), true);
174 $roles = get_assignable_roles($context);
175 if (!empty($course)) {
176 // add current default role, so that it is selectable even when user can not assign it
177 if ($current_role = get_record('role', 'id', $course->defaultrole
)) {
178 $roles[$current_role->id
] = strip_tags(format_string($current_role->name
, true));
182 if ($sitedefaultrole = get_record('role', 'id', $CFG->defaultcourseroleid
)) {
183 $choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name
.')';
185 $choices[0] = get_string('sitedefault');
187 $choices = $choices +
$roles;
190 foreach ($choices as $choiceid => $choice) {
191 $choices[$choiceid] = format_string($choice);
194 $mform->addElement('select', 'defaultrole', get_string('defaultrole', 'role'), $choices);
195 $mform->setDefault('defaultrole', 0);
199 $radio[] = &MoodleQuickForm
::createElement('radio', 'enrollable', null, get_string('no'), 0);
200 $radio[] = &MoodleQuickForm
::createElement('radio', 'enrollable', null, get_string('yes'), 1);
201 $radio[] = &MoodleQuickForm
::createElement('radio', 'enrollable', null, get_string('enroldate'), 2);
202 $mform->addGroup($radio, 'enrollable', get_string('enrollable'), ' ', false);
203 $mform->setHelpButton('enrollable', array('courseenrollable2', get_string('enrollable')), true);
204 $mform->setDefault('enrollable', 1);
206 $enroldatestartgrp = array();
207 $enroldatestartgrp[] = &MoodleQuickForm
::createElement('date_selector', 'enrolstartdate');
208 $enroldatestartgrp[] = &MoodleQuickForm
::createElement('checkbox', 'enrolstartdisabled', null, get_string('disable'));
209 $mform->addGroup($enroldatestartgrp, 'enrolstartdategrp', get_string('enrolstartdate'), ' ', false);
210 $mform->setDefault('enrolstartdate', 0);
211 $mform->setDefault('enrolstartdisabled', 1);
212 $mform->disabledIf('enrolstartdategrp', 'enrolstartdisabled', 'checked');
214 $enroldateendgrp = array();
215 $enroldateendgrp[] = &MoodleQuickForm
::createElement('date_selector', 'enrolenddate');
216 $enroldateendgrp[] = &MoodleQuickForm
::createElement('checkbox', 'enrolenddisabled', null, get_string('disable'));
217 $mform->addGroup($enroldateendgrp, 'enroldateendgrp', get_string('enrolenddate'), ' ', false);
218 $mform->setDefault('enrolenddate', 0);
219 $mform->setDefault('enrolenddisabled', 1);
220 $mform->disabledIf('enroldateendgrp', 'enrolenddisabled', 'checked');
223 $periodmenu[0] = get_string('unlimited');
224 for ($i=1; $i<=365; $i++
) {
225 $seconds = $i * 86400;
226 $periodmenu[$seconds] = get_string('numdays', '', $i);
228 $mform->addElement('select', 'enrolperiod', get_string('enrolperiod'), $periodmenu);
229 $mform->setDefault('enrolperiod', 0);
232 //--------------------------------------------------------------------------------
233 $mform->addElement('header','expirynotifyhdr', get_string('expirynotify'));
236 $choices['0'] = get_string('no');
237 $choices['1'] = get_string('yes');
238 $mform->addElement('select', 'expirynotify', get_string('notify'), $choices);
239 $mform->setHelpButton('expirynotify', array('expirynotify', get_string('expirynotify')), true);
240 $mform->setDefault('expirynotify', 0);
242 $mform->addElement('select', 'notifystudents', get_string('expirynotifystudents'), $choices);
243 $mform->setHelpButton('notifystudents', array('expirynotifystudents', get_string('expirynotifystudents')), true);
244 $mform->setDefault('notifystudents', 0);
246 $thresholdmenu=array();
247 for ($i=1; $i<=30; $i++
) {
248 $seconds = $i * 86400;
249 $thresholdmenu[$seconds] = get_string('numdays', '', $i);
251 $mform->addElement('select', 'expirythreshold', get_string('expirythreshold'), $thresholdmenu);
252 $mform->setHelpButton('expirythreshold', array('expirythreshold', get_string('expirythreshold')), true);
253 $mform->setDefault('expirythreshold', 10 * 86400);
255 //--------------------------------------------------------------------------------
256 $mform->addElement('header','', get_string('groups', 'group'));
259 $choices[NOGROUPS
] = get_string('no');
260 $choices[SEPARATEGROUPS
] = get_string('separate');
261 $choices[VISIBLEGROUPS
] = get_string('visible');
262 $mform->addElement('select', 'groupmode', get_string('groupmode'), $choices);
263 $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')), true);
264 $mform->setDefault('groupmode', 0);
267 $choices['0'] = get_string('no');
268 $choices['1'] = get_string('yes');
269 $mform->addElement('select', 'groupmodeforce', get_string('force'), $choices);
270 $mform->setHelpButton('groupmodeforce', array('groupmodeforce', get_string('groupmodeforce')), true);
271 $mform->setDefault('groupmodeforce', 0);
273 if (!empty($CFG->enablegroupings
)) {
274 //default groupings selector
276 $options[0] = get_string('none');
277 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
280 //--------------------------------------------------------------------------------
281 $mform->addElement('header','', get_string('availability'));
284 $choices['0'] = get_string('courseavailablenot');
285 $choices['1'] = get_string('courseavailable');
286 $mform->addElement('select', 'visible', get_string('availability'), $choices);
287 $mform->setHelpButton('visible', array('courseavailability', get_string('availability')), true);
288 $mform->setDefault('visible', 1);
290 $mform->addElement('passwordunmask', 'enrolpassword', get_string('enrolmentkey'), 'size="25"');
291 $mform->setHelpButton('enrolpassword', array('enrolmentkey', get_string('enrolmentkey')), true);
292 $mform->setDefault('enrolpassword', '');
293 $mform->setType('enrolpassword', PARAM_RAW
);
296 $choices['0'] = get_string('guestsno');
297 $choices['1'] = get_string('guestsyes');
298 $choices['2'] = get_string('guestskey');
299 $mform->addElement('select', 'guest', get_string('opentoguests'), $choices);
300 $mform->setHelpButton('guest', array('guestaccess', get_string('opentoguests')), true);
301 $mform->setDefault('guest', 0);
303 // 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
304 $enrol_object = $CFG;
305 if (!empty($course)) {
306 $enrol_object = $course;
308 if (method_exists(enrolment_factory
::factory($enrol_object->enrol
), 'print_entry') && $enrol_object->enrol
!= 'manual'){
310 $currencies = get_list_of_currencies();
311 $costgroup[]= &MoodleQuickForm
::createElement('text','cost', '', 'maxlength="6" size="6"');
312 $costgroup[]= &MoodleQuickForm
::createElement('select', 'currency', '', $currencies);
313 $mform->addGroup($costgroup, 'costgrp', get_string('cost'), ' ', false);
314 //defining a rule for a form element within a group :
315 $costgrprules=array();
316 //set the message to null to tell Moodle to use a default message
317 //available for most rules, fetched from language pack (err_{rulename}).
318 $costgrprules['cost'][]=array(null, 'numeric', null, 'client');
319 $mform->addGroupRule('costgrp',$costgrprules);
320 $mform->setHelpButton('costgrp', array('cost', get_string('cost')), true);
321 $mform->setDefault('cost', '');
322 $mform->setDefault('currency', empty($CFG->enrol_currency
) ?
'USD' : $CFG->enrol_currency
);
326 //--------------------------------------------------------------------------------
327 $mform->addElement('header','', get_string('language'));
330 $languages[''] = get_string('forceno');
331 $languages +
= get_list_of_languages();
332 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
334 //--------------------------------------------------------------------------------
335 if (has_capability('moodle/site:config', $systemcontext) && ((!empty($course->requested
) && $CFG->restrictmodulesfor
== 'requested') ||
$CFG->restrictmodulesfor
== 'all')) {
336 $mform->addElement('header', '', get_string('restrictmodules'));
339 $options['0'] = get_string('no');
340 $options['1'] = get_string('yes');
341 $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
342 $mods = array(0=>get_string('allownone'));
343 $mods +
= get_records_menu('modules', '','','','id, name');
346 $mform->addElement('select', 'allowedmods', get_string('to'), $mods,
347 array('multiple'=>'multiple', 'size'=>'10'));
348 $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
350 $mform->addElement('hidden', 'restrictmodules', null);
352 if ($CFG->restrictmodulesfor
== 'all') {
353 $mform->setDefault('allowedmods', explode(',',$CFG->defaultallowedmodules
));
354 if (!empty($CFG->restrictbydefault
)) {
355 $mform->setDefault('restrictmodules', 1);
358 $mform->setType('restrictmodules', PARAM_INT
);
360 /// customizable role names in this course
361 //--------------------------------------------------------------------------------
362 $mform->addElement('header','', get_string('roles'));
364 if ($roles = get_records('role')) {
365 foreach ($roles as $role) {
366 $mform->addElement('text', 'role_'.$role->id
, $role->name
);
367 if ($coursecontext) {
368 if ($rolename = get_record('role_names', 'roleid', $role->id
, 'contextid', $coursecontext->id
)) {
369 $mform->setDefault('role_'.$role->id
, $rolename->text
);
375 //--------------------------------------------------------------------------------
376 $this->add_action_buttons();
377 //--------------------------------------------------------------------------------
378 $mform->addElement('hidden', 'id', null);
379 $mform->setType('id', PARAM_INT
);
381 // fill in default teacher and student names to keep backwards compatibility for a while
382 $mform->addElement('hidden', 'teacher', get_string('defaultcourseteacher'));
383 $mform->addElement('hidden', 'teachers', get_string('defaultcourseteachers'));
384 $mform->addElement('hidden', 'student', get_string('defaultcoursestudent'));
385 $mform->addElement('hidden', 'students', get_string('defaultcoursestudents'));
388 function definition_after_data() {
391 $mform =& $this->_form
;
393 // add availabe groupings
394 if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
396 if ($groupings = get_records('groupings', 'courseid', $courseid)) {
397 foreach ($groupings as $grouping) {
398 $options[$grouping->id
] = format_string($grouping->name
);
401 $gr_el =& $mform->getElement('defaultgroupingid');
402 $gr_el->load($options);
407 /// perform some extra moodle validation
408 function validation($data){
410 if ($foundcourses = get_records('course', 'shortname', $data['shortname'])) {
411 if (!empty($data['id'])) {
412 unset($foundcourses[$data['id']]);
414 if (!empty($foundcourses)) {
415 foreach ($foundcourses as $foundcourse) {
416 $foundcoursenames[] = $foundcourse->fullname
;
418 $foundcoursenamestring = implode(',', $foundcoursenames);
419 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
423 if (empty($data['enrolenddisabled'])){
424 if ($data['enrolenddate'] <= $data['enrolstartdate']){
425 $errors['enroldateendgrp'] = get_string('enrolenddaterror');
429 if (0 == count($errors)){