34918-vm/user_dashboard.php
2025-10-13 09:51:27 +00:00

126 lines
6.2 KiB
PHP

<?php
session_start();
require_once 'db/config.php'; // Ensure DB config is loaded
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'user') {
if(isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'admin'){
header("location: admin_dashboard.php");
exit;
}
header("location: login.php");
exit;
}
$user_id = $_SESSION['user_id'];
// Fetch competitions, participations, and submissions
try {
$pdo = db();
// Fetch all competitions
$stmt = $pdo->query('SELECT id, title, description, start_date FROM competitions ORDER BY start_date ASC');
$competitions = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Fetch competitions the user has joined
$stmt = $pdo->prepare('SELECT competition_id FROM competition_participants WHERE user_id = ?');
$stmt->execute([$user_id]);
$joined_competitions = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
// Fetch user's submissions
$stmt = $pdo->prepare('SELECT competition_id, file_path, uploaded_at FROM competition_submissions WHERE user_id = ?');
$stmt->execute([$user_id]);
$submissions = $stmt->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$competitions = [];
$db_error = "Failed to load competitions.";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Dashboard - READY BUDDY</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/style.css?v=<?php echo time(); ?>">
</head>
<body>
<header class="navbar">
<div class="container">
<a class="navbar-brand" href="index.php">READY BUDDY</a>
<nav>
<a href="logout.php">Logout</a>
</nav>
</div>
</header>
<main class="container" style="margin-top: 2rem; margin-bottom: 2rem;">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1>User Dashboard</h1>
<h2>Welcome, <?php echo htmlspecialchars($_SESSION['user_name']); ?>!</h2>
</div>
<p>This is your user dashboard. You can view available competitions below.</p>
<section id="competitions" class="mt-5">
<h3>Available Competitions</h3>
<div class="row">
<?php if (isset($db_error)): ?>
<p class="text-danger"><?php echo $db_error; ?></p>
<?php elseif (empty($competitions)): ?>
<p>No competitions available at the moment.</p>
<?php else: ?>
<?php foreach ($competitions as $competition): ?>
<div class="col-md-4 mb-4">
<div class="card h-100">
<div class="card-body">
<h5 class="card-title"><?php echo htmlspecialchars($competition['title']); ?></h5>
<p class="card-text"><?php echo htmlspecialchars($competition['description']); ?></p>
<p class="card-text"><small class="text-muted">Starts on: <?php echo date("F j, Y", strtotime($competition['start_date'])); ?></small></p>
<?php if (in_array($competition['id'], $joined_competitions)): ?>
<div class="mt-3">
<h5>Your Submissions:</h5>
<?php if (isset($submissions[$competition['id']])): ?>
<ul>
<?php foreach ($submissions[$competition['id']] as $submission): ?>
<li><a href="<?php echo htmlspecialchars($submission['file_path']); ?>" target="_blank"><?php echo basename(htmlspecialchars($submission['file_path'])); ?></a> (<?php echo date("F j, Y", strtotime($submission['uploaded_at'])); ?>)</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>No submissions yet.</p>
<?php endif; ?>
<form action="upload_submission.php" method="post" enctype="multipart/form-data" class="mt-2">
<input type="hidden" name="competition_id" value="<?php echo $competition['id']; ?>">
<div class="form-group">
<label for="file_upload_<?php echo $competition['id']; ?>">Upload File:</label>
<input type="file" name="submission_file" id="file_upload_<?php echo $competition['id']; ?>" required>
</div>
<button type="submit" class="btn btn-primary btn-sm">Upload</button>
</form>
</div>
<?php else: ?>
<form action="join_competition.php" method="post" class="mt-3">
<input type="hidden" name="competition_id" value="<?php echo $competition['id']; ?>">
<button type="submit" class="btn btn-success">Join Competition</button>
</form>
<?php endif; ?>
</div>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</section>
</main>
<footer class="footer">
<div class="container">
<p>&copy; <?php echo date("Y"); ?> READY BUDDY. All rights reserved.</p>
</div>
</footer>
</body>
</html>