weekly release 2.3dev
[moodle.git] / calendar / event_form.php
CommitLineData
76d9df3f
SH
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 */
bfebaf64
MD
29if (!defined('MOODLE_INTERNAL')) {
30 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
31}
32
76d9df3f
SH
33require_once($CFG->dirroot.'/lib/formslib.php');
34
35/**
36 * The mform class for creating and editing a calendar
aa6c1ced 37 *
76d9df3f
SH
38 * @copyright 2009 Sam Hemelryk
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41class 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
52 if ($newevent) {
53 $eventtypes = $this->_customdata->eventtypes;
54 $options = array();
55 if (!empty($eventtypes->user)) {
56 $options['user'] = get_string('user');
57 }
58 if (!empty($eventtypes->groups) && is_array($eventtypes->groups)) {
59 $options['group'] = get_string('group');
60 }
61 if (!empty($eventtypes->courses)) {
62 $options['course'] = get_string('course');
63 }
64 if (!empty($eventtypes->site)) {
65 $options['site'] = get_string('site');
66 }
67
68 $mform->addElement('select', 'eventtype', get_string('eventkind', 'calendar'), $options);
69 $mform->addRule('eventtype', get_string('required'), 'required');
70
71 if (!empty($eventtypes->groups) && is_array($eventtypes->groups)) {
72 $groupoptions = array();
73 foreach ($eventtypes->groups as $group) {
74 $groupoptions[$group->id] = $group->name;
75 }
76 $mform->addElement('select', 'groupid', get_string('typegroup', 'calendar'), $groupoptions);
77 $mform->disabledIf('groupid', 'eventtype', 'noteq', 'group');
78 }
79 }
80
81 // Add some hidden fields
82 $mform->addElement('hidden', 'id');
83 $mform->setType('id', PARAM_INT);
84 $mform->setDefault('id', 0);
85
86 $mform->addElement('hidden', 'courseid');
87 $mform->setType('courseid', PARAM_INT);
88
89 $mform->addElement('hidden', 'userid');
90 $mform->setType('userid', PARAM_INT);
91 $mform->setDefault('userid', $USER->id);
92
93 $mform->addElement('hidden', 'modulename');
94 $mform->setType('modulename', PARAM_INT);
95 $mform->setDefault('modulename', '');
96
97 $mform->addElement('hidden', 'instance');
98 $mform->setType('instance', PARAM_INT);
99 $mform->setDefault('instance', 0);
100
101 $mform->addElement('hidden', 'action');
102 $mform->setType('action', PARAM_INT);
103
104 // Normal fields
105 $mform->addElement('text', 'name', get_string('eventname','calendar'), 'size="50"');
106 $mform->addRule('name', get_string('required'), 'required');
107 $mform->setType('name', PARAM_TEXT);
108
109 $mform->addElement('editor', 'description', get_string('eventdescription','calendar'), null, $this->_customdata->event->editoroptions);
110 $mform->setType('description', PARAM_RAW);
111
112 $mform->addElement('date_time_selector', 'timestart', get_string('date'));
113 $mform->addRule('timestart', get_string('required'), 'required');
114
115 $mform->addElement('radio', 'duration', get_string('eventduration', 'calendar'), get_string('durationnone', 'calendar'), 0);
116
117 $mform->addElement('radio', 'duration', null, get_string('durationuntil', 'calendar'), 1);
36dc3b71 118 $mform->addElement('date_time_selector', 'timedurationuntil', '&nbsp;');
76d9df3f
SH
119 $mform->disabledIf('timedurationuntil','duration','noteq', 1);
120
121 $mform->addElement('radio', 'duration', null, get_string('durationminutes', 'calendar'), 2);
122 $mform->addElement('text', 'timedurationminutes', null);
123 $mform->setType('timedurationminutes', PARAM_INT);
124 $mform->disabledIf('timedurationminutes','duration','noteq', 2);
125
126 $mform->setDefault('duration', ($hasduration)?1:0);
127
128 if ($newevent) {
129
130 $mform->addElement('checkbox', 'repeat', get_string('repeatevent', 'calendar'), null, 'repeat');
131 $mform->addElement('text', 'repeats', get_string('repeatweeksl', 'calendar'), 'maxlength="10" size="10"');
132 $mform->setType('repeats', PARAM_INT);
133 $mform->setDefault('repeats', 1);
134 $mform->disabledIf('repeats','repeat','notchecked');
135
136 } else if ($repeatedevents > 0) {
137
138 $mform->addElement('hidden', 'repeatid');
139 $mform->setType('repeatid', PARAM_INT);
140
141 $mform->addElement('header', 'repeatedevents', get_string('repeatedevents', 'calendar'));
142 $mform->addElement('checkbox', 'repeateditall', null, get_string('repeateditall', 'calendar', $repeatedevents), 'repeat');
143 $mform->setDefault('repeateditall', 'checked');
144
145 }
146
147 $this->add_action_buttons(false, get_string('savechanges'));
148 }
149
150 /**
151 * A bit of custom validation for this form
152 *
153 * @param array $data An assoc array of field=>value
154 * @param array $files An array of files
155 * @return array
156 */
157 function validation($data, $files) {
158 global $DB, $CFG;
159
160 $errors = parent::validation($data, $files);
161
162 if ($data['courseid'] > 0) {
163 if ($course = $DB->get_record('course', array('id'=>$data['courseid']))) {
164 if ($data['timestart'] < $course->startdate) {
165 $errors['timestart'] = get_string('errorbeforecoursestart', 'calendar');
166 }
167 } else {
168 $errors['courseid'] = get_string('invalidcourse', 'error');
169 }
170
171 }
172
173 if ($data['duration'] == 1 && $data['timestart'] > $data['timedurationuntil']) {
174 $errors['timedurationuntil'] = get_string('invalidtimedurationuntil', 'calendar');
175 } else if ($data['duration'] == 2 && (trim($data['timedurationminutes']) == '' || $data['timedurationminutes'] < 1)) {
176 $errors['timedurationminutes'] = get_string('invalidtimedurationminutes', 'calendar');
177 }
178
179 return $errors;
180 }
181
bfebaf64 182}