2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This file contains the forms to add
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
25 require_once($CFG->libdir
.'/formslib.php');
28 * class for displaying add form.
30 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class mod_attendance_add_form
extends moodleform
{
36 * Called to define this moodle form
40 public function definition() {
43 $mform =& $this->_form
;
45 $course = $this->_customdata
['course'];
46 $cm = $this->_customdata
['cm'];
47 $modcontext = $this->_customdata
['modcontext'];
49 $mform->addElement('header', 'general', get_string('addsession', 'attendance'));
51 $groupmode = groups_get_activity_groupmode($cm);
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
);
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
);
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
);
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
);
83 $groups = groups_get_all_groups($course->id
, 0, $cm->groupingid
);
86 $selectgroups = array();
87 foreach ($groups as $group) {
88 $selectgroups[$group->id
] = $group->name
;
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
);
94 if ($groupmode == VISIBLEGROUPS
) {
95 $mform->updateElementAttr($radio, array('disabled' => 'disabled'));
97 $mform->addElement('static', 'groups', get_string('groups', 'group'),
98 get_string('nogroups', 'attendance'));
99 if ($groupmode == SEPARATEGROUPS
) {
105 attendance_form_sessiondate_selector($mform);
107 // Select which status set to use.
108 $maxstatusset = attendance_get_max_statusset($this->_customdata
['att']->id
);
109 if ($maxstatusset > 0) {
111 for ($i = 0; $i <= $maxstatusset; $i++
) {
112 $opts[$i] = attendance_get_setname($this->_customdata
['att']->id
, $i);
114 $mform->addElement('select', 'statusset', get_string('usestatusset', 'mod_attendance'), $opts);
116 $mform->addElement('hidden', 'statusset', 0);
117 $mform->setType('statusset', PARAM_INT
);
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');
125 $mform->addElement('hidden', 'studentscanmark', '0');
126 $mform->settype('studentscanmark', PARAM_INT
);
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
);
133 // For multiple sessions.
135 $mform->addElement('header', 'headeraddmultiplesessions', get_string('addmultiplesessions', 'attendance'));
137 $mform->addElement('checkbox', 'addmultiply', '', get_string('repeatasfollows', 'attendance'));
138 $mform->addHelpButton('addmultiply', 'createmultiplesessions', 'attendance');
141 if ($CFG->calendar_startwday
=== '0') { // Week start from sunday.
142 $sdays[] =& $mform->createElement('checkbox', 'Sun', '', get_string('sunday', 'calendar'));
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'));
153 $mform->addGroup($sdays, 'sdays', get_string('repeaton', 'attendance'), array(' '), true
);
154 $mform->disabledIf('sdays', 'addmultiply', 'notchecked');
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');
164 $mform->addElement('date_selector', 'sessionenddate', get_string('repeatuntil', 'attendance'));
165 $mform->disabledIf('sessionenddate', 'addmultiply', 'notchecked');
167 $mform->addElement('hidden', 'coursestartdate', $course->startdate
);
168 $mform->setType('coursestartdate', PARAM_INT
);
170 $mform->addElement('hidden', 'previoussessiondate', 0);
171 $mform->setType('previoussessiondate', PARAM_INT
);
173 $this->add_action_buttons(true
, get_string('add', 'attendance'));
177 * Perform minimal validation on the settings form
179 * @param array $files
181 public function validation($data, $files) {
182 $errors = parent
::validation($data, $files);
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');
190 if (!empty($data['addmultiply']) && $data['sessiondate'] != 0 && $data['sessionenddate'] != 0 &&
191 $data['sessionenddate'] < $data['sessiondate']) {
192 $errors['sessionenddate'] = get_string('invalidsessionenddate', 'attendance');
195 if ($data['sessiontype'] == mod_attendance_structure
::SESSION_GROUP
and empty($data['groups'])) {
196 $errors['groups'] = get_string('errorgroupsnotselected', 'attendance');
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');
204 if (isset($data['sdays'])) {
205 if (!$this->checkweekdays($data['sessiondate'], $data['sessionenddate'], $data['sdays']) ) {
206 $errors['sdays'] = get_string('checkweekdays', 'attendance');
209 if ($addmulti && ceil(($data['sessionenddate'] - $data['sessiondate']) / YEARSECS
) > 1) {
210 $errors['sessionenddate'] = get_string('timeahead', 'attendance');
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']);
222 private function checkweekdays($sessiondate, $sessionenddate, $sdays) {
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') );
232 $period = new DatePeriod($start, $interval, $end);
233 foreach ($period as $date) {
235 foreach ($sdays as $name => $value) {
236 $key = array_search($name, $daysofweek);
237 if ($date->format("w") == $key) {