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  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          global $CFG, $OUTPUT;
  21  
  22          $mform  = $this->_form;
  23          $course = $this->_customdata['course'];
  24          $sectioninfo = $this->_customdata['cs'];
  25  
  26          $mform->addElement('header', 'generalhdr', get_string('general'));
  27  
  28          $mform->addElement('defaultcustom', 'name', get_string('sectionname'), [
  29              'defaultvalue' => $this->_customdata['defaultsectionname'],
  30              'customvalue' => $sectioninfo->name,
  31          ], ['size' => 30, 'maxlength' => 255]);
  32          $mform->setDefault('name', false);
  33          $mform->addGroupRule('name', array('name' => array(array(get_string('maximumchars', '', 255), 'maxlength', 255))));
  34  
  35          /// Prepare course and the editor
  36  
  37          $mform->addElement('editor', 'summary_editor', get_string('summary'), null, $this->_customdata['editoroptions']);
  38          $mform->addHelpButton('summary_editor', 'summary');
  39          $mform->setType('summary_editor', PARAM_RAW);
  40  
  41          $mform->addElement('hidden', 'id');
  42          $mform->setType('id', PARAM_INT);
  43  
  44          // additional fields that course format has defined
  45          $courseformat = course_get_format($course);
  46          $formatoptions = $courseformat->section_format_options(true);
  47          if (!empty($formatoptions)) {
  48              $elements = $courseformat->create_edit_form_elements($mform, true);
  49          }
  50  
  51          if (!empty($CFG->enableavailability)) {
  52              $mform->addElement('header', 'availabilityconditions',
  53                  get_string('restrictaccess', 'availability'));
  54              $mform->setExpanded('availabilityconditions', false);
  55  
  56              // Availability field. This is just a textarea; the user interface
  57              // interaction is all implemented in JavaScript. The field is named
  58              // availabilityconditionsjson for consistency with moodleform_mod.
  59              $mform->addElement('textarea', 'availabilityconditionsjson',
  60                  get_string('accessrestrictions', 'availability'),
  61                  ['class' => 'd-none']
  62              );
  63              // Availability loading indicator.
  64              $loadingcontainer = $OUTPUT->container(
  65                  $OUTPUT->render_from_template('core/loading', []),
  66                  'd-flex justify-content-center py-5 icon-size-5',
  67                  'availabilityconditions-loading'
  68              );
  69              $mform->addElement('html', $loadingcontainer);
  70          }
  71  
  72          $mform->_registerCancelButton('cancel');
  73      }
  74  
  75      public function definition_after_data() {
  76          global $CFG;
  77  
  78          $mform  = $this->_form;
  79          $course = $this->_customdata['course'];
  80  
  81          if (!empty($CFG->enableavailability)) {
  82              \core_availability\frontend::include_all_javascript($course, null,
  83                      $this->_customdata['cs']);
  84          }
  85  
  86          $this->add_action_buttons();
  87      }
  88  
  89      /**
  90       * Load in existing data as form defaults
  91       *
  92       * @param stdClass|array $default_values object or array of default values
  93       */
  94      function set_data($default_values) {
  95          if (!is_object($default_values)) {
  96              // we need object for file_prepare_standard_editor
  97              $default_values = (object)$default_values;
  98          }
  99          $editoroptions = $this->_customdata['editoroptions'];
 100          $default_values = file_prepare_standard_editor($default_values, 'summary', $editoroptions,
 101                  $editoroptions['context'], 'course', 'section', $default_values->id);
 102          if (strval($default_values->name) === '') {
 103              $default_values->name = false;
 104          }
 105          parent::set_data($default_values);
 106      }
 107  
 108      /**
 109       * Return submitted data if properly submitted or returns NULL if validation fails or
 110       * if there is no submitted data.
 111       *
 112       * @return object submitted data; NULL if not valid or not submitted or cancelled
 113       */
 114      function get_data() {
 115          $data = parent::get_data();
 116          if ($data !== null) {
 117              $editoroptions = $this->_customdata['editoroptions'];
 118              // Set name as an empty string if use default section name is checked.
 119              if ($data->name === false) {
 120                  $data->name = '';
 121              }
 122              $data = file_postupdate_standard_editor($data, 'summary', $editoroptions,
 123                      $editoroptions['context'], 'course', 'section', $data->id);
 124              $course = $this->_customdata['course'];
 125              foreach (course_get_format($course)->section_format_options() as $option => $unused) {
 126                  // fix issue with unset checkboxes not being returned at all
 127                  if (!isset($data->$option)) {
 128                      $data->$option = null;
 129                  }
 130              }
 131          }
 132          return $data;
 133      }
 134  
 135      public function validation($data, $files) {
 136          global $CFG;
 137          $errors = array();
 138  
 139          // Availability: Check availability field does not have errors.
 140          if (!empty($CFG->enableavailability)) {
 141              \core_availability\frontend::report_validation_errors($data, $errors);
 142          }
 143  
 144          return $errors;
 145      }
 146  }