-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathattendance_webservices_handler.php
181 lines (153 loc) · 6.38 KB
/
attendance_webservices_handler.php
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Web Services for Attendance plugin.
*
* @package mod_attendance
* @copyright 2015 Caio Bressan Doneda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(dirname(__FILE__).'/../locallib.php');
require_once(dirname(__FILE__).'/structure.php');
require_once(dirname(__FILE__).'/../../../lib/sessionlib.php');
require_once(dirname(__FILE__).'/../../../lib/datalib.php');
/**
* Class attendance_handler
* @copyright 2015 Caio Bressan Doneda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class attendance_handler {
/**
* For this user, this method searches in all the courses that this user has permission to take attendance,
* looking for today sessions and returns the courses with the sessions.
* @param int $userid
* @return array
*/
public static function get_courses_with_today_sessions($userid) {
$usercourses = enrol_get_users_courses($userid);
$attendanceinstance = get_all_instances_in_courses('attendance', $usercourses);
$coursessessions = [];
foreach ($attendanceinstance as $attendance) {
$context = context_course::instance($attendance->course);
if (has_capability('mod/attendance:takeattendances', $context, $userid)) {
$course = $usercourses[$attendance->course];
if (!isset($course->attendance_instance)) {
$course->attendance_instance = [];
}
$att = new stdClass();
$att->id = $attendance->id;
$att->course = $attendance->course;
$att->name = $attendance->name;
$att->grade = $attendance->grade;
$cm = new stdClass();
$cm->id = $attendance->coursemodule;
$att = new mod_attendance_structure($att, $cm, $course, $context);
$todaysessions = $att->get_today_sessions();
if (!empty($todaysessions)) {
$course->attendance_instance[$att->id] = [];
$course->attendance_instance[$att->id]['name'] = $att->name;
$course->attendance_instance[$att->id]['today_sessions'] = $todaysessions;
$coursessessions[$course->id] = $course;
}
}
}
return self::prepare_data($coursessessions);
}
/**
* Prepare data.
*
* @param array $coursessessions
* @return array
*/
private static function prepare_data($coursessessions) {
$courses = [];
foreach ($coursessessions as $c) {
$courses[$c->id] = new stdClass();
$courses[$c->id]->shortname = $c->shortname;
$courses[$c->id]->fullname = $c->fullname;
$courses[$c->id]->attendance_instances = $c->attendance_instance;
}
return $courses;
}
/**
* For this session, returns all the necessary data to take an attendance.
*
* @param int $sessionid
* @return mixed
*/
public static function get_session($sessionid) {
global $DB;
$session = $DB->get_record('attendance_sessions', ['id' => $sessionid]);
$session->courseid = $DB->get_field('attendance', 'course', ['id' => $session->attendanceid]);
$session->statuses = attendance_get_statuses($session->attendanceid, true, $session->statusset);
$coursecontext = context_course::instance($session->courseid);
$session->users = get_enrolled_users($coursecontext, 'mod/attendance:canbelisted',
$session->groupid, 'u.id, u.firstname, u.lastname');
$session->attendance_log = [];
if ($attendancelog = $DB->get_records('attendance_log', ['sessionid' => $sessionid],
'', 'studentid, statusid, remarks, id')) {
$session->attendance_log = $attendancelog;
}
return $session;
}
/**
* Update user status
*
* @param int $sessionid
* @param int $studentid
* @param int $takenbyid
* @param int $statusid
* @param int $statusset
*/
public static function update_user_status($sessionid, $studentid, $takenbyid, $statusid, $statusset) {
global $DB;
$record = new stdClass();
$record->statusset = $statusset;
$record->sessionid = $sessionid;
$record->timetaken = time();
$record->takenby = $takenbyid;
$record->statusid = $statusid;
$record->studentid = $studentid;
if ($attendancelog = $DB->get_record('attendance_log', ['sessionid' => $sessionid, 'studentid' => $studentid])) {
$record->id = $attendancelog->id;
$DB->update_record('attendance_log', $record);
} else {
$DB->insert_record('attendance_log', $record);
}
if ($attendancesession = $DB->get_record('attendance_sessions', ['id' => $sessionid])) {
$attendancesession->lasttaken = time();
$attendancesession->lasttakenby = $takenbyid;
$attendancesession->timemodified = time();
$DB->update_record('attendance_sessions', $attendancesession);
}
}
/**
* For this attendance instance, returns all sessions.
*
* @param int $attendanceid
* @return mixed
*/
public static function get_sessions($attendanceid) {
global $DB;
$sessions = $DB->get_records('attendance_sessions', ['attendanceid' => $attendanceid], 'id ASC');
$sessionsinfo = [];
foreach ($sessions as $session) {
$sessionsinfo[$session->id] = self::get_session($session->id);
}
return $sessionsinfo;
}
}