MDL-23308 all "course files" are legacy feature, we must tell this to all our users...
[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
bf34822b
PS
157 if (!empty($course->legacyfiles)) {
158 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
159 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
160 $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
161 }
162
a81bf9c3 163 if (!empty($CFG->allowcoursethemes)) {
33c07420 164 $themeobjects = get_list_of_themes();
a81bf9c3 165 $themes=array();
166 $themes[''] = get_string('forceno');
33c07420
SH
167 foreach ($themeobjects as $key=>$theme) {
168 $themes[$key] = $theme->name;
169 }
a81bf9c3 170 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
171 }
461f5127 172
e15d4dd0 173//--------------------------------------------------------------------------------
df997f84 174 enrol_course_edit_form($mform, $course, $context);
a81bf9c3 175
176//--------------------------------------------------------------------------------
ec3dac98 177 $mform->addElement('header','', get_string('groups', 'group'));
a81bf9c3 178
179 $choices = array();
467cebb0 180 $choices[NOGROUPS] = get_string('groupsnone', 'group');
181 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
182 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
83fc9994
DM
183 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
184 $mform->addHelpButton('groupmode', 'groupmode', 'group');
cfec0454 185 $mform->setDefault('groupmode', $courseconfig->groupmode);
a81bf9c3 186
187 $choices = array();
188 $choices['0'] = get_string('no');
189 $choices['1'] = get_string('yes');
83fc9994
DM
190 $mform->addElement('select', 'groupmodeforce', get_string('groupmodeforce', 'group'), $choices);
191 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
cfec0454 192 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
a81bf9c3 193
98da6021
PS
194 //default groupings selector
195 $options = array();
196 $options[0] = get_string('none');
197 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
24f41672 198
a81bf9c3 199//--------------------------------------------------------------------------------
200 $mform->addElement('header','', get_string('availability'));
201
202 $choices = array();
203 $choices['0'] = get_string('courseavailablenot');
204 $choices['1'] = get_string('courseavailable');
205 $mform->addElement('select', 'visible', get_string('availability'), $choices);
5e67868e 206 $mform->addHelpButton('visible', 'availability');
cfec0454 207 $mform->setDefault('visible', $courseconfig->visible);
db1218a9 208 if (!has_capability('moodle/course:visibility', $context)) {
9136a60c 209 $mform->hardFreeze('visible');
db1218a9
PS
210 if (!empty($course->id)) {
211 $mform->setConstant('visible', $course->visible);
212 } else {
213 $mform->setConstant('visible', $category->visible);
214 }
9136a60c 215 }
a81bf9c3 216
a81bf9c3 217//--------------------------------------------------------------------------------
218 $mform->addElement('header','', get_string('language'));
219
220 $languages=array();
221 $languages[''] = get_string('forceno');
1f96e907 222 $languages += get_string_manager()->get_list_of_translations();
a81bf9c3 223 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
cfec0454 224 $mform->setDefault('lang', $courseconfig->lang);
a81bf9c3 225
4e781c7b 226//--------------------------------------------------------------------------------
227 require_once($CFG->libdir.'/completionlib.php');
228 if(completion_info::is_enabled_for_site()) {
229 $mform->addElement('header','', get_string('progress','completion'));
f7c0d84b 230 $mform->addElement('select', 'enablecompletion', get_string('completion','completion'),
4e781c7b 231 array(0=>get_string('completiondisabled','completion'), 1=>get_string('completionenabled','completion')));
cfec0454 232 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
2be4d090
MD
233
234 $mform->addElement('checkbox', 'completionstartonenrol', get_string('completionstartonenrol', 'completion'));
235 $mform->setDefault('completionstartonenrol', $courseconfig->completionstartonenrol);
236 $mform->disabledIf('completionstartonenrol', 'enablecompletion', 'eq', 0);
4e781c7b 237 } else {
238 $mform->addElement('hidden', 'enablecompletion');
d18e0fe6 239 $mform->setType('enablecompletion', PARAM_INT);
4e781c7b 240 $mform->setDefault('enablecompletion',0);
2be4d090
MD
241
242 $mform->addElement('hidden', 'completionstartonenrol');
243 $mform->setType('completionstartonenrol', PARAM_INT);
244 $mform->setDefault('completionstartonenrol',0);
4e781c7b 245 }
246
a81bf9c3 247//--------------------------------------------------------------------------------
df997f84
PS
248 if (has_capability('moodle/site:config', $systemcontext)) {
249 if (((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
250 $mform->addElement('header', '', get_string('restrictmodules'));
251
252 $options = array();
253 $options['0'] = get_string('no');
254 $options['1'] = get_string('yes');
255 $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
256 if (!empty($CFG->restrictbydefault)) {
257 $mform->setDefault('restrictmodules', 1);
258 }
a81bf9c3 259
df997f84
PS
260 $mods = array(0=>get_string('allownone'));
261 $mods += $DB->get_records_menu('modules', array('visible'=>1), 'name', 'id, name');
262 $mform->addElement('select', 'allowedmods', get_string('to'), $mods, array('multiple'=>'multiple', 'size'=>'10'));
263 $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
264 // defaults are already in $course
265 } else {
266 // remove any mod restriction
267 $mform->addElement('hidden', 'restrictmodules', 0);
268 $mform->setType('restrictmodules', PARAM_INT);
269 }
2dbd6409 270 } else {
df997f84 271 $mform->addElement('hidden', 'restrictmodules');
d18e0fe6 272 $mform->setType('restrictmodules', PARAM_INT);
df997f84
PS
273 if (empty($course->id)) {
274 $mform->setConstant('restrictmodules', (int)($CFG->restrictmodulesfor == 'all'));
275 } else {
276 // keep previous
277 $mform->setConstant('restrictmodules', $course->restrictmodules);
a81bf9c3 278 }
279 }
a81bf9c3 280
0150c561 281/// customizable role names in this course
282//--------------------------------------------------------------------------------
7d2e9a52 283 $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
5e67868e 284 $mform->addHelpButton('rolerenaming', 'rolerenaming');
0150c561 285
bef12c99 286 if ($roles = get_all_roles()) {
287 if ($coursecontext) {
9101efd3 288 $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS_RAW);
bef12c99 289 }
290 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
0150c561 291 foreach ($roles as $role) {
bef12c99 292 $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->name));
293 if (isset($role->localname)) {
294 $mform->setDefault('role_'.$role->id, $role->localname);
295 }
296 $mform->setType('role_'.$role->id, PARAM_TEXT);
297 if (!in_array($role->id, $assignableroles)) {
298 $mform->setAdvanced('role_'.$role->id);
0150c561 299 }
300 }
301 }
302
a81bf9c3 303//--------------------------------------------------------------------------------
a23f0aaf 304 $this->add_action_buttons();
a81bf9c3 305//--------------------------------------------------------------------------------
306 $mform->addElement('hidden', 'id', null);
307 $mform->setType('id', PARAM_INT);
df997f84
PS
308
309/// finally set the current form data
310//--------------------------------------------------------------------------------
311 $this->set_data($course);
c08ac016 312 }
2dbd6409 313
24f41672 314 function definition_after_data() {
c3df0901 315 global $DB;
24f41672 316
df997f84 317 $mform = $this->_form;
24f41672 318
df997f84 319 // add available groupings
24f41672 320 if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
321 $options = array();
c3df0901 322 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
24f41672 323 foreach ($groupings as $grouping) {
324 $options[$grouping->id] = format_string($grouping->name);
325 }
326 }
327 $gr_el =& $mform->getElement('defaultgroupingid');
328 $gr_el->load($options);
329 }
330 }
f7c0d84b 331
a81bf9c3 332
333/// perform some extra moodle validation
a78890d5 334 function validation($data, $files) {
82672532 335 global $DB, $CFG;
c3df0901 336
a78890d5 337 $errors = parent::validation($data, $files);
c3df0901 338 if ($foundcourses = $DB->get_records('course', array('shortname'=>$data['shortname']))) {
a81bf9c3 339 if (!empty($data['id'])) {
340 unset($foundcourses[$data['id']]);
01963b95 341 }
342 if (!empty($foundcourses)) {
343 foreach ($foundcourses as $foundcourse) {
344 $foundcoursenames[] = $foundcourse->fullname;
345 }
a81bf9c3 346 $foundcoursenamestring = implode(',', $foundcoursenames);
347 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
01963b95 348 }
01963b95 349 }
a81bf9c3 350
df997f84 351 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
82672532 352
a78890d5 353 return $errors;
01963b95 354 }
a81bf9c3 355}
aa6c1ced 356