PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/calendar/event_form.php

https://bitbucket.org/ngmares/moodle
PHP | 184 lines | 106 code | 36 blank | 42 comment | 28 complexity | 29f1e41ed8a610a72cc55fd0c8be93d2 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0, 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. if ($newevent) {
  47. $eventtypes = $this->_customdata->eventtypes;
  48. $options = array();
  49. if (!empty($eventtypes->user)) {
  50. $options['user'] = get_string('user');
  51. }
  52. if (!empty($eventtypes->groups) && is_array($eventtypes->groups)) {
  53. $options['group'] = get_string('group');
  54. }
  55. if (!empty($eventtypes->courses)) {
  56. $options['course'] = get_string('course');
  57. }
  58. if (!empty($eventtypes->site)) {
  59. $options['site'] = get_string('site');
  60. }
  61. $mform->addElement('select', 'eventtype', get_string('eventkind', 'calendar'), $options);
  62. $mform->addRule('eventtype', get_string('required'), 'required');
  63. if (!empty($eventtypes->groups) && is_array($eventtypes->groups)) {
  64. $groupoptions = array();
  65. foreach ($eventtypes->groups as $group) {
  66. $groupoptions[$group->id] = $group->name;
  67. }
  68. $mform->addElement('select', 'groupid', get_string('typegroup', 'calendar'), $groupoptions);
  69. $mform->disabledIf('groupid', 'eventtype', 'noteq', 'group');
  70. }
  71. }
  72. // Add some hidden fields
  73. $mform->addElement('hidden', 'id');
  74. $mform->setType('id', PARAM_INT);
  75. $mform->setDefault('id', 0);
  76. $mform->addElement('hidden', 'courseid');
  77. $mform->setType('courseid', PARAM_INT);
  78. $mform->addElement('hidden', 'userid');
  79. $mform->setType('userid', PARAM_INT);
  80. $mform->setDefault('userid', $USER->id);
  81. $mform->addElement('hidden', 'modulename');
  82. $mform->setType('modulename', PARAM_INT);
  83. $mform->setDefault('modulename', '');
  84. $mform->addElement('hidden', 'instance');
  85. $mform->setType('instance', PARAM_INT);
  86. $mform->setDefault('instance', 0);
  87. $mform->addElement('hidden', 'action');
  88. $mform->setType('action', PARAM_INT);
  89. // Normal fields
  90. $mform->addElement('text', 'name', get_string('eventname','calendar'), 'size="50"');
  91. $mform->addRule('name', get_string('required'), 'required');
  92. $mform->setType('name', PARAM_TEXT);
  93. $mform->addElement('editor', 'description', get_string('eventdescription','calendar'), null, $this->_customdata->event->editoroptions);
  94. $mform->setType('description', PARAM_RAW);
  95. $mform->addElement('date_time_selector', 'timestart', get_string('date'));
  96. $mform->addRule('timestart', get_string('required'), 'required');
  97. $mform->addElement('radio', 'duration', get_string('eventduration', 'calendar'), get_string('durationnone', 'calendar'), 0);
  98. $mform->addElement('radio', 'duration', null, get_string('durationuntil', 'calendar'), 1);
  99. $mform->addElement('date_time_selector', 'timedurationuntil', '&nbsp;');
  100. $mform->disabledIf('timedurationuntil','duration','noteq', 1);
  101. $mform->addElement('radio', 'duration', null, get_string('durationminutes', 'calendar'), 2);
  102. $mform->addElement('text', 'timedurationminutes', null);
  103. $mform->setType('timedurationminutes', PARAM_INT);
  104. $mform->disabledIf('timedurationminutes','duration','noteq', 2);
  105. $mform->setDefault('duration', ($hasduration)?1:0);
  106. if ($newevent) {
  107. $mform->addElement('checkbox', 'repeat', get_string('repeatevent', 'calendar'), null, 'repeat');
  108. $mform->addElement('text', 'repeats', get_string('repeatweeksl', 'calendar'), 'maxlength="10" size="10"');
  109. $mform->setType('repeats', PARAM_INT);
  110. $mform->setDefault('repeats', 1);
  111. $mform->disabledIf('repeats','repeat','notchecked');
  112. } else if ($repeatedevents) {
  113. $mform->addElement('hidden', 'repeatid');
  114. $mform->setType('repeatid', PARAM_INT);
  115. $mform->addElement('header', 'repeatedevents', get_string('repeatedevents', 'calendar'));
  116. $mform->addElement('radio', 'repeateditall', null, get_string('repeateditall', 'calendar', $this->_customdata->event->eventrepeats), 1);
  117. $mform->addElement('radio', 'repeateditall', null, get_string('repeateditthis', 'calendar'), 0);
  118. $mform->setDefault('repeateditall', 1);
  119. }
  120. $this->add_action_buttons(false, get_string('savechanges'));
  121. }
  122. /**
  123. * A bit of custom validation for this form
  124. *
  125. * @param array $data An assoc array of field=>value
  126. * @param array $files An array of files
  127. * @return array
  128. */
  129. function validation($data, $files) {
  130. global $DB, $CFG;
  131. $errors = parent::validation($data, $files);
  132. if ($data['courseid'] > 0) {
  133. if ($course = $DB->get_record('course', array('id'=>$data['courseid']))) {
  134. if ($data['timestart'] < $course->startdate) {
  135. $errors['timestart'] = get_string('errorbeforecoursestart', 'calendar');
  136. }
  137. } else {
  138. $errors['courseid'] = get_string('invalidcourse', 'error');
  139. }
  140. }
  141. if ($data['duration'] == 1 && $data['timestart'] > $data['timedurationuntil']) {
  142. $errors['timedurationuntil'] = get_string('invalidtimedurationuntil', 'calendar');
  143. } else if ($data['duration'] == 2 && (trim($data['timedurationminutes']) == '' || $data['timedurationminutes'] < 1)) {
  144. $errors['timedurationminutes'] = get_string('invalidtimedurationminutes', 'calendar');
  145. }
  146. return $errors;
  147. }
  148. }