bump version.
[moodle-mod_attendance.git] / add_form.php
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 * This file contains the forms to add
19 *
20 * @package mod_attendance
21 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25 require_once($CFG->libdir.'/formslib.php');
26
27 /**
28 * class for displaying add form.
29 *
30 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 */
33 class mod_attendance_add_form extends moodleform {
34
35 /**
36 * Called to define this moodle form
37 *
38 * @return void
39 */
40 public function definition() {
41
42 global $CFG, $USER;
43 $mform =& $this->_form;
44
45 $course = $this->_customdata['course'];
46 $cm = $this->_customdata['cm'];
47 $modcontext = $this->_customdata['modcontext'];
48
49 $mform->addElement('header', 'general', get_string('addsession', 'attendance'));
50
51 $groupmode = groups_get_activity_groupmode($cm);
52 switch ($groupmode) {
53 case NOGROUPS:
54 $mform->addElement('static', 'sessiontypedescription', get_string('sessiontype', 'attendance'),
55 get_string('commonsession', 'attendance'));
56 $mform->addHelpButton('sessiontypedescription', 'sessiontype', 'attendance');
57 $mform->addElement('hidden', 'sessiontype', mod_attendance_structure::SESSION_COMMON);
58 $mform->setType('sessiontype', PARAM_INT);
59 break;
60 case SEPARATEGROUPS:
61 $mform->addElement('static', 'sessiontypedescription', get_string('sessiontype', 'attendance'),
62 get_string('groupsession', 'attendance'));
63 $mform->addHelpButton('sessiontypedescription', 'sessiontype', 'attendance');
64 $mform->addElement('hidden', 'sessiontype', mod_attendance_structure::SESSION_GROUP);
65 $mform->setType('sessiontype', PARAM_INT);
66 break;
67 case VISIBLEGROUPS:
68 $radio = array();
69 $radio[] = &$mform->createElement('radio', 'sessiontype', '',
70 get_string('commonsession', 'attendance'), mod_attendance_structure::SESSION_COMMON);
71 $radio[] = &$mform->createElement('radio', 'sessiontype', '',
72 get_string('groupsession', 'attendance'), mod_attendance_structure::SESSION_GROUP);
73 $mform->addGroup($radio, 'sessiontype', get_string('sessiontype', 'attendance'), ' ', false);
74 $mform->setType('sessiontype', PARAM_INT);
75 $mform->addHelpButton('sessiontype', 'sessiontype', 'attendance');
76 $mform->setDefault('sessiontype', mod_attendance_structure::SESSION_COMMON);
77 break;
78 }
79 if ($groupmode == SEPARATEGROUPS or $groupmode == VISIBLEGROUPS) {
80 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $modcontext)) {
81 $groups = groups_get_all_groups ($course->id, $USER->id, $cm->groupingid);
82 } else {
83 $groups = groups_get_all_groups($course->id, 0, $cm->groupingid);
84 }
85 if ($groups) {
86 $selectgroups = array();
87 foreach ($groups as $group) {
88 $selectgroups[$group->id] = $group->name;
89 }
90 $select = &$mform->addElement('select', 'groups', get_string('groups', 'group'), $selectgroups);
91 $select->setMultiple(true);
92 $mform->disabledIf('groups', 'sessiontype', 'neq', mod_attendance_structure::SESSION_GROUP);
93 } else {
94 if ($groupmode == VISIBLEGROUPS) {
95 $mform->updateElementAttr($radio, array('disabled' => 'disabled'));
96 }
97 $mform->addElement('static', 'groups', get_string('groups', 'group'),
98 get_string('nogroups', 'attendance'));
99 if ($groupmode == SEPARATEGROUPS) {
100 return;
101 }
102 }
103 }
104
105 attendance_form_sessiondate_selector($mform);
106
107 // Select which status set to use.
108 $maxstatusset = attendance_get_max_statusset($this->_customdata['att']->id);
109 if ($maxstatusset > 0) {
110 $opts = array();
111 for ($i = 0; $i <= $maxstatusset; $i++) {
112 $opts[$i] = attendance_get_setname($this->_customdata['att']->id, $i);
113 }
114 $mform->addElement('select', 'statusset', get_string('usestatusset', 'mod_attendance'), $opts);
115 } else {
116 $mform->addElement('hidden', 'statusset', 0);
117 $mform->setType('statusset', PARAM_INT);
118 }
119
120 // Students can mark own attendance.
121 $configcanmark = get_config('attendance', 'studentscanmark');
122 if (!empty($configcanmark)) {
123 $mform->addElement('checkbox', 'studentscanmark', '', get_string('studentscanmark', 'attendance'));
124 $mform->addHelpButton('studentscanmark', 'studentscanmark', 'attendance');
125 } else {
126 $mform->addElement('hidden', 'studentscanmark', '0');
127 $mform->settype('studentscanmark', PARAM_INT);
128 }
129
130 $mform->addElement('editor', 'sdescription', get_string('description', 'attendance'), array('rows' => 1, 'columns' => 80),
131 array('maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean' => true, 'context' => $modcontext));
132 $mform->setType('sdescription', PARAM_RAW);
133
134 // For multiple sessions.
135
136 $mform->addElement('header', 'headeraddmultiplesessions', get_string('addmultiplesessions', 'attendance'));
137
138 $mform->addElement('checkbox', 'addmultiply', '', get_string('repeatasfollows', 'attendance'));
139 $mform->addHelpButton('addmultiply', 'createmultiplesessions', 'attendance');
140
141 $sdays = array();
142 if ($CFG->calendar_startwday === '0') { // Week start from sunday.
143 $sdays[] =& $mform->createElement('checkbox', 'Sun', '', get_string('sunday', 'calendar'));
144 }
145 $sdays[] =& $mform->createElement('checkbox', 'Mon', '', get_string('monday', 'calendar'));
146 $sdays[] =& $mform->createElement('checkbox', 'Tue', '', get_string('tuesday', 'calendar'));
147 $sdays[] =& $mform->createElement('checkbox', 'Wed', '', get_string('wednesday', 'calendar'));
148 $sdays[] =& $mform->createElement('checkbox', 'Thu', '', get_string('thursday', 'calendar'));
149 $sdays[] =& $mform->createElement('checkbox', 'Fri', '', get_string('friday', 'calendar'));
150 $sdays[] =& $mform->createElement('checkbox', 'Sat', '', get_string('saturday', 'calendar'));
151 if ($CFG->calendar_startwday !== '0') { // Week start from sunday.
152 $sdays[] =& $mform->createElement('checkbox', 'Sun', '', get_string('sunday', 'calendar'));
153 }
154 $mform->addGroup($sdays, 'sdays', get_string('repeaton', 'attendance'), array('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'), true);
155 $mform->disabledIf('sdays', 'addmultiply', 'notchecked');
156
157 $period = array(1 => 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
158 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36);
159 $periodgroup = array();
160 $periodgroup[] =& $mform->createElement('select', 'period', '', $period, false, true);
161 $periodgroup[] =& $mform->createElement('static', 'perioddesc', '', get_string('week', 'attendance'));
162 $mform->addGroup($periodgroup, 'periodgroup', get_string('repeatevery', 'attendance'), array(' '), false);
163 $mform->disabledIf('periodgroup', 'addmultiply', 'notchecked');
164
165 $mform->addElement('date_selector', 'sessionenddate', get_string('repeatuntil', 'attendance'));
166 $mform->disabledIf('sessionenddate', 'addmultiply', 'notchecked');
167
168 $mform->addElement('hidden', 'coursestartdate', $course->startdate);
169 $mform->setType('coursestartdate', PARAM_INT);
170
171 $mform->addElement('hidden', 'previoussessiondate', 0);
172 $mform->setType('previoussessiondate', PARAM_INT);
173
174 $this->add_action_buttons(true, get_string('add', 'attendance'));
175 }
176
177 /**
178 * Perform minimal validation on the settings form
179 * @param array $data
180 * @param array $files
181 */
182 public function validation($data, $files) {
183 $errors = parent::validation($data, $files);
184
185 $sesstarttime = $data['sestime']['starthour'] * HOURSECS + $data['sestime']['startminute'] * MINSECS;
186 $sesendtime = $data['sestime']['endhour'] * HOURSECS + $data['sestime']['endminute'] * MINSECS;
187 if ($sesendtime < $sesstarttime) {
188 $errors['sestime'] = get_string('invalidsessionendtime', 'attendance');
189 }
190
191 if (!empty($data['addmultiply']) && $data['sessiondate'] != 0 && $data['sessionenddate'] != 0 &&
192 $data['sessionenddate'] < $data['sessiondate']) {
193 $errors['sessionenddate'] = get_string('invalidsessionenddate', 'attendance');
194 }
195
196 if ($data['sessiontype'] == mod_attendance_structure::SESSION_GROUP and empty($data['groups'])) {
197 $errors['groups'] = get_string('errorgroupsnotselected', 'attendance');
198 }
199
200 $addmulti = isset($data['addmultiply']) ? (int)$data['addmultiply'] : 0;
201 if (($addmulti != 0) && (!array_key_exists('sdays', $data) || empty($data['sdays']))) {
202 $data['sdays'] = array();
203 $errors['sdays'] = get_string('required', 'attendance');
204 }
205 if (isset($data['sdays'])) {
206 if (!$this->checkweekdays($data['sessiondate'], $data['sessionenddate'], $data['sdays']) ) {
207 $errors['sdays'] = get_string('checkweekdays', 'attendance');
208 }
209 }
210 if ($addmulti && ceil(($data['sessionenddate'] - $data['sessiondate']) / YEARSECS) > 1) {
211 $errors['sessionenddate'] = get_string('timeahead', 'attendance');
212 }
213
214 if ($data['sessiondate'] < $data['coursestartdate'] && $data['sessiondate'] != $data['previoussessiondate']) {
215 $errors['sessiondate'] = get_string('priorto', 'attendance',
216 userdate($data['coursestartdate'], get_string('strftimedmy', 'attendance')));
217 $this->_form->setConstant('previoussessiondate', $data['sessiondate']);
218 }
219
220 return $errors;
221 }
222
223 private function checkweekdays($sessiondate, $sessionenddate, $sdays) {
224
225 $found = false;
226
227 $daysofweek = array(0 => "Sun", 1 => "Mon", 2 => "Tue", 3 => "Wed", 4 => "Thu", 5 => "Fri", 6 => "Sat");
228 $start = new DateTime( date("Y-m-d", $sessiondate) );
229 $interval = new DateInterval('P1D');
230 $end = new DateTime( date("Y-m-d", $sessionenddate) );
231 $end->add( new DateInterval('P1D') );
232
233 $period = new DatePeriod($start, $interval, $end);
234 foreach ($period as $date) {
235 if (!$found) {
236 foreach ($sdays as $name => $value) {
237 $key = array_search($name, $daysofweek);
238 if ($date->format("w") == $key) {
239 $found = true;
240 break;
241 }
242 }
243 }
244 }
245
246 return $found;
247 }
248 }