Fixes #223 allow student attendance to be disabled at site-level
[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 if (!empty(get_config('attendance', 'studentscanmark'))) {
122 $mform->addElement('checkbox', 'studentscanmark', '', get_string('studentscanmark', 'attendance'));
123 $mform->addHelpButton('studentscanmark', 'studentscanmark', 'attendance');
124 } else {
125 $mform->addElement('hidden', 'studentscanmark', '0');
126 $mform->settype('studentscanmark', PARAM_INT);
127 }
128
129 $mform->addElement('editor', 'sdescription', get_string('description', 'attendance'), array('rows' => 1, 'columns' => 80),
130 array('maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean' => true, 'context' => $modcontext));
131 $mform->setType('sdescription', PARAM_RAW);
132
133 // For multiple sessions.
134
135 $mform->addElement('header', 'headeraddmultiplesessions', get_string('addmultiplesessions', 'attendance'));
136
137 $mform->addElement('checkbox', 'addmultiply', '', get_string('repeatasfollows', 'attendance'));
138 $mform->addHelpButton('addmultiply', 'createmultiplesessions', 'attendance');
139
140 $sdays = array();
141 if ($CFG->calendar_startwday === '0') { // Week start from sunday.
142 $sdays[] =& $mform->createElement('checkbox', 'Sun', '', get_string('sunday', 'calendar'));
143 }
144 $sdays[] =& $mform->createElement('checkbox', 'Mon', '', get_string('monday', 'calendar'));
145 $sdays[] =& $mform->createElement('checkbox', 'Tue', '', get_string('tuesday', 'calendar'));
146 $sdays[] =& $mform->createElement('checkbox', 'Wed', '', get_string('wednesday', 'calendar'));
147 $sdays[] =& $mform->createElement('checkbox', 'Thu', '', get_string('thursday', 'calendar'));
148 $sdays[] =& $mform->createElement('checkbox', 'Fri', '', get_string('friday', 'calendar'));
149 $sdays[] =& $mform->createElement('checkbox', 'Sat', '', get_string('saturday', 'calendar'));
150 if ($CFG->calendar_startwday !== '0') { // Week start from sunday.
151 $sdays[] =& $mform->createElement('checkbox', 'Sun', '', get_string('sunday', 'calendar'));
152 }
153 $mform->addGroup($sdays, 'sdays', get_string('repeaton', 'attendance'), array('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'), true);
154 $mform->disabledIf('sdays', 'addmultiply', 'notchecked');
155
156 $period = array(1 => 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
157 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36);
158 $periodgroup = array();
159 $periodgroup[] =& $mform->createElement('select', 'period', '', $period, false, true);
160 $periodgroup[] =& $mform->createElement('static', 'perioddesc', '', get_string('week', 'attendance'));
161 $mform->addGroup($periodgroup, 'periodgroup', get_string('repeatevery', 'attendance'), array(' '), false);
162 $mform->disabledIf('periodgroup', 'addmultiply', 'notchecked');
163
164 $mform->addElement('date_selector', 'sessionenddate', get_string('repeatuntil', 'attendance'));
165 $mform->disabledIf('sessionenddate', 'addmultiply', 'notchecked');
166
167 $mform->addElement('hidden', 'coursestartdate', $course->startdate);
168 $mform->setType('coursestartdate', PARAM_INT);
169
170 $mform->addElement('hidden', 'previoussessiondate', 0);
171 $mform->setType('previoussessiondate', PARAM_INT);
172
173 $this->add_action_buttons(true, get_string('add', 'attendance'));
174 }
175
176 /**
177 * Perform minimal validation on the settings form
178 * @param array $data
179 * @param array $files
180 */
181 public function validation($data, $files) {
182 $errors = parent::validation($data, $files);
183
184 $sesstarttime = $data['sestime']['starthour'] * HOURSECS + $data['sestime']['startminute'] * MINSECS;
185 $sesendtime = $data['sestime']['endhour'] * HOURSECS + $data['sestime']['endminute'] * MINSECS;
186 if ($sesendtime < $sesstarttime) {
187 $errors['sestime'] = get_string('invalidsessionendtime', 'attendance');
188 }
189
190 if (!empty($data['addmultiply']) && $data['sessiondate'] != 0 && $data['sessionenddate'] != 0 &&
191 $data['sessionenddate'] < $data['sessiondate']) {
192 $errors['sessionenddate'] = get_string('invalidsessionenddate', 'attendance');
193 }
194
195 if ($data['sessiontype'] == mod_attendance_structure::SESSION_GROUP and empty($data['groups'])) {
196 $errors['groups'] = get_string('errorgroupsnotselected', 'attendance');
197 }
198
199 $addmulti = isset($data['addmultiply']) ? (int)$data['addmultiply'] : 0;
200 if (($addmulti != 0) && (!array_key_exists('sdays', $data) || empty($data['sdays']))) {
201 $data['sdays'] = array();
202 $errors['sdays'] = get_string('required', 'attendance');
203 }
204 if (isset($data['sdays'])) {
205 if (!$this->checkweekdays($data['sessiondate'], $data['sessionenddate'], $data['sdays']) ) {
206 $errors['sdays'] = get_string('checkweekdays', 'attendance');
207 }
208 }
209 if ($addmulti && ceil(($data['sessionenddate'] - $data['sessiondate']) / YEARSECS) > 1) {
210 $errors['sessionenddate'] = get_string('timeahead', 'attendance');
211 }
212
213 if ($data['sessiondate'] < $data['coursestartdate'] && $data['sessiondate'] != $data['previoussessiondate']) {
214 $errors['sessiondate'] = get_string('priorto', 'attendance',
215 userdate($data['coursestartdate'], get_string('strftimedmy', 'attendance')));
216 $this->_form->setConstant('previoussessiondate', $data['sessiondate']);
217 }
218
219 return $errors;
220 }
221
222 private function checkweekdays($sessiondate, $sessionenddate, $sdays) {
223
224 $found = false;
225
226 $daysofweek = array(0 => "Sun", 1 => "Mon", 2 => "Tue", 3 => "Wed", 4 => "Thu", 5 => "Fri", 6 => "Sat");
227 $start = new DateTime( date("Y-m-d", $sessiondate) );
228 $interval = new DateInterval('P1D');
229 $end = new DateTime( date("Y-m-d", $sessionenddate) );
230 $end->add( new DateInterval('P1D') );
231
232 $period = new DatePeriod($start, $interval, $end);
233 foreach ($period as $date) {
234 if (!$found) {
235 foreach ($sdays as $name => $value) {
236 $key = array_search($name, $daysofweek);
237 if ($date->format("w") == $key) {
238 $found = true;
239 break;
240 }
241 }
242 }
243 }
244
245 return $found;
246 }
247 }