Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
use core\di;
4
use core\hook;
5
 
6
defined('MOODLE_INTERNAL') || die;
7
 
8
require_once($CFG->libdir.'/formslib.php');
9
require_once($CFG->libdir.'/completionlib.php');
10
require_once($CFG->libdir . '/pdflib.php');
11
 
12
/**
13
 * The form for handling editing a course.
14
 */
15
class course_edit_form extends moodleform {
16
    protected $course;
17
    protected $context;
18
 
19
    /**
20
     * Form definition.
21
     */
22
    function definition() {
23
        global $CFG, $PAGE;
24
 
25
        $mform    = $this->_form;
26
        $PAGE->requires->js_call_amd('core_course/formatchooser', 'init');
27
 
28
        $course        = $this->_customdata['course']; // this contains the data of this form
29
        $category      = $this->_customdata['category'];
30
        $editoroptions = $this->_customdata['editoroptions'];
31
        $returnto = $this->_customdata['returnto'];
32
        $returnurl = $this->_customdata['returnurl'];
33
 
34
        $systemcontext   = context_system::instance();
35
        $categorycontext = context_coursecat::instance($category->id);
36
 
37
        if (!empty($course->id)) {
38
            $coursecontext = context_course::instance($course->id);
39
            $context = $coursecontext;
40
        } else {
41
            $coursecontext = null;
42
            $context = $categorycontext;
43
        }
44
 
45
        $courseconfig = get_config('moodlecourse');
46
 
47
        $this->course  = $course;
48
        $this->context = $context;
49
 
50
        // Form definition with new course defaults.
51
        $mform->addElement('header','general', get_string('general', 'form'));
52
 
53
        $mform->addElement('hidden', 'returnto', null);
54
        $mform->setType('returnto', PARAM_ALPHANUM);
55
        $mform->setConstant('returnto', $returnto);
56
 
57
        $mform->addElement('hidden', 'returnurl', null);
58
        $mform->setType('returnurl', PARAM_LOCALURL);
59
        $mform->setConstant('returnurl', $returnurl);
60
 
1441 ariadna 61
        $mform->addElement('text', 'fullname', get_string('fullnamecourse'),
62
            ['maxlength' => \core_course\constants::FULLNAME_MAXIMUM_LENGTH, 'size' => 50]);
1 efrain 63
        $mform->addHelpButton('fullname', 'fullnamecourse');
64
        $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
65
        $mform->setType('fullname', PARAM_TEXT);
66
        if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
67
            $mform->hardFreeze('fullname');
68
            $mform->setConstant('fullname', $course->fullname);
69
        }
70
 
1441 ariadna 71
        $mform->addElement('text', 'shortname', get_string('shortnamecourse'),
72
            ['maxlength' => \core_course\constants::SHORTNAME_MAXIMUM_LENGTH, 'size' => 20]);
1 efrain 73
        $mform->addHelpButton('shortname', 'shortnamecourse');
74
        $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
75
        $mform->setType('shortname', PARAM_TEXT);
76
        if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
77
            $mform->hardFreeze('shortname');
78
            $mform->setConstant('shortname', $course->shortname);
79
        }
80
 
81
        // Verify permissions to change course category or keep current.
82
        if (empty($course->id)) {
83
            if (has_capability('moodle/course:create', $categorycontext)) {
84
                $displaylist = core_course_category::make_categories_list('moodle/course:create');
85
                $mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist);
86
                $mform->addRule('category', null, 'required', null, 'client');
87
                $mform->addHelpButton('category', 'coursecategory');
88
                $mform->setDefault('category', $category->id);
89
            } else {
90
                $mform->addElement('hidden', 'category', null);
91
                $mform->setType('category', PARAM_INT);
92
                $mform->setConstant('category', $category->id);
93
            }
