-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats_compare.php
More file actions
201 lines (180 loc) · 7.51 KB
/
stats_compare.php
File metadata and controls
201 lines (180 loc) · 7.51 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
<?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'] ?? '';
$month1 = $_GET['month1'] ?? date('m');
$year1 = $_GET['year1'] ?? date('Y');
$month2 = $_GET['month2'] ?? date('m');
$year2 = $_GET['year2'] ?? 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, $month, $year) {
return "https://emodul.eu/api/v1/modules/{$moduleId}/statistics/linear/range/month/{$month}/year/{$year}";
}
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);
}
function collectData($response, $labelSuffix) {
$result = [];
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 ? $date->format('d.m') : $p['x'];
$labels[] = $label;
$values[] = $p['y'];
}
if (!empty($labels)) {
$result[] = [
'label' => $zoneName . ' – ' . $labelSuffix,
'labels' => $labels,
'data' => $values,
'color' => sprintf('rgba(%d,%d,%d,1)', rand(50,200), rand(50,200), rand(50,200))
];
}
}
}
return $result;
}
$url1 = buildApiUrl($moduleId, $month1, $year1);
$url2 = buildApiUrl($moduleId, $month2, $year2);
$dataSets = array_merge(
collectData(getStats($url1, $auth['token']), "{$month1}/{$year1}"),
collectData(getStats($url2, $auth['token']), "{$month2}/{$year2}")
);
?>
<div class="container mt-5">
<h1 class="mb-4"><i class="fas fa-balance-scale"></i> Porównanie miesięcy (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) ?>">
<?php
function monthSelect($id, $selected) {
echo "<select name=\"$id\" id=\"$id\" class=\"form-select\">";
for ($m = 1; $m <= 12; $m++) {
echo "<option value=\"$m\" " . ((int)$selected === $m ? 'selected' : '') . ">" . str_pad($m, 2, '0', STR_PAD_LEFT) . "</option>";
}
echo "</select>";
}
function yearSelect($id, $selected) {
echo "<select name=\"$id\" id=\"$id\" class=\"form-select\">";
for ($y = date('Y'); $y >= 2023; $y--) {
echo "<option value=\"$y\" " . ((int)$selected === $y ? 'selected' : '') . ">$y</option>";
}
echo "</select>";
}
?>
<div class="col-md-3">
<label class="form-label">Miesiąc 1</label>
<?php monthSelect('month1', $month1); ?>
</div>
<div class="col-md-3">
<label class="form-label">Rok 1</label>
<?php yearSelect('year1', $year1); ?>
</div>
<div class="col-md-3">
<label class="form-label">Miesiąc 2</label>
<?php monthSelect('month2', $month2); ?>
</div>
<div class="col-md-3">
<label class="form-label">Rok 2</label>
<?php yearSelect('year2', $year2); ?>
</div>
<div class="col-md-12">
<button type="submit" class="btn btn-primary w-100"><i class="fas fa-sync-alt"></i> Porównaj</button>
</div>
</form>
<?php if (empty($dataSets)): ?>
<div class="alert alert-warning">Brak danych do wyświetlenia. Upewnij się, że dane istnieją dla obu miesięcy.</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="compareChart" height="120"></canvas>
<script>
const ctx = document.getElementById('compareChart').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 – dwa miesiące'
},
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; ?>
</div>
<?php include 'footer.php'; ?>