Implement data privacy provider.
[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 defined('MOODLE_INTERNAL') || die();
26
27 require_once($CFG->libdir.'/formslib.php');
28
29 /**
30 * class for displaying add form.
31 *
32 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35 class mod_attendance_add_form extends moodleform {
36
37 /**
38 * Called to define this moodle form
39 *
40 * @return void
41 */
42 public function definition() {
43
44 global $CFG, $USER;
45 $mform =& $this->_form;
46
47 $course = $this->_customdata['course'];
48 $cm = $this->_customdata['cm'];
49 $modcontext = $this->_customdata['modcontext'];
50
51 $pluginconfig = get_config('attendance');
52
53 $mform->addElement('header', 'general', get_string('addsession', 'attendance'));
54
55 $groupmode = groups_get_activity_groupmode($cm);
56 switch ($groupmode) {
57 case NOGROUPS:
58 $mform->addElement('static', 'sessiontypedescription', get_string('sessiontype', 'attendance'),
59 get_string('commonsession', 'attendance'));
60 $mform->addHelpButton('sessiontypedescription', 'sessiontype', 'attendance');
61 $mform->addElement('hidden', 'sessiontype', mod_attendance_structure::SESSION_COMMON);
62 $mform->setType('sessiontype', PARAM_INT);
63 break;
64 case SEPARATEGROUPS:
65 $mform->addElement('static', 'sessiontypedescription', get_string('sessiontype', 'attendance'),
66 get_string('groupsession', 'attendance'));
67 $mform->addHelpButton('sessiontypedescription', 'sessiontype', 'attendance');
68 $mform->addElement('hidden', 'sessiontype', mod_attendance_structure::SESSION_GROUP);
69 $mform->setType('sessiontype', PARAM_INT);
70 break;
71 case VISIBLEGROUPS:
72 $radio = array();
73 $radio[] = &$mform->createElement('radio', 'sessiontype', '', get_string('commonsession', 'attendance'),
74 mod_attendance_structure::SESSION_COMMON);
75 $radio[] = &$mform->createElement('radio', 'sessiontype', '', get_string('groupsession', 'attendance'),
76 mod_attendance_structure::SESSION_GROUP);
77 $mform->addGroup($radio, 'sessiontype', get_string('sessiontype', 'attendance'), ' ', false);
78 $mform->setType('sessiontype', PARAM_INT);
79 $mform->addHelpButton('sessiontype', 'sessiontype', 'attendance');
80 $mform->setDefault('sessiontype', mod_attendance_structure::SESSION_COMMON);
81 break;
82 }
83 if ($groupmode == SEPARATEGROUPS or $groupmode == VISIBLEGROUPS) {
84 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $modcontext)) {
85 $groups = groups_get_all_groups ($course->id, $USER->id, $cm->groupingid);
86 } else {
87 $groups = groups_get_all_groups($course->id, 0, $cm->groupingid);
88 }
89 if ($groups) {
90 $selectgroups = array();
91 foreach ($groups as $group) {
92 $selectgroups[$group->id] = $group->name;
93 }
94 $select = &$mform->addElement('select', 'groups', get_string('groups', 'group'), $selectgroups);
95 $select->setMultiple(true);
96 $mform->disabledIf('groups', 'sessiontype', 'eq', mod_attendance_structure::SESSION_COMMON);
97 } else {
98 if ($groupmode == VISIBLEGROUPS) {
99 $mform->updateElementAttr($radio, array('disabled' => 'disabled'));
100 }
101 $mform->addElement('static', 'groups', get_string('groups', 'group'),
102 get_string('nogroups', 'attendance'));
103 if ($groupmode == SEPARATEGROUPS) {
104 return;
105 }
106 }
107 }
108
109 attendance_form_sessiondate_selector($mform);
110
111 // Select which status set to use.
112 $maxstatusset = attendance_get_max_statusset($this->_customdata['att']->id);
113 if ($maxstatusset > 0) {
114 $opts = array();
115 for ($i = 0; $i <= $maxstatusset; $i++) {
116 $opts[$i] = attendance_get_setname($this->_customdata['att']->id, $i);
117 }
118 $mform->addElement('select', 'statusset', get_string('usestatusset', 'mod_attendance'), $opts);
119 } else {
120 $mform->addElement('hidden', 'statusset', 0);
121 $mform->setType('statusset', PARAM_INT);
122 }
123
124 $mform->addElement('editor', 'sdescription', get_string('description', 'attendance'), array('rows' => 1, 'columns' => 80),
125 array('maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean' => true, 'context' => $modcontext));
126 $mform->setType('sdescription', PARAM_RAW);
127
128 // If warnings allow selector for reporting.
129 if (!empty(get_config('attendance', 'enablewarnings'))) {
130 $mform->addElement('checkbox', 'absenteereport', '', get_string('includeabsentee', 'attendance'));
131 $mform->addHelpButton('absenteereport', 'includeabsentee', 'attendance');
132 if (isset($pluginconfig->absenteereport_default)) {
133 $mform->setDefault('absenteereport', $pluginconfig->absenteereport_default);
134 }
135 } else {
136 $mform->addElement('hidden', 'absenteereport', 1);
137 $mform->setType('absenteereport', PARAM_INT);
138 }
139 // For multiple sessions.
140 $mform->addElement('header', 'headeraddmultiplesessions', get_string('addmultiplesessions', 'attendance'));
141 if (!empty($pluginconfig->multisessionexpanded)) {
142 $mform->setExpanded('headeraddmultiplesessions');
143 }
144 $mform->addElement('checkbox', 'addmultiply', '', get_string('repeatasfollows', 'attendance'));
145 $mform->addHelpButton('addmultiply', 'createmultiplesessions', 'attendance');
146
147 $sdays = array();
148 if ($CFG->calendar_startwday === '0') { // Week start from sunday.
149 $sdays[] =& $mform->createElement('checkbox', 'Sun', '', get_string('sunday', 'calendar'));
150 }
151 $sdays[] =& $mform->createElement('checkbox', 'Mon', '', get_string('monday', 'calendar'));
152 $sdays[] =& $mform->createElement('checkbox', 'Tue', '', get_string('tuesday', 'calendar'));
153 $sdays[] =& $mform->createElement('checkbox', 'Wed', '', get_string('wednesday', 'calendar'));
154 $sdays[] =& $mform->createElement('checkbox', 'Thu', '', get_string('thursday', 'calendar'));
155 $sdays[] =& $mform->createElement('checkbox', 'Fri', '', get_string('friday', 'calendar'));
156 $sdays[] =& $mform->createElement('checkbox', 'Sat', '', get_string('saturday', 'calendar'));
157 if ($CFG->calendar_startwday !== '0') { // Week start from sunday.
158 $sdays[] =& $mform->createElement('checkbox', 'Sun', '', get_string('sunday', 'calendar'));
159 }
160 $mform->addGroup($sdays, 'sdays', get_string('repeaton', 'attendance'), array('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'), true);
161 $mform->disabledIf('sdays', 'addmultiply', 'notchecked');
162
163 $period = array(1 => 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
164 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36);
165 $periodgroup = array();
166 $periodgroup[] =& $mform->createElement('select', 'period', '', $period, false, true);
167 $periodgroup[] =& $mform->createElement('static', 'perioddesc', '', get_string('week', 'attendance'));
168 $mform->addGroup($periodgroup, 'periodgroup', get_string('repeatevery', 'attendance'), array(' '), false);
169 $mform->disabledIf('periodgroup', 'addmultiply', 'notchecked');
170
171 $mform->addElement('date_selector', 'sessionenddate', get_string('repeatuntil', 'attendance'));
172 $mform->disabledIf('sessionenddate', 'addmultiply', 'notchecked');
173
174 $mform->addElement('hidden', 'coursestartdate', $course->startdate);
175 $mform->setType('coursestartdate', PARAM_INT);
176
177 $mform->addElement('hidden', 'previoussessiondate', 0);
178 $mform->setType('previoussessiondate', PARAM_INT);
179
180 // Students can mark own attendance.
181 if (!empty(get_config('attendance', 'studentscanmark'))) {
182 $mform->addElement('header', 'headerstudentmarking', get_string('studentmarking', 'attendance'), true);
183 if (!empty($pluginconfig->studentrecordingexpanded)) {
184 $mform->setExpanded('headerstudentmarking');
185 }
186 $mform->addElement('checkbox', 'studentscanmark', '', get_string('studentscanmark', 'attendance'));
187 $mform->addHelpButton('studentscanmark', 'studentscanmark', 'attendance');
188
189 $options = attendance_get_automarkoptions();
190
191 $mform->addElement('select', 'automark', get_string('automark', 'attendance'), $options);
192 $mform->setType('automark', PARAM_INT);
193 $mform->addHelpButton('automark', 'automark', 'attendance');
194 $mform->hideif('automark', 'studentscanmark', 'notchecked');
195 $mform->setDefault('automark', $this->_customdata['att']->automark);
196
197 $mgroup = array();
198
199 $mgroup[] = & $mform->createElement('text', 'studentpassword', get_string('studentpassword', 'attendance'));
200 $mgroup[] = & $mform->createElement('checkbox', 'randompassword', '', get_string('randompassword', 'attendance'));
201 $mform->addGroup($mgroup, 'passwordgrp', get_string('passwordgrp', 'attendance'), array(' '), false);
202
203 $mform->setType('studentpassword', PARAM_TEXT);
204 $mform->addHelpButton('passwordgrp', 'passwordgrp', 'attendance');
205
206 $mform->hideif('passwordgrp', 'studentscanmark', 'notchecked');
207 $mform->hideif('studentpassword', 'randompassword', 'checked');
208 $mform->hideif('passwordgrp', 'automark', 'eq', ATTENDANCE_AUTOMARK_ALL);
209
210 $mform->addElement('checkbox', 'autoassignstatus', '', get_string('autoassignstatus', 'attendance'));
211 $mform->addHelpButton('autoassignstatus', 'autoassignstatus', 'attendance');
212 $mform->hideif('autoassignstatus', 'studentscanmark', 'notchecked');
213
214 if (isset($pluginconfig->autoassignstatus)) {
215 $mform->setDefault('autoassignstatus', $pluginconfig->autoassignstatus);
216 }
217 if (isset($pluginconfig->studentscanmark_default)) {
218 $mform->setDefault('studentscanmark', $pluginconfig->studentscanmark_default);
219 }
220 if (isset($pluginconfig->randompassword_default)) {
221 $mform->setDefault('randompassword', $pluginconfig->randompassword_default);
222 }
223 if (isset($pluginconfig->automark_default)) {
224 $mform->setDefault('automark', $pluginconfig->automark_default);
225 }
226 $mgroup2 = array();
227 $mgroup2[] = & $mform->createElement('text', 'subnet', get_string('requiresubnet', 'attendance'));
228 if (empty(get_config('attendance', 'subnetactivitylevel'))) {
229 $mform->setDefault('subnet', get_config('attendance', 'subnet'));
230 } else {
231 $mform->setDefault('subnet', $this->_customdata['att']->subnet);
232 }
233
234 $mgroup2[] = & $mform->createElement('checkbox', 'usedefaultsubnet', get_string('usedefaultsubnet', 'attendance'));
235 $mform->setDefault('usedefaultsubnet', 1);
236 $mform->setType('subnet', PARAM_TEXT);
237
238 $mform->addGroup($mgroup2, 'subnetgrp', get_string('requiresubnet', 'attendance'), array(' '), false);
239 $mform->setAdvanced('subnetgrp');
240 $mform->addHelpButton('subnetgrp', 'requiresubnet', 'attendance');
241
242 $mform->hideif('subnetgrp', 'studentscanmark', 'notchecked');
243 $mform->hideif('subnet', 'usedefaultsubnet', 'checked');
244
245 $mgroup3 = array();
246 $mgroup3[] = & $mform->createElement('checkbox', 'preventsharedip', '');
247 $mgroup3[] = & $mform->createElement('text', 'preventsharediptime',
248 get_string('preventsharediptime', 'attendance'), '', 'test');
249 $mgroup3[] = & $mform->createElement('static', 'preventsharediptimedesc', '',
250 get_string('preventsharedipminutes', 'attendance'));
251 $mform->addGroup($mgroup3, 'preventsharedgroup', get_string('preventsharedip', 'attendance'), array(' '), false);
252 $mform->addHelpButton('preventsharedgroup', 'preventsharedip', 'attendance');
253 $mform->setAdvanced('preventsharedgroup');
254 $mform->setType('preventsharediptime', PARAM_INT);
255 $mform->hideif('preventsharedgroup', 'studentscanmark', 'notchecked');
256 $mform->disabledIf('preventsharediptime', 'preventsharedip', 'notchecked');
257 if (isset($pluginconfig->preventsharedip)) {
258 $mform->setDefault('preventsharedip', $pluginconfig->preventsharedip);
259 }
260 if (isset($pluginconfig->preventsharediptime)) {
261 $mform->setDefault('preventsharediptime', $pluginconfig->preventsharediptime);
262 }
263
264 } else {
265 $mform->addElement('hidden', 'studentscanmark', '0');
266 $mform->settype('studentscanmark', PARAM_INT);
267 $mform->addElement('hidden', 'automark', '0');
268 $mform->setType('automark', PARAM_INT);
269 $mform->addElement('hidden', 'autoassignstatus', '0');
270 $mform->setType('autoassignstatus', PARAM_INT);
271
272 $mform->addElement('hidden', 'subnet', '');
273 $mform->setType('subnet', PARAM_TEXT);
274
275 $mform->addElement('hidden', 'preventsharedip', '0');
276 $mform->setType('preventsharedip', PARAM_INT);
277
278 $sharedtime = isset($pluginconfig->preventsharediptime) ? $pluginconfig->preventsharediptime : null;
279 $mform->addElement('hidden', 'preventsharediptime', $sharedtime);
280 $mform->setType('preventsharediptime', PARAM_INT);
281 }
282
283 $this->add_action_buttons(true, get_string('add', 'attendance'));
284 }
285
286 /**
287 * Perform minimal validation on the settings form
288 * @param array $data
289 * @param array $files
290 */
291 public function validation($data, $files) {
292 global $DB;
293 $errors = parent::validation($data, $files);
294
295 $sesstarttime = $data['sestime']['starthour'] * HOURSECS + $data['sestime']['startminute'] * MINSECS;
296 $sesendtime = $data['sestime']['endhour'] * HOURSECS + $data['sestime']['endminute'] * MINSECS;
297 if ($sesendtime < $sesstarttime) {
298 $errors['sestime'] = get_string('invalidsessionendtime', 'attendance');
299 }
300
301 if (!empty($data['addmultiply']) && $data['sessiondate'] != 0 && $data['sessionenddate'] != 0 &&
302 $data['sessionenddate'] < $data['sessiondate']) {
303 $errors['sessionenddate'] = get_string('invalidsessionenddate', 'attendance');
304 }
305
306 if ($data['sessiontype'] == mod_attendance_structure::SESSION_GROUP and empty($data['groups'])) {
307 $errors['groups'] = get_string('errorgroupsnotselected', 'attendance');
308 }
309
310 $addmulti = isset($data['addmultiply']) ? (int)$data['addmultiply'] : 0;
311 if (($addmulti != 0) && (!array_key_exists('sdays', $data) || empty($data['sdays']))) {
312 $data['sdays'] = array();
313 $errors['sdays'] = get_string('required', 'attendance');
314 }
315 if (isset($data['sdays'])) {
316 if (!$this->checkweekdays($data['sessiondate'], $data['sessionenddate'], $data['sdays']) ) {
317 $errors['sdays'] = get_string('checkweekdays', 'attendance');
318 }
319 }
320 if ($addmulti && ceil(($data['sessionenddate'] - $data['sessiondate']) / YEARSECS) > 1) {
321 $errors['sessionenddate'] = get_string('timeahead', 'attendance');
322 }
323
324 if ($data['sessiondate'] < $data['coursestartdate'] && $data['sessiondate'] != $data['previoussessiondate']) {
325 $errors['sessiondate'] = get_string('priorto', 'attendance',
326 userdate($data['coursestartdate'], get_string('strftimedmy', 'attendance')));
327 $this->_form->setConstant('previoussessiondate', $data['sessiondate']);
328 }
329
330 if ($data['automark'] == ATTENDANCE_AUTOMARK_CLOSE) {
331 $cm = $this->_customdata['cm'];
332 // Check that the selected statusset has a status to use when unmarked.
333 $sql = 'SELECT id
334 FROM {attendance_statuses}
335 WHERE deleted = 0 AND (attendanceid = 0 or attendanceid = ?)
336 AND setnumber = ? AND setunmarked = 1';
337 $params = array($cm->instance, $data['statusset']);
338 if (!$DB->record_exists_sql($sql, $params)) {
339 $errors['automark'] = get_string('noabsentstatusset', 'attendance');
340 }
341 }
342
343 if (!empty($data['studentscanmark']) && !empty($data['preventsharedip']) &&
344 empty($data['preventsharediptime'])) {
345 $errors['preventsharedgroup'] = get_string('iptimemissing', 'attendance');
346
347 }
348 return $errors;
349 }
350
351 /**
352 * Check weekdays function.
353 * @param int $sessiondate
354 * @param int $sessionenddate
355 * @param int $sdays
356 * @return bool
357 */
358 private function checkweekdays($sessiondate, $sessionenddate, $sdays) {
359
360 $found = false;
361
362 $daysofweek = array(0 => "Sun", 1 => "Mon", 2 => "Tue", 3 => "Wed", 4 => "Thu", 5 => "Fri", 6 => "Sat");
363 $start = new DateTime( date("Y-m-d", $sessiondate) );
364 $interval = new DateInterval('P1D');
365 $end = new DateTime( date("Y-m-d", $sessionenddate) );
366 $end->add( new DateInterval('P1D') );
367
368 $period = new DatePeriod($start, $interval, $end);
369 foreach ($period as $date) {
370 if (!$found) {
371 foreach ($sdays as $name => $value) {
372 $key = array_search($name, $daysofweek);
373 if ($date->format("w") == $key) {
374 $found = true;
375 break;
376 }
377 }
378 }
379 }
380
381 return $found;
382 }
383 }