Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

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