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