94
        } else {
95
            if (has_capability('moodle/course:changecategory', $coursecontext)) {
96
                $displaylist = core_course_category::make_categories_list('moodle/course:changecategory');
97
                if (!isset($displaylist[$course->category])) {
98
                    //always keep current
99
                    $displaylist[$course->category] = core_course_category::get($course->category, MUST_EXIST, true)
100
                        ->get_formatted_name();
101
                }
102
                $mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist);
103
                $mform->addRule('category', null, 'required', null, 'client');
104
                $mform->addHelpButton('category', 'coursecategory');
105
            } else {
106
                //keep current
107
                $mform->addElement('hidden', 'category', null);
108
                $mform->setType('category', PARAM_INT);
109
                $mform->setConstant('category', $course->category);
110
            }
111
        }
112
 
113
        $choices = array();
114
        $choices['0'] = get_string('hide');
115
        $choices['1'] = get_string('show');
116
        $mform->addElement('select', 'visible', get_string('coursevisibility'), $choices);
117
        $mform->addHelpButton('visible', 'coursevisibility');
118
        $mform->setDefault('visible', $courseconfig->visible);
119
        if (!empty($course->id)) {
120
            if (!has_capability('moodle/course:visibility', $coursecontext)) {
121
                $mform->hardFreeze('visible');
122
                $mform->setConstant('visible', $course->visible);
123
            }
124
        } else {
125
            if (!guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext)) {
126
                $mform->hardFreeze('visible');
127
                $mform->setConstant('visible', $courseconfig->visible);
128
            }
129
        }
130
 
131
        // Download course content.
132
        if ($CFG->downloadcoursecontentallowed) {
133
            $downloadchoices = [
134
                DOWNLOAD_COURSE_CONTENT_DISABLED => get_string('no'),
135
                DOWNLOAD_COURSE_CONTENT_ENABLED => get_string('yes'),
136
            ];
137
            $sitedefaultstring = $downloadchoices[$courseconfig->downloadcontentsitedefault];
138
            $downloadchoices[DOWNLOAD_COURSE_CONTENT_SITE_DEFAULT] = get_string('sitedefaultspecified', '', $sitedefaultstring);
139
            $downloadselectdefault = $courseconfig->downloadcontent ?? DOWNLOAD_COURSE_CONTENT_SITE_DEFAULT;
140
 
141
            $mform->addElement('select', 'downloadcontent', get_string('enabledownloadcoursecontent', 'course'), $downloadchoices);
142
            $mform->addHelpButton('downloadcontent', 'downloadcoursecontent', 'course');
143
            $mform->setDefault('downloadcontent', $downloadselectdefault);
144
 
145
            if ((!empty($course->id) && !has_capability('moodle/course:configuredownloadcontent', $coursecontext)) ||
146
                    (empty($course->id) &&
147
                    !guess_if_creator_will_have_course_capability('moodle/course:configuredownloadcontent', $categorycontext))) {
148
                $mform->hardFreeze('downloadcontent');
149
                $mform->setConstant('downloadcontent', $downloadselectdefault);
150
            }
151
        }
152
 
153
        // Get the task to change automatically the course visibility when the current day matches the course start date.
154
        $task = \core\task\manager::get_scheduled_task('\core\task\show_started_courses_task');
155
        $startdatestring = 'startdate';
156
        if (!empty($task) && !$task->get_disabled()) {
157
            // When the task is enabled, display a different help message.
158
            $startdatestring = 'startdatewithtaskenabled';
159
        }
160
        $mform->addElement('date_time_selector', 'startdate', get_string('startdate'));
161
        $mform->addHelpButton('startdate', $startdatestring);
162
        $date = (new DateTime())->setTimestamp(usergetmidnight(time()));
163
        $date->modify('+1 day');
164
        $mform->setDefault('startdate', $date->getTimestamp());
165
 
166
        // Get the task to change automatically the course visibility when the current day matches the course end date.
167
        $task = \core\task\manager::get_scheduled_task('\core\task\hide_ended_courses_task');
168
        $enddatestring = 'enddate';
169
        if (!empty($task) && !$task->get_disabled()) {
170
            // When the task is enabled, display a different help message.
171
            $enddatestring = 'enddatewithtaskenabled';
172
        }
