forked from intelliboard/intelliboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
366 lines (346 loc) · 14.4 KB
/
lib.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?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/>.
/**
* This plugin provides access to Moodle data in form of analytics and reports in real time.
*
*
* @package local_intelliboard
* @copyright 2017 IntelliBoard, Inc
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @website https://intelliboard.net/
*/
// In versions before Moodle 2.9, the supported callbacks have _extends_ (not imperative mood) in their names. This was a consistency bug fixed in MDL-49643.
function local_intelliboard_extends_navigation(global_navigation $nav){
global $CFG, $USER;
local_intelliboard_insert_tracking(false);
$context = context_system::instance();
if (isloggedin() and get_config('local_intelliboard', 't1') and has_capability('local/intelliboard:students', $context)) {
$alt_name = get_config('local_intelliboard', 't0');
$def_name = get_string('ts1', 'local_intelliboard');
$name = ($alt_name) ? $alt_name : $def_name;
$nav->add($name, new moodle_url($CFG->wwwroot.'/local/intelliboard/student/index.php'));
}
if (isloggedin() and get_config('local_intelliboard', 'n10')){
//Check if user is enrolled to any courses with "instructor" role(s)
$instructor_roles = get_config('local_intelliboard', 'filter10');
if (!empty($instructor_roles)) {
$access = false;
$roles = explode(',', $instructor_roles);
if (!empty($roles)) {
foreach ($roles as $role) {
if ($role and user_has_role_assignment($USER->id, $role)){
$access = true;
break;
}
}
if ($access) {
$alt_name = get_config('local_intelliboard', 'n11');
$def_name = get_string('n10', 'local_intelliboard');
$name = ($alt_name) ? $alt_name : $def_name;
$nav->add($name, new moodle_url($CFG->wwwroot.'/local/intelliboard/instructor/index.php'));
}
}
}
}
}
//call-back method to extend the navigation
function local_intelliboard_extend_navigation(global_navigation $nav){
global $CFG, $DB, $USER, $PAGE;
try {
$mynode = $PAGE->navigation->find('myprofile', navigation_node::TYPE_ROOTNODE);
$mynode->collapse = true;
$mynode->make_inactive();
local_intelliboard_insert_tracking(false);
$context = context_system::instance();
if (isloggedin() and get_config('local_intelliboard', 't1') and has_capability('local/intelliboard:students', $context)) {
$alt_name = get_config('local_intelliboard', 't0');
$def_name = get_string('ts1', 'local_intelliboard');
$name = ($alt_name) ? $alt_name : $def_name;
$url = new moodle_url($CFG->wwwroot.'/local/intelliboard/student/index.php');
$nav->add($name, $url);
$node = $mynode->add($name, $url, 0, null, 'intelliboard_student');
$node->showinflatnavigation = true;
}
if (isloggedin() and get_config('local_intelliboard', 'n10')) {
//Check if user is enrolled to any courses with "instructor" role(s)
$instructor_roles = get_config('local_intelliboard', 'filter10');
if (!empty($instructor_roles)) {
$access = false;
$roles = explode(',', $instructor_roles);
if (!empty($roles)) {
foreach ($roles as $role) {
if ($role and user_has_role_assignment($USER->id, $role)){
$access = true;
break;
}
}
if ($access) {
$alt_name = get_config('local_intelliboard', 'n11');
$def_name = get_string('n10', 'local_intelliboard');
$name = ($alt_name) ? $alt_name : $def_name;
$url = new moodle_url($CFG->wwwroot.'/local/intelliboard/instructor/index.php');
$nav->add($name, $url);
$node = $mynode->add($name, $url, 0, null, 'intelliboard_instructor');
$node->showinflatnavigation = true;
}
}
}
}
if (isloggedin() and get_config('local_intelliboard', 'competency_dashboard') and has_capability('local/intelliboard:competency', $context)) {
$alt_name = get_config('local_intelliboard', 'a11');
$def_name = get_string('a0', 'local_intelliboard');
$name = ($alt_name) ? $alt_name : $def_name;
$url = new moodle_url($CFG->wwwroot.'/local/intelliboard/competencies/index.php');
$nav->add($name, $url);
$node = $mynode->add($name, $url, 0, null, 'intelliboard_competency');
$node->showinflatnavigation = true;
}
} catch (Exception $e) {}
}
function local_intelliboard_user_details()
{
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
} else {
$user_agent = '';
}
$platform = "Unknown OS Platform";
$os_array = array(
'/windows nt 10.0/i' => 'Windows 10',
'/windows nt 6.3/i' => 'Windows 8.1',
'/windows nt 6.2/i' => 'Windows 8',
'/windows nt 6.1/i' => 'Windows 7',
'/windows nt 6.0/i' => 'Windows Vista',
'/windows nt 5.2/i' => 'Windows Server 2003/XP x64',
'/windows nt 5.1/i' => 'Windows XP',
'/windows xp/i' => 'Windows XP',
'/windows nt 5.0/i' => 'Windows 2000',
'/windows me/i' => 'Windows ME',
'/win98/i' => 'Windows 98',
'/win95/i' => 'Windows 95',
'/win16/i' => 'Windows 3.11',
'/macintosh|mac os x/i' => 'Mac OS X',
'/mac_powerpc/i' => 'Mac OS 9',
'/linux/i' => 'Linux',
'/ubuntu/i' => 'Ubuntu',
'/iphone/i' => 'iPhone',
'/ipod/i' => 'iPod',
'/ipad/i' => 'iPad',
'/android/i' => 'Android',
'/blackberry/i' => 'BlackBerry',
'/webos/i' => 'Mobile'
);
foreach ($os_array as $regex => $value) {
if (preg_match($regex, $user_agent)) {
$platform = $value;
}
}
$browser = "Unknown Browser";
$browser_array = array(
'/msie|trident/i' => 'Internet Explorer',
'/firefox/i' => 'Firefox',
'/chrome/i' => 'Chrome',
'/safari/i' => 'Safari',
'/edge/i' => 'Microsoft Edge',
'/opera/i' => 'Opera',
'/opr/i' => 'Opera',
'/netscape/i' => 'Netscape',
'/maxthon/i' => 'Maxthon',
'/konqueror/i' => 'Konqueror',
'/mobile/i' => 'Mobile browser'
);
foreach ($browser_array as $regex => $value) {
if (preg_match($regex, $user_agent)) {
$browser = $value;
break;
}
}
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}elseif (isset($_SERVER["HTTP_CLIENT_IP"])){
$ip = $_SERVER["HTTP_CLIENT_IP"];
}else{
$ip = $_SERVER["REMOTE_ADDR"];
}
$ip = ($ip) ? $ip : 0;
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$userlang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
} else {
$userlang = 'Unknown';
}
return array(
'useragent' => $browser,
'useros' => $platform,
'userip' => $ip,
'userlang' => $userlang
);
}
function local_intelliboard_insert_tracking($ajaxRequest = false){
global $CFG, $PAGE, $SITE, $DB, $USER;
$version = get_config('local_intelliboard', 'version');
$enabled = get_config('local_intelliboard', 'enabled');
$ajax = (int) get_config('local_intelliboard', 'ajax');
$inactivity = (int) get_config('local_intelliboard', 'inactivity');
$trackadmin = get_config('local_intelliboard', 'trackadmin');
$trackcourses = get_config('local_intelliboard', 'trackcourses');
$trackusers = get_config('local_intelliboard', 'trackusers');
$trackpoint = get_config('local_intelliboard', 'trackpoint');
$path = isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : '';
if(strpos($path,'cron.php') !== false){
return false;
}
if ($enabled and isloggedin() and !isguestuser()){
if(is_siteadmin() and !$trackadmin){
return false;
}
$intelliboardPage = (isset($_COOKIE['intelliboardPage'])) ? clean_param($_COOKIE['intelliboardPage'], PARAM_ALPHANUMEXT) : '';
$intelliboardParam = (isset($_COOKIE['intelliboardParam'])) ? clean_param($_COOKIE['intelliboardParam'], PARAM_INT) : 0;
$intelliboardTime = (isset($_COOKIE['intelliboardTime'])) ? clean_param($_COOKIE['intelliboardTime'], PARAM_INT) : 0;
if(!empty($intelliboardPage)){
$userDetails = (object)local_intelliboard_user_details();
if($data = $DB->get_record('local_intelliboard_tracking', array('userid' => $USER->id, 'page' => $intelliboardPage, 'param' => $intelliboardParam), 'id, visits, timespend, lastaccess')){
if(!$ajaxRequest){
$data->visits = $data->visits + 1;
$data->lastaccess = time();
}
$data->timespend = $data->timespend + $intelliboardTime;
$data->useragent = $userDetails->useragent;
$data->useros = $userDetails->useros;
$data->userlang = $userDetails->userlang;
$data->userip = $userDetails->userip;
$DB->update_record('local_intelliboard_tracking', $data);
}else{
$courseid = 0;
if($intelliboardPage == "module"){
$courseid = $DB->get_field_sql("SELECT c.id FROM {course} c, {course_modules} cm WHERE c.id = cm.course AND cm.id = $intelliboardParam");
}elseif($intelliboardPage == "course"){
$courseid = $intelliboardParam;
}
$data = new stdClass();
$data->userid = $USER->id;
$data->courseid = $courseid;
$data->page = $intelliboardPage;
$data->param = $intelliboardParam;
$data->visits = 1;
$data->timespend = $intelliboardTime;
$data->firstaccess = time();
$data->lastaccess = time();
$data->useragent = $userDetails->useragent;
$data->useros = $userDetails->useros;
$data->userlang = $userDetails->userlang;
$data->userip = $userDetails->userip;
$data->id = $DB->insert_record('local_intelliboard_tracking', $data, true);
}
if($version >= 2016011300){
$currentstamp = strtotime('today');
if($data->id){
if($log = $DB->get_record('local_intelliboard_logs', array('trackid' => $data->id, 'timepoint' => $currentstamp))){
if(!$ajaxRequest){
$log->visits = $log->visits + 1;
}
$log->timespend = $log->timespend + $intelliboardTime;
$DB->update_record('local_intelliboard_logs', $log);
}else{
$log = new stdClass();
$log->trackid = $data->id;
$log->visits = 1;
$log->timespend = $intelliboardTime;
$log->timepoint = $currentstamp;
$DB->insert_record('local_intelliboard_logs', $log);
}
}
$sessions = false; $courses = false;
if($trackpoint != $currentstamp){
set_config("trackpoint", $currentstamp, "local_intelliboard");
set_config("trackusers", '', "local_intelliboard");
set_config("trackcourses", '', "local_intelliboard");
}
if($intelliboardPage == 'course'){
if($trackcourses){
$instances = explode(',', $trackcourses);
if(!in_array($intelliboardParam, $instances)){
$courses = true;
set_config("trackcourses", $trackcourses.",".$intelliboardParam, "local_intelliboard");
}
}else{
$courses = true;
set_config("trackcourses", $intelliboardParam, "local_intelliboard");
}
}
if($trackusers){
$users = explode(',', $trackusers);
if(!in_array($USER->id, $users)){
$sessions = true;
set_config("trackusers", $trackusers.",".$USER->id, "local_intelliboard");
}
}else{
$sessions = true;
set_config("trackusers", $USER->id, "local_intelliboard");
}
if($data = $DB->get_record('local_intelliboard_totals', array('timepoint' => $currentstamp))){
if(!$ajaxRequest){
$data->visits = $data->visits + 1;
}
if($sessions){
$data->sessions = $data->sessions + 1;
}
if($courses){
$data->courses = $data->courses + 1;
}
$data->timespend = $data->timespend + $intelliboardTime;
$DB->update_record('local_intelliboard_totals', $data);
}else{
$data = new stdClass();
$data->sessions = 1;
$data->courses = ($courses)?1:0;
$data->visits = 1;
$data->timespend = $intelliboardTime;
$data->timepoint = $currentstamp;
$DB->insert_record('local_intelliboard_totals', $data);
}
}
}
if($ajaxRequest){
die("time ".$intelliboardTime);
}
if(isset($PAGE->cm->id)){
$intelliboardPage = 'module';
$intelliboardParam = $PAGE->cm->id;
}elseif(isset($PAGE->course->id) and $SITE->id != $PAGE->course->id){
$intelliboardPage = 'course';
$intelliboardParam = $PAGE->course->id;
}elseif(strpos($PAGE->url, '/profile/') !== false){
$intelliboardPage = 'user';
$intelliboardParam = $USER->id;
}else{
$intelliboardPage = 'site';
$intelliboardParam = 0;
}
if(!headers_sent()){
SetCookie('intelliboardPage', $intelliboardPage, time()+3600, "/");
SetCookie('intelliboardParam', $intelliboardParam, time()+3600, "/");
SetCookie('intelliboardTime', 0, time()+3600, "/");
}
$params = new stdClass();
$params->intelliboardAjax = $ajax;
$params->intelliboardAjaxUrl = $ajax ? "$CFG->wwwroot/local/intelliboard/ajax.php" : "";
$params->intelliboardInactivity = $inactivity;
$params->intelliboardPeriod = 1000;
$PAGE->requires->js('/local/intelliboard/module.js', false);
$PAGE->requires->js_init_call('intelliboardInit', array($params), false);
return true;
}
}