Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 *  Internal library of functions for module
19
 */
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
require_once($CFG->libdir.'/adminlib.php');
24
require_once($CFG->libdir.'/formslib.php');
25
require_once('admin/dashboard_time_form.php');
26
 
27
/**
28
 * Loads the saved data from report_training_data
29
 * @param int $starttime filters entries with starttime
30
 * @param int $endtime filters entries with endtime
31
 * @return Array $chartsdata of records or null
32
 */
33
function report_training_load_datas($starttime = null, $endtime = null) {
34
    global $DB;
35
 
36
    if (empty($starttime) || is_null($starttime)) {
37
        $conf = $DB->get_record('report_training', array());
38
        $starttime = $conf->starttime;
39
    }
40
    if (empty($endtime) || is_null($endtime)) {
41
        $endtime = time();
42
    }
43
    $chartsdata = $DB->get_records_sql('SELECT * FROM {report_training_data} WHERE objectdate >= ? AND objectdate <= ?',
44
        array($starttime, $endtime));
45
    if (! empty($chartsdata) || ! is_null($chartsdata)) {
46
 
47
        //var_dump($chartsdata);
48
 
49
        return $chartsdata;
50
    } else {
51
        return null;
52
    }
53
}
54
 
55
/**
56
 * Creates the Outputcontainer for dashboard.php
57
 * @param Array $chartout array of containers
58
 * @param Array $vtables array of html_tables
59
 * @return String all containers for output
60
 */
61
function report_training_create_containers($chartout, $vtables) {
62
    $vout = array();
63
    $vout[] = report_deviceanaltics_create_wrapper_container(
64
        'device_types',
65
        'dashboard_chart_device_types',
66
        $chartout[0],
67
        $vtables[0]);
68
    $vout[] = report_deviceanaltics_create_wrapper_container(
69
        'device_systems',
70
        'dashboard_chart_device_systems',
71
        $chartout[1],
72
        $vtables[1]);
73
    $vout[] = report_deviceanaltics_create_wrapper_container(
74
        'device_browser',
75
        'dashboard_chart_device_browser',
76
        $chartout[2],
77
        $vtables[2]);
78
    $vout[] = report_deviceanaltics_create_wrapper_container(
79
        'screen_sizes',
80
        'dashboard_chart_screen_sizes',
81
        $chartout[3],
82
        $vtables[3]);
83
    $vout[] = report_deviceanaltics_create_wrapper_container(
84
        'window_sizes',
85
        'dashboard_chart_window_sizes',
86
        $chartout[4],
87
        $vtables[4]);
88
    $vout[] = report_deviceanaltics_create_wrapper_container(
89
        'pointing_method',
90
        'dashboard_chart_pointing_method',
91
        $chartout[5],
92
        $vtables[5]);
93
    return $vout;
94
}
95
 
96
 
97
/**
98
 * Check if object holds screen data
99
 * @param Object $var entryobject
100
 * @return bool true/false
101
 * @deprecated
102
 */
103
function report_device_analytics_is_not_null($var) {
104
    return !is_null($var->devicedisplaysizex);
105
}
106
 
107
 
108
/**
109
 * Calculate the number of subversion inside array
110
 * @param Array $datarray array ob records
111
 * @return int count of subversion
112
 */
113
function report_device_analytics_calc_numbers_of_version($datarray) {
114
    $returnnumber = 0;
115
    foreach ($datarray as $value) {
116
         $returnnumber += $value;
117
    }
118
    return $returnnumber;
119
}
120
 
121
/**
122
 * Calculate the percent for element inside array
123
 * @param Array $datarray array with prechecked elements
124
 * @param String $calckey calc elementkey
125
 * @return float percent of searched value
126
 */
127
function report_device_analytics_calc_percent($datarray, $calckey) {
128
    $groundvalue = 0;
129
    $procvalue = $datarray[$calckey];
130
    foreach ($datarray as $val) {
131
        $groundvalue += $val;
132
    }
133
    return number_format((($procvalue / $groundvalue) * 100) , 2);
134
}
135
 
136
/**
137
 * Same as report_device_analytics_calc_percent, but from subarray
138
 * @param Array $datarray array with prechecked elements
139
 * @param String $calckey calc elementkey
140
 * @return float percent of searched value
141
 */
142
function report_device_analytics_calc_percent_from_sub($datarray, $calckey) {
143
    $groundvalue = 0;
144
    $procsub = array();
145
    foreach ($datarray as $key => $subvalue) {
146
        $subval = 0;
147
        foreach ($subvalue as $value) {
148
            $subval += $value;
149
        }
150
        $procsub[$key] = $subval;
151
        $groundvalue += $subval;
152
    }
153
    return number_format((($procsub[$calckey] / $groundvalue) * 100) , 2);
154
}
155
 
156
/**
157
 * Creates the wrapper container for charts and tables - also write tables
158
 * @param String $wrappername name of the div wrapper
159
 * @param String $headername heading line - from moodle/lang
160
 * @param String $chartoutput holds all information for the charts
161
 * @param html_table $vtables table object for html_writer
162
 * @return String $oretrun output-string
163
 */
