Merge pull request #83 from barrysspace/expired_self_enrolments_MDL26
[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', attendance::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', attendance::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'), attendance::SESSION_COMMON);
71 $radio[] = &$mform->createElement('radio', 'sessiontype', '',
72 get_string('groupsession', 'attendance'), attendance::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', attendance::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', attendance::SESSION_GROUP);
93 } else {
94 $mform->updateElementAttr($radio, array('disabled'=>'disabled'));
95 $mform->addElement('static', 'groups', get_string('groups', 'group'),
96 get_string('nogroups', 'attendance'));
97 if ($groupmode == SEPARATEGROUPS) {
98 return;
99 }
100 }
101 }
102
103 $mform->addElement('checkbox', 'addmultiply', '', get_string('createmultiplesessions', 'attendance'));
104 $mform->addHelpButton('addmultiply', 'createmultiplesessions', 'attendance');
105
106 // Students can mark own attendance.
107 $mform->addElement('checkbox', 'studentscanmark', '', get_string('studentscanmark','attendance'));
108 $mform->addHelpButton('studentscanmark', 'studentscanmark', 'attendance');
109
110 $mform->addElement('date_time_selector', 'sessiondate', get_string('sessiondate', 'attendance'));
111
112 for ($i=0; $i<=23; $i++) {
113 $hours[$i] = sprintf("%02d", $i);
114 }
115 for ($i=0; $i<60; $i+=5) {
116 $minutes[$i] = sprintf("%02d", $i);
117 }
118 $durtime = array();
119 $durtime[] =& $mform->createElement('select', 'hours', get_string('hour', 'form'), $hours, false, true);
120 $durtime[] =& $mform->createElement('select', 'minutes', get_string('minute', 'form'), $minutes, false, true);
121 $mform->addGroup($durtime, 'durtime', get_string('duration', 'attendance'), array(' '), true);
122
123 $mform->addElement('date_selector', 'sessionenddate', get_string('sessionenddate', 'attendance'));
124 $mform->disabledIf('sessionenddate', 'addmultiply', 'notchecked');
125
126 $sdays = array();
127 if ($CFG->calendar_startwday === '0') { // Week start from sunday.
128 $sdays[] =& $mform->createElement('checkbox', 'Sun', '', get_string('sunday', 'calendar'));
129 }
130 $sdays[] =& $mform->createElement('checkbox', 'Mon', '', get_string('monday', 'calendar'));
131 $sdays[] =& $mform->createElement('checkbox', 'Tue', '', get_string('tuesday', 'calendar'));
132 $sdays[] =& $mform->createElement('checkbox', 'Wed', '', get_string('wednesday', 'calendar'));
133 $sdays[] =& $mform->createElement('checkbox', 'Thu', '', get_string('thursday', 'calendar'));
134 $sdays[] =& $mform->createElement('checkbox', 'Fri', '', get_string('friday', 'calendar'));
135 $sdays[] =& $mform->createElement('checkbox', 'Sat', '', get_string('saturday', 'calendar'));
136 if ($CFG->calendar_startwday !== '0') { // Week start from sunday.
137 $sdays[] =& $mform->createElement('checkbox', 'Sun', '', get_string('sunday', 'calendar'));
138 }
139 $mform->addGroup($sdays, 'sdays', get_string('sessiondays', 'attendance'), array('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'), true);
140 $mform->disabledIf('sdays', 'addmultiply', 'notchecked');
141
142 $period = array(1=>1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36);
143 $periodgroup = array();
144 $periodgroup[] =& $mform->createElement('select', 'period', '', $period, false, true);
145 $periodgroup[] =& $mform->createElement('static', 'perioddesc', '', get_string('week', 'attendance'));
146 $mform->addGroup($periodgroup, 'periodgroup', get_string('period', 'attendance'), array(' '), false);
147 $mform->disabledIf('periodgroup', 'addmultiply', 'notchecked');
148
149 $mform->addElement('editor', 'sdescription', get_string('description', 'attendance'),
150 null, array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'noclean'=>true, 'context'=>$modcontext));
151 $mform->setType('sdescription', PARAM_RAW);
152
153 $submit_string = get_string('addsession', 'attendance');
154 $this->add_action_buttons(false, $submit_string);
155 }
156
157 /**
158 * Perform minimal validation on the settings form
159 * @param array $data
160 * @param array $files
161 */
162 public function validation($data, $files) {
163 $errors = parent::validation($data, $files);
164
165 if (!empty($data['addmultiply']) && $data['sessiondate'] != 0 && $data['sessionenddate'] != 0 && $data['sessionenddate'] < $data['sessiondate']) {
166 $errors['sessionenddate'] = get_string('invalidsessionenddate', 'attendance');
167 }
168
169 if ($data['sessiontype'] == attendance::SESSION_GROUP and empty($data['groups'])) {
170 $errors['groups'] = get_string('errorgroupsnotselected', 'attendance');
171 }
172
173 $addmulti = isset($data['addmultiply'])? (int)$data['addmultiply'] : 0;
174 if (($addmulti != 0) && (!array_key_exists('sdays',$data) || empty($data['sdays']))) {
175 $data['sdays']= array();
176 $errors['sdays'] = get_string('required', 'attendance');
177 }
178 return $errors;
179 }
180
181 }