This repository was archived by the owner on Oct 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuser_stats.php
More file actions
499 lines (464 loc) · 14.9 KB
/
user_stats.php
File metadata and controls
499 lines (464 loc) · 14.9 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
<script src="includes/utils.js"></script>
<?php
/*
This page is used to show all the stats the user wants to see. It uses GET params to know what users he wants to look and make the stats based on them. The stats shown are different if the user just want to see the stats for one user or for multiple.
Functions are declared at the start.
This page is the less descripted because it was done with not a lot of time and the intention of it was not to make the code clear, but see how far the page could be taken. If we wanted to make it clear, the page would have been constructed by other ways, like all the other pages. Therefore, this page is not designed to be the code example of the application, because of its complexity and extended code.
*/
session_start();
include("includes/header.html");
include('includes/print_messages.php');
/*
Sets the averages for the user chosen in the parameters.
Returns an array of arrays with those averages.
*/
function setUserAverages ($uid, $dbc) {
$q = "SELECT id_quiz, SUM(average)/COUNT(id_user) AS 'total average' FROM `statistics` WHERE id_user=$uid GROUP BY id_quiz";
$r = @mysqli_query ($dbc, $q);
$num = mysqli_num_rows($r);
$user_averages = array('total average' => [], 'total average user' => []);
if ($num > 0) {
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$user_averages ['total average'][] = round(floatval($row['total average']) * 100, 2);
}
}
$q = "SELECT id_user, SUM(average)/COUNT(id_user) AS 'total average' FROM `statistics` WHERE id_user=$uid GROUP BY id_user";
$r = @mysqli_query ($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$user_averages ['total average user'][] = round(floatval($row['total average']) * 100, 2);
}
return $user_averages;
}
/*
Sets the daily averages for the user chosen in the parameters in the range of days also specified in the parameters.
Returns an array of arrays with those averages.
*/
function setDailyUserStats ($uid, $oneweek, $today, $dbc) {
$daily_stats_user = array('daily_total_user' => [], 'daily_avg_user' => [], 'dates' => []);
$q = "SELECT COUNT(id_user) AS 'daily quizzes', SUM(average)/COUNT(id_user) AS 'daily total average', Convert(date, date) AS 'Date' FROM statistics WHERE (date BETWEEN '$oneweek' AND '$today') AND id_user=$uid GROUP BY Date ORDER BY date";
$r = @mysqli_query ($dbc, $q);
if (mysqli_num_rows($r) > 0) {
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$daily_stats_user['daily_total_user'] [] = $row['daily quizzes'];
$daily_stats_user['daily_avg_user'] [] = round(floatval($row['daily total average']) * 100, 2);
$daily_stats_user['dates'] [] = $row['Date'];
}
if (count($daily_stats_user['daily_total_user']) < 7) {
$day = -6;
for ($i=0; $i<7; $i++) {
$set = true;
for ($j=0;$j<count($daily_stats_user['dates']); $j++) {
if ($daily_stats_user['dates'][$j] == date('Y-m-d', strtotime($day.' day'))) $set = false;
}
if ($set) array_splice( $daily_stats_user['daily_total_user'], $i, 0, 0 );
$day++;
}
}
if (count($daily_stats_user['daily_avg_user']) < 7) {
$day = -6;
for ($i=0; $i<7; $i++) {
$set = true;
for ($j=0;$j<count($daily_stats_user['dates']); $j++) {
if ($daily_stats_user['dates'][$j] == date('Y-m-d', strtotime($day.' day'))) $set = false;
}
if ($set) array_splice( $daily_stats_user['daily_avg_user'], $i, 0, 0 );
$day++;
}
}
}
return $daily_stats_user;
}
// If there is some params it will process them
if (isset($_GET['uid']) && is_numeric($_GET['uid'])) {
require ('mysqli_connect.php');
$uid = intval($_GET['uid']);
// Needed to get the stats from the last 7 days, including today.
$oneweek = date('Y-m-d H:i:s',strtotime('-6 day', strtotime(date('Y-m-d')." 00:00:00")));
$today = date('Y-m-d H:i:s', strtotime('tomorrow'));
//var_dump($oneweek);
//var_dump($today);
$colors = ['red','yellow','green','blue','purple'];
$q = "SELECT nick FROM users WHERE id_user=$uid";
$r = @mysqli_query ($dbc, $q);
if (mysqli_num_rows($r) == 1) { // If the user exists.
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
$nick = $row['nick'];
$q = "SELECT title FROM quizzes";
$r = @mysqli_query ($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
// saves all the quizz titles
$quiz_titles [] = $row['title'];
}
// $stats is an array of arrays of arrays.
$stats [$nick] = array('user_averages' => [], 'daily_stats' => []);
$stats [$nick] ['user_averages'] = setUserAverages($uid, $dbc);
$stats [$nick] ['daily_stats'] = setDailyUserStats($uid, $oneweek, $today, $dbc);
// If there is only one user to process, the pie chart and the title will show, otherwise it won't.
if (!isset($_GET['uid1'])) {
$q = "SELECT id_user, COUNT(id_user) AS 'total' FROM `statistics` WHERE id_user=$uid GROUP BY id_user";
$r = @mysqli_query ($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$total_quizzes_user = $row['total'];
}
?>
<div class="row text-center login-title">
<div class="col-sm-12 text-center">
<h1 style="font-size: 4em; text-align: center !important;"><?php echo $nick;?></h1>
<h3>
You played <b><?php echo $total_quizzes_user; ?></b> quizzes. Here are your statistics.
</h3>
</div>
</div>
<div class="container-canvas">
<canvas id="canvas4"></canvas>
<script>
var alone = true;
</script>
<?php
// case where there are more than one user to process.
} else echo "<script>var alone=false;</script><div class='container-canvas'><div class='canvas'><canvas id='canvas5'></canvas></div>";
for ($i = 1; $i<5; $i++){
if (isset($_GET['uid'.$i]) && is_numeric($_GET['uid'.$i])) {
$uid = intval($_GET['uid'.$i]);
$q = "SELECT nick FROM users WHERE id_user=$uid";
$r = @mysqli_query ($dbc, $q);
if (mysqli_num_rows($r) == 1) {
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
$nick = $row['nick'];
$stats [$nick] = array('user_averages' => [], 'daily_stats' => []);
$stats [$nick] ['user_averages'] = setUserAverages($uid, $dbc);
$stats [$nick] ['daily_stats'] = setDailyUserStats($uid, $oneweek, $today, $dbc);
}
} else break;
}
// for all users
$q = "SELECT COUNT(id_user) AS 'total' FROM `users`";
$r = @mysqli_query ($dbc, $q);
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
$total_users = $row['total'];
$q = "SELECT id_quiz, SUM(average)/COUNT(id_user) AS 'total average' FROM `statistics` GROUP BY id_quiz";
$r = @mysqli_query ($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$all_averages [] = round(floatval($row['total average']) * 100, 2);
}
$q = "SELECT COUNT(id_user)/$total_users AS 'daily quizzes', SUM(average)/COUNT(id_user) AS 'daily total average', Convert(date, date) AS 'Date' FROM statistics WHERE (date BETWEEN '$oneweek' AND '$today') GROUP BY Date ORDER BY date";
$r = @mysqli_query ($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$daily_total_all [] = $row['daily quizzes'];
$daily_avg_all [] = round(floatval($row['daily total average']) * 100, 2);
$dates_all [] = $row['Date'];
}
if (count($daily_total_all) < 7) {
$day = -6;
for ($i=0; $i<7; $i++) {
$set = true;
for ($j=0;$j<count($dates_all); $j++) {
if ($dates_all[$j] == date('Y-m-d', strtotime($day.' day'))) $set = false;
}
if ($set) array_splice( $daily_total_all, $i, 0, 0 );
$day++;
}
}
if (count( $daily_avg_all) < 7) {
$day = -6;
for ($i=0; $i<7; $i++) {
$set = true;
for ($j=0;$j<count($dates_all); $j++) {
if ($dates_all[$j] == date('Y-m-d', strtotime($day.' day'))) $set = false;
}
if ($set) array_splice( $daily_avg_all, $i, 0, 0 );
$day++;
}
}
} else echo print_message('danger', 'No register in the last week.');
?>
<canvas id="canvas1"></canvas>
<canvas id="canvas2"></canvas>
<canvas id="canvas3"></canvas>
</div>
<script>
var last7days = [];
for (var i=6; i>=0; i--) {
last7days.push(moment().subtract(i, 'days').format('dddd'));
}
var randomScalingFactor = function() {
return Math.round(Math.random() * 100);
};
// From here the code is combining PHP with JS, as the JS needs the data obtained from the selects done before in PHP.
var color = Chart.helpers.color;
var config1 = {
type: 'radar',
data: {
labels:<?php
echo "[";
foreach ($quiz_titles as $key => $quiz_title) {
echo "'$quiz_title'";
if ($key != count($quiz_titles)) echo ",";
}
echo "]";
?>,
datasets: [<?php $i=0; foreach ($stats as $nick => $value) {?>{
label: "<?php echo $nick;?>",
backgroundColor: color(window.chartColors.<?php echo $colors[$i]; ?>).alpha(0.2).rgbString(),
borderColor: window.chartColors.<?php echo $colors[$i]; ?>,
pointBackgroundColor: window.chartColors.<?php echo $colors[$i]; ?>,
data: [<?php
foreach ($stats[$nick]['user_averages']['total average'] as $avg) {
echo $avg;
if ($key != count($avg)) echo ',';
}?>]
},<?php $i++; } ?>{
label: "All users average",
backgroundColor: color(window.chartColors.grey).alpha(0.2).rgbString(),
borderColor: window.chartColors.grey,
pointBackgroundColor: window.chartColors.grey,
data: [<?php foreach ($all_averages as $key => $avg) {
echo "$avg, ";
}?>]
}
]
},
options: {
title: {
display: true,
text: 'Comparation chart in % of correct answers per quiz'
},
scale: {
ticks: {
beginAtZero: true,
max: 100,
min: 0,
stepSize: 20
}
}
}
};
var config2 = {
type: 'line',
data: {
labels: last7days,
datasets: [<?php $i=0; $max=0; foreach ($stats as $nick => $value) {?>{
label: "<?php echo $nick;?>",
backgroundColor: window.chartColors.<?php echo $colors[$i]; ?>,
borderColor: window.chartColors.<?php echo $colors[$i]; ?>,
data: [<?php
foreach ($stats[$nick]['daily_stats']['daily_total_user'] as $avg) {
echo $avg;
if ($avg > $max) $max = $avg;
if ($key != count($avg)) echo ',';
}?>],
fill: false,
},<?php $i++; } ?>{
label: "Users average",
fill: false,
backgroundColor: window.chartColors.grey,
borderColor: window.chartColors.grey,
data: [
<?php
foreach ($daily_total_all as $daily) {
echo "$daily, ";
}
?>
],
}]
},
options: {
responsive: true,
title:{
display:true,
text:'Total quizzes done per day last week'
},
tooltips: {
mode: 'index',
intersect: false,
},
hover: {
mode: 'nearest',
intersect: true
},
scales: {
xAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Day'
}
}],
yAxes: [{
ticks : {
beginAtZero: true,
max: <?php echo $max;?> + 1
},
display: true,
scaleLabel: {
display: true,
labelString: 'Total quizzes'
}
}]
}
}
};
var config3 = {
type: 'line',
data: {
labels: last7days,
datasets: [<?php $i=0; foreach ($stats as $nick => $value) {?>{
label: "<?php echo $nick;?>",
backgroundColor: window.chartColors.<?php echo $colors[$i]; ?>,
borderColor: window.chartColors.<?php echo $colors[$i]; ?>,
data: [<?php
foreach ($stats[$nick]['daily_stats']['daily_avg_user'] as $avg) {
echo $avg;
if ($key != count($avg)) echo ',';
}?>],
fill: false,
},<?php $i++; } ?>{
label: "Users average",
fill: false,
backgroundColor: window.chartColors.grey,
borderColor: window.chartColors.grey,
data: [
<?php
foreach ($daily_avg_all as $daily_avg) {
echo "$daily_avg, ";
}
?>
],
}]
},
options: {
responsive: true,
title:{
display:true,
text:'Average on quizzes per day last week'
},
tooltips: {
mode: 'index',
intersect: false,
},
hover: {
mode: 'nearest',
intersect: true
},
scales: {
xAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Day'
}
}],
yAxes: [{
ticks: {
beginAtZero: true,
max: 100,
min: 0,
stepSize: 10
},
display: true,
scaleLabel: {
display: true,
labelString: '% of correct answers'
}
}]
}
}
};
//pie
var config4 = {
type: 'pie',
data: {
datasets: [{
data: [<?php
echo $stats[$nick]['user_averages']['total average user'][0].',';
$total = 100 - $stats[$nick]['user_averages']['total average user'][0];
echo $total;
?>],
backgroundColor: [
window.chartColors.red,
window.chartColors.blue
]
}],
labels: [
"Correct",
"Wrong"
]
},
options: {
title: {
display: true,
text: 'General % of correct answers'
},
responsive: true
}
};
//radar
var chartColors = window.chartColors;
var color = Chart.helpers.color;
var config5 = {
data: {
datasets: [{
data: [
<?php
$i=0;
foreach ($stats as $nick => $value) {
foreach ($stats[$nick]['user_averages']['total average user'] as $avg) {
echo $avg;
if ($key != count($avg)) echo ',';
}
$i++;
}
?>
],
backgroundColor: [
<?php
$i=0;
foreach ($stats as $nick => $value) {
foreach ($stats[$nick]['user_averages']['total average user'] as $avg) {
?>
color(chartColors.<?php echo $colors[$i]; ?>).alpha(0.5).rgbString()
<?php
if ($key != count($avg)) echo ',';
}
$i++;
}
?>
]
}],
labels: [
<?php $i=0; foreach ($stats as $nick => $value) {
echo '"'.$nick.'",';
}?>
]
},
options: {
responsive: true,
title: {
display: true,
text: 'General % of correct answers'
},
scale: {
ticks: {
beginAtZero: true,
max: 100
},
reverse: false
},
animation: {
animateRotate: true,
animateScale: true
}
}
};
window.onload = function() {
window.myRadar = new Chart(document.getElementById("canvas1"), config1);
window.myLine1 = new Chart(document.getElementById("canvas2").getContext("2d"), config2);
window.myLine2 = new Chart(document.getElementById("canvas3").getContext("2d"), config3);
if (alone) window.myPie = new Chart(document.getElementById("canvas4").getContext("2d"), config4);
else window.myPolarArea = Chart.PolarArea(document.getElementById("canvas5"), config5);
};
</script>
<?php
} else echo print_message('danger', 'No user selected.');
include("includes/footer.html");
?>