Search moodle.org's
Developer Documentation

   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  
  24          $mform->addElement('header', 'generalhdr', get_string('general'));
  25  
  26          $elementgroup = array();
  27          $elementgroup[] = $mform->createElement('text', 'name', '', array('size' => '30', 'maxlength' => '255'));
  28          $elementgroup[] = $mform->createElement('checkbox', 'usedefaultname', '', get_string('sectionusedefaultname'));
  29          $mform->addGroup($elementgroup, 'name_group', get_string('sectionname'), ' ', false);
  30          $mform->addGroupRule('name_group', array('name' => array(array(get_string('maximumchars', '', 255), 'maxlength', 255))));
  31  
  32          $mform->setDefault('usedefaultname', true);
  33          $mform->setType('name', PARAM_TEXT);
  34          $mform->disabledIf('name','usedefaultname','checked');
  35  
  36          /// Prepare course and the editor
  37  
  38          $mform->addElement('editor', 'summary_editor', get_string('summary'), null, $this->_customdata['editoroptions']);
  39          $mform->addHelpButton('summary_editor', 'summary');
  40          $mform->setType('summary_editor', PARAM_RAW);
  41  
  42          $mform->addElement('hidden', 'id');
  43          $mform->setType('id', PARAM_INT);
  44  
  45          // additional fields that course format has defined
  46          $courseformat = course_get_format($course);
  47          $formatoptions = $courseformat->section_format_options(true);
  48          if (!empty($formatoptions)) {
  49              $elements = $courseformat->create_edit_form_elements($mform, true);
  50          }
  51  
  52          $mform->_registerCancelButton('cancel');
  53      }
  54  
  55      public function definition_after_data() {
  56          global $CFG, $DB;
  57  
  58          $mform  = $this->_form;
  59          $course = $this->_customdata['course'];
  60          $context = context_course::instance($course->id);
  61  
  62          if (!empty($CFG->enableavailability)) {
  63              $mform->addElement('header', 'availabilityconditions',
  64                      get_string('restrictaccess', 'availability'));
  65              $mform->setExpanded('availabilityconditions', false);
  66  
  67              // Availability field. This is just a textarea; the user interface
  68              // interaction is all implemented in JavaScript. The field is named
  69              // availabilityconditionsjson for consistency with moodleform_mod.
  70              $mform->addElement('textarea', 'availabilityconditionsjson',
  71                      get_string('accessrestrictions', 'availability'));
  72              \core_availability\frontend::include_all_javascript($course, null,
  73                      $this->_customdata['cs']);
  74          }
  75  
  76          $this->add_action_buttons();
  77      }
  78  
  79      /**
  80       * Load in existing data as form defaults
  81       *
  82       * @param stdClass|array $default_values object or array of default values
  83       */
  84      function set_data($default_values) {
  85          if (!is_object($default_values)) {
  86              // we need object for file_prepare_standard_editor
  87              $default_values = (object)$default_values;
  88          }
  89          $editoroptions = $this->_customdata['editoroptions'];
  90          $default_values = file_prepare_standard_editor($default_values, 'summary', $editoroptions,
  91                  $editoroptions['context'], 'course', 'section', $default_values->id);
  92          $default_values->usedefaultname = (is_null($default_values->name));
  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              if (!empty($data->usedefaultname)) {
 107                  $data->name = null;
 108              }
 109              $data = file_postupdate_standard_editor($data, 'summary', $editoroptions,
 110                      $editoroptions['context'], 'course', 'section', $data->id);
 111              $course = $this->_customdata['course'];
 112              foreach (course_get_format($course)->section_format_options() as $option => $unused) {
 113                  // fix issue with unset checkboxes not being returned at all
 114                  if (!isset($data->$option)) {
 115                      $data->$option = null;
 116                  }
 117              }
 118          }
 119          return $data;
 120      }
 121  
 122      public function validation($data, $files) {
 123          global $CFG;
 124          $errors = array();
 125  
 126          // Availability: Check availability field does not have errors.
 127          if (!empty($CFG->enableavailability)) {
 128              \core_availability\frontend::report_validation_errors($data, $errors);
 129          }
 130  
 131          return $errors;
 132      }
 133  }

Search This Site: