forked from 0x4Dark/moodle-report_deviceanalytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocallib.php
353 lines (329 loc) · 13.1 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
345
346
347
348
349
350
351
352
353
<?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/>.
/**
* Internal library of functions for module
*
* @package report_deviceanalytics
* @copyright 2016 Mark Heumueller <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/formslib.php');
require_once('admin/dashboard_time_form.php');
/**
* Loads the saved data from report_deviceanalytics_data
* @param int $starttime filters entries with starttime
* @param int $endtime filters entries with endtime
* @return Array $chartsdata of records or null
*/
function report_deviceanalytics_load_datas($starttime = null, $endtime = null) {
global $DB;
if (empty($starttime) || is_null($starttime)) {
$conf = $DB->get_record('report_deviceanalytics', array());
$starttime = $conf->starttime;
}
if (empty($endtime) || is_null($endtime)) {
$endtime = time();
}
$chartsdata = $DB->get_records_sql('SELECT * FROM {report_deviceanalytics_data} WHERE objectdate >= ? AND objectdate <= ?',
array($starttime, $endtime));
if (! empty($chartsdata) || ! is_null($chartsdata)) {
return $chartsdata;
} else {
return null;
}
}
/**
* Creates the Outputcontainer for dashboard.php
* @param Array $chartout array of containers
* @param Array $vtables array of html_tables
* @return Array all containers for output
*/
function report_deviceanalytics_create_containers($chartout, $vtables) {
$vout = array();
$vout[] = report_deviceanaltics_create_wrapper_container(
'device_types',
'dashboard_chart_device_types',
$chartout[0],
$vtables[0]);
$vout[] = report_deviceanaltics_create_wrapper_container(
'device_systems',
'dashboard_chart_device_systems',
$chartout[1],
$vtables[1]);
$vout[] = report_deviceanaltics_create_wrapper_container(
'device_browser',
'dashboard_chart_device_browser',
$chartout[2],
$vtables[2]);
$vout[] = report_deviceanaltics_create_wrapper_container(
'screen_sizes',
'dashboard_chart_screen_sizes',
$chartout[3],
$vtables[3]);
$vout[] = report_deviceanaltics_create_wrapper_container(
'window_sizes',
'dashboard_chart_window_sizes',
$chartout[4],
$vtables[4]);
$vout[] = report_deviceanaltics_create_wrapper_container(
'pointing_method',
'dashboard_chart_pointing_method',
$chartout[5],
$vtables[5]);
$vout[] = report_deviceanaltics_create_wrapper_container(
'windowsizes_weight',
'dashboard_chart_displaysizes_weight',
$chartout[6],
$vtables[6]);
$vout[] = report_deviceanaltics_create_wrapper_container(
'windowsizes_weight',
'dashboard_chart_windowsizes_weight',
$chartout[7],
$vtables[7]);
$vout[] = report_deviceanaltics_create_wrapper_container(
'pixel_ratio',
'dashboard_chart_pixel_ratio',
$chartout[8],
$vtables[8]);
return $vout;
}
/**
* Check if object holds screen data
* @param Object $var entryobject
* @return bool true/false
* @deprecated
*/
function report_device_analytics_is_not_null($var) {
return !is_null($var->devicedisplaysizex);
}
/**
* Calculate the number of subversion inside array
* @param Array $datarray array ob records
* @return int count of subversion
*/
function report_device_analytics_calc_numbers_of_version($datarray) {
$returnnumber = 0;
foreach ($datarray as $value) {
$returnnumber += $value;
}
return $returnnumber;
}
/**
* Calculate the percent for element inside array
* @param Array $datarray array with prechecked elements
* @param String $calckey calc elementkey
* @return float percent of searched value
*/
function report_device_analytics_calc_percent($datarray, $calckey) {
$groundvalue = 0;
$procvalue = $datarray[$calckey];
foreach ($datarray as $val) {
$groundvalue += $val;
}
return number_format((($procvalue / $groundvalue) * 100) , 2);
}
/**
* Same as report_device_analytics_calc_percent, but from subarray
* @param Array $datarray array with prechecked elements
* @param String $calckey calc elementkey
* @return float percent of searched value
*/
function report_device_analytics_calc_percent_from_sub($datarray, $calckey) {
$groundvalue = 0;
$procsub = array();
foreach ($datarray as $key => $subvalue) {
$subval = 0;
foreach ($subvalue as $value) {
$subval += $value;
}
$procsub[$key] = $subval;
$groundvalue += $subval;
}
return number_format((($procsub[$calckey] / $groundvalue) * 100) , 2);
}
/**
* Creates the wrapper container for charts and tables - also write tables
* @param String $wrappername name of the div wrapper
* @param String $headername heading line - from moodle/lang
* @param String $chartoutput holds all information for the charts
* @param html_table $vtables table object for html_writer
* @return String $oretrun output-string
*/
function report_deviceanaltics_create_wrapper_container($wrappername, $headername, $chartoutput, $vtables) {
global $OUTPUT;
$oreturn = $OUTPUT->heading(get_string($headername, 'report_deviceanalytics'), 4);
$oreturn .= $OUTPUT->container_start('wrapper', $wrappername);
if (! empty($chartoutput) || ! is_null($chartoutput)) {
$oreturn .= $chartoutput;
}
if (! empty($vtables) || ! is_null($vtables)) {
$oreturn .= html_writer::table($vtables);
}
$oreturn .= $OUTPUT->container_end();
return $oreturn;
}
/**
* Calculate the Tables
* @param Array $datas - preselected entries form data table
* @return Array $returntables output-string
*/
function report_deviceanalytics_create_data_tables($datas) {
$returntables = array();
$returntables[0] = new html_table();
$returntables[0]->head = (array) get_strings(array('table_type', 'table_percent', 'table_count'), 'report_deviceanalytics');
$devicetypes = array();
foreach ($datas as $devicetypedata) {
$type = $devicetypedata->devicetype;
if (array_key_exists($type, $devicetypes)) {
$devicetypes[$devicetypedata->devicetype]++;
} else {
$devicetypes[$devicetypedata->devicetype] = 1;
}
}
foreach ($devicetypes as $tname => $count) {
$returntables[0]->data[] = array($tname, report_device_analytics_calc_percent($devicetypes, $tname)."%", $count);
}
$returntables[1] = new html_table();
$returntables[1]->head = (array) get_strings(array('table_os', 'table_percent', 'table_count'), 'report_deviceanalytics');
$deviceoses = array();
foreach ($datas as $deviceosdata) {
$type = $deviceosdata->devicesystem;
if (array_key_exists($type, $deviceoses)) {
$deviceoses[$deviceosdata->devicesystem]++;
} else {
$deviceoses[$deviceosdata->devicesystem] = 1;
}
}
foreach ($deviceoses as $tname => $count) {
$returntables[1]->data[] = array($tname, report_device_analytics_calc_percent($deviceoses, $tname)."%", $count);
}
$returntables[2] = new html_table();
$returntables[2]->head = (array) get_strings(array('table_browser', 'table_percent', 'table_count'), 'report_deviceanalytics');
$devicebrowser = array();
foreach ($datas as $devicebrowserdata) {
$browser = $devicebrowserdata->devicebrowser;
$browserversion = $devicebrowserdata->devicebrowserversion;
if (array_key_exists($browser, $devicebrowser)) {
if (array_key_exists($devicebrowserdata->devicebrowserversion, $devicebrowser[$devicebrowserdata->devicebrowser])) {
$devicebrowser[$devicebrowserdata->devicebrowser][$devicebrowserdata->devicebrowserversion]++;
} else {
$devicebrowser[$devicebrowserdata->devicebrowser][$devicebrowserdata->devicebrowserversion] = 1;
}
} else {
$devicebrowser[$devicebrowserdata->devicebrowser] = array();
$devicebrowser[$devicebrowserdata->devicebrowser][$devicebrowserdata->devicebrowserversion] = 1;
}
}
foreach ($devicebrowser as $bname => $sub) {
$returntables[2]->data[] = array(
'<b>'.$bname.'</b>',
report_device_analytics_calc_percent_from_sub($devicebrowser, $bname).'%',
report_device_analytics_calc_numbers_of_version($sub)
);
foreach ($sub as $vnum => $scount) {
$returntables[2]->data[] = array(
get_string('table_version', 'report_deviceanalytics').': '.$vnum,
report_device_analytics_calc_percent($sub, $vnum).'%',
$scount
);
}
}
$returntables[3] = null;
$returntables[4] = null;
$returntables[5] = new html_table();
$returntables[5]->head = (array) get_strings(array('table_pointing', 'table_percent', 'table_count'), 'report_deviceanalytics');
$devicepointing = array();
foreach ($datas as $devicepointdata) {
$ptype = $devicepointdata->devicepointingmethod;
if (array_key_exists($ptype, $devicepointing)) {
$devicepointing[$devicepointdata->devicepointingmethod]++;
} else {
$devicepointing[$devicepointdata->devicepointingmethod] = 1;
}
}
foreach ($devicepointing as $tname => $count) {
$returntables[5]->data[] = array($tname, report_device_analytics_calc_percent($devicepointing, $tname)."%", $count);
}
$returntables[6] = new html_table();
$returntables[6]->head = (array) get_strings(array('dashboard_chart_screen_sizes', 'table_percent', 'table_count'), 'report_deviceanalytics');
$weight = [];
foreach ($datas as $devicepointdata) {
$ptype = $devicepointdata->devicedisplaysizex . ' x ' . $devicepointdata->devicedisplaysizey;
if (array_key_exists($ptype, $weight)) {
$weight[$ptype]++;
} else {
$weight[$ptype] = 1;
}
}
arsort($weight, SORT_NUMERIC);
foreach ($weight as $tname => $count) {
$returntables[6]->data[] = array($tname, report_device_analytics_calc_percent($weight, $tname). '%', $count);
}
$returntables[7] = new html_table();
$returntables[7]->head = (array) get_strings(array('dashboard_chart_window_sizes', 'table_percent', 'table_count'), 'report_deviceanalytics');
$weight = [];
foreach ($datas as $devicepointdata) {
$ptype = $devicepointdata->devicewindowsizex . ' x ' . $devicepointdata->devicewindowsizey;
if (array_key_exists($ptype, $weight)) {
$weight[$ptype]++;
} else {
$weight[$ptype] = 1;
}
}
arsort($weight, SORT_NUMERIC);
foreach ($weight as $tname => $count) {
$returntables[7]->data[] = array($tname, report_device_analytics_calc_percent($weight, $tname). '%', $count);
}
$returntables[8] = new html_table();
$returntables[8]->head = (array) get_strings(array('pixel_ratio', 'table_percent', 'table_count'), 'report_deviceanalytics');
$weight = [];
foreach ($datas as $devicepointdata) {
$value = (1.0 * $devicepointdata->devicedisplaysizex) / $devicepointdata->devicewindowsizex;
if ($value < 1.0) {
$value = 1.0 / $value;
}
$ptype = sprintf('%01.1f', $value);
if (array_key_exists($ptype, $weight)) {
$weight[$ptype]++;
} else {
$weight[$ptype] = 1;
}
}
arsort($weight, SORT_NUMERIC);
foreach ($weight as $tname => $count) {
$returntables[8]->data[] = array($tname, report_device_analytics_calc_percent($weight, $tname). '%', $count);
}
return $returntables;
}
/**
* Create Canvas Elements for the charts output
* @return Array $returncharts array of canvas elements
*/
function report_deviceanalytics_create_charts() {
$returncharts = array();
$returncharts[0] = '<canvas class="rd_chart" id="chart_devicetypes"></canvas>';
$returncharts[1] = '<canvas class="rd_chart" id="chart_devicesystems"></canvas>';
$returncharts[2] = '<canvas class="rd_chart" id="chart_devicebrowsers"></canvas>';
$returncharts[3] = '<canvas class="rd_chart" id="chart_devicedisplaysize"></canvas>';
$returncharts[4] = '<canvas class="rd_chart" id="chart_devicewindowsize"></canvas>';
$returncharts[5] = null;
$returncharts[6] = null;
$returncharts[7] = null;
$returncharts[8] = null;
return $returncharts;
}