Search moodle.org's
Developer Documentation


   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. '/coursecatlib.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->yui_module('moodle-course-formatchooser', 'M.course.init_formatchooser',
  24                  array(array('formid' => $mform->getAttribute('id'))));
  25  
  26          $course        = $this->_customdata['course']; // this contains the data of this form
  27          $category      = $this->_customdata['category'];
  28          $editoroptions = $this->_customdata['editoroptions'];
  29          $returnto = $this->_customdata['returnto'];
  30          $returnurl = $this->_customdata['returnurl'];
  31  
  32          $systemcontext   = context_system::instance();
  33          $categorycontext = context_coursecat::instance($category->id);
  34  
  35          if (!empty($course->id)) {
  36              $coursecontext = context_course::instance($course->id);
  37              $context = $coursecontext;
  38          } else {
  39              $coursecontext = null;
  40              $context = $categorycontext;
  41          }
  42  
  43          $courseconfig = get_config('moodlecourse');
  44  
  45          $this->course  = $course;
  46          $this->context = $context;
  47  
  48          // Form definition with new course defaults.
  49          $mform->addElement('header','general', get_string('general', 'form'));
  50  
  51          $mform->addElement('hidden', 'returnto', null);
  52          $mform->setType('returnto', PARAM_ALPHANUM);
  53          $mform->setConstant('returnto', $returnto);
  54  
  55          $mform->addElement('hidden', 'returnurl', null);
  56          $mform->setType('returnurl', PARAM_LOCALURL);
  57          $mform->setConstant('returnurl', $returnurl);
  58  
  59          $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
  60          $mform->addHelpButton('fullname', 'fullnamecourse');
  61          $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
  62          $mform->setType('fullname', PARAM_TEXT);
  63          if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
  64              $mform->hardFreeze('fullname');
  65              $mform->setConstant('fullname', $course->fullname);
  66          }
  67  
  68          $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"');
  69          $mform->addHelpButton('shortname', 'shortnamecourse');
  70          $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
  71          $mform->setType('shortname', PARAM_TEXT);
  72          if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
  73              $mform->hardFreeze('shortname');
  74              $mform->setConstant('shortname', $course->shortname);
  75          }
  76  
  77          // Verify permissions to change course category or keep current.
  78          if (empty($course->id)) {
  79              if (has_capability('moodle/course:create', $categorycontext)) {
  80                  $displaylist = coursecat::make_categories_list('moodle/course:create');
  81                  $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
  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 = coursecat::make_categories_list('moodle/course:create');
  92                  if (!isset($displaylist[$course->category])) {
  93                      //always keep current
  94                      $displaylist[$course->category] = coursecat::get($course->category, MUST_EXIST, true)->get_formatted_name();
  95                  }
  96                  $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
  97                  $mform->addHelpButton('category', 'coursecategory');
  98              } else {
  99                  //keep current
 100                  $mform->addElement('hidden', 'category', null);
 101                  $mform->setType('category', PARAM_INT);
 102                  $mform->setConstant('category', $course->category);
 103              }
 104          }
 105  
 106          $choices = array();
 107          $choices['0'] = get_string('hide');
 108          $choices['1'] = get_string('show');
 109          $mform->addElement('select', 'visible', get_string('visible'), $choices);
 110          $mform->addHelpButton('visible', 'visible');
 111          $mform->setDefault('visible', $courseconfig->visible);
 112          if (!empty($course->id)) {
 113              if (!has_capability('moodle/course:visibility', $coursecontext)) {
 114                  $mform->hardFreeze('visible');
 115                  $mform->setConstant('visible', $course->visible);
 116              }
 117          } else {
 118              if (!guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext)) {
 119                  $mform->hardFreeze('visible');
 120                  $mform->setConstant('visible', $courseconfig->visible);
 121              }
 122          }
 123  
 124          $mform->addElement('date_selector', 'startdate', get_string('startdate'));
 125          $mform->addHelpButton('startdate', 'startdate');
 126          $mform->setDefault('startdate', time() + 3600 * 24);
 127  
 128          $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100"  size="10"');
 129          $mform->addHelpButton('idnumber', 'idnumbercourse');
 130          $mform->setType('idnumber', PARAM_RAW);
 131          if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
 132              $mform->hardFreeze('idnumber');
 133              $mform->setConstants('idnumber', $course->idnumber);
 134          }
 135  
 136          // Description.
 137          $mform->addElement('header', 'descriptionhdr', get_string('description'));
 138          $mform->setExpanded('descriptionhdr');
 139  
 140          $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
 141          $mform->addHelpButton('summary_editor', 'coursesummary');
 142          $mform->setType('summary_editor', PARAM_RAW);
 143          $summaryfields = 'summary_editor';
 144  
 145          if ($overviewfilesoptions = course_overviewfiles_options($course)) {
 146              $mform->addElement('filemanager', 'overviewfiles_filemanager', get_string('courseoverviewfiles'), null, $overviewfilesoptions);
 147              $mform->addHelpButton('overviewfiles_filemanager', 'courseoverviewfiles');
 148              $summaryfields .= ',overviewfiles_filemanager';
 149          }
 150  
 151          if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
 152              // Remove the description header it does not contain anything any more.
 153              $mform->removeElement('descriptionhdr');
 154              $mform->hardFreeze($summaryfields);
 155          }
 156  
 157          // Course format.
 158          $mform->addElement('header', 'courseformathdr', get_string('type_format', 'plugin'));
 159  
 160          $courseformats = get_sorted_course_formats(true);
 161          $formcourseformats = array();
 162          foreach ($courseformats as $courseformat) {
 163              $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
 164          }
 165          if (isset($course->format)) {
 166              $course->format = course_get_format($course)->get_format(); // replace with default if not found
 167              if (!in_array($course->format, $courseformats)) {
 168                  // this format is disabled. Still display it in the dropdown
 169                  $formcourseformats[$course->format] = get_string('withdisablednote', 'moodle',
 170                          get_string('pluginname', 'format_'.$course->format));
 171              }
 172          }
 173  
 174          $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
 175          $mform->addHelpButton('format', 'format');
 176          $mform->setDefault('format', $courseconfig->format);
 177  
 178          // Button to update format-specific options on format change (will be hidden by JavaScript).
 179          $mform->registerNoSubmitButton('updatecourseformat');
 180          $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'));
 181  
 182          // Just a placeholder for the course format options.
 183          $mform->addElement('hidden', 'addcourseformatoptionshere');
 184          $mform->setType('addcourseformatoptionshere', PARAM_BOOL);
 185  
 186          // Appearance.
 187          $mform->addElement('header', 'appearancehdr', get_string('appearance'));
 188  
 189          if (!empty($CFG->allowcoursethemes)) {
 190              $themeobjects = get_list_of_themes();
 191              $themes=array();
 192              $themes[''] = get_string('forceno');
 193              foreach ($themeobjects as $key=>$theme) {
 194                  if (empty($theme->hidefromselector)) {
 195                      $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
 196                  }
 197              }
 198              $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
 199          }
 200  
 201          $languages=array();
 202          $languages[''] = get_string('forceno');
 203          $languages += get_string_manager()->get_list_of_translations();
 204          $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
 205          $mform->setDefault('lang', $courseconfig->lang);
 206  
 207          // Multi-Calendar Support - see MDL-18375.
 208          $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
 209          // We do not want to show this option unless there is more than one calendar type to display.
 210          if (count($calendartypes) > 1) {
 211              $calendars = array();
 212              $calendars[''] = get_string('forceno');
 213              $calendars += $calendartypes;
 214              $mform->addElement('select', 'calendartype', get_string('forcecalendartype', 'calendar'), $calendars);
 215          }
 216  
 217          $options = range(0, 10);
 218          $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
 219          $mform->addHelpButton('newsitems', 'newsitemsnumber');
 220          $mform->setDefault('newsitems', $courseconfig->newsitems);
 221  
 222          $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
 223          $mform->addHelpButton('showgrades', 'showgrades');
 224          $mform->setDefault('showgrades', $courseconfig->showgrades);
 225  
 226          $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
 227          $mform->addHelpButton('showreports', 'showreports');
 228          $mform->setDefault('showreports', $courseconfig->showreports);
 229  
 230          // Files and uploads.
 231          $mform->addElement('header', 'filehdr', get_string('filesanduploads'));
 232  
 233          if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
 234              if (empty($course->legacyfiles)) {
 235                  //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
 236                  $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
 237              } else {
 238                  $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
 239              }
 240              $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
 241              $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
 242              if (!isset($courseconfig->legacyfiles)) {
 243                  // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
 244                  $courseconfig->legacyfiles = 0;
 245              }
 246              $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
 247          }
 248  
 249          // Handle non-existing $course->maxbytes on course creation.
 250          $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes;
 251  
 252          // Let's prepare the maxbytes popup.
 253          $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes);
 254          $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
 255          $mform->addHelpButton('maxbytes', 'maximumupload');
 256          $mform->setDefault('maxbytes', $courseconfig->maxbytes);
 257  
 258          // Completion tracking.
 259          if (completion_info::is_enabled_for_site()) {
 260              $mform->addElement('header', 'completionhdr', get_string('completion', 'completion'));
 261              $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion'));
 262              $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
 263              $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion');
 264          } else {
 265              $mform->addElement('hidden', 'enablecompletion');
 266              $mform->setType('enablecompletion', PARAM_INT);
 267              $mform->setDefault('enablecompletion', 0);
 268          }
 269  
 270          enrol_course_edit_form($mform, $course, $context);
 271  
 272          $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
 273  
 274          $choices = array();
 275          $choices[NOGROUPS] = get_string('groupsnone', 'group');
 276          $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
 277          $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
 278          $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
 279          $mform->addHelpButton('groupmode', 'groupmode', 'group');
 280          $mform->setDefault('groupmode', $courseconfig->groupmode);
 281  
 282          $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group'));
 283          $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
 284          $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
 285  
 286          //default groupings selector
 287          $options = array();
 288          $options[0] = get_string('none');
 289          $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
 290  
 291          if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:renameroles', $categorycontext))
 292                  || (!empty($course->id) && has_capability('moodle/course:renameroles', $coursecontext))) {
 293              // Customizable role names in this course.
 294              $mform->addElement('header', 'rolerenaming', get_string('rolerenaming'));
 295              $mform->addHelpButton('rolerenaming', 'rolerenaming');
 296  
 297              if ($roles = get_all_roles()) {
 298                  $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL);
 299                  $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
 300                  foreach ($roles as $role) {
 301                      $mform->addElement('text', 'role_' . $role->id, get_string('yourwordforx', '', $role->localname));
 302                      $mform->setType('role_' . $role->id, PARAM_TEXT);
 303                  }
 304              }
 305          }
 306  
 307          if (core_tag_tag::is_enabled('core', 'course') &&
 308                  ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:tag', $categorycontext))
 309                  || (!empty($course->id) && has_capability('moodle/course:tag', $coursecontext)))) {
 310              $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
 311              $mform->addElement('tags', 'tags', get_string('tags'),
 312                      array('itemtype' => 'course', 'component' => 'core'));
 313          }
 314  
 315          // When two elements we need a group.
 316          $buttonarray = array();
 317          $classarray = array('class' => 'form-submit');
 318          if ($returnto !== 0) {
 319              $buttonarray[] = &$mform->createElement('submit', 'saveandreturn', get_string('savechangesandreturn'), $classarray);
 320          }
 321          $buttonarray[] = &$mform->createElement('submit', 'saveanddisplay', get_string('savechangesanddisplay'), $classarray);
 322          $buttonarray[] = &$mform->createElement('cancel');
 323          $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
 324          $mform->closeHeaderBefore('buttonar');
 325  
 326          $mform->addElement('hidden', 'id', null);
 327          $mform->setType('id', PARAM_INT);
 328  
 329          // Finally set the current form data
 330          $this->set_data($course);
 331      }
 332  
 333      /**
 334       * Fill in the current page data for this course.
 335       */
 336      function definition_after_data() {
 337          global $DB;
 338  
 339          $mform = $this->_form;
 340  
 341          // add available groupings
 342          if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
 343              $options = array();
 344              if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
 345                  foreach ($groupings as $grouping) {
 346                      $options[$grouping->id] = format_string($grouping->name);
 347                  }
 348              }
 349              core_collator::asort($options);
 350              $gr_el =& $mform->getElement('defaultgroupingid');
 351              $gr_el->load($options);
 352          }
 353  
 354          // add course format options
 355          $formatvalue = $mform->getElementValue('format');
 356          if (is_array($formatvalue) && !empty($formatvalue)) {
 357              $courseformat = course_get_format((object)array('format' => $formatvalue[0]));
 358  
 359              $elements = $courseformat->create_edit_form_elements($mform);
 360              for ($i = 0; $i < count($elements); $i++) {
 361                  $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
 362                          'addcourseformatoptionshere');
 363              }
 364          }
 365      }
 366  
 367      /**
 368       * Validation.
 369       *
 370       * @param array $data
 371       * @param array $files
 372       * @return array the errors that were found
 373       */
 374      function validation($data, $files) {
 375          global $DB;
 376  
 377          $errors = parent::validation($data, $files);
 378  
 379          // Add field validation check for duplicate shortname.
 380          if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
 381              if (empty($data['id']) || $course->id != $data['id']) {
 382                  $errors['shortname'] = get_string('shortnametaken', '', $course->fullname);
 383              }
 384          }
 385  
 386          // Add field validation check for duplicate idnumber.
 387          if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) {
 388              if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) {
 389                  if (empty($data['id']) || $course->id != $data['id']) {
 390                      $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname);
 391                  }
 392              }
 393          }
 394  
 395          $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
 396  
 397          $courseformat = course_get_format((object)array('format' => $data['format']));
 398          $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
 399          if (!empty($formaterrors) && is_array($formaterrors)) {
 400              $errors = array_merge($errors, $formaterrors);
 401          }
 402  
 403          return $errors;
 404      }
 405  }
 406  

Search This Site: