Implement data privacy provider.
[moodle-mod_attendance.git] / student_attendance_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 * Student form class.
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 require_once($CFG->libdir.'/formslib.php');
27
28 /**
29 * Class mod_attendance_student_attendance_form
30 *
31 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34 class mod_attendance_student_attendance_form extends moodleform {
35 /**
36 * Called to define this moodle form
37 *
38 * @return void
39 */
40 public function definition() {
41 global $USER;
42
43 $mform =& $this->_form;
44
45 $attforsession = $this->_customdata['session'];
46 $attblock = $this->_customdata['attendance'];
47
48 $statuses = $attblock->get_statuses();
49 // Check if user has access to all statuses.
50 $disabledduetotime = false;
51 foreach ($statuses as $status) {
52 if ($status->studentavailability === '0') {
53 unset($statuses[$status->id]);
54 }
55 if (!empty($status->studentavailability) &&
56 time() > $attforsession->sessdate + ($status->studentavailability * 60)) {
57 unset($statuses[$status->id]);
58 $disabledduetotime = true;
59 }
60 }
61
62 $mform->addElement('hidden', 'sessid', null);
63 $mform->setType('sessid', PARAM_INT);
64 $mform->setConstant('sessid', $attforsession->id);
65
66 $mform->addElement('hidden', 'sesskey', null);
67 $mform->setType('sesskey', PARAM_INT);
68 $mform->setConstant('sesskey', sesskey());
69
70 // Set a title as the date and time of the session.
71 $sesstiontitle = userdate($attforsession->sessdate, get_string('strftimedate')).' '
72 .attendance_strftimehm($attforsession->sessdate);
73
74 $mform->addElement('header', 'session', $sesstiontitle);
75
76 // If a session description is set display it.
77 if (!empty($attforsession->description)) {
78 $mform->addElement('html', $attforsession->description);
79 }
80 if (!empty($attforsession->studentpassword)) {
81 $mform->addElement('text', 'studentpassword', get_string('password', 'attendance'));
82 $mform->setType('studentpassword', PARAM_TEXT);
83 $mform->addRule('studentpassword', get_string('passwordrequired', 'attendance'), 'required');
84 }
85 if (!$attforsession->autoassignstatus) {
86
87 // Create radio buttons for setting the attendance status.
88 $radioarray = array();
89 foreach ($statuses as $status) {
90 $name = html_writer::span($status->description, 'statusdesc');
91 $radioarray[] =& $mform->createElement('radio', 'status', '', $name, $status->id, array());
92 }
93 if ($disabledduetotime) {
94 $warning = html_writer::span(get_string('somedisabledstatus', 'attendance'), 'somedisabledstatus');
95 $radioarray[] =& $mform->createElement('static', '', '', $warning);
96 }
97 // Add the radio buttons as a control with the user's name in front.
98 $radiogroup = $mform->addGroup($radioarray, 'statusarray', $USER->firstname.' '.$USER->lastname.':', array(''), false);
99 $radiogroup->setAttributes(array('class' => 'statusgroup'));
100 $mform->addRule('statusarray', get_string('attendancenotset', 'attendance'), 'required', '', 'client', false, false);
101 }
102 $this->add_action_buttons();
103 }
104
105 /**
106 * Validate Form.
107 *
108 * @param array $data
109 * @param array $files
110 * @return array
111 */
112 public function validation($data, $files) {
113 $errors = array();
114 if (!($this->_customdata['session']->autoassignstatus)) {
115 // Check if this status is allowed to be set.
116 if (empty($data['status'])) {
117 $errors['statusarray'] = get_string('invalidstatus', 'attendance');
118 }
119 }
120
121 return $errors;
122 }
123 }