Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 403]

   1  <?php
   2  
   3  if (!defined('MOODLE_INTERNAL')) {
   4      die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
   5  }
   6  
   7  require_once($CFG->libdir.'/formslib.php');
   8  require_once($CFG->libdir.'/filelib.php');
   9  require_once($CFG->libdir.'/completionlib.php');
  10  require_once($CFG->libdir.'/gradelib.php');
  11  
  12  /**
  13   * Default form for editing course section
  14   *
  15   * Course format plugins may specify different editing form to use
  16   */
  17  class editsection_form extends moodleform {
  18  
  19      function definition() {
  20  
  21          $mform  = $this->_form;
  22          $course = $this->_customdata['course'];
  23          $sectioninfo = $this->_customdata['cs'];
  24  
  25          $mform->addElement('header', 'generalhdr', get_string('general'));
  26  
  27          $mform->addElement('defaultcustom', 'name', get_string('sectionname'), [
  28              'defaultvalue' => $this->_customdata['defaultsectionname'],
  29              'customvalue' => $sectioninfo->name,
  30          ], ['size' => 30, 'maxlength' => 255]);
  31          $mform->setDefault('name', false);
  32          $mform->addGroupRule('name', array('name' => array(array(get_string('maximumchars', '', 255), 'maxlength', 255))));
  33  
  34          /// Prepare course and the editor
  35  
  36          $mform->addElement('editor', 'summary_editor', get_string('summary'), null, $this->_customdata['editoroptions']);
  37          $mform->addHelpButton('summary_editor', 'summary');
  38          $mform->setType('summary_editor', PARAM_RAW);
  39  
  40          $mform->addElement('hidden', 'id');
  41          $mform->setType('id', PARAM_INT);
  42  
  43          // additional fields that course format has defined
  44          $courseformat = course_get_format($course);
  45          $formatoptions = $courseformat->section_format_options(true);
  46          if (!empty($formatoptions)) {
  47              $elements = $courseformat->create_edit_form_elements($mform, true);
  48          }
  49  
  50          $mform->_registerCancelButton('cancel');
  51      }
  52  
  53      public function definition_after_data() {
  54          global $CFG, $DB;
  55  
  56          $mform  = $this->_form;
  57          $course = $this->_customdata['course'];
  58          $context = context_course::instance($course->id);
  59  
  60          if (!empty($CFG->enableavailability)) {
  61              $mform->addElement('header', 'availabilityconditions',
  62                      get_string('restrictaccess', 'availability'));
  63              $mform->setExpanded('availabilityconditions', false);
  64  
  65              // Availability field. This is just a textarea; the user interface
  66              // interaction is all implemented in JavaScript. The field is named
  67              // availabilityconditionsjson for consistency with moodleform_mod.
  68              $mform->addElement('textarea', 'availabilityconditionsjson',
  69                      get_string('accessrestrictions', 'availability'));
  70              \core_availability\frontend::include_all_javascript($course, null,
  71                      $this->_customdata['cs']);
  72          }
  73  
  74          $this->add_action_buttons();
  75      }
  76  
  77      /**
  78       * Load in existing data as form defaults
  79       *
  80       * @param stdClass|array $default_values object or array of default values
  81       */
  82      function set_data($default_values) {
  83          if (!is_object($default_values)) {
  84              // we need object for file_prepare_standard_editor
  85              $default_values = (object)$default_values;
  86          }
  87          $editoroptions = $this->_customdata['editoroptions'];
  88          $default_values = file_prepare_standard_editor($default_values, 'summary', $editoroptions,
  89                  $editoroptions['context'], 'course', 'section', $default_values->id);
  90          if (strval($default_values->name) === '') {
  91              $default_values->name = false;
  92          }
  93          parent::set_data($default_values);
  94      }
  95  
  96      /**
  97       * Return submitted data if properly submitted or returns NULL if validation fails or
  98       * if there is no submitted data.
  99       *
 100       * @return object submitted data; NULL if not valid or not submitted or cancelled
 101       */
 102      function get_data() {
 103          $data = parent::get_data();
 104          if ($data !== null) {
 105              $editoroptions = $this->_customdata['editoroptions'];
 106              // Set name as an empty string if use default section name is checked.
 107              if ($data->name === false) {
 108                  $data->name = '';
 109              }
 110              $data = file_postupdate_standard_editor($data, 'summary', $editoroptions,
 111                      $editoroptions['context'], 'course', 'section', $data->id);
 112              $course = $this->_customdata['course'];
 113              foreach (course_get_format($course)->section_format_options() as $option => $unused) {
 114                  // fix issue with unset checkboxes not being returned at all
 115                  if (!isset($data->$option)) {
 116                      $data->$option = null;
 117                  }
 118              }
 119          }
 120          return $data;
 121      }
 122  
 123      public function validation($data, $files) {
 124          global $CFG;
 125          $errors = array();
 126  
 127          // Availability: Check availability field does not have errors.
 128          if (!empty($CFG->enableavailability)) {
 129              \core_availability\frontend::report_validation_errors($data, $errors);
 130          }
 131  
 132          return $errors;
 133      }
 134  }