36776-vm/progress.php
Flatlogic Bot 3be446013d Initialv1
2025-12-09 00:59:40 +00:00

156 lines
4.8 KiB
PHP

<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit();
}
require_once 'db/config.php';
$pdo = db();
$user_id = $_SESSION['user_id'];
// Fetch progress data
$stmt = $pdo->prepare("SELECT theme, completion_date FROM progress WHERE user_id = ? ORDER BY completion_date DESC");
$stmt->execute([$user_id]);
$progress_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
// --- Progress Calculation ---
// 1. Last 7 Days Grid
$last_7_days = [];
for ($i = 0; $i < 7; $i++) {
$date = date('Y-m-d', strtotime("-$i days"));
$last_7_days[$date] = false;
}
foreach ($progress_data as $progress) {
if (isset($last_7_days[$progress['completion_date']])) {
$last_7_days[$progress['completion_date']] = true;
}
}
// 2. Total Practices
$total_practices = count($progress_data);
// 3. Current Streak
$current_streak = 0;
if ($total_practices > 0) {
$dates = array_unique(array_column($progress_data, 'completion_date'));
rsort($dates);
$current_streak = 1;
$today = date('Y-m-d');
$yesterday = date('Y-m-d', strtotime('-1 day'));
if ($dates[0] != $today && $dates[0] != $yesterday) {
$current_streak = 0;
} else {
for ($i = 0; $i < count($dates) - 1; $i++) {
$date1 = new DateTime($dates[$i]);
$date2 = new DateTime($dates[$i+1]);
$diff = $date1->diff($date2)->days;
if ($diff == 1) {
$current_streak++;
} else {
break;
}
}
}
}
// 4. Practiced Themes
$practiced_themes = array_unique(array_column($progress_data, 'theme'));
$project_name = getenv('PROJECT_NAME') ?: 'Mindful Moments';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Your Progress - <?php echo htmlspecialchars($project_name); ?></title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="assets/css/custom.css">
<style>
.progress-grid {
display: grid;
grid-template-columns: repeat(7, 1fr);
gap: 10px;
}
.day-box {
width: 50px;
height: 50px;
border: 1px solid #ccc;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
.day-box.completed {
background-color: #d4edda;
}
.day-initial {
font-weight: bold;
}
</style>
</head>
<body>
<?php include 'index.php'; ?>
<div class="container mt-5">
<h2>Your Progress</h2>
<div class="row mt-4">
<div class="col-md-4">
<div class="card">
<div class="card-body text-center">
<h5 class="card-title">Total Practices</h5>
<p class="card-text display-4"><?php echo $total_practices; ?></p>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-body text-center">
<h5 class="card-title">Current Streak</h5>
<p class="card-text display-4"><?php echo $current_streak; ?> <?php echo ($current_streak == 1) ? 'day' : 'days'; ?></p>
</div>
</div>
</div>
</div>
<div class="mt-5">
<h5>Last 7 Days</h5>
<div class="progress-grid">
<?php foreach ($last_7_days as $date => $completed): ?>
<div class="day-box <?php echo $completed ? 'completed' : ''; ?>">
<span class="day-initial"><?php echo date('D', strtotime($date)); ?></span>
<span><?php echo date('d', strtotime($date)); ?></span>
</div>
<?php endforeach; ?>
</div>
</div>
<div class="mt-5">
<h5>Practiced Themes</h5>
<?php if (empty($practiced_themes)): ?>
<p>You haven't completed any rituals yet.</p>
<?php else: ?>
<ul class="list-group">
<?php foreach ($practiced_themes as $theme): ?>
<li class="list-group-item"><?php echo htmlspecialchars($theme); ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>