-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.php
More file actions
346 lines (306 loc) · 14.8 KB
/
Copy pathreport.php
File metadata and controls
346 lines (306 loc) · 14.8 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
<?php
/**
* Habit Tracker - Report & Analytics
* Display habit completion statistics and progress
*/
const DATA_FILE = 'data.json';
// ============================================================================
// Data Functions
// ============================================================================
function loadData(): array {
$content = file_get_contents(DATA_FILE);
$data = json_decode($content, true) ?? ['columns' => [], 'days' => [], 'startDate' => date('Y-m-d'), 'endDate' => date('Y-m-d')];
// Migrate old format (simple string array) to new format (objects with name and frequency)
if (!empty($data['columns']) && isset($data['columns'][0]) && is_string($data['columns'][0])) {
$data['columns'] = array_map(function($name) {
return ['name' => $name, 'frequency' => 7];
}, $data['columns']);
}
return $data;
}
function getHabitName(mixed $habit): string {
return is_array($habit) ? $habit['name'] : $habit;
}
function getHabitFrequency(mixed $habit): int {
return is_array($habit) ? ($habit['frequency'] ?? 7) : 7;
}
function calculateExpectedCompletions(int $trackingDays, int $frequencyPerWeek): int {
// Calculate expected completions based on weekly frequency
// For tracking period, expected = (tracking_days / 7) * frequency
$weeks = $trackingDays / 7;
return (int) round($weeks * $frequencyPerWeek);
}
function calculateWeeklyDots(array $data, string $habitName, int $frequency, DateTime $startDate, DateTime $endDate, DateTime $currentDate): array {
$weeks = [];
$totalGreen = 0;
$totalRed = 0;
$totalGray = 0;
// Iterate through each week
$weekStart = clone $startDate;
while ($weekStart <= $endDate) {
$weekEnd = clone $weekStart;
$weekEnd->modify('+6 days');
if ($weekEnd > $endDate) {
$weekEnd = clone $endDate;
}
// Count completions for this week
$weekCompletions = 0;
$checkDate = clone $weekStart;
while ($checkDate <= $weekEnd) {
$dateStr = $checkDate->format('Y-m-d');
if (isset($data['days'][$dateStr][$habitName]) && $data['days'][$dateStr][$habitName]) {
$weekCompletions++;
}
$checkDate->modify('+1 day');
}
$weekData = ['green' => 0, 'red' => 0, 'gray' => 0];
// Determine if this week is elapsed, current, or future
if ($weekEnd < $currentDate) {
// Fully elapsed week - cap at frequency, rest is red
$weekData['green'] = min($weekCompletions, $frequency);
$weekData['red'] = max(0, $frequency - $weekCompletions);
} elseif ($weekStart > $currentDate) {
// Future week - all gray
$weekData['gray'] = $frequency;
} else {
// Current week - calculate what's achievable vs impossible
$daysRemaining = $currentDate->diff($weekEnd)->days + 1; // days remaining including today
$maxPossible = $weekCompletions + $daysRemaining;
$weekData['green'] = min($weekCompletions, $frequency);
$impossibleToRecover = max(0, $frequency - $maxPossible);
$stillAchievable = max(0, min($daysRemaining, $frequency - $weekCompletions));
$weekData['red'] = $impossibleToRecover;
$weekData['gray'] = $stillAchievable;
}
$weeks[] = $weekData;
$totalGreen += $weekData['green'];
$totalRed += $weekData['red'];
$totalGray += $weekData['gray'];
$weekStart->modify('+7 days');
}
return [
'weeks' => $weeks,
'green' => $totalGreen,
'red' => $totalRed,
'gray' => $totalGray,
];
}
function calculateReportStats(array $data): array {
$startDate = new DateTime($data['startDate']);
$startDate->setTime(0, 0, 0);
$endDate = new DateTime($data['endDate']);
$endDate->setTime(0, 0, 0);
$currentDate = new DateTime('now');
$currentDate->setTime(0, 0, 0);
$trackingDays = $startDate->diff($endDate)->days + 1;
if ($currentDate > $endDate) {
$daysRemaining = 0;
} else {
$daysRemaining = max(0, $endDate->diff($currentDate)->days + 1);
}
// Calculate elapsed days (from start to today, capped at end date)
if ($currentDate < $startDate) {
$elapsedDays = 0;
} elseif ($currentDate > $endDate) {
$elapsedDays = $trackingDays;
} else {
$elapsedDays = $startDate->diff($currentDate)->days + 1;
}
// Build habit name to frequency map
$habitFrequencies = [];
$habitNames = [];
foreach ($data['columns'] as $habit) {
$name = getHabitName($habit);
$habitNames[] = $name;
$habitFrequencies[$name] = getHabitFrequency($habit);
}
// Initialize habit stats
$habitStats = array_fill_keys($habitNames, 0);
$totalChecks = 0;
// Count completions within tracking period
foreach ($data['days'] as $date => $habits) {
$habitDate = new DateTime($date);
if ($habitDate >= $startDate && $habitDate <= $endDate) {
foreach ($habits as $habit => $completed) {
if ($completed && isset($habitStats[$habit])) {
$habitStats[$habit]++;
$totalChecks++;
}
}
}
}
// Calculate expected completions based on frequency (total and elapsed)
$habitExpected = [];
$habitElapsedExpected = [];
$totalPossible = 0;
$totalElapsedPossible = 0;
foreach ($habitNames as $name) {
$freq = $habitFrequencies[$name];
$expected = calculateExpectedCompletions($trackingDays, $freq);
$elapsedExpected = calculateExpectedCompletions($elapsedDays, $freq);
$habitExpected[$name] = $expected;
$habitElapsedExpected[$name] = $elapsedExpected;
$totalPossible += $expected;
$totalElapsedPossible += $elapsedExpected;
}
$progressPercent = $totalPossible > 0 ? round(($totalChecks / $totalPossible) * 100) : 0;
return [
'startDate' => $startDate,
'endDate' => $endDate,
'trackingDays' => $trackingDays,
'elapsedDays' => $elapsedDays,
'daysRemaining' => $daysRemaining,
'habitStats' => $habitStats,
'habitExpected' => $habitExpected,
'habitElapsedExpected' => $habitElapsedExpected,
'habitFrequencies' => $habitFrequencies,
'totalChecks' => $totalChecks,
'totalPossible' => $totalPossible,
'totalElapsedPossible' => $totalElapsedPossible,
'progressPercent' => $progressPercent,
];
}
function calculateHabitProgress(int $completed, int $total): int {
return $total > 0 ? round(($completed / $total) * 100) : 0;
}
// ============================================================================
// Request Processing
// ============================================================================
$data = loadData();
$stats = calculateReportStats($data);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#334155">
<link rel="manifest" href="manifest.php">
<title>Report</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/lucide@latest"></script>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
<style>
body { font-family: 'Inter', sans-serif; }
</style>
</head>
<body class="bg-slate-50 min-h-screen">
<main class="max-w-3xl mx-auto px-4 py-8">
<!-- Header -->
<header class="flex items-center justify-between mb-8">
<h1 class="text-xl font-semibold text-slate-800">Report</h1>
<nav class="flex items-center gap-1">
<a href="admin.php" class="p-2 text-slate-400 hover:text-slate-600 hover:bg-slate-100 rounded-lg transition-colors" title="Settings">
<i data-lucide="settings" class="w-5 h-5"></i>
</a>
<a href="index.php" class="p-2 text-slate-400 hover:text-slate-600 hover:bg-slate-100 rounded-lg transition-colors" title="Tracker">
<i data-lucide="layout-grid" class="w-5 h-5"></i>
</a>
</nav>
</header>
<!-- Overview -->
<section class="grid grid-cols-3 gap-3 mb-8">
<div class="bg-white rounded-xl border border-slate-200 p-4">
<div class="text-2xl font-semibold text-slate-800"><?= $stats['progressPercent'] ?>%</div>
<div class="text-xs text-slate-400 uppercase tracking-wide mt-1">Complete</div>
</div>
<div class="bg-white rounded-xl border border-slate-200 p-4">
<div class="text-2xl font-semibold text-slate-800"><?= $stats['totalChecks'] ?></div>
<div class="text-xs text-slate-400 uppercase tracking-wide mt-1">Checks</div>
</div>
<div class="bg-white rounded-xl border border-slate-200 p-4">
<div class="text-2xl font-semibold text-slate-800"><?= $stats['daysRemaining'] ?></div>
<div class="text-xs text-slate-400 uppercase tracking-wide mt-1">Days Left</div>
</div>
</section>
<!-- Period -->
<section class="bg-white rounded-xl border border-slate-200 p-5 mb-4">
<div class="flex items-center justify-between">
<div class="flex items-center gap-3">
<div class="p-2 bg-slate-100 rounded-lg">
<i data-lucide="calendar" class="w-4 h-4 text-slate-500"></i>
</div>
<div>
<div class="text-sm text-slate-800"><?= $stats['startDate']->format('M j') ?> → <?= $stats['endDate']->format('M j, Y') ?></div>
<div class="text-xs text-slate-400"><?= $stats['trackingDays'] ?> days total</div>
</div>
</div>
<div class="text-right">
<div class="text-sm text-slate-800"><?= $stats['totalChecks'] ?>/<?= $stats['totalPossible'] ?></div>
<div class="text-xs text-slate-400">completed</div>
</div>
</div>
</section>
<!-- Habits -->
<section class="bg-white rounded-xl border border-slate-200 overflow-hidden">
<div class="px-5 py-4 border-b border-slate-100">
<h2 class="text-sm font-medium text-slate-800">Habits</h2>
</div>
<div class="divide-y divide-slate-50">
<?php foreach ($data['columns'] as $habit):
$habitName = getHabitName($habit);
$frequency = getHabitFrequency($habit);
$completed = $stats['habitStats'][$habitName];
$expected = $stats['habitExpected'][$habitName];
// Calculate dots per-week (no banking allowed)
$currentDate = new DateTime('now');
$currentDate->setTime(0, 0, 0);
$dots = calculateWeeklyDots($data, $habitName, $frequency, $stats['startDate'], $stats['endDate'], $currentDate);
$weeks = $dots['weeks'];
$greenDots = $dots['green'];
$redDots = $dots['red'];
$grayDots = $dots['gray'];
// Percentage based on green vs total expected (green + red + gray)
$totalDots = $greenDots + $redDots + $grayDots;
$percent = $totalDots > 0 ? round(($greenDots / $totalDots) * 100) : 0;
?>
<div class="px-5 py-4">
<div class="flex items-center justify-between mb-2">
<div class="flex items-center gap-2">
<span class="text-sm text-slate-700"><?= htmlspecialchars($habitName) ?></span>
<span class="text-xs text-slate-400"><?= $frequency ?>x/wk</span>
</div>
<span class="text-xs text-slate-400 tabular-nums"><?= $completed ?>/<?= $expected ?></span>
</div>
<div class="flex items-start gap-3">
<div class="flex-1 flex gap-1 flex-wrap items-center">
<?php foreach ($weeks as $week): ?>
<?php
$weekBg = '';
if ($week['red'] > 0) {
$weekBg = 'bg-red-100';
} elseif ($week['green'] > 0 && $week['gray'] === 0) {
$weekBg = 'bg-emerald-100';
}
?>
<div class="flex gap-0.5 border border-slate-200 rounded px-1 py-0.5 <?= $weekBg ?>">
<?php for ($i = 0; $i < $week['green']; $i++): ?>
<div class="w-2 h-2 rounded-full bg-emerald-500"></div>
<?php endfor; ?>
<?php for ($i = 0; $i < $week['red']; $i++): ?>
<div class="w-2 h-2 rounded-full bg-red-400"></div>
<?php endfor; ?>
<?php for ($i = 0; $i < $week['gray']; $i++): ?>
<div class="w-2 h-2 rounded-full bg-slate-200"></div>
<?php endfor; ?>
</div>
<?php endforeach; ?>
</div>
<span class="text-xs text-right text-slate-500 tabular-nums w-8"><?= $percent ?>%</span>
</div>
</div>
<?php endforeach; ?>
<?php if (empty($data['columns'])): ?>
<div class="px-5 py-8 text-center text-sm text-slate-400">
No habits yet
</div>
<?php endif; ?>
</div>
</section>
</main>
<script>
if ('serviceWorker' in navigator) navigator.serviceWorker.register('sw.js');
lucide.createIcons();
</script>
</body>
</html>