Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * The mform for creating a calendar event. Based on the old event form.
  19   *
  20   * @package    core_calendar
  21   * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core_calendar\local\event\forms;
  25  
  26  use context_system;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  require_once($CFG->dirroot.'/lib/formslib.php');
  31  
  32  /**
  33   * The mform class for creating a calendar event.
  34   *
  35   * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
  36   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class create extends \moodleform {
  39  
  40      use eventtype;
  41  
  42      /**
  43       * Build the editor options using the given context.
  44       *
  45       * @param \context $context A Moodle context
  46       * @return array
  47       */
  48      public static function build_editor_options(\context $context) {
  49          global $CFG;
  50  
  51          return [
  52              'context' => $context,
  53              'maxfiles' => EDITOR_UNLIMITED_FILES,
  54              'maxbytes' => $CFG->maxbytes,
  55              'noclean' => true,
  56              'autosave' => false
  57          ];
  58      }
  59  
  60      /**
  61       * The form definition
  62       */
  63      public function definition() {
  64          global $PAGE;
  65  
  66          $mform = $this->_form;
  67          $starttime = isset($this->_customdata['starttime']) ? $this->_customdata['starttime'] : 0;
  68          $editoroptions = !(empty($this->_customdata['editoroptions'])) ? $this->_customdata['editoroptions'] : null;
  69          $courseid = !(empty($this->_customdata['courseid'])) ? $this->_customdata['courseid'] : null;
  70  
  71          $eventtypes = $this->_customdata['eventtypes'];
  72  
  73          if (in_array(true, $eventtypes, true) === false) {
  74              print_error('nopermissiontoupdatecalendar');
  75          }
  76  
  77          $mform->setDisableShortforms();
  78          $mform->disable_form_change_checker();
  79  
  80          // Empty string so that the element doesn't get rendered.
  81          $mform->addElement('header', 'general', '');
  82  
  83          $this->add_default_hidden_elements($mform);
  84  
  85          // Event name field.
  86          $mform->addElement('text', 'name', get_string('eventname', 'calendar'), 'size="50"');
  87          $mform->addRule('name', get_string('required'), 'required', null, 'client');
  88          $mform->setType('name', PARAM_TEXT);
  89  
  90          // Event time start field.
  91          $mform->addElement('date_time_selector', 'timestart', get_string('date'), ['defaulttime' => $starttime]);
  92  
  93          // Add the select elements for the available event types.
  94          $this->add_event_type_elements($mform, $eventtypes);
  95  
  96          // Start of advanced elements.
  97          // Advanced elements are not visible to the user by default.
  98          // They are displayed through the user of a show more / less button.
  99          $mform->addElement('editor', 'description', get_string('eventdescription', 'calendar'), ['rows' => 3], $editoroptions);
 100          $mform->setType('description', PARAM_RAW);
 101          $mform->setAdvanced('description');
 102  
 103          $mform->addElement('text', 'location', get_string('location', 'moodle'), 'size="50"');
 104          $mform->setType('location', PARAM_RAW_TRIMMED);
 105          $mform->setAdvanced('location');
 106  
 107          // Add the variety of elements allowed for selecting event duration.
 108          $this->add_event_duration_elements($mform);
 109  
 110          // Add the form elements for repeating events.
 111          $this->add_event_repeat_elements($mform);
 112  
 113          // Add the javascript required to enhance this mform.
 114          $PAGE->requires->js_call_amd('core_calendar/event_form', 'init', [$mform->getAttribute('id')]);
 115      }
 116  
 117      /**
 118       * A bit of custom validation for this form
 119       *
 120       * @param array $data An assoc array of field=>value
 121       * @param array $files An array of files
 122       * @return array
 123       */
 124      public function validation($data, $files) {
 125          global $DB;
 126  
 127          $errors = parent::validation($data, $files);
 128          $eventtype = isset($data['eventtype']) ? $data['eventtype'] : null;
 129          $coursekey = ($eventtype == 'group') ? 'groupcourseid' : 'courseid';
 130          $courseid = (!empty($data[$coursekey])) ? $data[$coursekey] : null;
 131  
 132          $eventtypes = $this->_customdata['eventtypes'];
 133          if (empty($eventtype) || !isset($eventtypes[$eventtype]) || $eventtypes[$eventtype] == false) {
 134              $errors['eventtype'] = get_string('invalideventtype', 'calendar');
 135          }
 136  
 137          if ($courseid && $courseid > 0) {
 138              if ($course = $DB->get_record('course', ['id' => $courseid])) {
 139                  if ($data['timestart'] < $course->startdate) {
 140                      $errors['timestart'] = get_string('errorbeforecoursestart', 'calendar');
 141                  }
 142              } else {
 143                  $errors[$coursekey] = get_string('invalidcourse', 'error');
 144              }
 145          }
 146  
 147          if ($eventtype == 'course' && empty($courseid)) {
 148              $errors['courseid'] = get_string('selectacourse');
 149          }
 150  
 151          if ($eventtype == 'group' && (!empty($courseid) && empty($data['groupid']))) {
 152              $errors['groupcourseid'] = get_string('nogroups', 'core_group');
 153          }
 154  
 155          if ($eventtype == 'group' && empty($courseid)) {
 156              $errors['groupcourseid'] = get_string('selectacourse');
 157          }
 158  
 159          if ($data['duration'] == 1 && $data['timestart'] > $data['timedurationuntil']) {
 160              $errors['durationgroup'] = get_string('invalidtimedurationuntil', 'calendar');
 161          } else if ($data['duration'] == 2 && (trim($data['timedurationminutes']) == '' || $data['timedurationminutes'] < 1)) {
 162              $errors['durationgroup'] = get_string('invalidtimedurationminutes', 'calendar');
 163          }
 164  
 165          return $errors;
 166      }
 167  
 168      /**
 169       * Add the list of hidden elements that should appear in this form each
 170       * time. These elements will never be visible to the user.
 171       *
 172       * @param MoodleQuickForm $mform
 173       */
 174      protected function add_default_hidden_elements($mform) {
 175          global $USER;
 176  
 177          // Add some hidden fields.
 178          $mform->addElement('hidden', 'id');
 179          $mform->setType('id', PARAM_INT);
 180          $mform->setDefault('id', 0);
 181  
 182          $mform->addElement('hidden', 'userid');
 183          $mform->setType('userid', PARAM_INT);
 184          $mform->setDefault('userid', $USER->id);
 185  
 186          $mform->addElement('hidden', 'modulename');
 187          $mform->setType('modulename', PARAM_INT);
 188          $mform->setDefault('modulename', '');
 189  
 190          $mform->addElement('hidden', 'instance');
 191          $mform->setType('instance', PARAM_INT);
 192          $mform->setDefault('instance', 0);
 193  
 194          $mform->addElement('hidden', 'visible');
 195          $mform->setType('visible', PARAM_INT);
 196          $mform->setDefault('visible', 1);
 197      }
 198  
 199      /**
 200       * Add the various elements to express the duration options available
 201       * for an event.
 202       *
 203       * @param MoodleQuickForm $mform
 204       */
 205      protected function add_event_duration_elements($mform) {
 206          $group = [];
 207          $group[] = $mform->createElement('radio', 'duration', null, get_string('durationnone', 'calendar'), 0);
 208          $group[] = $mform->createElement('radio', 'duration', null, get_string('durationuntil', 'calendar'), 1);
 209          $group[] = $mform->createElement('date_time_selector', 'timedurationuntil', '');
 210          $group[] = $mform->createElement('radio', 'duration', null, get_string('durationminutes', 'calendar'), 2);
 211          $group[] = $mform->createElement('text', 'timedurationminutes', get_string('durationminutes', 'calendar'));
 212  
 213          $mform->addGroup($group, 'durationgroup', get_string('eventduration', 'calendar'), '<br />', false);
 214          $mform->setAdvanced('durationgroup');
 215  
 216          $mform->disabledIf('timedurationuntil',         'duration', 'noteq', 1);
 217          $mform->disabledIf('timedurationuntil[day]',    'duration', 'noteq', 1);
 218          $mform->disabledIf('timedurationuntil[month]',  'duration', 'noteq', 1);
 219          $mform->disabledIf('timedurationuntil[year]',   'duration', 'noteq', 1);
 220          $mform->disabledIf('timedurationuntil[hour]',   'duration', 'noteq', 1);
 221          $mform->disabledIf('timedurationuntil[minute]', 'duration', 'noteq', 1);
 222  
 223          $mform->setType('timedurationminutes', PARAM_INT);
 224          $mform->disabledIf('timedurationminutes', 'duration', 'noteq', 2);
 225  
 226          $mform->setDefault('duration', 0);
 227      }
 228  
 229      /**
 230       * Add the repeat elements for the form when creating a new event.
 231       *
 232       * @param MoodleQuickForm $mform
 233       */
 234      protected function add_event_repeat_elements($mform) {
 235          $mform->addElement('checkbox', 'repeat', get_string('repeatevent', 'calendar'), null);
 236          $mform->addElement('text', 'repeats', get_string('repeatweeksl', 'calendar'), 'maxlength="10" size="10"');
 237          $mform->setType('repeats', PARAM_INT);
 238          $mform->setDefault('repeats', 1);
 239          $mform->disabledIf('repeats', 'repeat', 'notchecked');
 240          $mform->setAdvanced('repeat');
 241          $mform->setAdvanced('repeats');
 242      }
 243  }