173
        $mform->addElement('date_time_selector', 'enddate', get_string('enddate'), array('optional' => true));
174
        $mform->addHelpButton('enddate', $enddatestring);
175
 
176
        if (!empty($CFG->enablecourserelativedates)) {
177
            $attributes = [
178
                'aria-describedby' => 'relativedatesmode_warning'
179
            ];
180
            if (!empty($course->id)) {
181
                $attributes['disabled'] = true;
182
            }
183
            $relativeoptions = [
184
 
185
                1 => get_string('yes'),
186
            ];
187
            $relativedatesmodegroup = [];
188
            $relativedatesmodegroup[] = $mform->createElement('select', 'relativedatesmode', get_string('relativedatesmode'),
189
                $relativeoptions, $attributes);
190
            $relativedatesmodegroup[] = $mform->createElement('html', html_writer::span(get_string('relativedatesmode_warning'),
191
                '', ['id' => 'relativedatesmode_warning']));
192
            $mform->addGroup($relativedatesmodegroup, 'relativedatesmodegroup', get_string('relativedatesmode'), null, false);
193
            $mform->addHelpButton('relativedatesmodegroup', 'relativedatesmode');
194
        }
195
 
196
        $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100"  size="10"');
197
        $mform->addHelpButton('idnumber', 'idnumbercourse');
198
        $mform->setType('idnumber', PARAM_RAW);
199
        if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
200
            $mform->hardFreeze('idnumber');
201
            $mform->setConstants('idnumber', $course->idnumber);
202
        }
203
 
204
        // Description.
205
        $mform->addElement('header', 'descriptionhdr', get_string('description'));
206
        $mform->setExpanded('descriptionhdr');
207
 
208
        $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
209
        $mform->addHelpButton('summary_editor', 'coursesummary');
210
        $mform->setType('summary_editor', PARAM_RAW);
211
        $summaryfields = 'summary_editor';
212
 
213
        if ($overviewfilesoptions = course_overviewfiles_options($course)) {
214
            $mform->addElement('filemanager', 'overviewfiles_filemanager', get_string('courseoverviewfiles'), null, $overviewfilesoptions);
215
            $mform->addHelpButton('overviewfiles_filemanager', 'courseoverviewfiles');
216
            $summaryfields .= ',overviewfiles_filemanager';
217
        }
218
 
219
        if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
220
            // Remove the description header it does not contain anything any more.
221
            $mform->removeElement('descriptionhdr');
222
            $mform->hardFreeze($summaryfields);
223
        }
224
 
225
        // Course format.
226
        $mform->addElement('header', 'courseformathdr', get_string('type_format', 'plugin'));
227
 
228
        $courseformats = get_sorted_course_formats(true);
229
        $formcourseformats = new core\output\choicelist();
230
        $formcourseformats->set_allow_empty(false);
231
        foreach ($courseformats as $courseformat) {
232
            $definition = [];
233
            $component = "format_$courseformat";
234
            if (get_string_manager()->string_exists('plugin_description', $component)) {
235
                $definition['description'] = get_string('plugin_description', $component);
236
            }
237
            $formcourseformats->add_option(
238
                $courseformat,
239
                get_string('pluginname', "format_$courseformat"),
240
                [
241
                    'description' => $definition,
242
                ],
243
            );
244
        }
245
        if (isset($course->format)) {
246
            $course->format = course_get_format($course)->get_format(); // Replace with default if not found.
247
            if (!in_array($course->format, $courseformats)) {
248
                // This format is disabled. Still display it in the dropdown.
249
                $formcourseformats->add_option(
250
                    $course->format,
251
                    get_string('withdisablednote', 'moodle', get_string('pluginname', 'format_'.$course->format)),
252
                );
253
            }
254
        }
255
 
256
        $mform->addElement(
257
            'choicedropdown',
258
            'format',
259
            get_string('format'),
260
            $formcourseformats,
261
            ['data-formatchooser-field' => 'selector'],
262
        );
263
        $mform->setDefault('format', $courseconfig->format);
264
 
265
        // Button to update format-specific options on format change (will be hidden by JavaScript).
266
        $mform->registerNoSubmitButton('updatecourseformat');
267
        $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'), [
268
            'data-formatchooser-field' => 'updateButton',
269
            'class' => 'd-none',
270
        ]);
271
 
272
        // Just a placeholder for the course format options.
273
        $mform->addElement('hidden', 'addcourseformatoptionshere');
274
        $mform->setType('addcourseformatoptionshere', PARAM_BOOL);
275
 
276
        // Appearance.
277
        $mform->addElement('header', 'appearancehdr', get_string('appearance'));
278
 
279
        if (!empty($CFG->allowcoursethemes)) {
280
            $themeobjects = get_list_of_themes();
281
            $themes=array();
282
            $themes[''] = get_string('forceno');
283
            foreach ($themeobjects as $key=>$theme) {
284
                if (empty($theme->hidefromselector)) {
285
                    $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
286
                }
287
            }
288
            $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
289
        }
290
 
291
        if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:setforcedlanguage', $categorycontext))
292
                || (!empty($course->id) && has_capability('moodle/course:setforcedlanguage', $coursecontext))) {
293
 
294
            $languages = ['' => get_string('forceno')];
295
            $languages += get_string_manager()->get_list_of_translations();
296
 
297
            $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
298
            $mform->setDefault('lang', $courseconfig->lang);
299
        }
300
 
301
        // Multi-Calendar Support - see MDL-18375.
302
        $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
303
        // We do not want to show this option unless there is more than one calendar type to display.
304
        if (count($calendartypes) > 1) {
305
            $calendars = array();
306
            $calendars[''] = get_string('forceno');
307
            $calendars += $calendartypes;
308
            $mform->addElement('select', 'calendartype', get_string('forcecalendartype', 'calendar'), $calendars);
309
        }
310
 
311
        $options = range(0, 10);
312
        $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
313
        $courseconfig = get_config('moodlecourse');
314
        $mform->setDefault('newsitems', $courseconfig->newsitems);
315
        $mform->addHelpButton('newsitems', 'newsitemsnumber');
316
 
317
        $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
318
        $mform->addHelpButton('showgrades', 'showgrades');
319
        $mform->setDefault('showgrades', $courseconfig->showgrades);
320
 
321
        $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
322
        $mform->addHelpButton('showreports', 'showreports');
323
        $mform->setDefault('showreports', $courseconfig->showreports);
324
 
325
        // Show activity dates.
326
        $mform->addElement('selectyesno', 'showactivitydates', get_string('showactivitydates'));
327
        $mform->addHelpButton('showactivitydates', 'showactivitydates');
328
        $mform->setDefault('showactivitydates', $courseconfig->showactivitydates);
329
 
330
        // Files and uploads.
331
        $mform->addElement('header', 'filehdr', get_string('filesanduploads'));
332
 
333
        if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
334
            if (empty($course->legacyfiles)) {
335
                //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
336
                $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
337
            } else {
338
                $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
339
            }
340
            $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
341
            $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
342
            if (!isset($courseconfig->legacyfiles)) {
343
                // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
344
                $courseconfig->legacyfiles = 0;
345
            }
346
            $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
347
        }
348
 
349
        // Handle non-existing $course->maxbytes on course creation.
350
        $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes;
351
 
352
        // Let's prepare the maxbytes popup.
353
        $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes);
354
        $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
355
        $mform->addHelpButton('maxbytes', 'maximumupload');
356
        $mform->setDefault('maxbytes', $courseconfig->maxbytes);
357
 
358
        // PDF font.
359
        if (!empty($CFG->enablepdfexportfont)) {
360
            $pdf = new \pdf;
361
            $fontlist = $pdf->get_export_fontlist();
362
            // Show the option if the font is defined more than one.
363
            if (count($fontlist) > 1) {
364
                $defaultfont = $courseconfig->pdfexportfont ?? 'freesans';
365
                if (empty($fontlist[$defaultfont])) {
366
                    $defaultfont = current($fontlist);
367
                }
368
                $mform->addElement('select', 'pdfexportfont', get_string('pdfexportfont', 'course'), $fontlist);
369
                $mform->addHelpButton('pdfexportfont', 'pdfexportfont', 'course');
370
                $mform->setDefault('pdfexportfont', $defaultfont);
371
            }
372
        }
