update README
[moodle-mod_attendance.git] / sessions.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 * Adding attendance sessions
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(dirname(__FILE__).'/../../config.php');
26 require_once(dirname(__FILE__).'/locallib.php');
27 require_once(dirname(__FILE__).'/add_form.php');
28 require_once(dirname(__FILE__).'/update_form.php');
29 require_once(dirname(__FILE__).'/duration_form.php');
30
31 $pageparams = new att_sessions_page_params();
32
33 $id = required_param('id', PARAM_INT);
34 $pageparams->action = required_param('action', PARAM_INT);
35
36 $cm = get_coursemodule_from_id('attendance', $id, 0, false, MUST_EXIST);
37 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
38 $att = $DB->get_record('attendance', array('id' => $cm->instance), '*', MUST_EXIST);
39
40 require_login($course, true, $cm);
41
42 $att = new attendance($att, $cm, $course, $PAGE->context, $pageparams);
43
44 $att->perm->require_manage_capability();
45
46 $PAGE->set_url($att->url_sessions());
47 $PAGE->set_title($course->shortname. ": ".$att->name);
48 $PAGE->set_heading($course->fullname);
49 $PAGE->set_cacheable(true);
50 $PAGE->set_button($OUTPUT->update_module_button($cm->id, 'attendance'));
51 $PAGE->navbar->add($att->name);
52
53 $formparams = array('course' => $course, 'cm' => $cm, 'modcontext' => $PAGE->context);
54 switch ($att->pageparams->action) {
55 case att_sessions_page_params::ACTION_ADD:
56 $url = $att->url_sessions(array('action' => att_sessions_page_params::ACTION_ADD));
57 $mform = new mod_attendance_add_form($url, $formparams);
58
59 if ($formdata = $mform->get_data()) {
60 $sessions = construct_sessions_data_for_add($formdata);
61 $att->add_sessions($sessions);
62 redirect($url, get_string('sessionsgenerated', 'attendance'));
63 }
64 break;
65 case att_sessions_page_params::ACTION_UPDATE:
66 $sessionid = required_param('sessionid', PARAM_INT);
67
68 $url = $att->url_sessions(array('action' => att_sessions_page_params::ACTION_UPDATE, 'sessionid' => $sessionid));
69 $formparams['sessionid'] = $sessionid;
70 $mform = new mod_attendance_update_form($url, $formparams);
71
72 if ($mform->is_cancelled()) {
73 redirect($att->url_manage());
74 }
75
76 if ($formdata = $mform->get_data()) {
77 $att->update_session_from_form_data($formdata, $sessionid);
78
79 redirect($att->url_manage(), get_string('sessionupdated', 'attendance'));
80 }
81 break;
82 case att_sessions_page_params::ACTION_DELETE:
83 $sessionid = required_param('sessionid', PARAM_INT);
84 $confirm = optional_param('confirm', null, PARAM_INT);
85
86 if (isset($confirm) && confirm_sesskey()) {
87 $att->delete_sessions(array($sessionid));
88 if ($att->grade > 0) {
89 att_update_all_users_grades($att->id, $att->course, $att->context, $cm);
90 }
91 redirect($att->url_manage(), get_string('sessiondeleted', 'attendance'));
92 }
93
94 $sessinfo = $att->get_session_info($sessionid);
95
96 $message = get_string('deletecheckfull', '', get_string('session', 'attendance'));
97 $message .= str_repeat(html_writer::empty_tag('br'), 2);
98 $message .= userdate($sessinfo->sessdate, get_string('strftimedmyhm', 'attendance'));
99 $message .= html_writer::empty_tag('br');
100 $message .= $sessinfo->description;
101
102 $params = array('action' => $att->pageparams->action, 'sessionid' => $sessionid, 'confirm' => 1, 'sesskey' => sesskey());
103
104 echo $OUTPUT->header();
105 echo $OUTPUT->heading(get_string('attendanceforthecourse', 'attendance').' :: ' .$course->fullname);
106 echo $OUTPUT->confirm($message, $att->url_sessions($params), $att->url_manage());
107 echo $OUTPUT->footer();
108 exit;
109 case att_sessions_page_params::ACTION_DELETE_SELECTED:
110 $confirm = optional_param('confirm', null, PARAM_INT);
111
112 if (isset($confirm) && confirm_sesskey()) {
113 $sessionsids = required_param('sessionsids', PARAM_ALPHANUMEXT);
114 $sessionsids = explode('_', $sessionsids);
115
116 $att->delete_sessions($sessionsids);
117 if ($att->grade > 0) {
118 att_update_all_users_grades($att->id, $att->course, $att->context, $cm);
119 }
120 redirect($att->url_manage(), get_string('sessiondeleted', 'attendance'));
121 }
122 $sessid = required_param('sessid', PARAM_SEQUENCE);
123
124 $sessionsinfo = $att->get_sessions_info($sessid);
125
126 $message = get_string('deletecheckfull', '', get_string('session', 'attendance'));
127 $message .= html_writer::empty_tag('br');
128 foreach ($sessionsinfo as $sessinfo) {
129 $message .= html_writer::empty_tag('br');
130 $message .= userdate($sessinfo->sessdate, get_string('strftimedmyhm', 'attendance'));
131 $message .= html_writer::empty_tag('br');
132 $message .= $sessinfo->description;
133 }
134
135 $sessionsids = implode('_', $sessid);
136 $params = array('action' => $att->pageparams->action, 'sessionsids' => $sessionsids,
137 'confirm' => 1, 'sesskey' => sesskey());
138
139 echo $OUTPUT->header();
140 echo $OUTPUT->heading(get_string('attendanceforthecourse', 'attendance').' :: ' .$course->fullname);
141 echo $OUTPUT->confirm($message, $att->url_sessions($params), $att->url_manage());
142 echo $OUTPUT->footer();
143 exit;
144 case att_sessions_page_params::ACTION_CHANGE_DURATION:
145 $sessid = optional_param('sessid', '', PARAM_SEQUENCE);
146 $ids = optional_param('ids', '', PARAM_ALPHANUMEXT);
147
148 $slist = isset($sessid) ? implode('_', $sessid) : '';
149
150 $url = $att->url_sessions(array('action' => att_sessions_page_params::ACTION_CHANGE_DURATION));
151 $formparams['ids'] = $slist;
152 $mform = new mod_attendance_duration_form($url, $formparams);
153
154 if ($mform->is_cancelled()) {
155 redirect($att->url_manage());
156 }
157
158 if ($formdata = $mform->get_data()) {
159 $sessionsids = explode('_', $ids);
160 $duration = $formdata->durtime['hours']*HOURSECS + $formdata->durtime['minutes']*MINSECS;
161 $att->update_sessions_duration($sessionsids, $duration);
162 redirect($att->url_manage(), get_string('sessionupdated', 'attendance'));
163 }
164
165 if ($slist === '') {
166 print_error('nosessionsselected', 'attendance', $att->url_manage());
167 }
168
169 break;
170 }
171
172 $output = $PAGE->get_renderer('mod_attendance');
173 $tabs = new attendance_tabs($att, attendance_tabs::TAB_ADD);
174 echo $output->header();
175 echo $output->heading(get_string('attendanceforthecourse', 'attendance').' :: ' .$course->fullname);
176 echo $output->render($tabs);
177
178 $mform->display();
179
180 echo $OUTPUT->footer();
181
182 function construct_sessions_data_for_add($formdata) {
183 global $CFG;
184
185 $duration = $formdata->durtime['hours']*HOURSECS + $formdata->durtime['minutes']*MINSECS;
186 $now = time();
187
188 $sessions = array();
189 if (isset($formdata->addmultiply)) {
190 $startdate = $formdata->sessiondate;
191 $starttime = $startdate - usergetmidnight($startdate);
192 $enddate = $formdata->sessionenddate + DAYSECS; // Because enddate in 0:0am.
193
194 if ($enddate < $startdate) {
195 return null;
196 }
197
198 $days = (int)ceil(($enddate - $startdate) / DAYSECS);
199
200 // Getting first day of week.
201 $sdate = $startdate;
202 $dinfo = usergetdate($sdate);
203 if ($CFG->calendar_startwday === '0') { // Week start from sunday.
204 $startweek = $startdate - $dinfo['wday'] * DAYSECS; // Call new variable.
205 } else {
206 $wday = $dinfo['wday'] === 0 ? 7 : $dinfo['wday'];
207 $startweek = $startdate - ($wday-1) * DAYSECS;
208 }
209
210 $wdaydesc = array(0=>'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
211
212 while ($sdate < $enddate) {
213 if ($sdate < $startweek + WEEKSECS) {
214 $dinfo = usergetdate($sdate);
215 if (isset($formdata->sdays) && array_key_exists($wdaydesc[$dinfo['wday']], $formdata->sdays)) {
216 $sess = new stdClass();
217 $sess->sessdate = usergetmidnight($sdate) + $starttime;
218 $sess->duration = $duration;
219 $sess->descriptionitemid = $formdata->sdescription['itemid'];
220 $sess->description = $formdata->sdescription['text'];
221 $sess->descriptionformat = $formdata->sdescription['format'];
222 $sess->timemodified = $now;
223 if (isset($formdata->studentscanmark)) { // Students will be able to mark their own attendance.
224 $sess->studentscanmark = 1;
225 }
226
227 fill_groupid($formdata, $sessions, $sess);
228 }
229 $sdate += DAYSECS;
230 } else {
231 $startweek += WEEKSECS * $formdata->period;
232 $sdate = $startweek;
233 }
234 }
235 } else {
236 $sess = new stdClass();
237 $sess->sessdate = $formdata->sessiondate;
238 $sess->duration = $duration;
239 $sess->descriptionitemid = $formdata->sdescription['itemid'];
240 $sess->description = $formdata->sdescription['text'];
241 $sess->descriptionformat = $formdata->sdescription['format'];
242 $sess->timemodified = $now;
243 if (isset($formdata->studentscanmark)) { // Students will be able to mark their own attendance.
244 $sess->studentscanmark = 1;
245 }
246
247 fill_groupid($formdata, $sessions, $sess);
248 }
249
250 return $sessions;
251 }
252
253 function fill_groupid($formdata, &$sessions, $sess) {
254 if ($formdata->sessiontype == attendance::SESSION_COMMON) {
255 $sess = clone $sess;
256 $sess->groupid = 0;
257 $sessions[] = $sess;
258 } else {
259 foreach ($formdata->groups as $groupid) {
260 $sess = clone $sess;
261 $sess->groupid = $groupid;
262 $sessions[] = $sess;
263 }
264 }
265 }