PageRenderTime 48ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/calendar/event_form.php

http://github.com/moodle/moodle
PHP | 197 lines | 117 code | 38 blank | 42 comment | 28 complexity | 777695f2cd811af7076877615bcf43ae MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. * The mform for creating and editing a calendar event
  18. *
  19. * @copyright 2009 Sam Hemelryk
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package calendar
  22. */
  23. /**
  24. * Always include formslib
  25. */
  26. if (!defined('MOODLE_INTERNAL')) {
  27. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  28. }
  29. require_once($CFG->dirroot.'/lib/formslib.php');
  30. /**
  31. * The mform class for creating and editing a calendar
  32. *
  33. * @copyright 2009 Sam Hemelryk
  34. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35. */
  36. class event_form extends moodleform {
  37. /**
  38. * The form definition
  39. */
  40. function definition () {
  41. global $CFG, $USER, $OUTPUT;
  42. $mform = $this->_form;
  43. $newevent = (empty($this->_customdata->event) || empty($this->_customdata->event->id));
  44. $repeatedevents = (!empty($this->_customdata->event->eventrepeats) && $this->_customdata->event->eventrepeats>0);
  45. $hasduration = (!empty($this->_customdata->hasduration) && $this->_customdata->hasduration);
  46. $mform->addElement('header', 'general', get_string('general'));
  47. if ($newevent) {
  48. $eventtypes = $this->_customdata->eventtypes;
  49. $options = array();
  50. if (!empty($eventtypes->user)) {
  51. $options['user'] = get_string('user');
  52. }
  53. if (!empty($eventtypes->groups) && is_array($eventtypes->groups)) {
  54. $options['group'] = get_string('group');
  55. }
  56. if (!empty($eventtypes->courses)) {
  57. $options['course'] = get_string('course');
  58. }
  59. if (!empty($eventtypes->site)) {
  60. $options['site'] = get_string('site');
  61. }
  62. $mform->addElement('select', 'eventtype', get_string('eventkind', 'calendar'), $options);
  63. $mform->addRule('eventtype', get_string('required'), 'required');
  64. if (!empty($eventtypes->groups) && is_array($eventtypes->groups)) {
  65. $groupoptions = array();
  66. foreach ($eventtypes->groups as $group) {
  67. $groupoptions[$group->id] = format_string($group->name, true,
  68. array('context' => context_course::instance($group->courseid)));
  69. }
  70. $mform->addElement('select', 'groupid', get_string('typegroup', 'calendar'), $groupoptions);
  71. $mform->disabledIf('groupid', 'eventtype', 'noteq', 'group');
  72. }
  73. }
  74. // Add some hidden fields
  75. $mform->addElement('hidden', 'id');
  76. $mform->setType('id', PARAM_INT);
  77. $mform->setDefault('id', 0);
  78. $mform->addElement('hidden', 'courseid');
  79. $mform->setType('courseid', PARAM_INT);
  80. $mform->addElement('hidden', 'userid');
  81. $mform->setType('userid', PARAM_INT);
  82. $mform->setDefault('userid', $USER->id);
  83. $mform->addElement('hidden', 'modulename');
  84. $mform->setType('modulename', PARAM_INT);
  85. $mform->setDefault('modulename', '');
  86. $mform->addElement('hidden', 'instance');
  87. $mform->setType('instance', PARAM_INT);
  88. $mform->setDefault('instance', 0);
  89. $mform->addElement('hidden', 'action');
  90. $mform->setType('action', PARAM_INT);
  91. // Normal fields
  92. $mform->addElement('text', 'name', get_string('eventname','calendar'), 'size="50"');
  93. $mform->addRule('name', get_string('required'), 'required');
  94. $mform->setType('name', PARAM_TEXT);
  95. $mform->addElement('editor', 'description', get_string('eventdescription','calendar'), null, $this->_customdata->event->editoroptions);
  96. $mform->setType('description', PARAM_RAW);
  97. $mform->addElement('date_time_selector', 'timestart', get_string('date'));
  98. $mform->addRule('timestart', get_string('required'), 'required');
  99. $mform->addElement('header', 'durationdetails', get_string('eventduration', 'calendar'));
  100. $group = array();
  101. $group[] =& $mform->createElement('radio', 'duration', null, get_string('durationnone', 'calendar'), 0);
  102. $group[] =& $mform->createElement('radio', 'duration', null, get_string('durationuntil', 'calendar'), 1);
  103. $group[] =& $mform->createElement('date_time_selector', 'timedurationuntil', '');
  104. $group[] =& $mform->createElement('radio', 'duration', null, get_string('durationminutes', 'calendar'), 2);
  105. $group[] =& $mform->createElement('text', 'timedurationminutes', get_string('durationminutes', 'calendar'));
  106. $mform->addGroup($group, 'durationgroup', '', '<br />', false);
  107. $mform->disabledIf('timedurationuntil', 'duration', 'noteq', 1);
  108. $mform->disabledIf('timedurationuntil[day]', 'duration', 'noteq', 1);
  109. $mform->disabledIf('timedurationuntil[month]', 'duration', 'noteq', 1);
  110. $mform->disabledIf('timedurationuntil[year]', 'duration', 'noteq', 1);
  111. $mform->disabledIf('timedurationuntil[hour]', 'duration', 'noteq', 1);
  112. $mform->disabledIf('timedurationuntil[minute]', 'duration', 'noteq', 1);
  113. $mform->setType('timedurationminutes', PARAM_INT);
  114. $mform->disabledIf('timedurationminutes','duration','noteq', 2);
  115. $mform->setDefault('duration', ($hasduration)?1:0);
  116. if ($newevent) {
  117. $mform->addElement('header', 'repeatevents', get_string('repeatedevents', 'calendar'));
  118. $mform->addElement('checkbox', 'repeat', get_string('repeatevent', 'calendar'), null);
  119. $mform->addElement('text', 'repeats', get_string('repeatweeksl', 'calendar'), 'maxlength="10" size="10"');
  120. $mform->setType('repeats', PARAM_INT);
  121. $mform->setDefault('repeats', 1);
  122. $mform->disabledIf('repeats','repeat','notchecked');
  123. } else if ($repeatedevents) {
  124. $mform->addElement('hidden', 'repeatid');
  125. $mform->setType('repeatid', PARAM_INT);
  126. $mform->addElement('header', 'repeatedevents', get_string('repeatedevents', 'calendar'));
  127. $mform->addElement('radio', 'repeateditall', null, get_string('repeateditall', 'calendar', $this->_customdata->event->eventrepeats), 1);
  128. $mform->addElement('radio', 'repeateditall', null, get_string('repeateditthis', 'calendar'), 0);
  129. $mform->setDefault('repeateditall', 1);
  130. }
  131. $this->add_action_buttons(false, get_string('savechanges'));
  132. }
  133. /**
  134. * A bit of custom validation for this form
  135. *
  136. * @param array $data An assoc array of field=>value
  137. * @param array $files An array of files
  138. * @return array
  139. */
  140. function validation($data, $files) {
  141. global $DB, $CFG;
  142. $errors = parent::validation($data, $files);
  143. if ($data['courseid'] > 0) {
  144. if ($course = $DB->get_record('course', array('id'=>$data['courseid']))) {
  145. if ($data['timestart'] < $course->startdate) {
  146. $errors['timestart'] = get_string('errorbeforecoursestart', 'calendar');
  147. }
  148. } else {
  149. $errors['courseid'] = get_string('invalidcourse', 'error');
  150. }
  151. }
  152. if ($data['duration'] == 1 && $data['timestart'] > $data['timedurationuntil']) {
  153. $errors['timedurationuntil'] = get_string('invalidtimedurationuntil', 'calendar');
  154. } else if ($data['duration'] == 2 && (trim($data['timedurationminutes']) == '' || $data['timedurationminutes'] < 1)) {
  155. $errors['timedurationminutes'] = get_string('invalidtimedurationminutes', 'calendar');
  156. }
  157. return $errors;
  158. }
  159. }