-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatistics.php
More file actions
434 lines (402 loc) · 18 KB
/
Copy pathstatistics.php
File metadata and controls
434 lines (402 loc) · 18 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
<?php
/**
* Statistics Page
*
* Display various statistics with filtering:
* - Date range filter
* - User filter
* - Hotel statistics
* - Activity charts
*/
// ============================================================================
// PHP CODE - Business Logic
// ============================================================================
require_once('lib/db_connection.php');
require_once('lib/config.php');
require_once('lib/SessionManager.php');
$sessionManager = new SessionManager($connection);
$sessionManager->checkRememberMe();
$isLoggedIn = $sessionManager->isLoggedIn();
$username = $sessionManager->getUsername();
$userId = $sessionManager->getUserId();
// Only logged-in users
if (!$isLoggedIn) {
header('Location: login.php');
exit;
}
// Get filters
$filterDateFrom = isset($_GET['date_from']) ? trim($_GET['date_from']) : date('Y-m-01'); // First day of month
$filterDateTo = isset($_GET['date_to']) ? trim($_GET['date_to']) : date('Y-m-d'); // Today
$filterUser = isset($_GET['user_id']) ? intval($_GET['user_id']) : null;
// Convert dates to Unix timestamps
$timestampFrom = strtotime($filterDateFrom . ' 00:00:00');
$timestampTo = strtotime($filterDateTo . ' 23:59:59');
// Get hotel statistics
$hotelStats = [
'total' => 0,
'total_capacity' => 0,
'total_rooms' => 0,
'total_guests' => 0,
'available_rooms' => 0,
'occupancy_rate' => 0
];
$result = $conn->query("SELECT COUNT(*) as total,
SUM(kapacitet) as total_capacity,
SUM(broj_soba) as total_rooms,
SUM(broj_gostiju) as total_guests,
SUM(slobodno_soba) as available_rooms
FROM hotels");
if ($row = $result->fetch_assoc()) {
$hotelStats = $row;
$hotelStats['occupancy_rate'] = $hotelStats['total_rooms'] > 0
? round((($hotelStats['total_rooms'] - $hotelStats['available_rooms']) / $hotelStats['total_rooms']) * 100, 2)
: 0;
}
// Get activity statistics (from audit log) with filters
$whereClauses = ["timestamp_unix BETWEEN ? AND ?"];
$params = [$timestampFrom, $timestampTo];
$types = 'ii';
if ($filterUser) {
$whereClauses[] = "user_id = ?";
$params[] = $filterUser;
$types .= 'i';
}
$whereClause = implode(' AND ', $whereClauses);
// Total activity
$stmt = $conn->prepare("SELECT COUNT(*) as total FROM audit_log WHERE $whereClause");
$stmt->bind_param($types, ...$params);
$stmt->execute();
$totalActivity = $stmt->get_result()->fetch_assoc()['total'];
$stmt->close();
// Activity by action
$stmt = $conn->prepare("SELECT action, COUNT(*) as count FROM audit_log WHERE $whereClause GROUP BY action");
$stmt->bind_param($types, ...$params);
$stmt->execute();
$activityByAction = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();
$activityStats = [
'INSERT' => 0,
'UPDATE' => 0,
'DELETE' => 0
];
foreach ($activityByAction as $action) {
$activityStats[$action['action']] = $action['count'];
}
// Activity by date (last 7 days in range)
$stmt = $conn->prepare("SELECT DATE(FROM_UNIXTIME(timestamp_unix)) as date, COUNT(*) as count
FROM audit_log
WHERE $whereClause
GROUP BY DATE(FROM_UNIXTIME(timestamp_unix))
ORDER BY date DESC
LIMIT 7");
$stmt->bind_param($types, ...$params);
$stmt->execute();
$activityByDate = array_reverse($stmt->get_result()->fetch_all(MYSQLI_ASSOC));
$stmt->close();
// Top users by activity
$stmt = $conn->prepare("SELECT u.username, COUNT(a.id) as count
FROM audit_log a
JOIN users u ON a.user_id = u.id
WHERE a.timestamp_unix BETWEEN ? AND ?
GROUP BY u.id
ORDER BY count DESC
LIMIT 5");
$stmt->bind_param('ii', $timestampFrom, $timestampTo);
$stmt->execute();
$topUsers = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();
// Cities statistics
$citiesStats = $conn->query("SELECT grad, COUNT(*) as count, SUM(kapacitet) as capacity
FROM hotels
GROUP BY grad
ORDER BY count DESC
LIMIT 10")->fetch_all(MYSQLI_ASSOC);
// Counties statistics
$countiesStats = $conn->query("SELECT zupanija, COUNT(*) as count
FROM hotels
GROUP BY zupanija
ORDER BY count DESC
LIMIT 10")->fetch_all(MYSQLI_ASSOC);
// Get all users for filter
$allUsers = $conn->query("SELECT id, username FROM users ORDER BY username")->fetch_all(MYSQLI_ASSOC);
// Page variables
$pageTitle = 'Statistika - Hotel Management';
$currentPage = 'statistics';
$customCSS = "
.stat-card {
border: none;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
padding: 20px;
text-align: center;
margin-bottom: 20px;
transition: all 0.3s;
}
.stat-card:hover {
transform: translateY(-5px);
box-shadow: 0 5px 20px rgba(0,0,0,0.15);
}
.stat-value {
font-size: 2.5rem;
font-weight: 700;
margin: 10px 0;
}
.chart-container {
position: relative;
height: 300px;
}
.progress-label {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 5px;
}
";
?>
<?php include 'templates/header.php'; ?>
<div class="container py-4">
<div class="row mb-4">
<div class="col">
<h2><i class="bi bi-graph-up-arrow"></i> Statistika</h2>
<p class="text-muted">Pregled statistike hotela i aktivnosti korisnika</p>
</div>
</div>
<!-- Filters -->
<div class="card mb-4">
<div class="card-header bg-primary text-white">
<h6 class="mb-0"><i class="bi bi-funnel-fill"></i> Filtriranje Podataka</h6>
</div>
<div class="card-body">
<form method="GET" action="statistics.php" class="row g-3">
<div class="col-md-4">
<label class="form-label">Od datuma:</label>
<input type="date" name="date_from" class="form-control" value="<?php echo htmlspecialchars($filterDateFrom); ?>" required>
</div>
<div class="col-md-4">
<label class="form-label">Do datuma:</label>
<input type="date" name="date_to" class="form-control" value="<?php echo htmlspecialchars($filterDateTo); ?>" required>
</div>
<div class="col-md-2">
<label class="form-label">Korisnik:</label>
<select name="user_id" class="form-select">
<option value="">Svi korisnici</option>
<?php foreach ($allUsers as $user): ?>
<option value="<?php echo $user['id']; ?>" <?php echo $filterUser == $user['id'] ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($user['username']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-2">
<label class="form-label"> </label>
<button type="submit" class="btn btn-primary w-100">
<i class="bi bi-search"></i> Filtriraj
</button>
</div>
</form>
</div>
</div>
<!-- Hotel Statistics -->
<h4 class="mb-3"><i class="bi bi-building-fill"></i> Statistika Hotela</h4>
<div class="row mb-4">
<div class="col-md-2">
<div class="stat-card" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white;">
<i class="bi bi-building-fill" style="font-size: 2rem;"></i>
<div class="stat-value"><?php echo $hotelStats['total']; ?></div>
<div>Ukupno Hotela</div>
</div>
</div>
<div class="col-md-2">
<div class="stat-card" style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); color: white;">
<i class="bi bi-people-fill" style="font-size: 2rem;"></i>
<div class="stat-value"><?php echo number_format($hotelStats['total_capacity']); ?></div>
<div>Kapacitet</div>
</div>
</div>
<div class="col-md-2">
<div class="stat-card" style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%); color: white;">
<i class="bi bi-door-open-fill" style="font-size: 2rem;"></i>
<div class="stat-value"><?php echo number_format($hotelStats['total_rooms']); ?></div>
<div>Ukupno Soba</div>
</div>
</div>
<div class="col-md-2">
<div class="stat-card" style="background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%); color: white;">
<i class="bi bi-person-fill" style="font-size: 2rem;"></i>
<div class="stat-value"><?php echo number_format($hotelStats['total_guests']); ?></div>
<div>Trenutni Gosti</div>
</div>
</div>
<div class="col-md-2">
<div class="stat-card" style="background: linear-gradient(135deg, #fa709a 0%, #fee140 100%); color: white;">
<i class="bi bi-check-circle-fill" style="font-size: 2rem;"></i>
<div class="stat-value"><?php echo number_format($hotelStats['available_rooms']); ?></div>
<div>Slobodne Sobe</div>
</div>
</div>
<div class="col-md-2">
<div class="stat-card" style="background: linear-gradient(135deg, #30cfd0 0%, #330867 100%); color: white;">
<i class="bi bi-percent" style="font-size: 2rem;"></i>
<div class="stat-value"><?php echo $hotelStats['occupancy_rate']; ?>%</div>
<div>Popunjenost</div>
</div>
</div>
</div>
<!-- Activity Statistics -->
<h4 class="mb-3"><i class="bi bi-activity"></i> Aktivnost (<?php echo date('d.m.Y', $timestampFrom); ?> - <?php echo date('d.m.Y', $timestampTo); ?>)</h4>
<?php if ($filterUser): ?>
<div class="alert alert-info">
<i class="bi bi-filter-circle"></i> Prikazana aktivnost za korisnika:
<strong><?php echo htmlspecialchars(array_filter($allUsers, fn($u) => $u['id'] == $filterUser)[0]['username'] ?? 'Unknown'); ?></strong>
</div>
<?php endif; ?>
<div class="row mb-4">
<div class="col-md-3">
<div class="stat-card" style="background: #6c757d; color: white;">
<i class="bi bi-activity" style="font-size: 2rem;"></i>
<div class="stat-value"><?php echo $totalActivity; ?></div>
<div>Ukupno Promjena</div>
</div>
</div>
<div class="col-md-3">
<div class="stat-card" style="background: #28a745; color: white;">
<i class="bi bi-plus-circle-fill" style="font-size: 2rem;"></i>
<div class="stat-value"><?php echo $activityStats['INSERT']; ?></div>
<div>INSERT</div>
</div>
</div>
<div class="col-md-3">
<div class="stat-card" style="background: #ffc107; color: white;">
<i class="bi bi-pencil-fill" style="font-size: 2rem;"></i>
<div class="stat-value"><?php echo $activityStats['UPDATE']; ?></div>
<div>UPDATE</div>
</div>
</div>
<div class="col-md-3">
<div class="stat-card" style="background: #dc3545; color: white;">
<i class="bi bi-trash-fill" style="font-size: 2rem;"></i>
<div class="stat-value"><?php echo $activityStats['DELETE']; ?></div>
<div>DELETE</div>
</div>
</div>
</div>
<div class="row">
<!-- Activity by Date -->
<div class="col-md-6 mb-4">
<div class="card">
<div class="card-header bg-info text-white">
<h6 class="mb-0"><i class="bi bi-calendar3"></i> Aktivnost po Danima (Zadnjih 7)</h6>
</div>
<div class="card-body">
<?php if (!empty($activityByDate)): ?>
<?php
$maxCount = max(array_column($activityByDate, 'count'));
foreach ($activityByDate as $day):
$percentage = $maxCount > 0 ? ($day['count'] / $maxCount) * 100 : 0;
?>
<div class="mb-3">
<div class="progress-label">
<span><?php echo date('d.m.Y', strtotime($day['date'])); ?></span>
<span class="badge bg-info"><?php echo $day['count']; ?> promjena</span>
</div>
<div class="progress" style="height: 25px;">
<div class="progress-bar bg-info" style="width: <?php echo $percentage; ?>%"></div>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<p class="text-muted text-center">Nema aktivnosti u odabranom periodu</p>
<?php endif; ?>
</div>
</div>
</div>
<!-- Top Users -->
<div class="col-md-6 mb-4">
<div class="card">
<div class="card-header bg-success text-white">
<h6 class="mb-0"><i class="bi bi-people-fill"></i> Top 5 Najaktivnijih Korisnika</h6>
</div>
<div class="card-body">
<?php if (!empty($topUsers)): ?>
<?php
$maxCount = max(array_column($topUsers, 'count'));
foreach ($topUsers as $user):
$percentage = $maxCount > 0 ? ($user['count'] / $maxCount) * 100 : 0;
?>
<div class="mb-3">
<div class="progress-label">
<span><i class="bi bi-person"></i> <?php echo htmlspecialchars($user['username']); ?></span>
<span class="badge bg-success"><?php echo $user['count']; ?> akcija</span>
</div>
<div class="progress" style="height: 25px;">
<div class="progress-bar bg-success" style="width: <?php echo $percentage; ?>%"></div>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<p class="text-muted text-center">Nema podataka</p>
<?php endif; ?>
</div>
</div>
</div>
<!-- Cities Statistics -->
<div class="col-md-6 mb-4">
<div class="card">
<div class="card-header bg-primary text-white">
<h6 class="mb-0"><i class="bi bi-geo-alt-fill"></i> Top 10 Gradova</h6>
</div>
<div class="card-body">
<?php if (!empty($citiesStats)): ?>
<?php
$maxCount = max(array_column($citiesStats, 'count'));
foreach ($citiesStats as $city):
$percentage = $maxCount > 0 ? ($city['count'] / $maxCount) * 100 : 0;
?>
<div class="mb-2">
<div class="progress-label">
<span><?php echo htmlspecialchars($city['grad']); ?></span>
<span><span class="badge bg-primary"><?php echo $city['count']; ?></span> <small class="text-muted">(<?php echo number_format($city['capacity']); ?> kapacitet)</small></span>
</div>
<div class="progress">
<div class="progress-bar" style="width: <?php echo $percentage; ?>%"></div>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<p class="text-muted text-center">Nema podataka</p>
<?php endif; ?>
</div>
</div>
</div>
<!-- Counties Statistics -->
<div class="col-md-6 mb-4">
<div class="card">
<div class="card-header bg-warning">
<h6 class="mb-0"><i class="bi bi-map-fill"></i> Top 10 Županija</h6>
</div>
<div class="card-body">
<?php if (!empty($countiesStats)): ?>
<?php
$maxCount = max(array_column($countiesStats, 'count'));
foreach ($countiesStats as $county):
$percentage = $maxCount > 0 ? ($county['count'] / $maxCount) * 100 : 0;
?>
<div class="mb-2">
<div class="progress-label">
<span><?php echo htmlspecialchars($county['zupanija']); ?></span>
<span class="badge bg-warning text-dark"><?php echo $county['count']; ?></span>
</div>
<div class="progress">
<div class="progress-bar bg-warning" style="width: <?php echo $percentage; ?>%"></div>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<p class="text-muted text-center">Nema podataka</p>
<?php endif; ?>
</div>
</div>
</div>
</div>
</div>
<?php include 'templates/footer.php'; ?>