-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserPoints.php
More file actions
221 lines (203 loc) · 6.79 KB
/
userPoints.php
File metadata and controls
221 lines (203 loc) · 6.79 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
<?php
require_once __DIR__ . '/controllers/ErrorHandler.php';
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: access');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Content-Type: application/json; charset=UTF-8');
header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');
require_once __DIR__ . '/database.php';
require_once __DIR__ . '/sendJson.php';
require_once __DIR__ . '/controllers/authHandler.php';
//operazioni admin
$authHandler = new AuthHandler();
if ($_SERVER['REQUEST_METHOD'] == 'GET') :
$id = null;
if(isset($_GET['id'])){
//singolo utente
$id = $_GET['id'];
}
//verifico se filtrare per utente
$whereCase = "";
if($id != null){
$whereCase = "WHERE u.id = '$id'";
}
//verifica se richiesto da superuser
$adminUser = $authHandler->userIsAdmin($connection);
$selectExtraInfo = "";
if($adminUser){
$selectExtraInfo = "u.extra_info,";
}
$sql = "SELECT
u.id AS user_id,
u.email AS nickname,
u.name AS username,
". $selectExtraInfo ."
u.isActive,
COALESCE(mb.total_points, 0) AS total_matches_points,
COALESCE(gb.total_points, 0) AS total_gironi_points,
COALESCE(mfb.total_points, 0) AS total_matches_fin_points,
COALESCE(mfb_bonus_o.total_bonus, 0) AS total_matches_fin_bonus_ottavi_migliori_terze,
COALESCE(mfb_bonus_q.total_bonus, 0) AS total_matches_fin_bonus_quarti,
COALESCE(mfb_bonus_s.total_bonus, 0) AS total_matches_fin_bonus_semi,
COALESCE(mfb_bonus_f.total_bonus, 0) AS total_matches_fin_bonus_final,
COALESCE(mfb_bonus_tot.total_bonus, 0) AS total_matches_fin_bonus_total,
COALESCE(gvb.total_points, 0) AS total_goal_veloce_points,
COALESCE(trb.total_points, 0) AS total_team_rivelaz_points,
COALESCE(cab.total_points, 0) AS total_capo_azz_points,
COALESCE(ceb.total_points, 0) AS total_capo_euro_points,
COALESCE(mb.total_points, 0) + COALESCE(gb.total_points, 0) + COALESCE(mfb.total_points, 0) + COALESCE(mfb_bonus_tot.total_bonus, 0) + COALESCE(gvb.total_points, 0) + COALESCE(trb.total_points, 0) + COALESCE(cab.total_points, 0) + COALESCE(ceb.total_points, 0) AS total_points
FROM
users u
LEFT JOIN
(
SELECT
user_id,
SUM(points) AS total_points
FROM
matches_bet
GROUP BY
user_id
) mb ON u.id = mb.user_id
LEFT JOIN
(
SELECT
user_id,
SUM(points) AS total_points
FROM
gironi_bet
GROUP BY
user_id
) gb ON u.id = gb.user_id
LEFT JOIN
(
SELECT
user_id,
SUM(points) AS total_points
FROM
matches_fin_bet
GROUP BY
user_id
) mfb ON u.id = mfb.user_id
LEFT JOIN
(
SELECT
user_id,
SUM(bonus) AS total_bonus
FROM
matches_fin_bet
GROUP BY
user_id
) mfb_bonus_tot ON u.id = mfb_bonus_tot.user_id
LEFT JOIN
(
SELECT
user_id,
SUM(bonus) AS total_bonus
FROM
matches_fin_bet as mfb
JOIN
matches_fin AS mf ON mfb.match_id = mf.id
WHERE
mf.fase = 2
GROUP BY
user_id
) mfb_bonus_o ON u.id = mfb_bonus_o.user_id
LEFT JOIN
(
SELECT
user_id,
SUM(bonus) AS total_bonus
FROM
matches_fin_bet as mfb
JOIN
matches_fin AS mf ON mfb.match_id = mf.id
WHERE
mf.fase = 3
GROUP BY
user_id
) mfb_bonus_q ON u.id = mfb_bonus_q.user_id
LEFT JOIN
(
SELECT
user_id,
SUM(bonus) AS total_bonus
FROM
matches_fin_bet as mfb
JOIN
matches_fin AS mf ON mfb.match_id = mf.id
WHERE
mf.fase = 4
GROUP BY
user_id
) mfb_bonus_s ON u.id = mfb_bonus_s.user_id
LEFT JOIN
(
SELECT
user_id,
SUM(bonus) AS total_bonus
FROM
matches_fin_bet as mfb
JOIN
matches_fin AS mf ON mfb.match_id = mf.id
WHERE
mf.fase = 5
GROUP BY
user_id
) mfb_bonus_f ON u.id = mfb_bonus_f.user_id
LEFT JOIN
(
SELECT
user_id,
SUM(points) AS total_points
FROM
goal_veloce_bet
GROUP BY
user_id
) gvb ON u.id = gvb.user_id
LEFT JOIN
(
SELECT
user_id,
SUM(points) AS total_points
FROM
team_rivelaz_bet
GROUP BY
user_id
) trb ON u.id = trb.user_id
LEFT JOIN
(
SELECT
user_id,
SUM(points) AS total_points
FROM
capo_azz_bet
GROUP BY
user_id
) cab ON u.id = cab.user_id
LEFT JOIN
(
SELECT
user_id,
SUM(points) AS total_points
FROM
capo_euro_bet
GROUP BY
user_id
) ceb ON u.id = ceb.user_id
".$whereCase."
ORDER BY
u.isActive DESC,
total_points DESC;";
//eseguo query
if($id != null){
$query = mysqli_query($connection, $sql);
$row = mysqli_fetch_array($query, MYSQLI_ASSOC);
if ($row === null) sendJson(404, 'User points non found!');
sendJson(200, '', $row);
}else{
$query = mysqli_query($connection, $sql);
$pointsDict = $query->fetch_all(MYSQLI_ASSOC);
$response = ["points" => $pointsDict];
sendJson(200, '', $response);
}
endif;