164
function report_deviceanaltics_create_wrapper_container($wrappername, $headername, $chartoutput, $vtables) {
165
    global $OUTPUT;
166
    $oreturn = $OUTPUT->heading(get_string($headername, 'report_training'), 4);
167
    $oreturn .= $OUTPUT->container_start('wrapper', $wrappername);
168
    if (! empty($chartoutput) || ! is_null($chartoutput)) {
169
        $oreturn .= $chartoutput;
170
    }
171
    if (! empty($vtables) || ! is_null($vtables)) {
172
        $oreturn .= html_writer::table($vtables);
173
    }
174
    $oreturn .= $OUTPUT->container_end();
175
    return $oreturn;
176
}
177
 
178
/**
179
 * Calculate the Tables
180
 * @param Array $datas - preselected entries form data table
181
 * @return String $returntables output-string
182
 */
183
function report_training_create_data_tables($datas) {
184
    $returntables = array();
185
    $returntables[0] = new html_table();
186
    $returntables[0]->head = (array) get_strings(array('table_type', 'table_percent', 'table_count'), 'report_training');
187
    $devicetypes = array();
188
    foreach ($datas as $devicetypedata) {
189
        $type = $devicetypedata->devicetype;
190
 
191
        if (array_key_exists($type, $devicetypes)) {
192
            $devicetypes[$devicetypedata->devicetype]++;
193
        } else {
194
            $devicetypes[$devicetypedata->devicetype] = 1;
195
        }
196
    }
197
    foreach ($devicetypes as $tname => $count) {
198
        $returntables[0]->data[] = array($tname, report_device_analytics_calc_percent($devicetypes, $tname)."%", $count);
199
    }
200
 
201
    $returntables[1] = new html_table();
202
    $returntables[1]->head = (array) get_strings(array('table_os', 'table_percent', 'table_count'), 'report_training');
203
    $deviceoses = array();
204
    foreach ($datas as $deviceosdata) {
205
        $type = $deviceosdata->devicesystem;
206
        if (array_key_exists($type, $deviceoses)) {
207
            $deviceoses[$deviceosdata->devicesystem]++;
208
        } else {
209
            $deviceoses[$deviceosdata->devicesystem] = 1;
210
        }
211
    }
212
    foreach ($deviceoses as $tname => $count) {
213
        $returntables[1]->data[] = array($tname, report_device_analytics_calc_percent($deviceoses, $tname)."%", $count);
214
    }
215
 
216
    $returntables[2] = new html_table();
217
    $returntables[2]->head = (array) get_strings(array('table_browser', 'table_percent', 'table_count'), 'report_training');
218
    $devicebrowser = array();
219
 
220
    foreach ($datas as $devicebrowserdata) {
221
        $browser = $devicebrowserdata->devicebrowser;
222
        $browserversion = $devicebrowserdata->devicebrowserversion;
223
        if (array_key_exists($browser, $devicebrowser)) {
224
            if (array_key_exists($devicebrowserdata->devicebrowserversion, $devicebrowser[$devicebrowserdata->devicebrowser])) {
225
                $devicebrowser[$devicebrowserdata->devicebrowser][$devicebrowserdata->devicebrowserversion]++;
226
            } else {
227
                $devicebrowser[$devicebrowserdata->devicebrowser][$devicebrowserdata->devicebrowserversion] = 1;
228
            }
229
        } else {
230
            $devicebrowser[$devicebrowserdata->devicebrowser] = array();
231
            $devicebrowser[$devicebrowserdata->devicebrowser][$devicebrowserdata->devicebrowserversion] = 1;
232
        }
233
    }
234
    foreach ($devicebrowser as $bname => $sub) {
235
        $returntables[2]->data[] = array(
236
            '<b>'.$bname.'</b>',
237
            report_device_analytics_calc_percent_from_sub($devicebrowser, $bname).'%',
238
            report_device_analytics_calc_numbers_of_version($sub)
239
        );
240
        foreach ($sub as $vnum => $scount) {
241
            $returntables[2]->data[] = array(
242
                get_string('table_version', 'report_training').': '.$vnum,
243
                report_device_analytics_calc_percent($sub, $vnum).'%',
244
                $scount
245
            );
246
        }
247
    }
248
 
249
    $returntables[3] = new html_table(); //era null
250
    $returntables[3] ->head = (array) get_strings(array('table_userid', 'table_name', 'table_fecha'), 'report_training'); //null;
251
    $usuarios = array();
252
    foreach ($datas as $userdata) {
253
        $userid = $userdata->userid;
254
 
255
        // if (array_key_exists($userid, $usuarios)) {
256
            $usuarios[$userdata->userid]++;
257
        // } else {
258
            // $usuarios[$userdata->userid] = 1;
259
        // }
260
    }
261
 
262
    foreach ($usuarios as $tname => $count) {
263
        $returntables[3]->data[] = array($tname, report_device_analytics_calc_percent($devicetypes, $tname)."%", $count);
264
    }
265
 
266
 
267
    $returntables[5] = new html_table();
268
    $returntables[5]->head = (array) get_strings(array('table_pointing', 'table_percent', 'table_count'), 'report_training');
269
    $devicepointing = array();
270
    foreach ($datas as $devicepointdata) {
271
        $ptype = $devicepointdata->devicepointingmethod;
272
        if (array_key_exists($ptype, $devicepointing)) {
273
            $devicepointing[$devicepointdata->devicepointingmethod]++;
274
        } else {
275
            $devicepointing[$devicepointdata->devicepointingmethod] = 1;
276
        }
277
    }
278
    foreach ($devicepointing as $tname => $count) {
279
        $returntables[5]->data[] = array($tname, report_device_analytics_calc_percent($devicepointing, $tname)."%", $count);
280
    }
281
 
282
    return $returntables;
283
}
284
 
