MDL-67264 core_course: Begin set up for Activity chooser
[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');
516c5eca 6require_once($CFG->libdir.'/completionlib.php');
39625498 7
2f00e1b2
MN
8/**
9 * The form for handling editing a course.
10 */
a81bf9c3 11class course_edit_form extends moodleform {
df997f84
PS
12 protected $course;
13 protected $context;
a81bf9c3 14
2f00e1b2
MN
15 /**
16 * Form definition.
17 */
a81bf9c3 18 function definition() {
2f00e1b2 19 global $CFG, $PAGE;
a81bf9c3 20
df997f84 21 $mform = $this->_form;
fc79ede5
MG
22 $PAGE->requires->yui_module('moodle-course-formatchooser', 'M.course.init_formatchooser',
23 array(array('formid' => $mform->getAttribute('id'))));
a81bf9c3 24
df997f84
PS
25 $course = $this->_customdata['course']; // this contains the data of this form
26 $category = $this->_customdata['category'];
8bdc9cac 27 $editoroptions = $this->_customdata['editoroptions'];
7feebf6f 28 $returnto = $this->_customdata['returnto'];
24d0d813 29 $returnurl = $this->_customdata['returnurl'];
a81bf9c3 30
9a5e297b
AA
31 $systemcontext = context_system::instance();
32 $categorycontext = context_coursecat::instance($category->id);
a81bf9c3 33
8bdc9cac 34 if (!empty($course->id)) {
9a5e297b 35 $coursecontext = context_course::instance($course->id);
a81bf9c3 36 $context = $coursecontext;
461f5127 37 } else {
a81bf9c3 38 $coursecontext = null;
39 $context = $categorycontext;
40 }
aa6c1ced 41
df997f84
PS
42 $courseconfig = get_config('moodlecourse');
43
44 $this->course = $course;
45 $this->context = $context;
46
2f00e1b2 47 // Form definition with new course defaults.
2b63df96 48 $mform->addElement('header','general', get_string('general', 'form'));
a81bf9c3 49
7feebf6f 50 $mform->addElement('hidden', 'returnto', null);
51 $mform->setType('returnto', PARAM_ALPHANUM);
52 $mform->setConstant('returnto', $returnto);
53
24d0d813
SH
54 $mform->addElement('hidden', 'returnurl', null);
55 $mform->setType('returnurl', PARAM_LOCALURL);
56 $mform->setConstant('returnurl', $returnurl);
57
aec15827
FM
58 $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
59 $mform->addHelpButton('fullname', 'fullnamecourse');
60 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
61 $mform->setType('fullname', PARAM_TEXT);
62 if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
63 $mform->hardFreeze('fullname');
64 $mform->setConstant('fullname', $course->fullname);
65 }
66
67 $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"');
68 $mform->addHelpButton('shortname', 'shortnamecourse');
69 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
70 $mform->setType('shortname', PARAM_TEXT);
71 if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
72 $mform->hardFreeze('shortname');
73 $mform->setConstant('shortname', $course->shortname);
74 }
75
aec15827 76 // Verify permissions to change course category or keep current.
df997f84
PS
77 if (empty($course->id)) {
78 if (has_capability('moodle/course:create', $categorycontext)) {
442f12f8 79 $displaylist = core_course_category::make_categories_list('moodle/course:create');
aec15827
FM
80 $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
81 $mform->addHelpButton('category', 'coursecategory');
df997f84
PS
82 $mform->setDefault('category', $category->id);
83 } else {
84 $mform->addElement('hidden', 'category', null);
85 $mform->setType('category', PARAM_INT);
86 $mform->setConstant('category', $category->id);
87 }
a81bf9c3 88 } else {
df997f84 89 if (has_capability('moodle/course:changecategory', $coursecontext)) {
442f12f8 90 $displaylist = core_course_category::make_categories_list('moodle/course:changecategory');
df997f84
PS
91 if (!isset($displaylist[$course->category])) {
92 //always keep current
442f12f8
MG
93 $displaylist[$course->category] = core_course_category::get($course->category, MUST_EXIST, true)
94 ->get_formatted_name();
df997f84 95 }
aec15827
FM
96 $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
97 $mform->addHelpButton('category', 'coursecategory');
df997f84
PS
98 } else {
99 //keep current
100 $mform->addElement('hidden', 'category', null);
101 $mform->setType('category', PARAM_INT);
102 $mform->setConstant('category', $course->category);
103 }
50569ba3 104 }
105
aec15827
FM
106 $choices = array();
107 $choices['0'] = get_string('hide');
108 $choices['1'] = get_string('show');
ba17a2c3
JB
109 $mform->addElement('select', 'visible', get_string('coursevisibility'), $choices);
110 $mform->addHelpButton('visible', 'coursevisibility');
aec15827 111 $mform->setDefault('visible', $courseconfig->visible);
dba74526
PS
112 if (!empty($course->id)) {
113 if (!has_capability('moodle/course:visibility', $coursecontext)) {
114 $mform->hardFreeze('visible');
aec15827 115 $mform->setConstant('visible', $course->visible);
dba74526
PS
116 }
117 } else {
54d5308e 118 if (!guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext)) {
dba74526 119 $mform->hardFreeze('visible');
aec15827
FM
120 $mform->setConstant('visible', $courseconfig->visible);
121 }
122 }
57422d71 123 $mform->addElement('date_time_selector', 'startdate', get_string('startdate'));
aec15827 124 $mform->addHelpButton('startdate', 'startdate');
57422d71
SB
125 $date = (new DateTime())->setTimestamp(usergetmidnight(time()));
126 $date->modify('+1 day');
127 $mform->setDefault('startdate', $date->getTimestamp());
d1f8c1bd 128
57422d71 129 $mform->addElement('date_time_selector', 'enddate', get_string('enddate'), array('optional' => true));
fbcdb0d7
DNA
130 $mform->addHelpButton('enddate', 'enddate');
131
9d8cdb9b 132 if (!empty($CFG->enablecourserelativedates)) {
b850ddfd
JP
133 $attributes = [
134 'aria-describedby' => 'relativedatesmode_warning'
135 ];
9d8cdb9b
JP
136 if (!empty($course->id)) {
137 $attributes['disabled'] = true;
138 }
139 $relativeoptions = [
140 0 => get_string('no'),
141 1 => get_string('yes'),
142 ];
143 $relativedatesmodegroup = [];
144 $relativedatesmodegroup[] = $mform->createElement('select', 'relativedatesmode', get_string('relativedatesmode'),
145 $relativeoptions, $attributes);
b850ddfd
JP
146 $relativedatesmodegroup[] = $mform->createElement('html', html_writer::span(get_string('relativedatesmode_warning'),
147 '', ['id' => 'relativedatesmode_warning']));
9d8cdb9b
JP
148 $mform->addGroup($relativedatesmodegroup, 'relativedatesmodegroup', get_string('relativedatesmode'), null, false);
149 $mform->addHelpButton('relativedatesmodegroup', 'relativedatesmode');
150 }
151
169a974d
FM
152 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
153 $mform->addHelpButton('idnumber', 'idnumbercourse');
154 $mform->setType('idnumber', PARAM_RAW);
155 if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
156 $mform->hardFreeze('idnumber');
157 $mform->setConstants('idnumber', $course->idnumber);
d1f8c1bd 158 }
a81bf9c3 159
169a974d
FM
160 // Description.
161 $mform->addElement('header', 'descriptionhdr', get_string('description'));
162 $mform->setExpanded('descriptionhdr');
aec15827 163
169a974d
FM
164 $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
165 $mform->addHelpButton('summary_editor', 'coursesummary');
166 $mform->setType('summary_editor', PARAM_RAW);
167 $summaryfields = 'summary_editor';
aec15827 168
169a974d
FM
169 if ($overviewfilesoptions = course_overviewfiles_options($course)) {
170 $mform->addElement('filemanager', 'overviewfiles_filemanager', get_string('courseoverviewfiles'), null, $overviewfilesoptions);
171 $mform->addHelpButton('overviewfiles_filemanager', 'courseoverviewfiles');
172 $summaryfields .= ',overviewfiles_filemanager';
173 }
174
175 if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
176 // Remove the description header it does not contain anything any more.
177 $mform->removeElement('descriptionhdr');
178 $mform->hardFreeze($summaryfields);
179 }
180
181 // Course format.
182 $mform->addElement('header', 'courseformathdr', get_string('type_format', 'plugin'));
50569ba3 183
3776335c 184 $courseformats = get_sorted_course_formats(true);
a81bf9c3 185 $formcourseformats = array();
3776335c 186 foreach ($courseformats as $courseformat) {
ff67afbd 187 $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
a81bf9c3 188 }
3776335c
MG
189 if (isset($course->format)) {
190 $course->format = course_get_format($course)->get_format(); // replace with default if not found
191 if (!in_array($course->format, $courseformats)) {
192 // this format is disabled. Still display it in the dropdown
193 $formcourseformats[$course->format] = get_string('withdisablednote', 'moodle',
194 get_string('pluginname', 'format_'.$course->format));
195 }
196 }
197
a81bf9c3 198 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
5e67868e 199 $mform->addHelpButton('format', 'format');
cfec0454 200 $mform->setDefault('format', $courseconfig->format);
a81bf9c3 201
aec15827 202 // Button to update format-specific options on format change (will be hidden by JavaScript).
fc79ede5
MG
203 $mform->registerNoSubmitButton('updatecourseformat');
204 $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'));
205
aec15827
FM
206 // Just a placeholder for the course format options.
207 $mform->addElement('hidden', 'addcourseformatoptionshere');
208 $mform->setType('addcourseformatoptionshere', PARAM_BOOL);
209
169a974d
FM
210 // Appearance.
211 $mform->addElement('header', 'appearancehdr', get_string('appearance'));
212
aec15827
FM
213 if (!empty($CFG->allowcoursethemes)) {
214 $themeobjects = get_list_of_themes();
215 $themes=array();
216 $themes[''] = get_string('forceno');
217 foreach ($themeobjects as $key=>$theme) {
218 if (empty($theme->hidefromselector)) {
219 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
220 }
221 }
222 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
223 }
224
225 $languages=array();
226 $languages[''] = get_string('forceno');
227 $languages += get_string_manager()->get_list_of_translations();
ecce4518
YS
228 if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:setforcedlanguage', $categorycontext))
229 || (!empty($course->id) && has_capability('moodle/course:setforcedlanguage', $coursecontext))) {
230 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
231 $mform->setDefault('lang', $courseconfig->lang);
232 }
a81bf9c3 233
df5d27d8
MN
234 // Multi-Calendar Support - see MDL-18375.
235 $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
236 // We do not want to show this option unless there is more than one calendar type to display.
237 if (count($calendartypes) > 1) {
238 $calendars = array();
239 $calendars[''] = get_string('forceno');
240 $calendars += $calendartypes;
241 $mform->addElement('select', 'calendartype', get_string('forcecalendartype', 'calendar'), $calendars);
242 }
7e52491c 243
019186b7
AT
244 $options = range(0, 10);
245 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
246 $courseconfig = get_config('moodlecourse');
247 $mform->setDefault('newsitems', $courseconfig->newsitems);
248 $mform->addHelpButton('newsitems', 'newsitemsnumber');
249
65877218 250 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
5e67868e 251 $mform->addHelpButton('showgrades', 'showgrades');
cfec0454 252 $mform->setDefault('showgrades', $courseconfig->showgrades);
a81bf9c3 253
65877218 254 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
5e67868e 255 $mform->addHelpButton('showreports', 'showreports');
cfec0454 256 $mform->setDefault('showreports', $courseconfig->showreports);
a81bf9c3 257
169a974d
FM
258 // Files and uploads.
259 $mform->addElement('header', 'filehdr', get_string('filesanduploads'));
260
deb73728
PS
261 if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
262 if (empty($course->legacyfiles)) {
263 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
264 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
265 } else {
266 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
267 }
bf34822b
PS
268 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
269 $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
532a91d4
PS
270 if (!isset($courseconfig->legacyfiles)) {
271 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
272 $courseconfig->legacyfiles = 0;
273 }
c00280a1 274 $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
bf34822b
PS
275 }
276
169a974d
FM
277 // Handle non-existing $course->maxbytes on course creation.
278 $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes;
279
280 // Let's prepare the maxbytes popup.
281 $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes);
282 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
283 $mform->addHelpButton('maxbytes', 'maximumupload');
284 $mform->setDefault('maxbytes', $courseconfig->maxbytes);
285
286 // Completion tracking.
287 if (completion_info::is_enabled_for_site()) {
288 $mform->addElement('header', 'completionhdr', get_string('completion', 'completion'));
289 $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion'));
290 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
291 $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion');
292 } else {
293 $mform->addElement('hidden', 'enablecompletion');
294 $mform->setType('enablecompletion', PARAM_INT);
295 $mform->setDefault('enablecompletion', 0);
296 }
297
df997f84 298 enrol_course_edit_form($mform, $course, $context);
a81bf9c3 299
687732ba 300 $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
a81bf9c3 301
302 $choices = array();
467cebb0 303 $choices[NOGROUPS] = get_string('groupsnone', 'group');
304 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
305 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
83fc9994
DM
306 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
307 $mform->addHelpButton('groupmode', 'groupmode', 'group');
cfec0454 308 $mform->setDefault('groupmode', $courseconfig->groupmode);
a81bf9c3 309
aec15827 310 $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group'));
83fc9994 311 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
cfec0454 312 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
a81bf9c3 313
98da6021
PS
314 //default groupings selector
315 $options = array();
316 $options[0] = get_string('none');
317 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
24f41672 318
502336f2
AH
319 if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:renameroles', $categorycontext))
320 || (!empty($course->id) && has_capability('moodle/course:renameroles', $coursecontext))) {
321 // Customizable role names in this course.
322 $mform->addElement('header', 'rolerenaming', get_string('rolerenaming'));
323 $mform->addHelpButton('rolerenaming', 'rolerenaming');
324
325 if ($roles = get_all_roles()) {
326 $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL);
327 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
328 foreach ($roles as $role) {
329 $mform->addElement('text', 'role_' . $role->id, get_string('yourwordforx', '', $role->localname));
330 $mform->setType('role_' . $role->id, PARAM_TEXT);
331 }
0150c561 332 }
333 }
334
74fa9f76 335 if (core_tag_tag::is_enabled('core', 'course') &&
0d1e5456
MG
336 ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:tag', $categorycontext))
337 || (!empty($course->id) && has_capability('moodle/course:tag', $coursecontext)))) {
338 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
74fa9f76
MG
339 $mform->addElement('tags', 'tags', get_string('tags'),
340 array('itemtype' => 'course', 'component' => 'core'));
0d1e5456
MG
341 }
342
7a0162f1
DM
343 // Add custom fields to the form.
344 $handler = core_course\customfield\course_handler::create();
345 $handler->set_parent_context($categorycontext); // For course handler only.
346 $handler->instance_form_definition($mform, empty($course->id) ? 0 : $course->id);
347
24d0d813
SH
348 // When two elements we need a group.
349 $buttonarray = array();
9a3ef389 350 $classarray = array('class' => 'form-submit');
24d0d813 351 if ($returnto !== 0) {
9a3ef389 352 $buttonarray[] = &$mform->createElement('submit', 'saveandreturn', get_string('savechangesandreturn'), $classarray);
24d0d813 353 }
9a3ef389 354 $buttonarray[] = &$mform->createElement('submit', 'saveanddisplay', get_string('savechangesanddisplay'), $classarray);
24d0d813
SH
355 $buttonarray[] = &$mform->createElement('cancel');
356 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
357 $mform->closeHeaderBefore('buttonar');
2f00e1b2 358
a81bf9c3 359 $mform->addElement('hidden', 'id', null);
360 $mform->setType('id', PARAM_INT);
df997f84 361
7a0162f1
DM
362 // Prepare custom fields data.
363 $handler->instance_form_before_set_data($course);
2f00e1b2 364 // Finally set the current form data
df997f84 365 $this->set_data($course);
c08ac016 366 }
2dbd6409 367
2f00e1b2
MN
368 /**
369 * Fill in the current page data for this course.
370 */
24f41672 371 function definition_after_data() {
c3df0901 372 global $DB;
24f41672 373
df997f84 374 $mform = $this->_form;
24f41672 375
df997f84 376 // add available groupings
8643c576
DM
377 $courseid = $mform->getElementValue('id');
378 if ($courseid and $mform->elementExists('defaultgroupingid')) {
24f41672 379 $options = array();
c3df0901 380 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
24f41672 381 foreach ($groupings as $grouping) {
382 $options[$grouping->id] = format_string($grouping->name);
383 }
384 }
3ba63534 385 core_collator::asort($options);
24f41672 386 $gr_el =& $mform->getElement('defaultgroupingid');
387 $gr_el->load($options);
388 }
f7c0d84b 389
fc79ede5
MG
390 // add course format options
391 $formatvalue = $mform->getElementValue('format');
392 if (is_array($formatvalue) && !empty($formatvalue)) {
8643c576
DM
393
394 $params = array('format' => $formatvalue[0]);
395 // Load the course as well if it is available, course formats may need it to work out
396 // they preferred course end date.
397 if ($courseid) {
398 $params['id'] = $courseid;
399 }
400 $courseformat = course_get_format((object)$params);
fc79ede5
MG
401
402 $elements = $courseformat->create_edit_form_elements($mform);
403 for ($i = 0; $i < count($elements); $i++) {
404 $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
405 'addcourseformatoptionshere');
406 }
e47d61e1 407
019186b7
AT
408 // Remove newsitems element if format does not support news.
409 if (!$courseformat->supports_news()) {
410 $mform->removeElement('newsitems');
e47d61e1 411 }
fc79ede5 412 }
7a0162f1
DM
413
414 // Tweak the form with values provided by custom fields in use.
415 $handler = core_course\customfield\course_handler::create();
416 $handler->instance_form_definition_after_data($mform, empty($courseid) ? 0 : $courseid);
fc79ede5 417 }
a81bf9c3 418
2f00e1b2
MN
419 /**
420 * Validation.
421 *
422 * @param array $data
423 * @param array $files
424 * @return array the errors that were found
425 */
a78890d5 426 function validation($data, $files) {
c3bf6181 427 global $DB;
c3df0901 428
a78890d5 429 $errors = parent::validation($data, $files);
5536a561
FD
430
431 // Add field validation check for duplicate shortname.
432 if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
433 if (empty($data['id']) || $course->id != $data['id']) {
434 $errors['shortname'] = get_string('shortnametaken', '', $course->fullname);
01963b95 435 }
5536a561
FD
436 }
437
438 // Add field validation check for duplicate idnumber.
c3bf6181 439 if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) {
5536a561
FD
440 if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) {
441 if (empty($data['id']) || $course->id != $data['id']) {
c3bf6181 442 $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname);
01963b95 443 }
01963b95 444 }
01963b95 445 }
a81bf9c3 446
8643c576
DM
447 if ($errorcode = course_validate_dates($data)) {
448 $errors['enddate'] = get_string($errorcode, 'error');
fbcdb0d7
DNA
449 }
450
df997f84 451 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
82672532 452
fc79ede5
MG
453 $courseformat = course_get_format((object)array('format' => $data['format']));
454 $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
455 if (!empty($formaterrors) && is_array($formaterrors)) {
456 $errors = array_merge($errors, $formaterrors);
457 }
458
7a0162f1
DM
459 // Add the custom fields validation.
460 $handler = core_course\customfield\course_handler::create();
461 $errors = array_merge($errors, $handler->instance_form_validation($data, $files));
462
a78890d5 463 return $errors;
01963b95 464 }
a81bf9c3 465}