removed shorttag
[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() {
f33e1ed4 8 global $USER, $CFG, $DB;
a81bf9c3 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) {
c3df0901 28 if ($DB->count_records('course_meta', array('parent_course'=>$course->id)) > 0) {
a81bf9c3 29 $disable_meta = get_string('metaalreadyhascourses');
30 }
31
32 } else {
6682b29c 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;
43 }
44 }
a81bf9c3 45 }
6682b29c 46 unset($managers);
47 unset($users);
48 unset($enrolroles);
461f5127 49 }
50 } else {
a81bf9c3 51 $coursecontext = null;
52 $context = $categorycontext;
53 }
54
55/// form definition with new course defaults
56//--------------------------------------------------------------------------------
2b63df96 57 $mform->addElement('header','general', get_string('general', 'form'));
a81bf9c3 58
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]);
67 }
461f5127 68 }
a81bf9c3 69 $mform->addElement('select', 'category', get_string('category'), $displaylist);
70 } else {
71 $mform->addElement('hidden', 'category', null);
461f5127 72 }
a81bf9c3 73 $mform->setHelpButton('category', array('coursecategory', get_string('category')));
74 $mform->setDefault('category', $category->id);
75 $mform->setType('category', PARAM_INT);
461f5127 76
d6cc2341 77 $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
78 $mform->setHelpButton('fullname', array('coursefullname', get_string('fullnamecourse')), true);
a81bf9c3 79 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
80 $mform->setType('fullname', PARAM_MULTILANG);
461f5127 81
d6cc2341 82 $mform->addElement('text','shortname', get_string('shortnamecourse'),'maxlength="100" size="20"');
83 $mform->setHelpButton('shortname', array('courseshortname', get_string('shortnamecourse')), true);
a81bf9c3 84 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
85 $mform->setType('shortname', PARAM_MULTILANG);
01963b95 86
c3df0901 87 $fullname = get_string('defaultcoursefullname');
88 $shortname = get_string('defaultcourseshortname');
89 while ($DB->record_exists('course', array('fullname'=>$fullname))
90 or $DB->record_exists('course', array('fullname'=>$fullname))) {
91 $fullname++;
92 $shortname++;
93 }
94 $mform->setDefault('fullname', $fullname);
95 $mform->setDefault('shortname', $shortname);
96
97
a81bf9c3 98 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
99 $mform->setHelpButton('idnumber', array('courseidnumber', get_string('idnumbercourse')), true);
100 $mform->setType('idnumber', PARAM_RAW);
01963b95 101
a81bf9c3 102 $mform->addElement('htmleditor','summary', get_string('summary'), array('rows'=> '10', 'cols'=>'65'));
103 $mform->setHelpButton('summary', array('text', get_string('helptext')), true);
a81bf9c3 104 $mform->setType('summary', PARAM_RAW);
105
106 $courseformats = get_list_of_plugins('course/format');
107 $formcourseformats = array();
108 foreach ($courseformats as $courseformat) {
ae628043 109 $formcourseformats["$courseformat"] = get_string("format$courseformat","format_$courseformat");
110 if($formcourseformats["$courseformat"]=="[[format$courseformat]]") {
111 $formcourseformats["$courseformat"] = get_string("format$courseformat");
a23f0aaf 112 }
a81bf9c3 113 }
114 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
115 $mform->setHelpButton('format', array('courseformats', get_string('courseformats')), true);
116 $mform->setDefault('format', 'weeks');
117
118 for ($i=1; $i<=52; $i++) {
119 $sectionmenu[$i] = "$i";
120 }
121 $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
122 $mform->setDefault('numsections', 10);
123
124 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
125 $mform->setHelpButton('startdate', array('coursestartdate', get_string('startdate')), true);
126 $mform->setDefault('startdate', time() + 3600 * 24);
127
128 $choices = array();
129 $choices['0'] = get_string('hiddensectionscollapsed');
130 $choices['1'] = get_string('hiddensectionsinvisible');
131 $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
132 $mform->setHelpButton('hiddensections', array('coursehiddensections', get_string('hiddensections')), true);
133 $mform->setDefault('hiddensections', 0);
134
65877218 135 $options = range(0, 10);
a81bf9c3 136 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
137 $mform->setHelpButton('newsitems', array('coursenewsitems', get_string('newsitemsnumber')), true);
138 $mform->setDefault('newsitems', 5);
139
65877218 140 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
a81bf9c3 141 $mform->setHelpButton('showgrades', array('coursegrades', get_string('grades')), true);
142 $mform->setDefault('showgrades', 1);
143
65877218 144 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
a81bf9c3 145 $mform->setHelpButton('showreports', array('coursereports', get_string('activityreport')), true);
146 $mform->setDefault('showreports', 0);
147
148 $choices = get_max_upload_sizes($CFG->maxbytes);
149 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
150 $mform->setHelpButton('maxbytes', array('courseuploadsize', get_string('maximumupload')), true);
151
152 if (!empty($CFG->allowcoursethemes)) {
153 $themes=array();
154 $themes[''] = get_string('forceno');
155 $themes += get_list_of_themes();
156 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
157 }
461f5127 158
01963b95 159 $meta=array();
160 $meta[0] = get_string('no');
161 $meta[1] = get_string('yes');
a81bf9c3 162 if ($disable_meta === false) {
163 $mform->addElement('select', 'metacourse', get_string('managemeta'), $meta);
3d397ee0 164 $mform->setHelpButton('metacourse', array('metacourse', get_string('metacourse')), true);
165 $mform->setDefault('metacourse', 0);
a81bf9c3 166 } else {
3d397ee0 167 // no metacourse element - we do not want to change it anyway!
168 $mform->addElement('static', 'nometacourse', get_string('managemeta'),
a81bf9c3 169 ((empty($course->metacourse)) ? $meta[0] : $meta[1]) . " - $disable_meta ");
3d397ee0 170 $mform->setHelpButton('nometacourse', array('metacourse', get_string('metacourse')), true);
a81bf9c3 171 }
01963b95 172
e15d4dd0 173//--------------------------------------------------------------------------------
174 $mform->addElement('header','enrolhdr', get_string('enrolments'));
175
176 $choices = array();
177 $modules = explode(',', $CFG->enrol_plugins_enabled);
178 foreach ($modules as $module) {
179 $name = get_string('enrolname', "enrol_$module");
180 $plugin = enrolment_factory::factory($module);
181 if (method_exists($plugin, 'print_entry')) {
182 $choices[$name] = $module;
183 }
184 }
185 asort($choices);
186 $choices = array_flip($choices);
187 $choices = array_merge(array('' => get_string('sitedefault').' ('.get_string('enrolname', "enrol_$CFG->enrol").')'), $choices);
188 $mform->addElement('select', 'enrol', get_string('enrolmentplugins'), $choices);
189 $mform->setHelpButton('enrol', array('courseenrolmentplugins', get_string('enrolmentplugins')), true);
190
191
a81bf9c3 192 $roles = get_assignable_roles($context);
193 if (!empty($course)) {
194 // add current default role, so that it is selectable even when user can not assign it
c3df0901 195 if ($current_role = $DB->get_record('role', array('id'=>$course->defaultrole))) {
a81bf9c3 196 $roles[$current_role->id] = strip_tags(format_string($current_role->name, true));
197 }
198 }
199 $choices = array();
c3df0901 200 if ($sitedefaultrole = $DB->get_record('role', array('id'=>$CFG->defaultcourseroleid))) {
a81bf9c3 201 $choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name.')';
01963b95 202 } else {
a81bf9c3 203 $choices[0] = get_string('sitedefault');
01963b95 204 }
a81bf9c3 205 $choices = $choices + $roles;
f20d7c41 206
207 // fix for MDL-9197
208 foreach ($choices as $choiceid => $choice) {
209 $choices[$choiceid] = format_string($choice);
210 }
211
a81bf9c3 212 $mform->addElement('select', 'defaultrole', get_string('defaultrole', 'role'), $choices);
213 $mform->setDefault('defaultrole', 0);
214
01963b95 215
a81bf9c3 216 $radio = array();
217 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('no'), 0);
218 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('yes'), 1);
219 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('enroldate'), 2);
220 $mform->addGroup($radio, 'enrollable', get_string('enrollable'), ' ', false);
6be58b1c 221 $mform->setHelpButton('enrollable', array('courseenrollable2', get_string('enrollable')), true);
a81bf9c3 222 $mform->setDefault('enrollable', 1);
223
224 $enroldatestartgrp = array();
225 $enroldatestartgrp[] = &MoodleQuickForm::createElement('date_selector', 'enrolstartdate');
226 $enroldatestartgrp[] = &MoodleQuickForm::createElement('checkbox', 'enrolstartdisabled', null, get_string('disable'));
2dbd6409 227 $mform->addGroup($enroldatestartgrp, 'enrolstartdategrp', get_string('enrolstartdate'), ' ', false);
a81bf9c3 228 $mform->setDefault('enrolstartdate', 0);
229 $mform->setDefault('enrolstartdisabled', 1);
2dbd6409 230 $mform->disabledIf('enrolstartdategrp', 'enrolstartdisabled', 'checked');
a81bf9c3 231
232 $enroldateendgrp = array();
233 $enroldateendgrp[] = &MoodleQuickForm::createElement('date_selector', 'enrolenddate');
234 $enroldateendgrp[] = &MoodleQuickForm::createElement('checkbox', 'enrolenddisabled', null, get_string('disable'));
235 $mform->addGroup($enroldateendgrp, 'enroldateendgrp', get_string('enrolenddate'), ' ', false);
236 $mform->setDefault('enrolenddate', 0);
237 $mform->setDefault('enrolenddisabled', 1);
2dbd6409 238 $mform->disabledIf('enroldateendgrp', 'enrolenddisabled', 'checked');
a81bf9c3 239
240 $periodmenu=array();
241 $periodmenu[0] = get_string('unlimited');
242 for ($i=1; $i<=365; $i++) {
243 $seconds = $i * 86400;
244 $periodmenu[$seconds] = get_string('numdays', '', $i);
245 }
246 $mform->addElement('select', 'enrolperiod', get_string('enrolperiod'), $periodmenu);
247 $mform->setDefault('enrolperiod', 0);
248
e15d4dd0 249
a81bf9c3 250//--------------------------------------------------------------------------------
251 $mform->addElement('header','expirynotifyhdr', get_string('expirynotify'));
252
253 $choices = array();
254 $choices['0'] = get_string('no');
255 $choices['1'] = get_string('yes');
ec3dac98 256 $mform->addElement('select', 'expirynotify', get_string('notify'), $choices);
a81bf9c3 257 $mform->setHelpButton('expirynotify', array('expirynotify', get_string('expirynotify')), true);
258 $mform->setDefault('expirynotify', 0);
259
260 $mform->addElement('select', 'notifystudents', get_string('expirynotifystudents'), $choices);
261 $mform->setHelpButton('notifystudents', array('expirynotifystudents', get_string('expirynotifystudents')), true);
262 $mform->setDefault('notifystudents', 0);
263
264 $thresholdmenu=array();
265 for ($i=1; $i<=30; $i++) {
266 $seconds = $i * 86400;
267 $thresholdmenu[$seconds] = get_string('numdays', '', $i);
268 }
269 $mform->addElement('select', 'expirythreshold', get_string('expirythreshold'), $thresholdmenu);
270 $mform->setHelpButton('expirythreshold', array('expirythreshold', get_string('expirythreshold')), true);
271 $mform->setDefault('expirythreshold', 10 * 86400);
272
273//--------------------------------------------------------------------------------
ec3dac98 274 $mform->addElement('header','', get_string('groups', 'group'));
a81bf9c3 275
276 $choices = array();
467cebb0 277 $choices[NOGROUPS] = get_string('groupsnone', 'group');
278 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
279 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
a81bf9c3 280 $mform->addElement('select', 'groupmode', get_string('groupmode'), $choices);
281 $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')), true);
282 $mform->setDefault('groupmode', 0);
283
284 $choices = array();
285 $choices['0'] = get_string('no');
286 $choices['1'] = get_string('yes');
287 $mform->addElement('select', 'groupmodeforce', get_string('force'), $choices);
288 $mform->setHelpButton('groupmodeforce', array('groupmodeforce', get_string('groupmodeforce')), true);
289 $mform->setDefault('groupmodeforce', 0);
290
24f41672 291 if (!empty($CFG->enablegroupings)) {
292 //default groupings selector
293 $options = array();
294 $options[0] = get_string('none');
295 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
296 }
297
a81bf9c3 298//--------------------------------------------------------------------------------
299 $mform->addElement('header','', get_string('availability'));
300
301 $choices = array();
302 $choices['0'] = get_string('courseavailablenot');
303 $choices['1'] = get_string('courseavailable');
304 $mform->addElement('select', 'visible', get_string('availability'), $choices);
305 $mform->setHelpButton('visible', array('courseavailability', get_string('availability')), true);
306 $mform->setDefault('visible', 1);
307
1898ccac 308 $mform->addElement('passwordunmask', 'enrolpassword', get_string('enrolmentkey'), 'size="25"');
309 $mform->setHelpButton('enrolpassword', array('enrolmentkey', get_string('enrolmentkey')), true);
310 $mform->setDefault('enrolpassword', '');
311 $mform->setType('enrolpassword', PARAM_RAW);
a81bf9c3 312
313 $choices = array();
314 $choices['0'] = get_string('guestsno');
315 $choices['1'] = get_string('guestsyes');
316 $choices['2'] = get_string('guestskey');
317 $mform->addElement('select', 'guest', get_string('opentoguests'), $choices);
318 $mform->setHelpButton('guest', array('guestaccess', get_string('opentoguests')), true);
319 $mform->setDefault('guest', 0);
a937b164 320
321 // 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
322 $enrol_object = $CFG;
323 if (!empty($course)) {
82d3334a 324 $enrol_object = $course;
a937b164 325 }
c5340330 326 // If the print_entry method exists and the course enrol method isn't manual (both set or inherited from site), show cost
327 if (method_exists(enrolment_factory::factory($enrol_object->enrol), 'print_entry') && !($enrol_object->enrol == 'manual' || (empty($enrol_object->enrol) && $CFG->enrol == 'manual'))) {
a81bf9c3 328 $costgroup=array();
329 $currencies = get_list_of_currencies();
330 $costgroup[]= &MoodleQuickForm::createElement('text','cost', '', 'maxlength="6" size="6"');
331 $costgroup[]= &MoodleQuickForm::createElement('select', 'currency', '', $currencies);
332 $mform->addGroup($costgroup, 'costgrp', get_string('cost'), '&nbsp;', false);
5bc97c98 333 //defining a rule for a form element within a group :
334 $costgrprules=array();
335 //set the message to null to tell Moodle to use a default message
336 //available for most rules, fetched from language pack (err_{rulename}).
337 $costgrprules['cost'][]=array(null, 'numeric', null, 'client');
338 $mform->addGroupRule('costgrp',$costgrprules);
a81bf9c3 339 $mform->setHelpButton('costgrp', array('cost', get_string('cost')), true);
340 $mform->setDefault('cost', '');
341 $mform->setDefault('currency', empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency);
e995c46e 342
a81bf9c3 343 }
344
345//--------------------------------------------------------------------------------
346 $mform->addElement('header','', get_string('language'));
347
348 $languages=array();
349 $languages[''] = get_string('forceno');
350 $languages += get_list_of_languages();
351 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
352
353//--------------------------------------------------------------------------------
354 if (has_capability('moodle/site:config', $systemcontext) && ((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
355 $mform->addElement('header', '', get_string('restrictmodules'));
356
357 $options = array();
358 $options['0'] = get_string('no');
359 $options['1'] = get_string('yes');
2dbd6409 360 $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
a81bf9c3 361 $mods = array(0=>get_string('allownone'));
3b112723 362 $mods += $DB->get_records_menu('modules', array(), 'name', 'id, name');
a81bf9c3 363
364
2dbd6409 365 $mform->addElement('select', 'allowedmods', get_string('to'), $mods,
366 array('multiple'=>'multiple', 'size'=>'10'));
367 $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
368 } else {
a81bf9c3 369 $mform->addElement('hidden', 'restrictmodules', null);
370 }
371 if ($CFG->restrictmodulesfor == 'all') {
372 $mform->setDefault('allowedmods', explode(',',$CFG->defaultallowedmodules));
373 if (!empty($CFG->restrictbydefault)) {
374 $mform->setDefault('restrictmodules', 1);
375 }
376 }
377 $mform->setType('restrictmodules', PARAM_INT);
378
0150c561 379/// customizable role names in this course
380//--------------------------------------------------------------------------------
7d2e9a52 381 $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
382 $mform->setHelpButton('rolerenaming', array('rolerenaming', get_string('rolerenaming')), true);
0150c561 383
c3df0901 384 if ($roles = $DB->get_records('role')) {
0150c561 385 foreach ($roles as $role) {
386 $mform->addElement('text', 'role_'.$role->id, $role->name);
387 if ($coursecontext) {
c3df0901 388 if ($rolename = $DB->get_record('role_names', array('roleid'=>$role->id, 'contextid'=>$coursecontext->id))) {
87486420 389 $mform->setDefault('role_'.$role->id, $rolename->name);
0150c561 390 }
391 }
392 }
393 }
394
a81bf9c3 395//--------------------------------------------------------------------------------
a23f0aaf 396 $this->add_action_buttons();
a81bf9c3 397//--------------------------------------------------------------------------------
398 $mform->addElement('hidden', 'id', null);
399 $mform->setType('id', PARAM_INT);
400
401 // fill in default teacher and student names to keep backwards compatibility for a while
402 $mform->addElement('hidden', 'teacher', get_string('defaultcourseteacher'));
403 $mform->addElement('hidden', 'teachers', get_string('defaultcourseteachers'));
404 $mform->addElement('hidden', 'student', get_string('defaultcoursestudent'));
405 $mform->addElement('hidden', 'students', get_string('defaultcoursestudents'));
c08ac016 406 }
2dbd6409 407
24f41672 408 function definition_after_data() {
c3df0901 409 global $DB;
24f41672 410
411 $mform =& $this->_form;
412
413 // add availabe groupings
414 if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
415 $options = array();
c3df0901 416 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
24f41672 417 foreach ($groupings as $grouping) {
418 $options[$grouping->id] = format_string($grouping->name);
419 }
420 }
421 $gr_el =& $mform->getElement('defaultgroupingid');
422 $gr_el->load($options);
423 }
424 }
425
a81bf9c3 426
427/// perform some extra moodle validation
a78890d5 428 function validation($data, $files) {
c3df0901 429 global $DB;
430
a78890d5 431 $errors = parent::validation($data, $files);
c3df0901 432 if ($foundcourses = $DB->get_records('course', array('shortname'=>$data['shortname']))) {
a81bf9c3 433 if (!empty($data['id'])) {
434 unset($foundcourses[$data['id']]);
01963b95 435 }
436 if (!empty($foundcourses)) {
437 foreach ($foundcourses as $foundcourse) {
438 $foundcoursenames[] = $foundcourse->fullname;
439 }
a81bf9c3 440 $foundcoursenamestring = implode(',', $foundcoursenames);
441 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
01963b95 442 }
01963b95 443 }
a81bf9c3 444
445 if (empty($data['enrolenddisabled'])){
446 if ($data['enrolenddate'] <= $data['enrolstartdate']){
447 $errors['enroldateendgrp'] = get_string('enrolenddaterror');
01963b95 448 }
449 }
a81bf9c3 450
a78890d5 451 return $errors;
01963b95 452 }
a81bf9c3 453}
a937b164 454?>