MDL-23255 fixed inverted isset preventing visibility change in course edit form ...
[moodle.git] / course / edit_form.php
CommitLineData
8bdc9cac 1<?php
a81bf9c3 2
df997f84 3defined('MOODLE_INTERNAL') || die;
bfebaf64 4
39625498 5require_once($CFG->libdir.'/formslib.php');
6
a81bf9c3 7class course_edit_form extends moodleform {
df997f84
PS
8 protected $course;
9 protected $context;
a81bf9c3 10
11 function definition() {
f33e1ed4 12 global $USER, $CFG, $DB;
a81bf9c3 13
df997f84 14 $mform = $this->_form;
a81bf9c3 15
df997f84
PS
16 $course = $this->_customdata['course']; // this contains the data of this form
17 $category = $this->_customdata['category'];
8bdc9cac 18 $editoroptions = $this->_customdata['editoroptions'];
a81bf9c3 19
df997f84 20 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
a81bf9c3 21 $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
22
8bdc9cac 23 if (!empty($course->id)) {
a81bf9c3 24 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
25 $context = $coursecontext;
461f5127 26 } else {
a81bf9c3 27 $coursecontext = null;
28 $context = $categorycontext;
29 }
aa6c1ced 30
df997f84
PS
31 $courseconfig = get_config('moodlecourse');
32
33 $this->course = $course;
34 $this->context = $context;
35
a81bf9c3 36/// form definition with new course defaults
37//--------------------------------------------------------------------------------
2b63df96 38 $mform->addElement('header','general', get_string('general', 'form'));
a81bf9c3 39
df997f84
PS
40 // verify permissions to change course category or keep current
41 if (empty($course->id)) {
42 if (has_capability('moodle/course:create', $categorycontext)) {
43 $displaylist = array();
44 $parentlist = array();
45 make_categories_list($displaylist, $parentlist, 'moodle/course:create');
46 $mform->addElement('select', 'category', get_string('category'), $displaylist);
47 $mform->addHelpButton('category', 'category');
48 $mform->setDefault('category', $category->id);
49 } else {
50 $mform->addElement('hidden', 'category', null);
51 $mform->setType('category', PARAM_INT);
52 $mform->setConstant('category', $category->id);
53 }
a81bf9c3 54 } else {
df997f84
PS
55 if (has_capability('moodle/course:changecategory', $coursecontext)) {
56 $displaylist = array();
57 $parentlist = array();
58 make_categories_list($displaylist, $parentlist, 'moodle/course:create');
59 if (!isset($displaylist[$course->category])) {
60 //always keep current
61 $displaylist[$course->category] = format_string($DB->get_field('course_categories', 'name', array('id'=>$course->category)));
62 }
63 $mform->addElement('select', 'category', get_string('category'), $displaylist);
64 $mform->addHelpButton('category', 'category');
65 } else {
66 //keep current
67 $mform->addElement('hidden', 'category', null);
68 $mform->setType('category', PARAM_INT);
69 $mform->setConstant('category', $course->category);
70 }
50569ba3 71 }
72
828a673d 73 $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
503d243a 74 $mform->addHelpButton('fullname', 'fullnamecourse');
01bc9ff1 75 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
76 $mform->setType('fullname', PARAM_MULTILANG);
8bdc9cac 77 if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
01bc9ff1 78 $mform->hardFreeze('fullname');
cc444336 79 $mform->setConstant('fullname', $course->fullname);
f7c0d84b 80 }
c3df0901 81
828a673d 82 $mform->addElement('text','shortname', get_string('shortnamecourse'),'maxlength="100" size="20"');
5e67868e 83 $mform->addHelpButton('shortname', 'shortnamecourse');
01bc9ff1 84 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
85 $mform->setType('shortname', PARAM_MULTILANG);
8bdc9cac 86 if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
01bc9ff1 87 $mform->hardFreeze('shortname');
cc444336 88 $mform->setConstant('shortname', $course->shortname);
f7c0d84b 89 }
90
01bc9ff1 91 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
5e67868e 92 $mform->addHelpButton('idnumber', 'idnumbercourse');
01bc9ff1 93 $mform->setType('idnumber', PARAM_RAW);
8bdc9cac 94 if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
01bc9ff1 95 $mform->hardFreeze('idnumber');
cc444336 96 $mform->setConstants('idnumber', $course->idnumber);
f7c0d84b 97 }
01963b95 98
aa6c1ced 99
5e67868e
DM
100 $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
101 $mform->addHelpButton('summary_editor', 'coursesummary');
8bdc9cac 102 $mform->setType('summary_editor', PARAM_RAW);
a81bf9c3 103
8bdc9cac
SH
104 if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
105 $mform->hardFreeze('summary_editor');
50569ba3 106 }
107
17da2e6f 108 $courseformats = get_plugin_list('format');
a81bf9c3 109 $formcourseformats = array();
17da2e6f 110 foreach ($courseformats as $courseformat => $formatdir) {
ff67afbd 111 $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
a81bf9c3 112 }
113 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
5e67868e 114 $mform->addHelpButton('format', 'format');
cfec0454 115 $mform->setDefault('format', $courseconfig->format);
a81bf9c3 116
117 for ($i=1; $i<=52; $i++) {
118 $sectionmenu[$i] = "$i";
119 }
120 $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
cfec0454 121 $mform->setDefault('numsections', $courseconfig->numsections);
a81bf9c3 122
123 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
5e67868e 124 $mform->addHelpButton('startdate', 'startdate');
a81bf9c3 125 $mform->setDefault('startdate', time() + 3600 * 24);
126
127 $choices = array();
128 $choices['0'] = get_string('hiddensectionscollapsed');
129 $choices['1'] = get_string('hiddensectionsinvisible');
130 $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
5e67868e 131 $mform->addHelpButton('hiddensections', 'hiddensections');
cfec0454 132 $mform->setDefault('hiddensections', $courseconfig->hiddensections);
a81bf9c3 133
65877218 134 $options = range(0, 10);
a81bf9c3 135 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
5e67868e 136 $mform->addHelpButton('newsitems', 'newsitemsnumber');
cfec0454 137 $mform->setDefault('newsitems', $courseconfig->newsitems);
a81bf9c3 138
65877218 139 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
5e67868e 140 $mform->addHelpButton('showgrades', 'showgrades');
cfec0454 141 $mform->setDefault('showgrades', $courseconfig->showgrades);
a81bf9c3 142
65877218 143 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
5e67868e 144 $mform->addHelpButton('showreports', 'showreports');
cfec0454 145 $mform->setDefault('showreports', $courseconfig->showreports);
a81bf9c3 146
147 $choices = get_max_upload_sizes($CFG->maxbytes);
148 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
5e67868e 149 $mform->addHelpButton('maxbytes', 'maximumupload');
cfec0454 150 $mform->setDefault('maxbytes', $courseconfig->maxbytes);
a81bf9c3 151
bf34822b
PS
152 if (!empty($course->legacyfiles)) {
153 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
154 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
155 $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
156 }
157
a81bf9c3 158 if (!empty($CFG->allowcoursethemes)) {
33c07420 159 $themeobjects = get_list_of_themes();
a81bf9c3 160 $themes=array();
161 $themes[''] = get_string('forceno');
33c07420
SH
162 foreach ($themeobjects as $key=>$theme) {
163 $themes[$key] = $theme->name;
164 }
a81bf9c3 165 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
166 }
461f5127 167
e15d4dd0 168//--------------------------------------------------------------------------------
df997f84 169 enrol_course_edit_form($mform, $course, $context);
a81bf9c3 170
171//--------------------------------------------------------------------------------
ec3dac98 172 $mform->addElement('header','', get_string('groups', 'group'));
a81bf9c3 173
174 $choices = array();
467cebb0 175 $choices[NOGROUPS] = get_string('groupsnone', 'group');
176 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
177 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
83fc9994
DM
178 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
179 $mform->addHelpButton('groupmode', 'groupmode', 'group');
cfec0454 180 $mform->setDefault('groupmode', $courseconfig->groupmode);
a81bf9c3 181
182 $choices = array();
183 $choices['0'] = get_string('no');
184 $choices['1'] = get_string('yes');
83fc9994
DM
185 $mform->addElement('select', 'groupmodeforce', get_string('groupmodeforce', 'group'), $choices);
186 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
cfec0454 187 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
a81bf9c3 188
98da6021
PS
189 //default groupings selector
190 $options = array();
191 $options[0] = get_string('none');
192 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
24f41672 193
a81bf9c3 194//--------------------------------------------------------------------------------
195 $mform->addElement('header','', get_string('availability'));
196
197 $choices = array();
198 $choices['0'] = get_string('courseavailablenot');
199 $choices['1'] = get_string('courseavailable');
200 $mform->addElement('select', 'visible', get_string('availability'), $choices);
5e67868e 201 $mform->addHelpButton('visible', 'availability');
cfec0454 202 $mform->setDefault('visible', $courseconfig->visible);
db1218a9 203 if (!has_capability('moodle/course:visibility', $context)) {
9136a60c 204 $mform->hardFreeze('visible');
db1218a9
PS
205 if (!empty($course->id)) {
206 $mform->setConstant('visible', $course->visible);
207 } else {
208 $mform->setConstant('visible', $category->visible);
209 }
9136a60c 210 }
a81bf9c3 211
a81bf9c3 212//--------------------------------------------------------------------------------
213 $mform->addElement('header','', get_string('language'));
214
215 $languages=array();
216 $languages[''] = get_string('forceno');
1f96e907 217 $languages += get_string_manager()->get_list_of_translations();
a81bf9c3 218 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
cfec0454 219 $mform->setDefault('lang', $courseconfig->lang);
a81bf9c3 220
4e781c7b 221//--------------------------------------------------------------------------------
222 require_once($CFG->libdir.'/completionlib.php');
223 if(completion_info::is_enabled_for_site()) {
224 $mform->addElement('header','', get_string('progress','completion'));
f7c0d84b 225 $mform->addElement('select', 'enablecompletion', get_string('completion','completion'),
4e781c7b 226 array(0=>get_string('completiondisabled','completion'), 1=>get_string('completionenabled','completion')));
cfec0454 227 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
2be4d090
MD
228
229 $mform->addElement('checkbox', 'completionstartonenrol', get_string('completionstartonenrol', 'completion'));
230 $mform->setDefault('completionstartonenrol', $courseconfig->completionstartonenrol);
231 $mform->disabledIf('completionstartonenrol', 'enablecompletion', 'eq', 0);
4e781c7b 232 } else {
233 $mform->addElement('hidden', 'enablecompletion');
d18e0fe6 234 $mform->setType('enablecompletion', PARAM_INT);
4e781c7b 235 $mform->setDefault('enablecompletion',0);
2be4d090
MD
236
237 $mform->addElement('hidden', 'completionstartonenrol');
238 $mform->setType('completionstartonenrol', PARAM_INT);
239 $mform->setDefault('completionstartonenrol',0);
4e781c7b 240 }
241
a81bf9c3 242//--------------------------------------------------------------------------------
df997f84
PS
243 if (has_capability('moodle/site:config', $systemcontext)) {
244 if (((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
245 $mform->addElement('header', '', get_string('restrictmodules'));
246
247 $options = array();
248 $options['0'] = get_string('no');
249 $options['1'] = get_string('yes');
250 $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
251 if (!empty($CFG->restrictbydefault)) {
252 $mform->setDefault('restrictmodules', 1);
253 }
a81bf9c3 254
df997f84
PS
255 $mods = array(0=>get_string('allownone'));
256 $mods += $DB->get_records_menu('modules', array('visible'=>1), 'name', 'id, name');
257 $mform->addElement('select', 'allowedmods', get_string('to'), $mods, array('multiple'=>'multiple', 'size'=>'10'));
258 $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
259 // defaults are already in $course
260 } else {
261 // remove any mod restriction
262 $mform->addElement('hidden', 'restrictmodules', 0);
263 $mform->setType('restrictmodules', PARAM_INT);
264 }
2dbd6409 265 } else {
df997f84 266 $mform->addElement('hidden', 'restrictmodules');
d18e0fe6 267 $mform->setType('restrictmodules', PARAM_INT);
df997f84
PS
268 if (empty($course->id)) {
269 $mform->setConstant('restrictmodules', (int)($CFG->restrictmodulesfor == 'all'));
270 } else {
271 // keep previous
272 $mform->setConstant('restrictmodules', $course->restrictmodules);
a81bf9c3 273 }
274 }
a81bf9c3 275
0150c561 276/// customizable role names in this course
277//--------------------------------------------------------------------------------
7d2e9a52 278 $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
5e67868e 279 $mform->addHelpButton('rolerenaming', 'rolerenaming');
0150c561 280
bef12c99 281 if ($roles = get_all_roles()) {
282 if ($coursecontext) {
9101efd3 283 $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS_RAW);
bef12c99 284 }
285 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
0150c561 286 foreach ($roles as $role) {
bef12c99 287 $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->name));
288 if (isset($role->localname)) {
289 $mform->setDefault('role_'.$role->id, $role->localname);
290 }
291 $mform->setType('role_'.$role->id, PARAM_TEXT);
292 if (!in_array($role->id, $assignableroles)) {
293 $mform->setAdvanced('role_'.$role->id);
0150c561 294 }
295 }
296 }
297
a81bf9c3 298//--------------------------------------------------------------------------------
a23f0aaf 299 $this->add_action_buttons();
a81bf9c3 300//--------------------------------------------------------------------------------
301 $mform->addElement('hidden', 'id', null);
302 $mform->setType('id', PARAM_INT);
df997f84
PS
303
304/// finally set the current form data
305//--------------------------------------------------------------------------------
306 $this->set_data($course);
c08ac016 307 }
2dbd6409 308
24f41672 309 function definition_after_data() {
c3df0901 310 global $DB;
24f41672 311
df997f84 312 $mform = $this->_form;
24f41672 313
df997f84 314 // add available groupings
24f41672 315 if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
316 $options = array();
c3df0901 317 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
24f41672 318 foreach ($groupings as $grouping) {
319 $options[$grouping->id] = format_string($grouping->name);
320 }
321 }
322 $gr_el =& $mform->getElement('defaultgroupingid');
323 $gr_el->load($options);
324 }
325 }
f7c0d84b 326
a81bf9c3 327
328/// perform some extra moodle validation
a78890d5 329 function validation($data, $files) {
82672532 330 global $DB, $CFG;
c3df0901 331
a78890d5 332 $errors = parent::validation($data, $files);
c3df0901 333 if ($foundcourses = $DB->get_records('course', array('shortname'=>$data['shortname']))) {
a81bf9c3 334 if (!empty($data['id'])) {
335 unset($foundcourses[$data['id']]);
01963b95 336 }
337 if (!empty($foundcourses)) {
338 foreach ($foundcourses as $foundcourse) {
339 $foundcoursenames[] = $foundcourse->fullname;
340 }
a81bf9c3 341 $foundcoursenamestring = implode(',', $foundcoursenames);
342 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
01963b95 343 }
01963b95 344 }
a81bf9c3 345
df997f84 346 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
82672532 347
a78890d5 348 return $errors;
01963b95 349 }
a81bf9c3 350}
aa6c1ced 351