Fix issue when checking if a status set has an absent flag when updating a session.
[moodle-mod_attendance.git] / update_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 * Update form
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 for displaying update 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_update_form extends moodleform {
35
36 /**
37 * Called to define this moodle form
38 *
39 * @return void
40 */
41 public function definition() {
42
43 global $DB;
44 $mform =& $this->_form;
45
46 $modcontext = $this->_customdata['modcontext'];
47 $sessionid = $this->_customdata['sessionid'];
48
49 if (!$sess = $DB->get_record('attendance_sessions', array('id' => $sessionid) )) {
50 error('No such session in this course');
51 }
52 $attendancesubnet = $DB->get_field('attendance', 'subnet', array('id' => $sess->attendanceid));
53 $defopts = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean' => true, 'context' => $modcontext);
54 $sess = file_prepare_standard_editor($sess, 'description', $defopts, $modcontext, 'mod_attendance', 'session', $sess->id);
55
56 $starttime = $sess->sessdate - usergetmidnight($sess->sessdate);
57 $starthour = floor($starttime / HOURSECS);
58 $startminute = floor(($starttime - $starthour * HOURSECS) / MINSECS);
59
60 $enddate = $sess->sessdate + $sess->duration;
61 $endtime = $enddate - usergetmidnight($enddate);
62 $endhour = floor($endtime / HOURSECS);
63 $endminute = floor(($endtime - $endhour * HOURSECS) / MINSECS);
64
65 $data = array('sessiondate' => $sess->sessdate,
66 'sestime' => array('starthour' => $starthour, 'startminute' => $startminute,
67 'endhour' => $endhour, 'endminute' => $endminute),
68 'sdescription' => $sess->description_editor,
69 'studentscanmark' => $sess->studentscanmark,
70 'studentpassword' => $sess->studentpassword,
71 'autoassignstatus' => $sess->autoassignstatus,
72 'subnet' => $sess->subnet,
73 'automark' => $sess->automark,
74 'absenteereport' => $sess->absenteereport,
75 'automarkcompleted' => 0);
76 if ($sess->subnet == $attendancesubnet) {
77 $data['usedefaultsubnet'] = 1;
78 } else {
79 $data['usedefaultsubnet'] = 0;
80 }
81
82 $mform->addElement('header', 'general', get_string('changesession', 'attendance'));
83
84 if ($sess->groupid == 0) {
85 $strtype = get_string('commonsession', 'attendance');
86 } else {
87 $groupname = $DB->get_field('groups', 'name', array('id' => $sess->groupid));
88 $strtype = get_string('group') . ': ' . $groupname;
89 }
90 $mform->addElement('static', 'sessiontypedescription', get_string('sessiontype', 'attendance'), $strtype);
91
92 $olddate = construct_session_full_date_time($sess->sessdate, $sess->duration);
93 $mform->addElement('static', 'olddate', get_string('olddate', 'attendance'), $olddate);
94
95 attendance_form_sessiondate_selector($mform);
96
97 // Show which status set is in use.
98 $maxstatusset = attendance_get_max_statusset($this->_customdata['att']->id);
99 if ($maxstatusset > 0) {
100 $mform->addElement('static', 'statusset', get_string('usestatusset', 'mod_attendance'),
101 attendance_get_setname($this->_customdata['att']->id, $sess->statusset));
102 } else {
103 $mform->addElement('hidden', 'statusset', $maxstatusset);
104 $mform->setType('statusset', PARAM_INT);
105 }
106
107 $mform->addElement('editor', 'sdescription', get_string('description', 'attendance'),
108 array('rows' => 1, 'columns' => 80), $defopts);
109 $mform->setType('sdescription', PARAM_RAW);
110
111 // If warnings allow selector for reporting.
112 if (!empty(get_config('attendance', 'enablewarnings'))) {
113 $mform->addElement('checkbox', 'absenteereport', '', get_string('includeabsentee', 'attendance'));
114 $mform->addHelpButton('absenteereport', 'includeabsentee', 'attendance');
115 }
116
117 // Students can mark own attendance.
118 if (!empty(get_config('attendance', 'studentscanmark'))) {
119 $mform->addElement('header', 'headerstudentmarking', get_string('studentmarking', 'attendance'), true);
120 $mform->setExpanded('headerstudentmarking');
121
122 $mform->addElement('checkbox', 'studentscanmark', '', get_string('studentscanmark', 'attendance'));
123 $mform->addHelpButton('studentscanmark', 'studentscanmark', 'attendance');
124
125 $options2 = attendance_get_automarkoptions();
126
127 $mform->addElement('select', 'automark', get_string('automark', 'attendance'), $options2);
128 $mform->setType('automark', PARAM_INT);
129 $mform->addHelpButton('automark', 'automark', 'attendance');
130 $mform->hideif('automark', 'studentscanmark', 'notchecked');
131
132 $mform->addElement('text', 'studentpassword', get_string('studentpassword', 'attendance'));
133 $mform->setType('studentpassword', PARAM_TEXT);
134 $mform->addHelpButton('studentpassword', 'passwordgrp', 'attendance');
135 $mform->hideif('studentpassword', 'studentscanmark', 'notchecked');
136 $mform->hideif('studentpassword', 'automark', 'eq', ATTENDANCE_AUTOMARK_ALL);
137 $mform->hideif('randompassword', 'automark', 'eq', ATTENDANCE_AUTOMARK_ALL);
138 $mform->addElement('checkbox', 'autoassignstatus', '', get_string('autoassignstatus', 'attendance'));
139 $mform->addHelpButton('autoassignstatus', 'autoassignstatus', 'attendance');
140 $mform->hideif('autoassignstatus', 'studentscanmark', 'notchecked');
141
142 $mgroup = array();
143 $mgroup[] = & $mform->createElement('text', 'subnet', get_string('requiresubnet', 'attendance'));
144 $mform->setDefault('subnet', $this->_customdata['att']->subnet);
145 $mgroup[] = & $mform->createElement('checkbox', 'usedefaultsubnet', get_string('usedefaultsubnet', 'attendance'));
146 $mform->setDefault('usedefaultsubnet', 1);
147 $mform->setType('subnet', PARAM_TEXT);
148
149 $mform->addGroup($mgroup, 'subnetgrp', get_string('requiresubnet', 'attendance'), array(' '), false);
150 $mform->setAdvanced('subnetgrp');
151 $mform->addHelpButton('subnetgrp', 'requiresubnet', 'attendance');
152
153 $mform->hideif('subnetgrp', 'studentscanmark', 'notchecked');
154 $mform->hideif('subnet', 'usedefaultsubnet', 'checked');
155
156 $mform->addElement('hidden', 'automarkcompleted', '0');
157 $mform->settype('automarkcompleted', PARAM_INT);
158
159 } else {
160 $mform->addElement('hidden', 'studentscanmark', '0');
161 $mform->settype('studentscanmark', PARAM_INT);
162 $mform->addElement('hidden', 'subnet', '0');
163 $mform->settype('subnet', PARAM_TEXT);
164 $mform->addElement('hidden', 'automark', '0');
165 $mform->settype('automark', PARAM_INT);
166 $mform->addElement('hidden', 'automarkcompleted', '0');
167 $mform->settype('automarkcompleted', PARAM_INT);
168 $mform->addElement('hidden', 'autoassignstatus', '0');
169 $mform->setType('autoassignstatus', PARAM_INT);
170 }
171
172 $mform->setDefaults($data);
173
174 $this->add_action_buttons(true);
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 global $DB;
184 $errors = parent::validation($data, $files);
185
186 $sesstarttime = $data['sestime']['starthour'] * HOURSECS + $data['sestime']['startminute'] * MINSECS;
187 $sesendtime = $data['sestime']['endhour'] * HOURSECS + $data['sestime']['endminute'] * MINSECS;
188 if ($sesendtime < $sesstarttime) {
189 $errors['sestime'] = get_string('invalidsessionendtime', 'attendance');
190 }
191
192 if ($data['automark'] == ATTENDANCE_AUTOMARK_CLOSE) {
193 $cm = $this->_customdata['cm'];
194 // Check that the selected statusset has a status to use when unmarked.
195 $sql = 'SELECT id
196 FROM {attendance_statuses}
197 WHERE deleted = 0 AND (attendanceid = 0 or attendanceid = ?)
198 AND setnumber = ? AND setunmarked = 1';
199 $params = array($cm->instance, $data['statusset']);
200 if (!$DB->record_exists_sql($sql, $params)) {
201 $errors['automark'] = get_string('noabsentstatusset', 'attendance');
202 }
203 }
204
205 return $errors;
206 }
207 }