MDL-70781 course: setting for displaying completion conditions
[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');
f9f41f50 80 $mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist);
aec15827 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 }
f9f41f50 96 $mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist);
aec15827 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 }
d358b91e
MH
123
124 // Download course content.
125 if ($CFG->downloadcoursecontentallowed) {
126 $downloadchoices = [
127 DOWNLOAD_COURSE_CONTENT_DISABLED => get_string('no'),
128 DOWNLOAD_COURSE_CONTENT_ENABLED => get_string('yes'),
129 ];
130 $sitedefaultstring = $downloadchoices[$courseconfig->downloadcontentsitedefault];
131 $downloadchoices[DOWNLOAD_COURSE_CONTENT_SITE_DEFAULT] = get_string('sitedefaultspecified', '', $sitedefaultstring);
132 $downloadselectdefault = $courseconfig->downloadcontent ?? DOWNLOAD_COURSE_CONTENT_SITE_DEFAULT;
133
134 $mform->addElement('select', 'downloadcontent', get_string('enabledownloadcoursecontent', 'course'), $downloadchoices);
135 $mform->addHelpButton('downloadcontent', 'downloadcoursecontent', 'course');
136 $mform->setDefault('downloadcontent', $downloadselectdefault);
137
fbac2e92
MH
138 if ((!empty($course->id) && !has_capability('moodle/course:configuredownloadcontent', $coursecontext)) ||
139 (empty($course->id) &&
140 !guess_if_creator_will_have_course_capability('moodle/course:configuredownloadcontent', $categorycontext))) {
d358b91e
MH
141 $mform->hardFreeze('downloadcontent');
142 $mform->setConstant('downloadcontent', $downloadselectdefault);
143 }
144 }
145
57422d71 146 $mform->addElement('date_time_selector', 'startdate', get_string('startdate'));
aec15827 147 $mform->addHelpButton('startdate', 'startdate');
57422d71
SB
148 $date = (new DateTime())->setTimestamp(usergetmidnight(time()));
149 $date->modify('+1 day');
150 $mform->setDefault('startdate', $date->getTimestamp());
d1f8c1bd 151
57422d71 152 $mform->addElement('date_time_selector', 'enddate', get_string('enddate'), array('optional' => true));
fbcdb0d7
DNA
153 $mform->addHelpButton('enddate', 'enddate');
154
9d8cdb9b 155 if (!empty($CFG->enablecourserelativedates)) {
b850ddfd
JP
156 $attributes = [
157 'aria-describedby' => 'relativedatesmode_warning'
158 ];
9d8cdb9b
JP
159 if (!empty($course->id)) {
160 $attributes['disabled'] = true;
161 }
162 $relativeoptions = [
163 0 => get_string('no'),
164 1 => get_string('yes'),
165 ];
166 $relativedatesmodegroup = [];
167 $relativedatesmodegroup[] = $mform->createElement('select', 'relativedatesmode', get_string('relativedatesmode'),
168 $relativeoptions, $attributes);
b850ddfd
JP
169 $relativedatesmodegroup[] = $mform->createElement('html', html_writer::span(get_string('relativedatesmode_warning'),
170 '', ['id' => 'relativedatesmode_warning']));
9d8cdb9b
JP
171 $mform->addGroup($relativedatesmodegroup, 'relativedatesmodegroup', get_string('relativedatesmode'), null, false);
172 $mform->addHelpButton('relativedatesmodegroup', 'relativedatesmode');
173 }
174
169a974d
FM
175 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
176 $mform->addHelpButton('idnumber', 'idnumbercourse');
177 $mform->setType('idnumber', PARAM_RAW);
178 if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
179 $mform->hardFreeze('idnumber');
180 $mform->setConstants('idnumber', $course->idnumber);
d1f8c1bd 181 }
a81bf9c3 182
169a974d
FM
183 // Description.
184 $mform->addElement('header', 'descriptionhdr', get_string('description'));
185 $mform->setExpanded('descriptionhdr');
aec15827 186
169a974d
FM
187 $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
188 $mform->addHelpButton('summary_editor', 'coursesummary');
189 $mform->setType('summary_editor', PARAM_RAW);
190 $summaryfields = 'summary_editor';
aec15827 191
169a974d
FM
192 if ($overviewfilesoptions = course_overviewfiles_options($course)) {
193 $mform->addElement('filemanager', 'overviewfiles_filemanager', get_string('courseoverviewfiles'), null, $overviewfilesoptions);
194 $mform->addHelpButton('overviewfiles_filemanager', 'courseoverviewfiles');
195 $summaryfields .= ',overviewfiles_filemanager';
196 }
197
198 if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
199 // Remove the description header it does not contain anything any more.
200 $mform->removeElement('descriptionhdr');
201 $mform->hardFreeze($summaryfields);
202 }
203
204 // Course format.
205 $mform->addElement('header', 'courseformathdr', get_string('type_format', 'plugin'));
50569ba3 206
3776335c 207 $courseformats = get_sorted_course_formats(true);
a81bf9c3 208 $formcourseformats = array();
3776335c 209 foreach ($courseformats as $courseformat) {
ff67afbd 210 $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
a81bf9c3 211 }
3776335c
MG
212 if (isset($course->format)) {
213 $course->format = course_get_format($course)->get_format(); // replace with default if not found
214 if (!in_array($course->format, $courseformats)) {
215 // this format is disabled. Still display it in the dropdown
216 $formcourseformats[$course->format] = get_string('withdisablednote', 'moodle',
217 get_string('pluginname', 'format_'.$course->format));
218 }
219 }
220
a81bf9c3 221 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
5e67868e 222 $mform->addHelpButton('format', 'format');
cfec0454 223 $mform->setDefault('format', $courseconfig->format);
a81bf9c3 224
aec15827 225 // Button to update format-specific options on format change (will be hidden by JavaScript).
fc79ede5
MG
226 $mform->registerNoSubmitButton('updatecourseformat');
227 $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'));
228
aec15827
FM
229 // Just a placeholder for the course format options.
230 $mform->addElement('hidden', 'addcourseformatoptionshere');
231 $mform->setType('addcourseformatoptionshere', PARAM_BOOL);
232
169a974d
FM
233 // Appearance.
234 $mform->addElement('header', 'appearancehdr', get_string('appearance'));
235
aec15827
FM
236 if (!empty($CFG->allowcoursethemes)) {
237 $themeobjects = get_list_of_themes();
238 $themes=array();
239 $themes[''] = get_string('forceno');
240 foreach ($themeobjects as $key=>$theme) {
241 if (empty($theme->hidefromselector)) {
242 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
243 }
244 }
245 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
246 }
247
248 $languages=array();
249 $languages[''] = get_string('forceno');
250 $languages += get_string_manager()->get_list_of_translations();
ecce4518
YS
251 if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:setforcedlanguage', $categorycontext))
252 || (!empty($course->id) && has_capability('moodle/course:setforcedlanguage', $coursecontext))) {
253 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
254 $mform->setDefault('lang', $courseconfig->lang);
255 }
a81bf9c3 256
df5d27d8
MN
257 // Multi-Calendar Support - see MDL-18375.
258 $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
259 // We do not want to show this option unless there is more than one calendar type to display.
260 if (count($calendartypes) > 1) {
261 $calendars = array();
262 $calendars[''] = get_string('forceno');
263 $calendars += $calendartypes;
264 $mform->addElement('select', 'calendartype', get_string('forcecalendartype', 'calendar'), $calendars);
265 }
7e52491c 266
019186b7
AT
267 $options = range(0, 10);
268 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
269 $courseconfig = get_config('moodlecourse');
270 $mform->setDefault('newsitems', $courseconfig->newsitems);
271 $mform->addHelpButton('newsitems', 'newsitemsnumber');
272
65877218 273 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
5e67868e 274 $mform->addHelpButton('showgrades', 'showgrades');
cfec0454 275 $mform->setDefault('showgrades', $courseconfig->showgrades);
a81bf9c3 276
65877218 277 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
5e67868e 278 $mform->addHelpButton('showreports', 'showreports');
cfec0454 279 $mform->setDefault('showreports', $courseconfig->showreports);
a81bf9c3 280
8b47f169
SL
281 // Show activity dates.
282 $mform->addElement('selectyesno', 'showactivitydates', get_string('showactivitydates'));
283 $mform->addHelpButton('showactivitydates', 'showactivitydates');
284 $mform->setDefault('showactivitydates', $courseconfig->showactivitydates);
285
169a974d
FM
286 // Files and uploads.
287 $mform->addElement('header', 'filehdr', get_string('filesanduploads'));
288
deb73728
PS
289 if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
290 if (empty($course->legacyfiles)) {
291 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
292 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
293 } else {
294 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
295 }
bf34822b
PS
296 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
297 $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
532a91d4
PS
298 if (!isset($courseconfig->legacyfiles)) {
299 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
300 $courseconfig->legacyfiles = 0;
301 }
c00280a1 302 $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
bf34822b
PS
303 }
304
169a974d
FM
305 // Handle non-existing $course->maxbytes on course creation.
306 $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes;
307
308 // Let's prepare the maxbytes popup.
309 $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes);
310 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
311 $mform->addHelpButton('maxbytes', 'maximumupload');
312 $mform->setDefault('maxbytes', $courseconfig->maxbytes);
313
314 // Completion tracking.
315 if (completion_info::is_enabled_for_site()) {
316 $mform->addElement('header', 'completionhdr', get_string('completion', 'completion'));
317 $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion'));
318 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
319 $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion');
94a8b91c
SL
320
321 $showcompletionconditions = $courseconfig->showcompletionconditions ?? COMPLETION_SHOW_CONDITIONS;
322 $mform->addElement('selectyesno', 'showcompletionconditions', get_string('showcompletionconditions', 'completion'));
323 $mform->addHelpButton('showcompletionconditions', 'showcompletionconditions', 'completion');
324 $mform->setDefault('showcompletionconditions', $showcompletionconditions);
325 $mform->hideIf('showcompletionconditions', 'enablecompletion', 'eq', COMPLETION_HIDE_CONDITIONS);
169a974d
FM
326 } else {
327 $mform->addElement('hidden', 'enablecompletion');
328 $mform->setType('enablecompletion', PARAM_INT);
329 $mform->setDefault('enablecompletion', 0);
330 }
331
df997f84 332 enrol_course_edit_form($mform, $course, $context);
a81bf9c3 333
687732ba 334 $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
a81bf9c3 335
336 $choices = array();
467cebb0 337 $choices[NOGROUPS] = get_string('groupsnone', 'group');
338 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
339 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
83fc9994
DM
340 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
341 $mform->addHelpButton('groupmode', 'groupmode', 'group');
cfec0454 342 $mform->setDefault('groupmode', $courseconfig->groupmode);
a81bf9c3 343
aec15827 344 $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group'));
83fc9994 345 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
cfec0454 346 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
a81bf9c3 347
98da6021
PS
348 //default groupings selector
349 $options = array();
350 $options[0] = get_string('none');
351 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
24f41672 352
502336f2
AH
353 if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:renameroles', $categorycontext))
354 || (!empty($course->id) && has_capability('moodle/course:renameroles', $coursecontext))) {
355 // Customizable role names in this course.
356 $mform->addElement('header', 'rolerenaming', get_string('rolerenaming'));
357 $mform->addHelpButton('rolerenaming', 'rolerenaming');
358
359 if ($roles = get_all_roles()) {
360 $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL);
361 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
362 foreach ($roles as $role) {
363 $mform->addElement('text', 'role_' . $role->id, get_string('yourwordforx', '', $role->localname));
364 $mform->setType('role_' . $role->id, PARAM_TEXT);
365 }
0150c561 366 }
367 }
368
74fa9f76 369 if (core_tag_tag::is_enabled('core', 'course') &&
0d1e5456
MG
370 ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:tag', $categorycontext))
371 || (!empty($course->id) && has_capability('moodle/course:tag', $coursecontext)))) {
372 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
74fa9f76
MG
373 $mform->addElement('tags', 'tags', get_string('tags'),
374 array('itemtype' => 'course', 'component' => 'core'));
0d1e5456
MG
375 }
376
7a0162f1
DM
377 // Add custom fields to the form.
378 $handler = core_course\customfield\course_handler::create();
379 $handler->set_parent_context($categorycontext); // For course handler only.
380 $handler->instance_form_definition($mform, empty($course->id) ? 0 : $course->id);
381
24d0d813
SH
382 // When two elements we need a group.
383 $buttonarray = array();
9a3ef389 384 $classarray = array('class' => 'form-submit');
24d0d813 385 if ($returnto !== 0) {
9a3ef389 386 $buttonarray[] = &$mform->createElement('submit', 'saveandreturn', get_string('savechangesandreturn'), $classarray);
24d0d813 387 }
9a3ef389 388 $buttonarray[] = &$mform->createElement('submit', 'saveanddisplay', get_string('savechangesanddisplay'), $classarray);
24d0d813
SH
389 $buttonarray[] = &$mform->createElement('cancel');
390 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
391 $mform->closeHeaderBefore('buttonar');
2f00e1b2 392
a81bf9c3 393 $mform->addElement('hidden', 'id', null);
394 $mform->setType('id', PARAM_INT);
df997f84 395
7a0162f1
DM
396 // Prepare custom fields data.
397 $handler->instance_form_before_set_data($course);
2f00e1b2 398 // Finally set the current form data
df997f84 399 $this->set_data($course);
c08ac016 400 }
2dbd6409 401
2f00e1b2
MN
402 /**
403 * Fill in the current page data for this course.
404 */
24f41672 405 function definition_after_data() {
c3df0901 406 global $DB;
24f41672 407
df997f84 408 $mform = $this->_form;
24f41672 409
df997f84 410 // add available groupings
8643c576
DM
411 $courseid = $mform->getElementValue('id');
412 if ($courseid and $mform->elementExists('defaultgroupingid')) {
24f41672 413 $options = array();
c3df0901 414 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
24f41672 415 foreach ($groupings as $grouping) {
416 $options[$grouping->id] = format_string($grouping->name);
417 }
418 }
3ba63534 419 core_collator::asort($options);
24f41672 420 $gr_el =& $mform->getElement('defaultgroupingid');
421 $gr_el->load($options);
422 }
f7c0d84b 423
fc79ede5
MG
424 // add course format options
425 $formatvalue = $mform->getElementValue('format');
426 if (is_array($formatvalue) && !empty($formatvalue)) {
8643c576
DM
427
428 $params = array('format' => $formatvalue[0]);
429 // Load the course as well if it is available, course formats may need it to work out
430 // they preferred course end date.
431 if ($courseid) {
432 $params['id'] = $courseid;
433 }
434 $courseformat = course_get_format((object)$params);
fc79ede5
MG
435
436 $elements = $courseformat->create_edit_form_elements($mform);
437 for ($i = 0; $i < count($elements); $i++) {
438 $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
439 'addcourseformatoptionshere');
440 }
e47d61e1 441
019186b7
AT
442 // Remove newsitems element if format does not support news.
443 if (!$courseformat->supports_news()) {
444 $mform->removeElement('newsitems');
e47d61e1 445 }
fc79ede5 446 }
7a0162f1
DM
447
448 // Tweak the form with values provided by custom fields in use.
449 $handler = core_course\customfield\course_handler::create();
450 $handler->instance_form_definition_after_data($mform, empty($courseid) ? 0 : $courseid);
fc79ede5 451 }
a81bf9c3 452
2f00e1b2
MN
453 /**
454 * Validation.
455 *
456 * @param array $data
457 * @param array $files
458 * @return array the errors that were found
459 */
a78890d5 460 function validation($data, $files) {
c3bf6181 461 global $DB;
c3df0901 462
a78890d5 463 $errors = parent::validation($data, $files);
5536a561
FD
464
465 // Add field validation check for duplicate shortname.
466 if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
467 if (empty($data['id']) || $course->id != $data['id']) {
468 $errors['shortname'] = get_string('shortnametaken', '', $course->fullname);
01963b95 469 }
5536a561
FD
470 }
471
472 // Add field validation check for duplicate idnumber.
c3bf6181 473 if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) {
5536a561
FD
474 if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) {
475 if (empty($data['id']) || $course->id != $data['id']) {
c3bf6181 476 $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname);
01963b95 477 }
01963b95 478 }
01963b95 479 }
a81bf9c3 480
8643c576
DM
481 if ($errorcode = course_validate_dates($data)) {
482 $errors['enddate'] = get_string($errorcode, 'error');
fbcdb0d7
DNA
483 }
484
df997f84 485 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
82672532 486
fc79ede5
MG
487 $courseformat = course_get_format((object)array('format' => $data['format']));
488 $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
489 if (!empty($formaterrors) && is_array($formaterrors)) {
490 $errors = array_merge($errors, $formaterrors);
491 }
492
7a0162f1
DM
493 // Add the custom fields validation.
494 $handler = core_course\customfield\course_handler::create();
495 $errors = array_merge($errors, $handler->instance_form_validation($data, $files));
496
a78890d5 497 return $errors;
01963b95 498 }
a81bf9c3 499}