Feature: Prevent students from sharing device while self-marking.
[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 'preventsharedip' => $sess->preventsharedip,
77 'preventsharediptime' => $sess->preventsharediptime);
78 if ($sess->subnet == $attendancesubnet) {
79 $data['usedefaultsubnet'] = 1;
80 } else {
81 $data['usedefaultsubnet'] = 0;
82 }
83
84 $mform->addElement('header', 'general', get_string('changesession', 'attendance'));
85
86 if ($sess->groupid == 0) {
87 $strtype = get_string('commonsession', 'attendance');
88 } else {
89 $groupname = $DB->get_field('groups', 'name', array('id' => $sess->groupid));
90 $strtype = get_string('group') . ': ' . $groupname;
91 }
92 $mform->addElement('static', 'sessiontypedescription', get_string('sessiontype', 'attendance'), $strtype);
93
94 $olddate = construct_session_full_date_time($sess->sessdate, $sess->duration);
95 $mform->addElement('static', 'olddate', get_string('olddate', 'attendance'), $olddate);
96
97 attendance_form_sessiondate_selector($mform);
98
99 // Show which status set is in use.
100 $maxstatusset = attendance_get_max_statusset($this->_customdata['att']->id);
101 if ($maxstatusset > 0) {
102 $mform->addElement('static', 'statusset', get_string('usestatusset', 'mod_attendance'),
103 attendance_get_setname($this->_customdata['att']->id, $sess->statusset));
104 } else {
105 $mform->addElement('hidden', 'statusset', $maxstatusset);
106 $mform->setType('statusset', PARAM_INT);
107 }
108
109 $mform->addElement('editor', 'sdescription', get_string('description', 'attendance'),
110 array('rows' => 1, 'columns' => 80), $defopts);
111 $mform->setType('sdescription', PARAM_RAW);
112
113 // If warnings allow selector for reporting.
114 if (!empty(get_config('attendance', 'enablewarnings'))) {
115 $mform->addElement('checkbox', 'absenteereport', '', get_string('includeabsentee', 'attendance'));
116 $mform->addHelpButton('absenteereport', 'includeabsentee', 'attendance');
117 }
118
119 // Students can mark own attendance.
120 if (!empty(get_config('attendance', 'studentscanmark'))) {
121 $mform->addElement('header', 'headerstudentmarking', get_string('studentmarking', 'attendance'), true);
122 $mform->setExpanded('headerstudentmarking');
123
124 $mform->addElement('checkbox', 'studentscanmark', '', get_string('studentscanmark', 'attendance'));
125 $mform->addHelpButton('studentscanmark', 'studentscanmark', 'attendance');
126
127 $options2 = attendance_get_automarkoptions();
128
129 $mform->addElement('select', 'automark', get_string('automark', 'attendance'), $options2);
130 $mform->setType('automark', PARAM_INT);
131 $mform->addHelpButton('automark', 'automark', 'attendance');
132 $mform->hideif('automark', 'studentscanmark', 'notchecked');
133
134 $mform->addElement('text', 'studentpassword', get_string('studentpassword', 'attendance'));
135 $mform->setType('studentpassword', PARAM_TEXT);
136 $mform->addHelpButton('studentpassword', 'passwordgrp', 'attendance');
137 $mform->hideif('studentpassword', 'studentscanmark', 'notchecked');
138 $mform->hideif('studentpassword', 'automark', 'eq', ATTENDANCE_AUTOMARK_ALL);
139 $mform->hideif('randompassword', 'automark', 'eq', ATTENDANCE_AUTOMARK_ALL);
140 $mform->addElement('checkbox', 'autoassignstatus', '', get_string('autoassignstatus', 'attendance'));
141 $mform->addHelpButton('autoassignstatus', 'autoassignstatus', 'attendance');
142 $mform->hideif('autoassignstatus', 'studentscanmark', 'notchecked');
143
144 $mgroup = array();
145 $mgroup[] = & $mform->createElement('text', 'subnet', get_string('requiresubnet', 'attendance'));
146 $mform->setDefault('subnet', $this->_customdata['att']->subnet);
147 $mgroup[] = & $mform->createElement('checkbox', 'usedefaultsubnet', get_string('usedefaultsubnet', 'attendance'));
148 $mform->setDefault('usedefaultsubnet', 1);
149 $mform->setType('subnet', PARAM_TEXT);
150
151 $mform->addGroup($mgroup, 'subnetgrp', get_string('requiresubnet', 'attendance'), array(' '), false);
152 $mform->setAdvanced('subnetgrp');
153 $mform->addHelpButton('subnetgrp', 'requiresubnet', 'attendance');
154
155 $mform->hideif('subnetgrp', 'studentscanmark', 'notchecked');
156 $mform->hideif('subnet', 'usedefaultsubnet', 'checked');
157
158 $mform->addElement('hidden', 'automarkcompleted', '0');
159 $mform->settype('automarkcompleted', PARAM_INT);
160
161 $mgroup3 = array();
162 $mgroup3[] = & $mform->createElement('checkbox', 'preventsharedip', '');
163 $mgroup3[] = & $mform->createElement('text', 'preventsharediptime',
164 get_string('preventsharediptime', 'attendance'), '', 'test');
165 $mgroup3[] = & $mform->createElement('static', 'preventsharediptimedesc', '',
166 get_string('preventsharedipminutes', 'attendance'));
167 $mform->addGroup($mgroup3, 'preventsharedgroup',
168 get_string('preventsharedip', 'attendance'), array(' '), false);
169 $mform->addHelpButton('preventsharedgroup', 'preventsharedip', 'attendance');
170 $mform->setAdvanced('preventsharedgroup');
171 $mform->setType('preventsharediptime', PARAM_INT);
172 $mform->hideif('preventsharedgroup', 'studentscanmark', 'notchecked');
173 $mform->disabledIf('preventsharediptime', 'preventsharedip', 'notchecked');
174 } else {
175 $mform->addElement('hidden', 'studentscanmark', '0');
176 $mform->settype('studentscanmark', PARAM_INT);
177 $mform->addElement('hidden', 'subnet', '0');
178 $mform->settype('subnet', PARAM_TEXT);
179 $mform->addElement('hidden', 'automark', '0');
180 $mform->settype('automark', PARAM_INT);
181 $mform->addElement('hidden', 'automarkcompleted', '0');
182 $mform->settype('automarkcompleted', PARAM_INT);
183 $mform->addElement('hidden', 'autoassignstatus', '0');
184 $mform->setType('autoassignstatus', PARAM_INT);
185 }
186
187 $mform->setDefaults($data);
188
189 $this->add_action_buttons(true);
190 }
191
192 /**
193 * Perform minimal validation on the settings form
194 * @param array $data
195 * @param array $files
196 */
197 public function validation($data, $files) {
198 global $DB;
199 $errors = parent::validation($data, $files);
200
201 $sesstarttime = $data['sestime']['starthour'] * HOURSECS + $data['sestime']['startminute'] * MINSECS;
202 $sesendtime = $data['sestime']['endhour'] * HOURSECS + $data['sestime']['endminute'] * MINSECS;
203 if ($sesendtime < $sesstarttime) {
204 $errors['sestime'] = get_string('invalidsessionendtime', 'attendance');
205 }
206
207 if ($data['automark'] == ATTENDANCE_AUTOMARK_CLOSE) {
208 $cm = $this->_customdata['cm'];
209 // Check that the selected statusset has a status to use when unmarked.
210 $sql = 'SELECT id
211 FROM {attendance_statuses}
212 WHERE deleted = 0 AND (attendanceid = 0 or attendanceid = ?)
213 AND setnumber = ? AND setunmarked = 1';
214 $params = array($cm->instance, $data['statusset']);
215 if (!$DB->record_exists_sql($sql, $params)) {
216 $errors['automark'] = get_string('noabsentstatusset', 'attendance');
217 }
218 }
219
220 if (!empty($data['studentscanmark']) && !empty($data['preventsharedip']) &&
221 empty($data['preventsharediptime'])) {
222 $errors['preventsharedgroup'] = get_string('iptimemissing', 'attendance');
223
224 }
225 return $errors;
226 }
227 }