|
| 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 <[email protected]> |
| 22 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 23 | + */ |
| 24 | + |
| 25 | +defined('MOODLE_INTERNAL') || die(); |
| 26 | + |
| 27 | +require_once($CFG->libdir.'/formslib.php'); |
| 28 | + |
| 29 | +/** |
| 30 | + * class for displaying add form. |
| 31 | + * |
| 32 | + * @copyright 2011 Artem Andreev <[email protected]> |
| 33 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 34 | + */ |
| 35 | +class mod_attendance_add_form extends moodleform { |
| 36 | + |
| 37 | + /** |
| 38 | + * Called to define this moodle form |
| 39 | + * |
| 40 | + * @return void |
| 41 | + */ |
| 42 | + public function definition() { |
| 43 | + |
| 44 | + global $CFG, $USER; |
| 45 | + $mform =& $this->_form; |
| 46 | + |
| 47 | + $course = $this->_customdata['course']; |
| 48 | + $cm = $this->_customdata['cm']; |
| 49 | + $modcontext = $this->_customdata['modcontext']; |
| 50 | + |
| 51 | + $mform->addElement('header', 'general', get_string('addsession', 'attendance')); |
| 52 | + |
| 53 | + $groupmode = groups_get_activity_groupmode($cm); |
| 54 | + switch ($groupmode) { |
| 55 | + case NOGROUPS: |
| 56 | + $mform->addElement('static', 'sessiontypedescription', get_string('sessiontype', 'attendance'), |
| 57 | + get_string('commonsession', 'attendance')); |
| 58 | + $mform->addHelpButton('sessiontypedescription', 'sessiontype', 'attendance'); |
| 59 | + $mform->addElement('hidden', 'sessiontype', mod_attendance_structure::SESSION_COMMON); |
| 60 | + $mform->setType('sessiontype', PARAM_INT); |
| 61 | + break; |
| 62 | + case SEPARATEGROUPS: |
| 63 | + $mform->addElement('static', 'sessiontypedescription', get_string('sessiontype', 'attendance'), |
| 64 | + get_string('groupsession', 'attendance')); |
| 65 | + $mform->addHelpButton('sessiontypedescription', 'sessiontype', 'attendance'); |
| 66 | + $mform->addElement('hidden', 'sessiontype', mod_attendance_structure::SESSION_GROUP); |
| 67 | + $mform->setType('sessiontype', PARAM_INT); |
| 68 | + break; |
| 69 | + case VISIBLEGROUPS: |
| 70 | + $radio = array(); |
| 71 | + $radio[] = &$mform->createElement('radio', 'sessiontype', '', get_string('commonsession', 'attendance'), |
| 72 | + mod_attendance_structure::SESSION_COMMON); |
| 73 | + $radio[] = &$mform->createElement('radio', 'sessiontype', '', get_string('groupsession', 'attendance'), |
| 74 | + mod_attendance_structure::SESSION_GROUP); |
| 75 | + $mform->addGroup($radio, 'sessiontype', get_string('sessiontype', 'attendance'), ' ', false); |
| 76 | + $mform->setType('sessiontype', PARAM_INT); |
| 77 | + $mform->addHelpButton('sessiontype', 'sessiontype', 'attendance'); |
| 78 | + $mform->setDefault('sessiontype', mod_attendance_structure::SESSION_COMMON); |
| 79 | + break; |
| 80 | + } |
| 81 | + if ($groupmode == SEPARATEGROUPS or $groupmode == VISIBLEGROUPS) { |
| 82 | + if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $modcontext)) { |
| 83 | + $groups = groups_get_all_groups ($course->id, $USER->id, $cm->groupingid); |
| 84 | + } else { |
| 85 | + $groups = groups_get_all_groups($course->id, 0, $cm->groupingid); |
| 86 | + } |
| 87 | + if ($groups) { |
| 88 | + $selectgroups = array(); |
| 89 | + foreach ($groups as $group) { |
| 90 | + $selectgroups[$group->id] = $group->name; |
| 91 | + } |
| 92 | + $select = &$mform->addElement('select', 'groups', get_string('groups', 'group'), $selectgroups); |
| 93 | + $select->setMultiple(true); |
| 94 | + $mform->disabledIf('groups', 'sessiontype', 'neq', mod_attendance_structure::SESSION_GROUP); |
| 95 | + } else { |
| 96 | + if ($groupmode == VISIBLEGROUPS) { |
| 97 | + $mform->updateElementAttr($radio, array('disabled' => 'disabled')); |
| 98 | + } |
| 99 | + $mform->addElement('static', 'groups', get_string('groups', 'group'), |
| 100 | + get_string('nogroups', 'attendance')); |
| 101 | + if ($groupmode == SEPARATEGROUPS) { |
| 102 | + return; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + attendance_form_sessiondate_selector($mform); |
| 108 | + |
| 109 | + // Select which status set to use. |
| 110 | + $maxstatusset = attendance_get_max_statusset($this->_customdata['att']->id); |
| 111 | + if ($maxstatusset > 0) { |
| 112 | + $opts = array(); |
| 113 | + for ($i = 0; $i <= $maxstatusset; $i++) { |
| 114 | + $opts[$i] = attendance_get_setname($this->_customdata['att']->id, $i); |
| 115 | + } |
| 116 | + $mform->addElement('select', 'statusset', get_string('usestatusset', 'mod_attendance'), $opts); |
| 117 | + } else { |
| 118 | + $mform->addElement('hidden', 'statusset', 0); |
| 119 | + $mform->setType('statusset', PARAM_INT); |
| 120 | + } |
| 121 | + |
| 122 | + // Students can mark own attendance. |
| 123 | + if (!empty(get_config('attendance', 'studentscanmark'))) { |
| 124 | + $mform->addElement('checkbox', 'studentscanmark', '', get_string('studentscanmark', 'attendance')); |
| 125 | + $mform->addHelpButton('studentscanmark', 'studentscanmark', 'attendance'); |
| 126 | + } else { |
| 127 | + $mform->addElement('hidden', 'studentscanmark', '0'); |
| 128 | + $mform->settype('studentscanmark', PARAM_INT); |
| 129 | + } |
| 130 | + |
| 131 | + $mform->addElement('editor', 'sdescription', get_string('description', 'attendance'), array('rows' => 1, 'columns' => 80), |
| 132 | + array('maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean' => true, 'context' => $modcontext)); |
| 133 | + $mform->setType('sdescription', PARAM_RAW); |
| 134 | + |
| 135 | + // For multiple sessions. |
| 136 | + |
| 137 | + $mform->addElement('header', 'headeraddmultiplesessions', get_string('addmultiplesessions', 'attendance')); |
| 138 | + |
| 139 | + $mform->addElement('checkbox', 'addmultiply', '', get_string('repeatasfollows', 'attendance')); |
| 140 | + $mform->addHelpButton('addmultiply', 'createmultiplesessions', 'attendance'); |
| 141 | + |
| 142 | + $sdays = array(); |
| 143 | + if ($CFG->calendar_startwday === '0') { // Week start from sunday. |
| 144 | + $sdays[] =& $mform->createElement('checkbox', 'Sun', '', get_string('sunday', 'calendar')); |
| 145 | + } |
| 146 | + $sdays[] =& $mform->createElement('checkbox', 'Mon', '', get_string('monday', 'calendar')); |
| 147 | + $sdays[] =& $mform->createElement('checkbox', 'Tue', '', get_string('tuesday', 'calendar')); |
| 148 | + $sdays[] =& $mform->createElement('checkbox', 'Wed', '', get_string('wednesday', 'calendar')); |
| 149 | + $sdays[] =& $mform->createElement('checkbox', 'Thu', '', get_string('thursday', 'calendar')); |
| 150 | + $sdays[] =& $mform->createElement('checkbox', 'Fri', '', get_string('friday', 'calendar')); |
| 151 | + $sdays[] =& $mform->createElement('checkbox', 'Sat', '', get_string('saturday', 'calendar')); |
| 152 | + if ($CFG->calendar_startwday !== '0') { // Week start from sunday. |
| 153 | + $sdays[] =& $mform->createElement('checkbox', 'Sun', '', get_string('sunday', 'calendar')); |
| 154 | + } |
| 155 | + $mform->addGroup($sdays, 'sdays', get_string('repeaton', 'attendance'), array(' '), true); |
| 156 | + $mform->disabledIf('sdays', 'addmultiply', 'notchecked'); |
| 157 | + |
| 158 | + $period = array(1 => 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, |
| 159 | + 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36); |
| 160 | + $periodgroup = array(); |
| 161 | + $periodgroup[] =& $mform->createElement('select', 'period', '', $period, false, true); |
| 162 | + $periodgroup[] =& $mform->createElement('static', 'perioddesc', '', get_string('week', 'attendance')); |
| 163 | + $mform->addGroup($periodgroup, 'periodgroup', get_string('repeatevery', 'attendance'), array(' '), false); |
| 164 | + $mform->disabledIf('periodgroup', 'addmultiply', 'notchecked'); |
| 165 | + |
| 166 | + $mform->addElement('date_selector', 'sessionenddate', get_string('repeatuntil', 'attendance')); |
| 167 | + $mform->disabledIf('sessionenddate', 'addmultiply', 'notchecked'); |
| 168 | + |
| 169 | + $mform->addElement('hidden', 'coursestartdate', $course->startdate); |
| 170 | + $mform->setType('coursestartdate', PARAM_INT); |
| 171 | + |
| 172 | + $mform->addElement('hidden', 'previoussessiondate', 0); |
| 173 | + $mform->setType('previoussessiondate', PARAM_INT); |
| 174 | + |
| 175 | + $this->add_action_buttons(true, get_string('add', 'attendance')); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Perform minimal validation on the settings form |
| 180 | + * @param array $data |
| 181 | + * @param array $files |
| 182 | + */ |
| 183 | + public function validation($data, $files) { |
| 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 (!empty($data['addmultiply']) && $data['sessiondate'] != 0 && $data['sessionenddate'] != 0 && |
| 193 | + $data['sessionenddate'] < $data['sessiondate']) { |
| 194 | + $errors['sessionenddate'] = get_string('invalidsessionenddate', 'attendance'); |
| 195 | + } |
| 196 | + |
| 197 | + if ($data['sessiontype'] == mod_attendance_structure::SESSION_GROUP and empty($data['groups'])) { |
| 198 | + $errors['groups'] = get_string('errorgroupsnotselected', 'attendance'); |
| 199 | + } |
| 200 | + |
| 201 | + $addmulti = isset($data['addmultiply']) ? (int)$data['addmultiply'] : 0; |
| 202 | + if (($addmulti != 0) && (!array_key_exists('sdays', $data) || empty($data['sdays']))) { |
| 203 | + $data['sdays'] = array(); |
| 204 | + $errors['sdays'] = get_string('required', 'attendance'); |
| 205 | + } |
| 206 | + if (isset($data['sdays'])) { |
| 207 | + if (!$this->checkweekdays($data['sessiondate'], $data['sessionenddate'], $data['sdays']) ) { |
| 208 | + $errors['sdays'] = get_string('checkweekdays', 'attendance'); |
| 209 | + } |
| 210 | + } |
| 211 | + if ($addmulti && ceil(($data['sessionenddate'] - $data['sessiondate']) / YEARSECS) > 1) { |
| 212 | + $errors['sessionenddate'] = get_string('timeahead', 'attendance'); |
| 213 | + } |
| 214 | + |
| 215 | + if ($data['sessiondate'] < $data['coursestartdate'] && $data['sessiondate'] != $data['previoussessiondate']) { |
| 216 | + $errors['sessiondate'] = get_string('priorto', 'attendance', |
| 217 | + userdate($data['coursestartdate'], get_string('strftimedmy', 'attendance'))); |
| 218 | + $this->_form->setConstant('previoussessiondate', $data['sessiondate']); |
| 219 | + } |
| 220 | + |
| 221 | + return $errors; |
| 222 | + } |
| 223 | + |
| 224 | + private function checkweekdays($sessiondate, $sessionenddate, $sdays) { |
| 225 | + |
| 226 | + $found = false; |
| 227 | + |
| 228 | + $daysofweek = array(0 => "Sun", 1 => "Mon", 2 => "Tue", 3 => "Wed", 4 => "Thu", 5 => "Fri", 6 => "Sat"); |
| 229 | + $start = new DateTime( date("Y-m-d", $sessiondate) ); |
| 230 | + $interval = new DateInterval('P1D'); |
| 231 | + $end = new DateTime( date("Y-m-d", $sessionenddate) ); |
| 232 | + $end->add( new DateInterval('P1D') ); |
| 233 | + |
| 234 | + $period = new DatePeriod($start, $interval, $end); |
| 235 | + foreach ($period as $date) { |
| 236 | + if (!$found) { |
| 237 | + foreach ($sdays as $name => $value) { |
| 238 | + $key = array_search($name, $daysofweek); |
| 239 | + if ($date->format("w") == $key) { |
| 240 | + $found = true; |
| 241 | + break; |
| 242 | + } |
| 243 | + } |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + return $found; |
| 248 | + } |
| 249 | +} |
0 commit comments