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]

   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              throw new \moodle_exception('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          $categoryid = (!empty($data['categoryid'])) ? $data['categoryid'] : null;
 132  
 133          $eventtypes = $this->_customdata['eventtypes'];
 134          if (empty($eventtype) || !isset($eventtypes[$eventtype]) || $eventtypes[$eventtype] == false) {
 135              $errors['eventtype'] = get_string('invalideventtype', 'calendar');
 136          }
 137  
 138          if ($courseid && $courseid > 0) {
 139              if ($course = $DB->get_record('course', ['id' => $courseid])) {
 140                  if ($data['timestart'] < $course->startdate) {
 141                      $errors['timestart'] = get_string('errorbeforecoursestart', 'calendar');
 142                  }
 143              } else {
 144                  $errors[$coursekey] = get_string('invalidcourse', 'error');
 145              }
 146          }
 147  
 148          if ($eventtype == 'course' && empty($courseid)) {
 149              $errors['courseid'] = get_string('selectacourse');
 150          }
 151  
 152          if ($eventtype == 'category' && empty($categoryid)) {
 153              $errors['categoryid'] = get_string('selectacategory');
 154          }
 155  
 156          if ($eventtype == 'group' && (!empty($courseid) && empty($data['groupid']))) {
 157              $errors['groupcourseid'] = get_string('nogroups', 'core_group');
 158          }
 159  
 160          if ($eventtype == 'group' && empty($courseid)) {
 161              $errors['groupcourseid'] = get_string('selectacourse');
 162          }
 163  
 164          if ($data['duration'] == 1 && $data['timestart'] > $data['timedurationuntil']) {
 165              $errors['durationgroup'] = get_string('invalidtimedurationuntil', 'calendar');
 166          } else if ($data['duration'] == 2 && (trim($data['timedurationminutes']) == '' || $data['timedurationminutes'] < 1)) {
 167              $errors['durationgroup'] = get_string('invalidtimedurationminutes', 'calendar');
 168          }
 169  
 170          return $errors;
 171      }
 172  
 173      /**
 174       * Add the list of hidden elements that should appear in this form each
 175       * time. These elements will never be visible to the user.
 176       *
 177       * @param MoodleQuickForm $mform
 178       */
 179      protected function add_default_hidden_elements($mform) {
 180          global $USER;
 181  
 182          // Add some hidden fields.
 183          $mform->addElement('hidden', 'id');
 184          $mform->setType('id', PARAM_INT);
 185          $mform->setDefault('id', 0);
 186  
 187          $mform->addElement('hidden', 'userid');
 188          $mform->setType('userid', PARAM_INT);
 189          $mform->setDefault('userid', $USER->id);
 190  
 191          $mform->addElement('hidden', 'modulename');
 192          $mform->setType('modulename', PARAM_INT);
 193          $mform->setDefault('modulename', '');
 194  
 195          $mform->addElement('hidden', 'instance');
 196          $mform->setType('instance', PARAM_INT);
 197          $mform->setDefault('instance', 0);
 198  
 199          $mform->addElement('hidden', 'visible');
 200          $mform->setType('visible', PARAM_INT);
 201          $mform->setDefault('visible', 1);
 202      }
 203  
 204      /**
 205       * Add the various elements to express the duration options available
 206       * for an event.
 207       *
 208       * @param MoodleQuickForm $mform
 209       */
 210      protected function add_event_duration_elements($mform) {
 211          $group = [];
 212          $group[] = $mform->createElement('radio', 'duration', null, get_string('durationnone', 'calendar'), 0);
 213          $group[] = $mform->createElement('radio', 'duration', null, get_string('durationuntil', 'calendar'), 1);
 214          $group[] = $mform->createElement('date_time_selector', 'timedurationuntil', '');
 215          $group[] = $mform->createElement('radio', 'duration', null, get_string('durationminutes', 'calendar'), 2);
 216          $group[] = $mform->createElement('text', 'timedurationminutes', get_string('durationminutes', 'calendar'));
 217  
 218          $mform->addGroup($group, 'durationgroup', get_string('eventduration', 'calendar'), '<br />', false);
 219          $mform->setAdvanced('durationgroup');
 220  
 221          $mform->disabledIf('timedurationuntil',         'duration', 'noteq', 1);
 222          $mform->disabledIf('timedurationuntil[day]',    'duration', 'noteq', 1);
 223          $mform->disabledIf('timedurationuntil[month]',  'duration', 'noteq', 1);
 224          $mform->disabledIf('timedurationuntil[year]',   'duration', 'noteq', 1);
 225          $mform->disabledIf('timedurationuntil[hour]',   'duration', 'noteq', 1);
 226          $mform->disabledIf('timedurationuntil[minute]', 'duration', 'noteq', 1);
 227  
 228          $mform->setType('timedurationminutes', PARAM_INT);
 229          $mform->disabledIf('timedurationminutes', 'duration', 'noteq', 2);
 230  
 231          $mform->setDefault('duration', 0);
 232      }
 233  
 234      /**
 235       * Add the repeat elements for the form when creating a new event.
 236       *
 237       * @param MoodleQuickForm $mform
 238       */
 239      protected function add_event_repeat_elements($mform) {
 240          $mform->addElement('checkbox', 'repeat', get_string('repeatevent', 'calendar'), null);
 241          $mform->addElement('text', 'repeats', get_string('repeatweeksl', 'calendar'), 'maxlength="10" size="10"');
 242          $mform->setType('repeats', PARAM_INT);
 243          $mform->setDefault('repeats', 1);
 244          $mform->disabledIf('repeats', 'repeat', 'notchecked');
 245          $mform->setAdvanced('repeat');
 246          $mform->setAdvanced('repeats');
 247      }
 248  }