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.
   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * The mform for creating and editing a calendar event
  20   *
  21   * @copyright 2009 Sam Hemelryk
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   * @package calendar
  24   */
  25  
  26   /**
  27    * Always include formslib
  28    */
  29  if (!defined('MOODLE_INTERNAL')) {
  30      die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
  31  }
  32  
  33  require_once($CFG->dirroot.'/lib/formslib.php');
  34  
  35  /**
  36   * The mform class for creating and editing a calendar
  37   *
  38   * @copyright 2009 Sam Hemelryk
  39   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class event_form extends moodleform {
  42      /**
  43       * The form definition
  44       */
  45      function definition () {
  46          global $CFG, $USER, $OUTPUT;
  47          $mform = $this->_form;
  48          $newevent = (empty($this->_customdata->event) || empty($this->_customdata->event->id));
  49          $repeatedevents = (!empty($this->_customdata->event->eventrepeats) && $this->_customdata->event->eventrepeats>0);
  50          $hasduration = (!empty($this->_customdata->hasduration) && $this->_customdata->hasduration);
  51          $mform->addElement('header', 'general', get_string('general'));
  52  
  53          if ($newevent) {
  54              $eventtypes = $this->_customdata->eventtypes;
  55              $options = array();
  56              if (!empty($eventtypes->user)) {
  57                  $options['user'] = get_string('user');
  58              }
  59              if (!empty($eventtypes->groups) && is_array($eventtypes->groups)) {
  60                  $options['group'] = get_string('group');
  61              }
  62              if (!empty($eventtypes->courses)) {
  63                  $options['course'] = get_string('course');
  64              }
  65              if (!empty($eventtypes->site)) {
  66                  $options['site'] = get_string('site');
  67              }
  68  
  69              $mform->addElement('select', 'eventtype', get_string('eventkind', 'calendar'), $options);
  70              $mform->addRule('eventtype', get_string('required'), 'required');
  71  
  72              if (!empty($eventtypes->groups) && is_array($eventtypes->groups)) {
  73                  $groupoptions = array();
  74                  foreach ($eventtypes->groups as $group) {
  75                      $groupoptions[$group->id] = format_string($group->name, true,
  76                          array('context' => context_course::instance($group->courseid)));
  77                  }
  78                  $mform->addElement('select', 'groupid', get_string('typegroup', 'calendar'), $groupoptions);
  79                  $mform->disabledIf('groupid', 'eventtype', 'noteq', 'group');
  80              }
  81          }
  82  
  83          // Add some hidden fields
  84          $mform->addElement('hidden', 'id');
  85          $mform->setType('id', PARAM_INT);
  86          $mform->setDefault('id', 0);
  87  
  88          $mform->addElement('hidden', 'courseid');
  89          $mform->setType('courseid', PARAM_INT);
  90  
  91          $mform->addElement('hidden', 'userid');
  92          $mform->setType('userid', PARAM_INT);
  93          $mform->setDefault('userid', $USER->id);
  94  
  95          $mform->addElement('hidden', 'modulename');
  96          $mform->setType('modulename', PARAM_INT);
  97          $mform->setDefault('modulename', '');
  98  
  99          $mform->addElement('hidden', 'instance');
 100          $mform->setType('instance', PARAM_INT);
 101          $mform->setDefault('instance', 0);
 102  
 103          $mform->addElement('hidden', 'action');
 104          $mform->setType('action', PARAM_INT);
 105  
 106          // Normal fields
 107          $mform->addElement('text', 'name', get_string('eventname','calendar'), 'size="50"');
 108          $mform->addRule('name', get_string('required'), 'required');
 109          $mform->setType('name', PARAM_TEXT);
 110  
 111          $mform->addElement('editor', 'description', get_string('eventdescription','calendar'), null, $this->_customdata->event->editoroptions);
 112          $mform->setType('description', PARAM_RAW);
 113  
 114          $mform->addElement('date_time_selector', 'timestart', get_string('date'));
 115          $mform->addRule('timestart', get_string('required'), 'required');
 116  
 117          $mform->addElement('header', 'durationdetails', get_string('eventduration', 'calendar'));
 118  
 119          $group = array();
 120          $group[] =& $mform->createElement('radio', 'duration', null, get_string('durationnone', 'calendar'), 0);
 121          $group[] =& $mform->createElement('radio', 'duration', null, get_string('durationuntil', 'calendar'), 1);
 122          $group[] =& $mform->createElement('date_time_selector', 'timedurationuntil', '');
 123          $group[] =& $mform->createElement('radio', 'duration', null, get_string('durationminutes', 'calendar'), 2);
 124          $group[] =& $mform->createElement('text', 'timedurationminutes', get_string('durationminutes', 'calendar'));
 125  
 126          $mform->addGroup($group, 'durationgroup', '', '<br />', false);
 127  
 128          $mform->disabledIf('timedurationuntil',         'duration', 'noteq', 1);
 129          $mform->disabledIf('timedurationuntil[day]',    'duration', 'noteq', 1);
 130          $mform->disabledIf('timedurationuntil[month]',  'duration', 'noteq', 1);
 131          $mform->disabledIf('timedurationuntil[year]',   'duration', 'noteq', 1);
 132          $mform->disabledIf('timedurationuntil[hour]',   'duration', 'noteq', 1);
 133          $mform->disabledIf('timedurationuntil[minute]', 'duration', 'noteq', 1);
 134  
 135          $mform->setType('timedurationminutes', PARAM_INT);
 136          $mform->disabledIf('timedurationminutes','duration','noteq', 2);
 137  
 138          $mform->setDefault('duration', ($hasduration)?1:0);
 139  
 140          if ($newevent) {
 141  
 142              $mform->addElement('header', 'repeatevents', get_string('repeatedevents', 'calendar'));
 143              $mform->addElement('checkbox', 'repeat', get_string('repeatevent', 'calendar'), null);
 144              $mform->addElement('text', 'repeats', get_string('repeatweeksl', 'calendar'), 'maxlength="10" size="10"');
 145              $mform->setType('repeats', PARAM_INT);
 146              $mform->setDefault('repeats', 1);
 147              $mform->disabledIf('repeats','repeat','notchecked');
 148  
 149          } else if ($repeatedevents) {
 150  
 151              $mform->addElement('hidden', 'repeatid');
 152              $mform->setType('repeatid', PARAM_INT);
 153  
 154              $mform->addElement('header', 'repeatedevents', get_string('repeatedevents', 'calendar'));
 155              $mform->addElement('radio', 'repeateditall', null, get_string('repeateditall', 'calendar', $this->_customdata->event->eventrepeats), 1);
 156              $mform->addElement('radio', 'repeateditall', null, get_string('repeateditthis', 'calendar'), 0);
 157  
 158              $mform->setDefault('repeateditall', 1);
 159  
 160          }
 161  
 162          $this->add_action_buttons(false, get_string('savechanges'));
 163      }
 164  
 165      /**
 166       * A bit of custom validation for this form
 167       *
 168       * @param array $data An assoc array of field=>value
 169       * @param array $files An array of files
 170       * @return array
 171       */
 172      function validation($data, $files) {
 173          global $DB, $CFG;
 174  
 175          $errors = parent::validation($data, $files);
 176  
 177          if ($data['courseid'] > 0) {
 178              if ($course = $DB->get_record('course', array('id'=>$data['courseid']))) {
 179                  if ($data['timestart'] < $course->startdate) {
 180                      $errors['timestart'] = get_string('errorbeforecoursestart', 'calendar');
 181                  }
 182              } else {
 183                  $errors['courseid'] = get_string('invalidcourse', 'error');
 184              }
 185  
 186          }
 187  
 188          if ($data['duration'] == 1 && $data['timestart'] > $data['timedurationuntil']) {
 189              $errors['timedurationuntil'] = get_string('invalidtimedurationuntil', 'calendar');
 190          } else if ($data['duration'] == 2 && (trim($data['timedurationminutes']) == '' || $data['timedurationminutes'] < 1)) {
 191              $errors['timedurationminutes'] = get_string('invalidtimedurationminutes', 'calendar');
 192          }
 193  
 194          return $errors;
 195      }
 196  
 197  }