285
/**
286
 * Create Canvas Elements for the charts output
287
 * @return Array $returncharts array of canvas elements
288
 */
289
function report_training_create_charts() {
290
    $returncharts = array();
291
    $returncharts[0] = '<canvas class="rd_chart" id="chart_devicetypes"></canvas>';
292
    $returncharts[1] = '<canvas class="rd_chart" id="chart_devicesystems"></canvas>';
293
    $returncharts[2] = '<canvas class="rd_chart" id="chart_devicebrowsers"></canvas>';
294
    $returncharts[3] = '<canvas class="rd_chart" id="chart_devicedisplaysize"></canvas>';
295
    $returncharts[4] = '<canvas class="rd_chart" id="chart_devicewindowsize"></canvas>';
296
    $returncharts[5] = null;
297
    return $returncharts;
298
}
299
 
300
/**
301
 * Loads the saved data from report_training_data
302
 * @param string $userid filters entries by user name
303
 * @return Array $conf of records or null
304
 */
305
function report_training_get_user_connections($userid = null) {
306
    global $DB;
307
    if ($userid !== null) {
308
        $conf = $DB->get_records('report_training_data', ['userid' => (int) $userid], '', 'devicesystem,devicebrowser,devicetype,ip');
309
        return $conf;
310
    } else {
311
        return null;
312
    }
313
}
314
// TODO: CAMBIAR CONSULTA DEL LAST ACCES A FORM_DATA
315
/**
316
 * Loads the saved data from report_training_activities
317
 * @param string $userid filters entries by user id
318
 * @return Array $conf of records or null
319
 */
320
function report_training_get_user_courses_activity($userid = null) {
321
    global $DB;
322
    if ($userid !== null) {
323
        $user_last_access = $DB->get_records('report_training_activities', ["userid" => (int) $userid]);
324
 
325
        foreach($user_last_access as $record) {
326
            $course = $DB->get_record('course', ["id" => $record->courseid], 'fullname');
327
            if ($course) {
328
                $coursename = $course->fullname;
329
                $coursemodules = get_fast_modinfo($record->courseid);
330
                $module = $coursemodules->get_cm($record->cmid);
331
                $record->timeaccess = date("d-m-Y", $record->timeaccess);
332
                $record->coursename = $coursename;
333
                $record->modname = $module->name;
334
            } else {
335
                $record->coursename = null;
336
            }
337
        }
338
 
339
        return $user_last_access;
340
    } else {
341
        return null;
342
    }
343
}
344
 
345
/**
346
 * Creates tables with user connections data
347
 * @param Array $userconnections provides data to create the table
348
 * @return html_table $table element to display user data or null
349
 */
350
function report_training_create_user_connections_table($userconnections = null) {
351
 
352
    if ($userconnections !== null) {
353
        $table = new html_table();
354
 
355
        $table->head = array(get_string('table_os', 'report_training'), get_string('table_browser', 'report_training'), get_string('dashboard_chart_device_types', 'report_training'), get_string('userdata_table_ip', 'report_training'));
356
 
357
        foreach($userconnections as $connectioninfo) {
358
            $table->data[] = array($connectioninfo->devicesystem, $connectioninfo->devicebrowser, $connectioninfo->devicetype, $connectioninfo->ip);
359
        }
360
        return $table;
361
    } else {
362
        return null;
363
    }
364
}
365
 
366
/**
367
 * Creates tables with user activity data
368
 * @param Array $useractivityrecords provides data to create the table
369
 * @return html_table $table element to display user data or null
370
 */
371
function report_training_create_activity_user_table($useractivityrecords = null) {
372
    if ($useractivityrecords !== null) {
373
        $table = new html_table();
374
 
375
        $table->head = array(get_string('userdata_table_viewed_date', 'report_training'), get_string('userdata_table_course_name', 'report_training'), get_string('userdata_table_module_name', 'report_training'), get_string('userdata_table_module_link', 'report_training'));
376
 
377
        foreach($useractivityrecords as $record) {
378
            if ($record->coursename) {
379
                $table->data[] = array($record->timeaccess, $record->coursename, $record->modname,'<a class="report-training-table-link" href='.$record->viewurl.'>'.get_string('userdata_table_module_click', 'report_training') . '</a>');
380
            }
381
        }
382
 
383
        return $table;
384
    } else {
385
        return null;
386
    }
387
}