373
 
374
        // Completion tracking.
375
        if (completion_info::is_enabled_for_site()) {
376
            $mform->addElement('header', 'completionhdr', get_string('completion', 'completion'));
377
            $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion'));
378
            $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
379
            $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion');
380
 
381
            $showcompletionconditions = $courseconfig->showcompletionconditions ?? COMPLETION_SHOW_CONDITIONS;
382
            $mform->addElement('selectyesno', 'showcompletionconditions', get_string('showcompletionconditions', 'completion'));
383
            $mform->addHelpButton('showcompletionconditions', 'showcompletionconditions', 'completion');
384
            $mform->setDefault('showcompletionconditions', $showcompletionconditions);
385
            $mform->hideIf('showcompletionconditions', 'enablecompletion', 'eq', COMPLETION_DISABLED);
386
        } else {
387
            $mform->addElement('hidden', 'enablecompletion');
388
            $mform->setType('enablecompletion', PARAM_INT);
389
            $mform->setDefault('enablecompletion', 0);
390
        }
391
 
392
        enrol_course_edit_form($mform, $course, $context);
393
 
394
        $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
395
 
396
        $choices = array();
397
        $choices[NOGROUPS] = get_string('groupsnone', 'group');
398
        $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
399
        $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
400
        $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
401
        $mform->addHelpButton('groupmode', 'groupmode', 'group');
402
        $mform->setDefault('groupmode', $courseconfig->groupmode);
403
 
404
        $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group'));
405
        $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
406
        $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
407
 
408
        //default groupings selector
409
        $options = array();
410
        $options[0] = get_string('none');
411
        $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
412
 
413
        if (core_tag_tag::is_enabled('core', 'course') &&
414
                ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:tag', $categorycontext))
415
                || (!empty($course->id) && has_capability('moodle/course:tag', $coursecontext)))) {
416
            $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
417
            $mform->addElement('tags', 'tags', get_string('tags'),
418
                    array('itemtype' => 'course', 'component' => 'core'));
419
        }
420
 
421
        // Add custom fields to the form.
422
        $handler = core_course\customfield\course_handler::create();
423
        $handler->set_parent_context($categorycontext); // For course handler only.
424
        $handler->instance_form_definition($mform, empty($course->id) ? 0 : $course->id);
425
 
426
        $hook = new \core_course\hook\after_form_definition($this, $mform);
427
        di::get(hook\manager::class)->dispatch($hook);
428
 
429
        // When two elements we need a group.
430
        $buttonarray = array();
431
        $classarray = array('class' => 'form-submit');
432
        if ($returnto !== 0) {
433
            $buttonarray[] = &$mform->createElement('submit', 'saveandreturn', get_string('savechangesandreturn'), $classarray);
434
        }
435
        $buttonarray[] = &$mform->createElement('submit', 'saveanddisplay', get_string('savechangesanddisplay'), $classarray);
436
        $buttonarray[] = &$mform->createElement('cancel');
437
        $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
438
        $mform->closeHeaderBefore('buttonar');
439
 
440
        $mform->addElement('hidden', 'id', null);
441
        $mform->setType('id', PARAM_INT);
442
 
443
        // Communication api call to set the communication data in the form for handling actions for group feature changes.
444
        // We only need to set the data for courses already created.
445
        if (!empty($course->id)) {
446
            $communication = core_communication\helper::load_by_course(
447
                courseid: $course->id,
448
                context: $coursecontext,
449
            );
450
            $communication->set_data($course);
451
        }
452
 
453
        // Prepare custom fields data.
454
        $handler->instance_form_before_set_data($course);
455
        // Finally set the current form data
456
        $this->set_data($course);
457
    }
458
 
