Finally fixed XHTML error on this page.
[moodle.git] / course / edit_form.php
CommitLineData
a81bf9c3 1<?php //$Id$
2
39625498 3require_once($CFG->libdir.'/formslib.php');
4
a81bf9c3 5class course_edit_form extends moodleform {
6
7 function definition() {
8 global $USER, $CFG;
9
10 $mform =& $this->_form;
a81bf9c3 11
12 $course = $this->_customdata['course'];
13 $category = $this->_customdata['category'];
14
15 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
16 $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
17
18 $disable_meta = false; // basic meta course state protection; server-side security checks not needed
19
20 if (!empty($course)) {
21 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
22 $context = $coursecontext;
23
24 if (course_in_meta($course)) {
25 $disable_meta = get_string('metaalreadyinmeta');
26
27 } else if ($course->metacourse) {
3d397ee0 28 if (count_records('course_meta', 'parent_course', $course->id) > 0) {
a81bf9c3 29 $disable_meta = get_string('metaalreadyhascourses');
30 }
31
32 } else {
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');
37 }
461f5127 38 }
39 } else {
a81bf9c3 40 $coursecontext = null;
41 $context = $categorycontext;
42 }
43
44/// form definition with new course defaults
45//--------------------------------------------------------------------------------
2b63df96 46 $mform->addElement('header','general', get_string('general', 'form'));
a81bf9c3 47
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]);
56 }
461f5127 57 }
a81bf9c3 58 $mform->addElement('select', 'category', get_string('category'), $displaylist);
59 } else {
60 $mform->addElement('hidden', 'category', null);
461f5127 61 }
a81bf9c3 62 $mform->setHelpButton('category', array('coursecategory', get_string('category')));
63 $mform->setDefault('category', $category->id);
64 $mform->setType('category', PARAM_INT);
461f5127 65
a81bf9c3 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);
461f5127 71
a81bf9c3 72 $mform->addElement('text','shortname', get_string('shortname'),'maxlength="15" size="10"');
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);
01963b95 77
a81bf9c3 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);
01963b95 81
a81bf9c3 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);
87
88 $courseformats = get_list_of_plugins('course/format');
89 $formcourseformats = array();
90 foreach ($courseformats as $courseformat) {
ae628043 91 $formcourseformats["$courseformat"] = get_string("format$courseformat","format_$courseformat");
92 if($formcourseformats["$courseformat"]=="[[format$courseformat]]") {
93 $formcourseformats["$courseformat"] = get_string("format$courseformat");
a23f0aaf 94 }
a81bf9c3 95 }
96 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
97 $mform->setHelpButton('format', array('courseformats', get_string('courseformats')), true);
98 $mform->setDefault('format', 'weeks');
99
100 for ($i=1; $i<=52; $i++) {
101 $sectionmenu[$i] = "$i";
102 }
103 $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
104 $mform->setDefault('numsections', 10);
105
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);
109
110 $choices = array();
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);
116
65877218 117 $options = range(0, 10);
a81bf9c3 118 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
119 $mform->setHelpButton('newsitems', array('coursenewsitems', get_string('newsitemsnumber')), true);
120 $mform->setDefault('newsitems', 5);
121
65877218 122 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
a81bf9c3 123 $mform->setHelpButton('showgrades', array('coursegrades', get_string('grades')), true);
124 $mform->setDefault('showgrades', 1);
125
65877218 126 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
a81bf9c3 127 $mform->setHelpButton('showreports', array('coursereports', get_string('activityreport')), true);
128 $mform->setDefault('showreports', 0);
129
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);
133
134 if (!empty($CFG->allowcoursethemes)) {
135 $themes=array();
136 $themes[''] = get_string('forceno');
137 $themes += get_list_of_themes();
138 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
139 }
461f5127 140
01963b95 141 $meta=array();
142 $meta[0] = get_string('no');
143 $meta[1] = get_string('yes');
a81bf9c3 144 if ($disable_meta === false) {
145 $mform->addElement('select', 'metacourse', get_string('managemeta'), $meta);
3d397ee0 146 $mform->setHelpButton('metacourse', array('metacourse', get_string('metacourse')), true);
147 $mform->setDefault('metacourse', 0);
a81bf9c3 148 } else {
3d397ee0 149 // no metacourse element - we do not want to change it anyway!
150 $mform->addElement('static', 'nometacourse', get_string('managemeta'),
a81bf9c3 151 ((empty($course->metacourse)) ? $meta[0] : $meta[1]) . " - $disable_meta ");
3d397ee0 152 $mform->setHelpButton('nometacourse', array('metacourse', get_string('metacourse')), true);
a81bf9c3 153 }
01963b95 154
a81bf9c3 155 $roles = get_assignable_roles($context);
156 if (!empty($course)) {
157 // add current default role, so that it is selectable even when user can not assign it
158 if ($current_role = get_record('role', 'id', $course->defaultrole)) {
159 $roles[$current_role->id] = strip_tags(format_string($current_role->name, true));
160 }
161 }
162 $choices = array();
163 if ($sitedefaultrole = get_record('role', 'id', $CFG->defaultcourseroleid)) {
164 $choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name.')';
01963b95 165 } else {
a81bf9c3 166 $choices[0] = get_string('sitedefault');
01963b95 167 }
a81bf9c3 168 $choices = $choices + $roles;
169 $mform->addElement('select', 'defaultrole', get_string('defaultrole', 'role'), $choices);
170 $mform->setDefault('defaultrole', 0);
171
172//--------------------------------------------------------------------------------
173 $mform->addElement('header','enrolhdr', get_string('enrolments'));
174
e517b6da 175 $choices = array();
a81bf9c3 176 $modules = explode(',', $CFG->enrol_plugins_enabled);
177 foreach ($modules as $module) {
178 $name = get_string('enrolname', "enrol_$module");
179 $plugin = enrolment_factory::factory($module);
180 if (method_exists($plugin, 'print_entry')) {
181 $choices[$name] = $module;
182 }
01963b95 183 }
a81bf9c3 184 asort($choices);
185 $choices = array_flip($choices);
186 $choices = array_merge(array('' => get_string('sitedefault').' ('.get_string('enrolname', "enrol_$CFG->enrol").')'), $choices);
187 $mform->addElement('select', 'enrol', get_string('enrolmentplugins'), $choices);
188 $mform->setHelpButton('enrol', array('courseenrolmentplugins', get_string('enrolmentplugins')), true);
01963b95 189
a81bf9c3 190 $radio = array();
191 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('no'), 0);
192 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('yes'), 1);
193 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('enroldate'), 2);
194 $mform->addGroup($radio, 'enrollable', get_string('enrollable'), ' ', false);
195 $mform->setHelpButton('enrollable', array('courseenrollable', get_string('enrollable')), true);
196 $mform->setDefault('enrollable', 1);
197
198 $enroldatestartgrp = array();
199 $enroldatestartgrp[] = &MoodleQuickForm::createElement('date_selector', 'enrolstartdate');
200 $enroldatestartgrp[] = &MoodleQuickForm::createElement('checkbox', 'enrolstartdisabled', null, get_string('disable'));
2dbd6409 201 $mform->addGroup($enroldatestartgrp, 'enrolstartdategrp', get_string('enrolstartdate'), ' ', false);
a81bf9c3 202 $mform->setDefault('enrolstartdate', 0);
203 $mform->setDefault('enrolstartdisabled', 1);
2dbd6409 204 $mform->disabledIf('enrolstartdategrp', 'enrolstartdisabled', 'checked');
a81bf9c3 205
206 $enroldateendgrp = array();
207 $enroldateendgrp[] = &MoodleQuickForm::createElement('date_selector', 'enrolenddate');
208 $enroldateendgrp[] = &MoodleQuickForm::createElement('checkbox', 'enrolenddisabled', null, get_string('disable'));
209 $mform->addGroup($enroldateendgrp, 'enroldateendgrp', get_string('enrolenddate'), ' ', false);
210 $mform->setDefault('enrolenddate', 0);
211 $mform->setDefault('enrolenddisabled', 1);
2dbd6409 212 $mform->disabledIf('enroldateendgrp', 'enrolenddisabled', 'checked');
a81bf9c3 213
214 $periodmenu=array();
215 $periodmenu[0] = get_string('unlimited');
216 for ($i=1; $i<=365; $i++) {
217 $seconds = $i * 86400;
218 $periodmenu[$seconds] = get_string('numdays', '', $i);
219 }
220 $mform->addElement('select', 'enrolperiod', get_string('enrolperiod'), $periodmenu);
221 $mform->setDefault('enrolperiod', 0);
222
223//--------------------------------------------------------------------------------
224 $mform->addElement('header','expirynotifyhdr', get_string('expirynotify'));
225
226 $choices = array();
227 $choices['0'] = get_string('no');
228 $choices['1'] = get_string('yes');
ec3dac98 229 $mform->addElement('select', 'expirynotify', get_string('notify'), $choices);
a81bf9c3 230 $mform->setHelpButton('expirynotify', array('expirynotify', get_string('expirynotify')), true);
231 $mform->setDefault('expirynotify', 0);
232
233 $mform->addElement('select', 'notifystudents', get_string('expirynotifystudents'), $choices);
234 $mform->setHelpButton('notifystudents', array('expirynotifystudents', get_string('expirynotifystudents')), true);
235 $mform->setDefault('notifystudents', 0);
236
237 $thresholdmenu=array();
238 for ($i=1; $i<=30; $i++) {
239 $seconds = $i * 86400;
240 $thresholdmenu[$seconds] = get_string('numdays', '', $i);
241 }
242 $mform->addElement('select', 'expirythreshold', get_string('expirythreshold'), $thresholdmenu);
243 $mform->setHelpButton('expirythreshold', array('expirythreshold', get_string('expirythreshold')), true);
244 $mform->setDefault('expirythreshold', 10 * 86400);
245
246//--------------------------------------------------------------------------------
ec3dac98 247 $mform->addElement('header','', get_string('groups', 'group'));
a81bf9c3 248
249 $choices = array();
ec3dac98 250 $choices[NOGROUPS] = get_string('no');
251 $choices[SEPARATEGROUPS] = get_string('separate');
252 $choices[VISIBLEGROUPS] = get_string('visible');
a81bf9c3 253 $mform->addElement('select', 'groupmode', get_string('groupmode'), $choices);
254 $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')), true);
255 $mform->setDefault('groupmode', 0);
256
257 $choices = array();
258 $choices['0'] = get_string('no');
259 $choices['1'] = get_string('yes');
260 $mform->addElement('select', 'groupmodeforce', get_string('force'), $choices);
261 $mform->setHelpButton('groupmodeforce', array('groupmodeforce', get_string('groupmodeforce')), true);
262 $mform->setDefault('groupmodeforce', 0);
263
264//--------------------------------------------------------------------------------
265 $mform->addElement('header','', get_string('availability'));
266
267 $choices = array();
268 $choices['0'] = get_string('courseavailablenot');
269 $choices['1'] = get_string('courseavailable');
270 $mform->addElement('select', 'visible', get_string('availability'), $choices);
271 $mform->setHelpButton('visible', array('courseavailability', get_string('availability')), true);
272 $mform->setDefault('visible', 1);
273
274 $mform->addElement('text', 'password', get_string('enrolmentkey'), 'size="25"');
275 $mform->setHelpButton('password', array('enrolmentkey', get_string('enrolmentkey')), true);
276 $mform->setDefault('password', '');
277 $mform->setType('password', PARAM_RAW);
278
279 $choices = array();
280 $choices['0'] = get_string('guestsno');
281 $choices['1'] = get_string('guestsyes');
282 $choices['2'] = get_string('guestskey');
283 $mform->addElement('select', 'guest', get_string('opentoguests'), $choices);
284 $mform->setHelpButton('guest', array('guestaccess', get_string('opentoguests')), true);
285 $mform->setDefault('guest', 0);
286
287 if (!empty($course) && method_exists(enrolment_factory::factory($course->enrol), 'print_entry') && $course->enrol != 'manual') {
288 $costgroup=array();
289 $currencies = get_list_of_currencies();
290 $costgroup[]= &MoodleQuickForm::createElement('text','cost', '', 'maxlength="6" size="6"');
291 $costgroup[]= &MoodleQuickForm::createElement('select', 'currency', '', $currencies);
292 $mform->addGroup($costgroup, 'costgrp', get_string('cost'), '&nbsp;', false);
5bc97c98 293 //defining a rule for a form element within a group :
294 $costgrprules=array();
295 //set the message to null to tell Moodle to use a default message
296 //available for most rules, fetched from language pack (err_{rulename}).
297 $costgrprules['cost'][]=array(null, 'numeric', null, 'client');
298 $mform->addGroupRule('costgrp',$costgrprules);
a81bf9c3 299 $mform->setHelpButton('costgrp', array('cost', get_string('cost')), true);
300 $mform->setDefault('cost', '');
301 $mform->setDefault('currency', empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency);
e995c46e 302
a81bf9c3 303 }
304
305//--------------------------------------------------------------------------------
306 $mform->addElement('header','', get_string('language'));
307
308 $languages=array();
309 $languages[''] = get_string('forceno');
310 $languages += get_list_of_languages();
311 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
312
313//--------------------------------------------------------------------------------
314 if (has_capability('moodle/site:config', $systemcontext) && ((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
315 $mform->addElement('header', '', get_string('restrictmodules'));
316
317 $options = array();
318 $options['0'] = get_string('no');
319 $options['1'] = get_string('yes');
2dbd6409 320 $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
a81bf9c3 321 $mods = array(0=>get_string('allownone'));
322 $mods += get_records_menu('modules', '','','','id, name');
a81bf9c3 323
324
2dbd6409 325 $mform->addElement('select', 'allowedmods', get_string('to'), $mods,
326 array('multiple'=>'multiple', 'size'=>'10'));
327 $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
328 } else {
a81bf9c3 329 $mform->addElement('hidden', 'restrictmodules', null);
330 }
331 if ($CFG->restrictmodulesfor == 'all') {
332 $mform->setDefault('allowedmods', explode(',',$CFG->defaultallowedmodules));
333 if (!empty($CFG->restrictbydefault)) {
334 $mform->setDefault('restrictmodules', 1);
335 }
336 }
337 $mform->setType('restrictmodules', PARAM_INT);
338
0150c561 339/// customizable role names in this course
340//--------------------------------------------------------------------------------
341 $mform->addElement('header','', get_string('roles'));
342
343 if ($roles = get_records('role')) {
344 foreach ($roles as $role) {
345 $mform->addElement('text', 'role_'.$role->id, $role->name);
346 if ($coursecontext) {
347 if ($rolename = get_record('role_names', 'roleid', $role->id, 'contextid', $coursecontext->id)) {
348 $mform->setDefault('role_'.$role->id, $rolename->text);
349 }
350 }
351 }
352 }
353
a81bf9c3 354//--------------------------------------------------------------------------------
a23f0aaf 355 $this->add_action_buttons();
a81bf9c3 356//--------------------------------------------------------------------------------
357 $mform->addElement('hidden', 'id', null);
358 $mform->setType('id', PARAM_INT);
359
360 // fill in default teacher and student names to keep backwards compatibility for a while
361 $mform->addElement('hidden', 'teacher', get_string('defaultcourseteacher'));
362 $mform->addElement('hidden', 'teachers', get_string('defaultcourseteachers'));
363 $mform->addElement('hidden', 'student', get_string('defaultcoursestudent'));
364 $mform->addElement('hidden', 'students', get_string('defaultcoursestudents'));
c08ac016 365 }
2dbd6409 366
a81bf9c3 367
368/// perform some extra moodle validation
369 function validation($data){
01963b95 370 $errors= array();
a81bf9c3 371 if ($foundcourses = get_records('course', 'shortname', $data['shortname'])) {
372 if (!empty($data['id'])) {
373 unset($foundcourses[$data['id']]);
01963b95 374 }
375 if (!empty($foundcourses)) {
376 foreach ($foundcourses as $foundcourse) {
377 $foundcoursenames[] = $foundcourse->fullname;
378 }
a81bf9c3 379 $foundcoursenamestring = implode(',', $foundcoursenames);
380 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
01963b95 381 }
01963b95 382 }
a81bf9c3 383
384 if (empty($data['enrolenddisabled'])){
385 if ($data['enrolenddate'] <= $data['enrolstartdate']){
386 $errors['enroldateendgrp'] = get_string('enrolenddaterror');
01963b95 387 }
388 }
a81bf9c3 389
390 if (0 == count($errors)){
01963b95 391 return true;
a81bf9c3 392 } else {
01963b95 393 return $errors;
394 }
395 }
a81bf9c3 396}
397?>