timeline: if a section is set to hidden and the user is not capable of editing a...
[moodle-blog-course-format.git] / course / edit_form.php
blob72418e15d4937ca59f00c5738dc94d84487faae8
1 <?php //$Id$
3 require_once($CFG->libdir.'/formslib.php');
5 class course_edit_form extends moodleform {
7 function definition() {
8 global $USER, $CFG;
10 $courseconfig = get_config('moodlecourse');
11 $mform =& $this->_form;
13 $course = $this->_customdata['course'];
14 $category = $this->_customdata['category'];
16 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
17 $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
19 $disable_meta = false; // basic meta course state protection; server-side security checks not needed
21 if (!empty($course)) {
22 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
23 $context = $coursecontext;
25 if (course_in_meta($course)) {
26 $disable_meta = get_string('metaalreadyinmeta');
28 } else if ($course->metacourse) {
29 if (count_records('course_meta', 'parent_course', $course->id) > 0) {
30 $disable_meta = get_string('metaalreadyhascourses');
33 } else {
34 // if users already enrolled directly into coures, do not allow switching to meta,
35 // users with metacourse manage permission are exception
36 // please note that we do not need exact results - anything unexpected here prevents metacourse
37 $managers = get_users_by_capability($coursecontext, 'moodle/course:managemetacourse', 'u.id');
38 $enrolroles = get_roles_with_capability('moodle/course:view', CAP_ALLOW, $coursecontext);
39 if ($users = get_role_users(array_keys($enrolroles), $coursecontext, false, 'u.id', 'u.id ASC')) {
40 foreach($users as $user) {
41 if (!isset($managers[$user->id])) {
42 $disable_meta = get_string('metaalreadyhasenrolments');
43 break;
47 unset($managers);
48 unset($users);
49 unset($enrolroles);
51 } else {
52 $coursecontext = null;
53 $context = $categorycontext;
56 /// form definition with new course defaults
57 //--------------------------------------------------------------------------------
58 $mform->addElement('header','general', get_string('general', 'form'));
60 // Must have create course capability in both categories in order to move course
61 if (has_capability('moodle/course:create', $categorycontext)) {
62 $displaylist = array();
63 $parentlist = array();
64 make_categories_list($displaylist, $parentlist, 'moodle/course:create');
65 $mform->addElement('select', 'category', get_string('category'), $displaylist);
66 } else {
67 $mform->addElement('hidden', 'category', null);
68 $mform->setType('category', PARAM_INT);
70 $mform->setHelpButton('category', array('coursecategory', get_string('category')));
71 $mform->setDefault('category', $category->id);
72 $mform->setType('category', PARAM_INT);
74 if ($course and !has_capability('moodle/course:changecategory', $coursecontext)) {
75 $mform->hardFreeze('category');
76 $mform->setConstant('category', $category->id);
79 $mform->addElement('text','fullname', get_string('fullname'),'maxlength="254" size="50"');
80 $mform->setHelpButton('fullname', array('coursefullname', get_string('fullname')), true);
81 $mform->setDefault('fullname', get_string('defaultcoursefullname'));
82 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
83 $mform->setType('fullname', PARAM_MULTILANG);
84 if ($course and !has_capability('moodle/course:changefullname', $coursecontext)) {
85 $mform->hardFreeze('fullname');
86 $mform->setConstant('fullname', $course->fullname);
89 $mform->addElement('text','shortname', get_string('shortname'),'maxlength="100" size="20"');
90 $mform->setHelpButton('shortname', array('courseshortname', get_string('shortname')), true);
91 $mform->setDefault('shortname', get_string('defaultcourseshortname'));
92 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
93 $mform->setType('shortname', PARAM_MULTILANG);
94 if ($course and !has_capability('moodle/course:changeshortname', $coursecontext)) {
95 $mform->hardFreeze('shortname');
96 $mform->setConstant('shortname', $course->shortname);
99 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
100 $mform->setHelpButton('idnumber', array('courseidnumber', get_string('idnumbercourse')), true);
101 $mform->setType('idnumber', PARAM_RAW);
102 if ($course and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
103 $mform->hardFreeze('idnumber');
104 $mform->setConstants('idnumber', $course->idnumber);
107 $mform->addElement('htmleditor','summary', get_string('summary'), array('rows'=> '10', 'cols'=>'65'));
108 $mform->setHelpButton('summary', array('text', get_string('helptext')), true);
109 $mform->setType('summary', PARAM_RAW);
111 if ($course and !has_capability('moodle/course:changesummary', $coursecontext)) {
112 $mform->hardFreeze('summary');
115 $courseformats = get_list_of_plugins('course/format');
116 $formcourseformats = array();
117 foreach ($courseformats as $courseformat) {
118 $formcourseformats["$courseformat"] = get_string("format$courseformat","format_$courseformat");
119 if($formcourseformats["$courseformat"]=="[[format$courseformat]]") {
120 $formcourseformats["$courseformat"] = get_string("format$courseformat");
123 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
124 $mform->setHelpButton('format', array('courseformats', get_string('courseformats')), true);
125 $mform->setDefault('format', $courseconfig->format);
127 for ($i=1; $i<=52; $i++) {
128 $sectionmenu[$i] = "$i";
130 if (!empty($course) && $course->numsections > 42) {
131 for ($i = $course->numsections -5; $i <= ($course->numsections+10); $i++) {
132 $sectionmenu[$i] = "$i";
135 $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
136 $mform->setDefault('numsections', $courseconfig->numsections);
138 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
139 $mform->setHelpButton('startdate', array('coursestartdate', get_string('startdate')), true);
140 $mform->setDefault('startdate', time() + 3600 * 24);
142 $choices = array();
143 $choices['0'] = get_string('hiddensectionscollapsed');
144 $choices['1'] = get_string('hiddensectionsinvisible');
145 $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
146 $mform->setHelpButton('hiddensections', array('coursehiddensections', get_string('hiddensections')), true);
147 $mform->setDefault('hiddensections', $courseconfig->hiddensections);
149 $options = range(0, 10);
150 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
151 $mform->setHelpButton('newsitems', array('coursenewsitems', get_string('newsitemsnumber')), true);
152 $mform->setDefault('newsitems', $courseconfig->newsitems);
154 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
155 $mform->setHelpButton('showgrades', array('coursegrades', get_string('grades')), true);
156 $mform->setDefault('showgrades', $courseconfig->showgrades);
158 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
159 $mform->setHelpButton('showreports', array('coursereports', get_string('activityreport')), true);
160 $mform->setDefault('showreports', $courseconfig->showreports);
162 $choices = get_max_upload_sizes($CFG->maxbytes);
163 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
164 $mform->setHelpButton('maxbytes', array('courseuploadsize', get_string('maximumupload')), true);
165 $mform->setDefault('maxbytes', $courseconfig->maxbytes);
167 if (!empty($CFG->allowcoursethemes)) {
168 $themes=array();
169 $themes[''] = get_string('forceno');
170 $themes += get_list_of_themes();
171 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
174 $meta=array();
175 $meta[0] = get_string('no');
176 $meta[1] = get_string('yes');
177 if ($disable_meta === false) {
178 $mform->addElement('select', 'metacourse', get_string('managemeta'), $meta);
179 $mform->setHelpButton('metacourse', array('metacourse', get_string('metacourse')), true);
180 $mform->setDefault('metacourse', $courseconfig->metacourse);
181 } else {
182 // no metacourse element - we do not want to change it anyway!
183 $mform->addElement('static', 'nometacourse', get_string('managemeta'),
184 ((empty($course->metacourse)) ? $meta[0] : $meta[1]) . " - $disable_meta ");
185 $mform->setHelpButton('nometacourse', array('metacourse', get_string('metacourse')), true);
188 //--------------------------------------------------------------------------------
189 $mform->addElement('header','enrolhdr', get_string('enrolments'));
191 $choices = array();
192 $modules = explode(',', $CFG->enrol_plugins_enabled);
193 foreach ($modules as $module) {
194 $name = get_string('enrolname', "enrol_$module");
195 $plugin = enrolment_factory::factory($module);
196 if (method_exists($plugin, 'print_entry')) {
197 $choices[$name] = $module;
200 asort($choices);
201 $choices = array_flip($choices);
202 $choices = array_merge(array('' => get_string('sitedefault').' ('.get_string('enrolname', "enrol_$CFG->enrol").')'), $choices);
203 $mform->addElement('select', 'enrol', get_string('enrolmentplugins'), $choices);
204 $mform->setHelpButton('enrol', array('courseenrolmentplugins', get_string('enrolmentplugins')), true);
207 $roles = get_assignable_roles($context);
208 if (!empty($course)) {
209 // add current default role, so that it is selectable even when user can not assign it
210 if ($current_role = get_record('role', 'id', $course->defaultrole)) {
211 $roles[$current_role->id] = strip_tags(format_string($current_role->name, true));
214 $choices = array();
215 if ($sitedefaultrole = get_record('role', 'id', $CFG->defaultcourseroleid)) {
216 $choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name.')';
217 } else {
218 $choices[0] = get_string('sitedefault');
220 $choices = $choices + $roles;
222 // fix for MDL-9197
223 foreach ($choices as $choiceid => $choice) {
224 $choices[$choiceid] = format_string($choice);
227 $mform->addElement('select', 'defaultrole', get_string('defaultrole', 'role'), $choices);
228 $mform->setDefault('defaultrole', 0);
231 $radio = array();
232 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('no'), 0);
233 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('yes'), 1);
234 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('enroldate'), 2);
235 $mform->addGroup($radio, 'enrollable', get_string('enrollable'), ' ', false);
236 $mform->setHelpButton('enrollable', array('courseenrollable2', get_string('enrollable')), true);
237 $mform->setDefault('enrollable', 1);
239 $enroldatestartgrp = array();
240 $enroldatestartgrp[] = &MoodleQuickForm::createElement('date_selector', 'enrolstartdate');
241 $enroldatestartgrp[] = &MoodleQuickForm::createElement('checkbox', 'enrolstartdisabled', null, get_string('disable'));
242 $mform->addGroup($enroldatestartgrp, 'enrolstartdategrp', get_string('enrolstartdate'), ' ', false);
243 $mform->setDefault('enrolstartdate', 0);
244 $mform->setDefault('enrolstartdisabled', 1);
245 $mform->disabledIf('enrolstartdategrp', 'enrolstartdisabled', 'checked');
247 $enroldateendgrp = array();
248 $enroldateendgrp[] = &MoodleQuickForm::createElement('date_selector', 'enrolenddate');
249 $enroldateendgrp[] = &MoodleQuickForm::createElement('checkbox', 'enrolenddisabled', null, get_string('disable'));
250 $mform->addGroup($enroldateendgrp, 'enroldateendgrp', get_string('enrolenddate'), ' ', false);
251 $mform->setDefault('enrolenddate', 0);
252 $mform->setDefault('enrolenddisabled', 1);
253 $mform->disabledIf('enroldateendgrp', 'enrolenddisabled', 'checked');
255 $periodmenu=array();
256 $periodmenu[0] = get_string('unlimited');
257 for ($i=1; $i<=365; $i++) {
258 $seconds = $i * 86400;
259 $periodmenu[$seconds] = get_string('numdays', '', $i);
261 $mform->addElement('select', 'enrolperiod', get_string('enrolperiod'), $periodmenu);
262 $mform->setDefault('enrolperiod', 0);
265 //--------------------------------------------------------------------------------
266 $mform->addElement('header','expirynotifyhdr', get_string('expirynotify'));
268 $choices = array();
269 $choices['0'] = get_string('no');
270 $choices['1'] = get_string('yes');
271 $mform->addElement('select', 'expirynotify', get_string('notify'), $choices);
272 $mform->setHelpButton('expirynotify', array('expirynotify', get_string('expirynotify')), true);
273 $mform->setDefault('expirynotify', 0);
275 $mform->addElement('select', 'notifystudents', get_string('expirynotifystudents'), $choices);
276 $mform->setHelpButton('notifystudents', array('expirynotifystudents', get_string('expirynotifystudents')), true);
277 $mform->setDefault('notifystudents', 0);
279 $thresholdmenu=array();
280 for ($i=1; $i<=30; $i++) {
281 $seconds = $i * 86400;
282 $thresholdmenu[$seconds] = get_string('numdays', '', $i);
284 $mform->addElement('select', 'expirythreshold', get_string('expirythreshold'), $thresholdmenu);
285 $mform->setHelpButton('expirythreshold', array('expirythreshold', get_string('expirythreshold')), true);
286 $mform->setDefault('expirythreshold', 10 * 86400);
288 //--------------------------------------------------------------------------------
289 $mform->addElement('header','', get_string('groups', 'group'));
291 $choices = array();
292 $choices[NOGROUPS] = get_string('groupsnone', 'group');
293 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
294 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
295 $mform->addElement('select', 'groupmode', get_string('groupmode'), $choices);
296 $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')), true);
297 $mform->setDefault('groupmode', 0);
299 $choices = array();
300 $choices['0'] = get_string('no');
301 $choices['1'] = get_string('yes');
302 $mform->addElement('select', 'groupmodeforce', get_string('force'), $choices);
303 $mform->setHelpButton('groupmodeforce', array('groupmodeforce', get_string('groupmodeforce')), true);
304 $mform->setDefault('groupmodeforce', 0);
306 if (!empty($CFG->enablegroupings)) {
307 //default groupings selector
308 $options = array();
309 $options[0] = get_string('none');
310 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
313 //--------------------------------------------------------------------------------
314 $mform->addElement('header','', get_string('availability'));
316 $choices = array();
317 $choices['0'] = get_string('courseavailablenot');
318 $choices['1'] = get_string('courseavailable');
319 $mform->addElement('select', 'visible', get_string('availability'), $choices);
320 $mform->setHelpButton('visible', array('courseavailability', get_string('availability')), true);
321 $mform->setDefault('visible', 1);
322 if ($course and !has_capability('moodle/course:visibility', $coursecontext)) {
323 $mform->hardFreeze('visible');
324 $mform->setConstant('visible', $course->visible);
327 $mform->addElement('passwordunmask', 'enrolpassword', get_string('enrolmentkey'), 'size="25"');
328 $mform->setHelpButton('enrolpassword', array('enrolmentkey', get_string('enrolmentkey')), true);
329 $mform->setDefault('enrolpassword', '');
330 $mform->setType('enrolpassword', PARAM_RAW);
332 if (empty($course) or ($course->password !== '' and $course->id != SITEID)) {
333 // do not require password in existing courses that do not have password yet - backwards compatibility ;-)
334 if (!empty($CFG->enrol_manual_requirekey)) {
335 $mform->addRule('enrolpassword', get_string('required'), 'required', null, 'client');
339 $choices = array();
340 $choices['0'] = get_string('guestsno');
341 $choices['1'] = get_string('guestsyes');
342 $choices['2'] = get_string('guestskey');
343 $mform->addElement('select', 'guest', get_string('opentoguests'), $choices);
344 $mform->setHelpButton('guest', array('guestaccess', get_string('opentoguests')), true);
345 $mform->setDefault('guest', 0);
347 // 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
348 $enrol_object = $CFG;
349 if (!empty($course)) {
350 $enrol_object = $course;
352 // If the print_entry method exists and the course enrol method isn't manual (both set or inherited from site), show cost
353 if (method_exists(enrolment_factory::factory($enrol_object->enrol), 'print_entry') && !($enrol_object->enrol == 'manual' || (empty($enrol_object->enrol) && $CFG->enrol == 'manual'))) {
354 $costgroup=array();
355 $currencies = get_list_of_currencies();
356 $costgroup[]= &MoodleQuickForm::createElement('text','cost', '', 'maxlength="6" size="6"');
357 $costgroup[]= &MoodleQuickForm::createElement('select', 'currency', '', $currencies);
358 $mform->addGroup($costgroup, 'costgrp', get_string('cost'), '&nbsp;', false);
359 //defining a rule for a form element within a group :
360 $costgrprules=array();
361 //set the message to null to tell Moodle to use a default message
362 //available for most rules, fetched from language pack (err_{rulename}).
363 $costgrprules['cost'][]=array(null, 'numeric', null, 'client');
364 $mform->addGroupRule('costgrp',$costgrprules);
365 $mform->setHelpButton('costgrp', array('cost', get_string('cost')), true);
366 $mform->setDefault('cost', '');
367 $mform->setDefault('currency', empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency);
371 //--------------------------------------------------------------------------------
372 $mform->addElement('header','', get_string('language'));
374 $languages=array();
375 $languages[''] = get_string('forceno');
376 $languages += get_list_of_languages();
377 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
379 //--------------------------------------------------------------------------------
380 if (has_capability('moodle/site:config', $systemcontext) && ((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
381 $mform->addElement('header', '', get_string('restrictmodules'));
383 $options = array();
384 $options['0'] = get_string('no');
385 $options['1'] = get_string('yes');
386 $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
387 $mods = array(0=>get_string('allownone'));
388 $mods += get_records_menu('modules', '','','','id, name');
391 $mform->addElement('select', 'allowedmods', get_string('to'), $mods,
392 array('multiple'=>'multiple', 'size'=>'10'));
393 $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
394 } else {
395 $mform->addElement('hidden', 'restrictmodules', null);
396 $mform->setType('restrictmodules', PARAM_INT);
398 if ($CFG->restrictmodulesfor == 'all') {
399 $mform->setDefault('allowedmods', explode(',',$CFG->defaultallowedmodules));
400 if (!empty($CFG->restrictbydefault)) {
401 $mform->setDefault('restrictmodules', 1);
404 $mform->setType('restrictmodules', PARAM_INT);
406 /// customizable role names in this course
407 //--------------------------------------------------------------------------------
408 $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
409 $mform->setHelpButton('rolerenaming', array('rolerenaming', get_string('rolerenaming')), true);
411 if ($roles = get_records('role')) {
412 foreach ($roles as $role) {
413 $mform->addElement('text', 'role_'.$role->id, $role->name);
414 $mform->setType('role_'.$role->id, PARAM_TEXT);
415 if ($coursecontext) {
416 if ($rolename = get_record('role_names', 'roleid', $role->id, 'contextid', $coursecontext->id)) {
417 $mform->setDefault('role_'.$role->id, $rolename->name);
423 //--------------------------------------------------------------------------------
424 $this->add_action_buttons();
425 //--------------------------------------------------------------------------------
426 $mform->addElement('hidden', 'id', null);
427 $mform->setType('id', PARAM_INT);
429 // fill in default teacher and student names to keep backwards compatibility for a while
430 $mform->addElement('hidden', 'teacher', get_string('defaultcourseteacher'));
431 $mform->setType('teacher', PARAM_RAW);
432 $mform->addElement('hidden', 'teachers', get_string('defaultcourseteachers'));
433 $mform->setType('teachers', PARAM_RAW);
434 $mform->addElement('hidden', 'student', get_string('defaultcoursestudent'));
435 $mform->setType('student', PARAM_RAW);
436 $mform->addElement('hidden', 'students', get_string('defaultcoursestudents'));
437 $mform->setType('students', PARAM_RAW);
440 function definition_after_data() {
441 global $CFG;
443 $mform =& $this->_form;
445 // add availabe groupings
446 if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
447 $options = array();
448 if ($groupings = get_records('groupings', 'courseid', $courseid)) {
449 foreach ($groupings as $grouping) {
450 $options[$grouping->id] = format_string($grouping->name);
453 $gr_el =& $mform->getElement('defaultgroupingid');
454 $gr_el->load($options);
459 /// perform some extra moodle validation
460 function validation($data, $files) {
461 global $CFG;
463 $errors = parent::validation($data, $files);
464 if ($foundcourses = get_records('course', 'shortname', $data['shortname'])) {
465 if (!empty($data['id'])) {
466 unset($foundcourses[$data['id']]);
468 if (!empty($foundcourses)) {
469 foreach ($foundcourses as $foundcourse) {
470 $foundcoursenames[] = $foundcourse->fullname;
472 $foundcoursenamestring = implode(',', $foundcoursenames);
473 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
477 if (empty($data['enrolenddisabled'])){
478 if ($data['enrolenddate'] <= $data['enrolstartdate']){
479 $errors['enroldateendgrp'] = get_string('enrolenddaterror');
483 if (!empty($CFG->enrol_manual_usepasswordpolicy) and isset($data['enrolpassword']) and $data['enrolpassword'] != '') {
484 $course = $this->_customdata['course'];
485 if ($course->password !== $data['enrolpassword']) {
486 // enforce password policy only if changing password - backwards compatibility
487 $errmsg = '';
488 if (!check_password_policy($data['enrolpassword'], $errmsg)) {
489 $errors['enrolpassword'] = $errmsg;
494 return $errors;