MDL-13613 added better detection for outdated course modinfo; merged from MOODLE_19_S...
[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
4f6bd3b9 72 $mform->addElement('text','shortname', get_string('shortname'),'maxlength="100" size="20"');
a81bf9c3 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
e15d4dd0 155//--------------------------------------------------------------------------------
156 $mform->addElement('header','enrolhdr', get_string('enrolments'));
157
158 $choices = array();
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;
165 }
166 }
167 asort($choices);
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);
172
173
a81bf9c3 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));
179 }
180 }
181 $choices = array();
182 if ($sitedefaultrole = get_record('role', 'id', $CFG->defaultcourseroleid)) {
183 $choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name.')';
01963b95 184 } else {
a81bf9c3 185 $choices[0] = get_string('sitedefault');
01963b95 186 }
a81bf9c3 187 $choices = $choices + $roles;
f20d7c41 188
189 // fix for MDL-9197
190 foreach ($choices as $choiceid => $choice) {
191 $choices[$choiceid] = format_string($choice);
192 }
193
a81bf9c3 194 $mform->addElement('select', 'defaultrole', get_string('defaultrole', 'role'), $choices);
195 $mform->setDefault('defaultrole', 0);
196
01963b95 197
a81bf9c3 198 $radio = array();
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);
6be58b1c 203 $mform->setHelpButton('enrollable', array('courseenrollable2', get_string('enrollable')), true);
a81bf9c3 204 $mform->setDefault('enrollable', 1);
205
206 $enroldatestartgrp = array();
207 $enroldatestartgrp[] = &MoodleQuickForm::createElement('date_selector', 'enrolstartdate');
208 $enroldatestartgrp[] = &MoodleQuickForm::createElement('checkbox', 'enrolstartdisabled', null, get_string('disable'));
2dbd6409 209 $mform->addGroup($enroldatestartgrp, 'enrolstartdategrp', get_string('enrolstartdate'), ' ', false);
a81bf9c3 210 $mform->setDefault('enrolstartdate', 0);
211 $mform->setDefault('enrolstartdisabled', 1);
2dbd6409 212 $mform->disabledIf('enrolstartdategrp', 'enrolstartdisabled', 'checked');
a81bf9c3 213
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);
2dbd6409 220 $mform->disabledIf('enroldateendgrp', 'enrolenddisabled', 'checked');
a81bf9c3 221
222 $periodmenu=array();
223 $periodmenu[0] = get_string('unlimited');
224 for ($i=1; $i<=365; $i++) {
225 $seconds = $i * 86400;
226 $periodmenu[$seconds] = get_string('numdays', '', $i);
227 }
228 $mform->addElement('select', 'enrolperiod', get_string('enrolperiod'), $periodmenu);
229 $mform->setDefault('enrolperiod', 0);
230
e15d4dd0 231
a81bf9c3 232//--------------------------------------------------------------------------------
233 $mform->addElement('header','expirynotifyhdr', get_string('expirynotify'));
234
235 $choices = array();
236 $choices['0'] = get_string('no');
237 $choices['1'] = get_string('yes');
ec3dac98 238 $mform->addElement('select', 'expirynotify', get_string('notify'), $choices);
a81bf9c3 239 $mform->setHelpButton('expirynotify', array('expirynotify', get_string('expirynotify')), true);
240 $mform->setDefault('expirynotify', 0);
241
242 $mform->addElement('select', 'notifystudents', get_string('expirynotifystudents'), $choices);
243 $mform->setHelpButton('notifystudents', array('expirynotifystudents', get_string('expirynotifystudents')), true);
244 $mform->setDefault('notifystudents', 0);
245
246 $thresholdmenu=array();
247 for ($i=1; $i<=30; $i++) {
248 $seconds = $i * 86400;
249 $thresholdmenu[$seconds] = get_string('numdays', '', $i);
250 }
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);
254
255//--------------------------------------------------------------------------------
ec3dac98 256 $mform->addElement('header','', get_string('groups', 'group'));
a81bf9c3 257
258 $choices = array();
467cebb0 259 $choices[NOGROUPS] = get_string('groupsnone', 'group');
260 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
261 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
a81bf9c3 262 $mform->addElement('select', 'groupmode', get_string('groupmode'), $choices);
263 $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')), true);
264 $mform->setDefault('groupmode', 0);
265
266 $choices = array();
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);
272
24f41672 273 if (!empty($CFG->enablegroupings)) {
274 //default groupings selector
275 $options = array();
276 $options[0] = get_string('none');
277 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
278 }
279
a81bf9c3 280//--------------------------------------------------------------------------------
281 $mform->addElement('header','', get_string('availability'));
282
283 $choices = array();
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);
289
1898ccac 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);
a81bf9c3 294
295 $choices = array();
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);
a937b164 302
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)) {
82d3334a 306 $enrol_object = $course;
a937b164 307 }
308 if (method_exists(enrolment_factory::factory($enrol_object->enrol), 'print_entry') && $enrol_object->enrol != 'manual'){
a81bf9c3 309 $costgroup=array();
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'), '&nbsp;', false);
5bc97c98 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);
a81bf9c3 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);
e995c46e 323
a81bf9c3 324 }
325
326//--------------------------------------------------------------------------------
327 $mform->addElement('header','', get_string('language'));
328
329 $languages=array();
330 $languages[''] = get_string('forceno');
331 $languages += get_list_of_languages();
332 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
333
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'));
337
338 $options = array();
339 $options['0'] = get_string('no');
340 $options['1'] = get_string('yes');
2dbd6409 341 $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
a81bf9c3 342 $mods = array(0=>get_string('allownone'));
343 $mods += get_records_menu('modules', '','','','id, name');
a81bf9c3 344
345
2dbd6409 346 $mform->addElement('select', 'allowedmods', get_string('to'), $mods,
347 array('multiple'=>'multiple', 'size'=>'10'));
348 $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
349 } else {
a81bf9c3 350 $mform->addElement('hidden', 'restrictmodules', null);
351 }
352 if ($CFG->restrictmodulesfor == 'all') {
353 $mform->setDefault('allowedmods', explode(',',$CFG->defaultallowedmodules));
354 if (!empty($CFG->restrictbydefault)) {
355 $mform->setDefault('restrictmodules', 1);
356 }
357 }
358 $mform->setType('restrictmodules', PARAM_INT);
359
0150c561 360/// customizable role names in this course
361//--------------------------------------------------------------------------------
362 $mform->addElement('header','', get_string('roles'));
363
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);
370 }
371 }
372 }
373 }
374
a81bf9c3 375//--------------------------------------------------------------------------------
a23f0aaf 376 $this->add_action_buttons();
a81bf9c3 377//--------------------------------------------------------------------------------
378 $mform->addElement('hidden', 'id', null);
379 $mform->setType('id', PARAM_INT);
380
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'));
c08ac016 386 }
2dbd6409 387
24f41672 388 function definition_after_data() {
389 global $CFG;
390
391 $mform =& $this->_form;
392
393 // add availabe groupings
394 if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
395 $options = array();
396 if ($groupings = get_records('groupings', 'courseid', $courseid)) {
397 foreach ($groupings as $grouping) {
398 $options[$grouping->id] = format_string($grouping->name);
399 }
400 }
401 $gr_el =& $mform->getElement('defaultgroupingid');
402 $gr_el->load($options);
403 }
404 }
405
a81bf9c3 406
407/// perform some extra moodle validation
a78890d5 408 function validation($data, $files) {
409 $errors = parent::validation($data, $files);
a81bf9c3 410 if ($foundcourses = get_records('course', 'shortname', $data['shortname'])) {
411 if (!empty($data['id'])) {
412 unset($foundcourses[$data['id']]);
01963b95 413 }
414 if (!empty($foundcourses)) {
415 foreach ($foundcourses as $foundcourse) {
416 $foundcoursenames[] = $foundcourse->fullname;
417 }
a81bf9c3 418 $foundcoursenamestring = implode(',', $foundcoursenames);
419 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
01963b95 420 }
01963b95 421 }
a81bf9c3 422
423 if (empty($data['enrolenddisabled'])){
424 if ($data['enrolenddate'] <= $data['enrolstartdate']){
425 $errors['enroldateendgrp'] = get_string('enrolenddaterror');
01963b95 426 }
427 }
a81bf9c3 428
a78890d5 429 return $errors;
01963b95 430 }
a81bf9c3 431}
a937b164 432?>