-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistic.php
150 lines (129 loc) · 5.97 KB
/
statistic.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
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.2/dist/chart.umd.min.js"></script>
<title>W | Statistics</title>
</head>
<body>
<?php
include "header.php";
if (isset($_SESSION['ID_user'])) {?>
<div class="container text-center">
<h1>Statistiques</h1>
<div class="row mx-2">
<div class="col-4"><h3>Total des interactions</h3><canvas id="interactionstats"></canvas></div>
<div class="col-4"></div>
<div class="col-4"><h3>Posts par semaines et par mois</h3><canvas id="postweekstats"></canvas><canvas id="postmonthstats"></canvas></div>
</div>
</div>
<script type="module">
(async function() {
const monthName = (month) => {
const months = ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'];
return months[month - 1];
}
const globaldata = <?php
// get the number of likes received by the user
$sql = "SELECT COUNT(*) AS likesrecieved FROM `like` WHERE `like`.`ID_post` IN (SELECT ID FROM post WHERE ID_user = ?);";
$qry = $db->prepare($sql);
$qry->execute([$_SESSION['ID_user']]);
$likesrecieved = $qry->fetch(PDO::FETCH_ASSOC)['likesrecieved'];
$sql = "SELECT COUNT(*) AS likesgiven FROM `like` WHERE `like`.`ID_user` = ?;";
$qry = $db->prepare($sql);
$qry->execute([$_SESSION['ID_user']]);
$likesgiven = $qry->fetch(PDO::FETCH_ASSOC)['likesgiven'];
$sql = "SELECT COUNT(*) AS dislikesrecieved FROM `dislike` WHERE `dislike`.`ID_post` IN (SELECT ID FROM post WHERE ID_user = ?);";
$qry = $db->prepare($sql);
$qry->execute([$_SESSION['ID_user']]);
$dislikesrecieved = $qry->fetch(PDO::FETCH_ASSOC)['dislikesrecieved'];
$sql = "SELECT COUNT(*) AS dislikesgiven FROM `dislike` WHERE `dislike`.`ID_user` = ?;";
$qry = $db->prepare($sql);
$qry->execute([$_SESSION['ID_user']]);
$dislikesgiven = $qry->fetch(PDO::FETCH_ASSOC)['dislikesgiven'];
$sql = "SELECT COUNT(*) AS followers FROM follow WHERE ID_followed = ?;";
$qry = $db->prepare($sql);
$qry->execute([$_SESSION['ID_user']]);
$followers = $qry->fetch(PDO::FETCH_ASSOC)['followers'];
$sql = "SELECT COUNT(*) AS following FROM follow WHERE ID_user = ?;";
$qry = $db->prepare($sql);
$qry->execute([$_SESSION['ID_user']]);
$following = $qry->fetch(PDO::FETCH_ASSOC)['following'];
$list = [
['label' => 'Likes reçus', 'value' => $likesrecieved],
['label' => 'Likes donnés', 'value' => $likesgiven],
['label' => 'Dislikes reçus', 'value' => $dislikesrecieved],
['label' => 'Dislikes donnés', 'value' => $dislikesgiven],
['label' => 'Followers', 'value' => $followers],
['label' => 'Following', 'value' => $following]
];
echo json_encode($list);
?>
new Chart(
document.getElementById('interactionstats'), {
type: 'doughnut',
data: {
datasets: [{
label: ' ',
data: globaldata.map(x => x.value),
hoverOffset: 4
}],
labels: globaldata.map(x => x.label)
}
}
);
const weeksdata = <?php
$sql = "SELECT COUNT(*) AS posts, WEEK(date) AS week FROM post
WHERE ID_user = ? AND isDeleted = 0
GROUP BY week";
$qry = $db->prepare($sql);
$qry->execute([$_SESSION['ID_user']]);
$weeksdata = $qry->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($weeksdata);
?>;
new Chart(
document.getElementById('postweekstats'), {
data: {
datasets: [
{
type: 'line',
label: 'Posts et commentaires par semaines',
data: weeksdata.map(x => x.posts),
tension: 0.1
}
],
labels: weeksdata.map(x => x.week)
}
}
);
const monthdata = <?php
$sql = "SELECT COUNT(*) AS posts, MONTH(date) AS month FROM post
WHERE ID_user = ? AND isDeleted = 0
GROUP BY month";
$qry = $db->prepare($sql);
$qry->execute([$_SESSION['ID_user']]);
$monthdata = $qry->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($monthdata);
?>;
new Chart(
document.getElementById('postmonthstats'), {
data: {
datasets: [
{
type: 'line',
label: 'Posts et commentaires par mois',
data: monthdata.map(x => x.posts),
tension: 0.1
}
],
labels: monthdata.map(x => monthName(x.month))
}
}
);
})();
</script>
<?php }
?>
</body>
</html>