-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.php
More file actions
205 lines (183 loc) · 7.31 KB
/
stats.php
File metadata and controls
205 lines (183 loc) · 7.31 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
<?php
include 'config.php';
include 'functions.php';
include 'header.php';
require_once 'auth.php';
/** @var array $config */
$auth = login($config['username'], $config['password']);
$modulesList = [];
$moduleId = $_GET['udid'] ?? '';
$userId = $_GET['user_id'] ?? '';
$range = $_GET['range'] ?? 'day';
$month = $_GET['month'] ?? date('m');
$year = $_GET['year'] ?? date('Y');
$dataSets = [];
if (!empty($auth['token']) && !empty($auth['user_id'])) {
$modulesList = getModules($auth['user_id'], $auth['token']);
if (empty($userId) && empty($moduleId) && !empty($modulesList)) {
$userId = $auth['user_id'];
$moduleId = $modulesList[0]['udid'] ?? '';
}
}
if (empty($moduleId) || empty($userId)) {
echo '<div class="alert alert-danger m-4">Brak dostępnych modułów lub nieprawidłowe dane logowania.</div>';
include 'footer.php';
exit;
}
function buildApiUrl($moduleId, $range, $month, $year) {
$base = "https://emodul.eu/api/v1/modules/{$moduleId}/statistics/linear/range/{$range}";
if ($range === 'month') {
return $base . "/{$month}";
}
return $base;
}
function getStats($url, $token) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer $token"]);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
$url = buildApiUrl($moduleId, $range, $month, $year);
$response = getStats($url, $auth['token']);
if (
is_array($response)
&& isset($response['status'], $response['data']['history'])
&& $response['status'] === 'success'
) {
foreach ($response['data']['history'] as $zoneRaw => $points) {
$zoneName = preg_replace('/^\|.*?\|/', '', $zoneRaw);
$labels = [];
$values = [];
foreach ($points as $p) {
if (!isset($p['x'], $p['y'])) continue;
$date = DateTime::createFromFormat('YmdHi', $p['x']);
$label = $date ? (
$range === 'day' ? $date->format('H:i') :
($range === 'month' ? $date->format('d.m') :
($range === 'year' ? $date->format('Y-m') : $p['x']))
) : $p['x'];
$labels[] = $label;
$values[] = $p['y'];
}
if (!empty($labels)) {
$dataSets[] = [
'label' => $zoneName,
'labels' => $labels,
'data' => $values,
'color' => sprintf('rgba(%d,%d,%d,1)', rand(50,200), rand(50,200), rand(50,200))
];
}
}
}
?>
<div class="container mt-5">
<h1 class="mb-4"><i class="fas fa-chart-line"></i> Statystyki – <?= htmlspecialchars(ucfirst($range)) ?> (linear)</h1>
<form method="get" class="row g-3 mb-4 align-items-end">
<input type="hidden" name="user_id" value="<?= htmlspecialchars($userId) ?>">
<input type="hidden" name="udid" value="<?= htmlspecialchars($moduleId) ?>">
<div class="col-md-3">
<label for="range" class="form-label">Zakres danych</label>
<select name="range" id="range" class="form-select">
<option value="day" <?= $range === 'day' ? 'selected' : '' ?>>Ostatnia doba</option>
<option value="week" <?= $range === 'week' ? 'selected' : '' ?>>Ostatni tydzień</option>
<option value="month" <?= $range === 'month' ? 'selected' : '' ?>>Miesiąc</option>
</select>
</div>
<div class="col-md-3">
<label for="month" class="form-label">Miesiąc</label>
<select name="month" id="month" class="form-select" <?= $range !== 'month' ? 'disabled' : '' ?>>
<?php for ($m = 1; $m <= 12; $m++): ?>
<option value="<?= $m ?>" <?= (int)$month === $m ? 'selected' : '' ?>>
<?= str_pad($m, 2, '0', STR_PAD_LEFT) ?>
</option>
<?php endfor; ?>
</select>
</div>
<div class="col-md-3">
<label for="year" class="form-label">Rok</label>
<select name="year" id="year" class="form-select" <?= $range !== 'month' ? 'disabled' : '' ?>>
<?php for ($y = date('Y'); $y >= 2023; $y--): ?>
<option value="<?= $y ?>" <?= (int)$year === $y ? 'selected' : '' ?>><?= $y ?></option>
<?php endfor; ?>
</select>
</div>
<div class="col-md-3">
<button type="submit" class="btn btn-primary w-100">
<i class="fas fa-sync-alt"></i> Pokaż
</button>
</div>
</form>
<script>
document.getElementById('range').addEventListener('change', function () {
const isMonth = this.value === 'month';
document.getElementById('month').disabled = !isMonth;
document.getElementById('year').disabled = !isMonth;
});
</script>
<?php if (empty($dataSets)): ?>
<div class="alert alert-warning">Brak danych do wyświetlenia.</div>
<?php else: ?>
<div class="d-flex justify-content-end mb-2">
<button id="hideAllBtn" class="btn btn-outline-secondary btn-sm">
<i class="fas fa-eye-slash"></i> Ukryj wszystkie serie
</button>
</div>
<canvas id="statsChart" height="120"></canvas>
<script>
const ctx = document.getElementById('statsChart').getContext('2d');
const chart = new Chart(ctx, {
type: 'line',
data: {
labels: <?= json_encode($dataSets[0]['labels']) ?>,
datasets: [
<?php foreach ($dataSets as $set): ?>
{
label: <?= json_encode($set['label']) ?>,
data: <?= json_encode($set['data']) ?>,
borderColor: '<?= $set['color'] ?>',
backgroundColor: '<?= str_replace('1)', '0.2)', $set['color']) ?>',
fill: false,
tension: 0.3
},
<?php endforeach; ?>
]
},
options: {
responsive: true,
interaction: {
mode: 'index',
intersect: false
},
plugins: {
title: {
display: true,
text: 'Porównanie stref – <?= htmlspecialchars(ucfirst($range)) ?>'
},
legend: {
display: true,
position: 'bottom'
}
},
scales: {
x: {
ticks: {
maxRotation: 45,
autoSkip: true,
maxTicksLimit: 30
}
}
}
}
});
document.getElementById('hideAllBtn').addEventListener('click', () => {
chart.data.datasets.forEach((_, i) => {
chart.hide(i);
});
});
</script>
<?php endif; ?>
<a href="zones.php?user_id=<?= urlencode($userId) ?>&udid=<?= urlencode($moduleId) ?>" class="btn btn-secondary mt-4">« Powrót</a>
</div>
<?php include 'footer.php'; ?>