-
Notifications
You must be signed in to change notification settings - Fork 5
/
locallib.php
344 lines (303 loc) · 9.71 KB
/
locallib.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?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/>.
/**
* Plugin capabilities
*
* @package block_studentstracker
* @author Pierre Duverneix
* @copyright 2022 Pierre Duverneix
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Main studentstracker class
*
* @author Pierre Duverneix
*/
class studentstracker {
/**
* @var int
*/
private $usercount;
/**
* @var array
*/
private $users;
/**
* @var string
*/
private $dateformat;
/**
* @var int
*/
private $days;
/**
* @var int
*/
private $dayscritical;
/**
* @var string
*/
private $colordays;
/**
* @var string
*/
private $colordayscritical;
/**
* @var string
*/
private $colornever;
/**
* @var string
*/
private $textnever;
/**
* @var array
*/
private $trackedroles;
/**
* @var array
*/
private $trackedgroups;
/**
* @var string
*/
private $textheader;
/**
* @var string
*/
private $textfooter;
/**
* @var string
*/
private $sorting;
/**
* @var int
*/
private $truncate;
/**
* @var int
*/
private $excludeolder;
/**
* Constructor.
*/
public function __construct() {
}
/**
* Getter.
*
* @param $property
* @throws coding_exception
*/
public function __get($property) {
if (property_exists($this, $property)) {
return $this->$property;
}
throw new coding_exception('Invalid property requested.');
}
/**
* Setter.
*
* @param $property
* @throws coding_exception
*/
public function __set($property, $value) {
if (property_exists($this, $property)) {
$this->$property = $value;
return $this;
} else {
throw new coding_exception('Invalid property requested.');
}
}
/**
* Retrieves the list of the enrolled users of the courses and apply the logic.
*
* @param $context The context object
* @param $courseid The course object id
* @return $this
*/
public function get_enrolled_users($context, $courseid) {
global $DB, $OUTPUT;
$usercount = 0;
$users = get_enrolled_users($context, '', 0, 'u.*', null, 0, 0, true);
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
foreach ($users as $enrol) {
if (groups_user_groups_visible($course, $enrol->id)) {
$enrol->lastaccesstimestamp = $this->get_last_access($context->instanceid, $enrol->id);
if ($this->excludeolder != '' && $enrol->lastaccesstimestamp < strtotime("-$this->excludeolder day", time())) {
continue;
}
$enrol->hasrole = self::has_role($this->trackedroles, $context->id, $enrol->id);
if ((in_array("0", $this->trackedgroups) == false) && (count($this->trackedgroups) > 0)) {
if (!($this->is_in_groups($courseid, $enrol->id))) {
continue;
}
}
if ($enrol->lastaccesstimestamp > 0) {
$enrol->lastaccesscourse = date($this->dateformat, $enrol->lastaccesstimestamp);
} else {
$enrol->lastaccesscourse = $this->textnever;
}
$enrol->messaging = self::messaging($enrol);
$enrol->datelastaccess = date($this->dateformat, $enrol->lastaccess);
$enrol->picture = self::profile($enrol, $context, $OUTPUT);
$enrol->lastaccess = date($this->dateformat, $enrol->lastaccess);
if ($enrol->lastaccesstimestamp < 1) {
$enrol->rowcolor = $this->colornever;
$enrol->rowclass = 'studentstracker-never';
} else if ($enrol->lastaccesstimestamp > 1 && $enrol->lastaccesstimestamp < strtotime($this->days, time())
&& ($enrol->lastaccesstimestamp < strtotime($this->dayscritical, time())) ) {
// Critical access level.
$enrol->rowcolor = $this->colordayscritical;
$enrol->rowclass = 'studentstracker-critical';
} else if ( ($enrol->lastaccesstimestamp < strtotime($this->days, time()))
&& ($enrol->lastaccesstimestamp >= strtotime($this->dayscritical, time())) ) {
// First access level.
$enrol->rowcolor = $this->colordays;
$enrol->rowclass = 'studentstracker-first';
} else {
continue;
}
if ($enrol->hasrole) {
$usercount++;
}
}
}
if (isset($this->sorting)) {
usort($users, self::sort_objects($this->sorting));
}
$this->usercount = $usercount;
$this->users = $users;
return $this;
}
/**
* Check if the given user has the tracked roles.
*
* @param $roleids The role ids
* @param $courseid
* @param $userid
* @return bool
*/
private static function has_role($roleids, $courseid, $userid) {
global $DB;
$params = array();
foreach ($roleids as $role) {
array_push($params, (int)$role);
}
array_push($params, $courseid, (int)$userid);
$roles = join(',', array_fill(0, count($roleids), '?'));
$r = $DB->count_records_sql("SELECT COUNT(id) FROM {role_assignments} WHERE roleid IN($roles) AND contextid=? AND userid=?",
$params);
if ($r > 0) {
return true;
} else {
return false;
}
}
/**
* Check if the given user is part of the tracked groups.
*
* @param $courseid
* @param $userid
* @return bool
*/
private function is_in_groups($courseid, $userid) {
$usergroups = groups_get_user_groups($courseid, $userid);
foreach ($this->trackedgroups as $group) {
if (in_array(intval($group), $usergroups[0], true)) {
return true;
}
}
return false;
}
/**
* Get the user's last access in a course.
*
* @param $courseid
* @param $userid
* @return $lastaccess string
*/
private function get_last_access($courseid, $userid) {
global $DB;
$lastaccess = $DB->get_field('user_lastaccess', 'timeaccess', array('courseid' => $courseid, 'userid' => $userid));
if ($lastaccess < 1) {
return 0;
}
return $lastaccess;
}
/**
* Link to the private message page for a given user.
*
* @param $user The user object
*/
public static function messaging($user) {
global $OUTPUT;
$userid = optional_param('user2', $user->id, PARAM_INT);
$url = new moodle_url('/message/index.php');
if ($user->id) {
$url->param('id', $userid);
}
return html_writer::link($url, '<img src="'.$OUTPUT->image_url('t/message').'">', array());
}
/**
* Link to the user's profile.
*
* @param $user The user object
* @param $context The context object
* @param $output The core_renderer to use when generating the output.
*/
public static function profile($user, $context, $output) {
$url = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $context->instanceid));
return html_writer::link($url, $output->user_picture($user, array('size' => 15, 'alttext' => false, 'link' => false)) .
"$user->firstname $user->lastname", array());
}
/**
* Sorting function used for the sorting config option.
*
* @param $key The key used for sorting the objects
*/
public static function sort_objects($key) {
if ($key == 'date_desc') {
return function ($a, $b) use ($key) {
if (isset($a->lastaccesstimestamp) && isset($b->lastaccesstimestamp)) {
return strnatcmp($b->lastaccesstimestamp, $a->lastaccesstimestamp);
}
};
} else if ($key == 'date_asc') {
return function ($a, $b) use ($key) {
if (isset($a->lastaccesstimestamp) && isset($b->lastaccesstimestamp)) {
return strnatcmp($a->lastaccesstimestamp, $b->lastaccesstimestamp);
}
};
}
return function ($a, $b) use ($key) {
return strnatcmp($a->{$key}, $b->{$key});
};
}
/**
* Call the plugin renderer with the data.
*
* @return \block_studentstracker\output\main_content Studentstracker main_content renderer
*/
public function generate_content() {
return new \block_studentstracker\output\main_content(
$this->usercount,
$this->users,
$this->truncate,
$this->textheader,
$this->textfooter);
}
}