459
    /**
460
     * Fill in the current page data for this course.
461
     */
462
    function definition_after_data() {
463
        global $DB;
464
 
465
        $mform = $this->_form;
466
 
467
        // add available groupings
468
        $courseid = $mform->getElementValue('id');
469
        if ($courseid and $mform->elementExists('defaultgroupingid')) {
470
            $options = array();
471
            if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
472
                foreach ($groupings as $grouping) {
473
                    $options[$grouping->id] = format_string($grouping->name);
474
                }
475
            }
476
            core_collator::asort($options);
477
            $gr_el =& $mform->getElement('defaultgroupingid');
478
            $gr_el->load($options);
479
        }
480
 
481
        // add course format options
482
        $formatvalue = $mform->getElementValue('format');
483
        if (is_array($formatvalue) && !empty($formatvalue)) {
484
 
485
            $params = array('format' => $formatvalue[0]);
486
            // Load the course as well if it is available, course formats may need it to work out
487
            // they preferred course end date.
488
            if ($courseid) {
489
                $params['id'] = $courseid;
490
            }
491
            $courseformat = course_get_format((object)$params);
492
 
493
            $elements = $courseformat->create_edit_form_elements($mform);
494
            for ($i = 0; $i < count($elements); $i++) {
495
                $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
496
                        'addcourseformatoptionshere');
497
            }
498
 
499
            // Remove newsitems element if format does not support news.
500
            if (!$courseformat->supports_news()) {
501
                $mform->removeElement('newsitems');
502
            }
503
        }
504
 
505
        // Tweak the form with values provided by custom fields in use.
506
        $handler  = core_course\customfield\course_handler::create();
507
        $handler->instance_form_definition_after_data($mform, empty($courseid) ? 0 : $courseid);
508
 
509
        $hook = new \core_course\hook\after_form_definition_after_data($this, $mform);
510
        di::get(hook\manager::class)->dispatch($hook);
511
    }
512
 
513
    /**
514
     * Validation.
515
     *
516
     * @param array $data
517
     * @param array $files
518
     * @return array the errors that were found
519
     */
520
    function validation($data, $files) {
521
        global $DB;
522
 
523
        $errors = parent::validation($data, $files);
524
 
525
        // Add field validation check for duplicate shortname.
526
        if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
527
            if (empty($data['id']) || $course->id != $data['id']) {
528
                $errors['shortname'] = get_string('shortnametaken', '', $course->fullname);
529
            }
530
        }
531
 
532
        // Add field validation check for duplicate idnumber.
533
        if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) {
534
            if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) {
535
                if (empty($data['id']) || $course->id != $data['id']) {
536
                    $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname);
537
                }
538
            }
539
        }
540
 
541
        if ($errorcode = course_validate_dates($data)) {
542
            $errors['enddate'] = get_string($errorcode, 'error');
543
        }
544
 
545
        $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
546
 
547
        $courseformat = course_get_format((object)array('format' => $data['format']));
548
        $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
549
        if (!empty($formaterrors) && is_array($formaterrors)) {
550
            $errors = array_merge($errors, $formaterrors);
551
        }
552
 
553
        // Add the custom fields validation.
554
        $handler = core_course\customfield\course_handler::create();
555
        $errors  = array_merge($errors, $handler->instance_form_validation($data, $files));
556
 
557
        $hook = new \core_course\hook\after_form_validation($this, $data, $files);
558
        di::get(hook\manager::class)->dispatch($hook);
559
        $pluginerrors = $hook->get_errors();
560
        if (!empty($pluginerrors)) {
561
            $errors = array_merge($errors, $pluginerrors);
562
        }
563
 
564
        return $errors;
565
    }
566
 
567
    /**
568
     * Returns course object.
569
     *
570
     * @return \stdClass
571
     */
572
    public function get_course(): stdClass {
573
        return $this->course;
574
    }
575
 
576
    /**
577
     * Returns context.
578
     *
579
     * @return \core\context
580
     */
581
    public function get_context(): \core\context {
582
        return $this->context;
583
    }
584
}