Auto commit: 2025-10-13T09:51:27.472Z
This commit is contained in:
parent
bd8f34e7d7
commit
f424a16609
9
db/migrations/002_create_competition_participants.sql
Normal file
9
db/migrations/002_create_competition_participants.sql
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS `competition_participants` (
|
||||||
|
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
`user_id` INT NOT NULL,
|
||||||
|
`competition_id` INT NOT NULL,
|
||||||
|
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
|
||||||
|
FOREIGN KEY (`competition_id`) REFERENCES `competitions`(`id`) ON DELETE CASCADE,
|
||||||
|
UNIQUE KEY `user_competition` (`user_id`, `competition_id`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||||
9
db/migrations/003_create_competition_submissions.sql
Normal file
9
db/migrations/003_create_competition_submissions.sql
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS `competition_submissions` (
|
||||||
|
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
`user_id` INT NOT NULL,
|
||||||
|
`competition_id` INT NOT NULL,
|
||||||
|
`file_path` VARCHAR(255) NOT NULL,
|
||||||
|
`uploaded_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
|
||||||
|
FOREIGN KEY (`competition_id`) REFERENCES `competitions`(`id`) ON DELETE CASCADE
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||||
25
join_competition.php
Normal file
25
join_competition.php
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
require_once 'db/config.php';
|
||||||
|
|
||||||
|
if (!isset($_SESSION['user_id'])) {
|
||||||
|
header("location: login.php");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['competition_id'])) {
|
||||||
|
$user_id = $_SESSION['user_id'];
|
||||||
|
$competition_id = $_POST['competition_id'];
|
||||||
|
|
||||||
|
try {
|
||||||
|
$pdo = db();
|
||||||
|
$stmt = $pdo->prepare("INSERT INTO competition_participants (user_id, competition_id) VALUES (?, ?)");
|
||||||
|
$stmt->execute([$user_id, $competition_id]);
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
// Handle potential errors, like trying to join the same competition twice
|
||||||
|
// You might want to log this error or show a message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
header("location: user_dashboard.php");
|
||||||
|
exit;
|
||||||
@ -23,6 +23,8 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|||||||
$stmt->execute([$email]);
|
$stmt->execute([$email]);
|
||||||
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if ($user && password_verify($password, $user['password'])) {
|
if ($user && password_verify($password, $user['password'])) {
|
||||||
$_SESSION['user_id'] = $user['id'];
|
$_SESSION['user_id'] = $user['id'];
|
||||||
$_SESSION['user_name'] = $user['name'];
|
$_SESSION['user_name'] = $user['name'];
|
||||||
@ -125,7 +127,7 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|||||||
<?php if(!empty($error)): ?>
|
<?php if(!empty($error)): ?>
|
||||||
<div class="alert"><?php echo $error; ?></div>
|
<div class="alert"><?php echo $error; ?></div>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
<form action="login.php" method="post">
|
<form action="/login" method="post">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="email">Email</label>
|
<label for="email">Email</label>
|
||||||
<input type="email" name="email" id="email" class="form-control" required>
|
<input type="email" name="email" id="email" class="form-control" required>
|
||||||
|
|||||||
@ -137,7 +137,7 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|||||||
<?php if(!empty($success)): ?>
|
<?php if(!empty($success)): ?>
|
||||||
<div class="alert alert-success"><?php echo $success; ?></div>
|
<div class="alert alert-success"><?php echo $success; ?></div>
|
||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
<form action="register.php" method="post">
|
<form action="/register" method="post">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="name">Full Name</label>
|
<label for="name">Full Name</label>
|
||||||
<input type="text" name="name" id="name" class="form-control" required>
|
<input type="text" name="name" id="name" class="form-control" required>
|
||||||
|
|||||||
54
upload_submission.php
Normal file
54
upload_submission.php
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
require_once 'db/config.php';
|
||||||
|
|
||||||
|
if (!isset($_SESSION['user_id'])) {
|
||||||
|
header("location: login.php");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['competition_id']) && isset($_FILES['submission_file'])) {
|
||||||
|
$user_id = $_SESSION['user_id'];
|
||||||
|
$competition_id = $_POST['competition_id'];
|
||||||
|
$file = $_FILES['submission_file'];
|
||||||
|
|
||||||
|
// File upload error handling
|
||||||
|
if ($file['error'] !== UPLOAD_ERR_OK) {
|
||||||
|
// Handle upload error
|
||||||
|
header("location: user_dashboard.php?upload_error=1");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create uploads directory if it doesn't exist
|
||||||
|
$upload_dir = 'uploads/';
|
||||||
|
if (!is_dir($upload_dir)) {
|
||||||
|
mkdir($upload_dir, 0775, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate a unique filename
|
||||||
|
$file_extension = pathinfo($file['name'], PATHINFO_EXTENSION);
|
||||||
|
$unique_filename = uniqid('submission_', true) . '.' . $file_extension;
|
||||||
|
$file_path = $upload_dir . $unique_filename;
|
||||||
|
|
||||||
|
// Move the file to the uploads directory
|
||||||
|
if (move_uploaded_file($file['tmp_name'], $file_path)) {
|
||||||
|
try {
|
||||||
|
$pdo = db();
|
||||||
|
$stmt = $pdo->prepare("INSERT INTO competition_submissions (user_id, competition_id, file_path) VALUES (?, ?, ?)");
|
||||||
|
$stmt->execute([$user_id, $competition_id, $file_path]);
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
// Handle DB error
|
||||||
|
// You might want to log this error
|
||||||
|
// If DB insert fails, you might want to delete the uploaded file
|
||||||
|
unlink($file_path);
|
||||||
|
header("location: user_dashboard.php?upload_error=2");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
header("location: user_dashboard.php");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
header("location: user_dashboard.php");
|
||||||
|
exit;
|
||||||
@ -11,13 +11,27 @@ if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'user') {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch competitions from the database
|
$user_id = $_SESSION['user_id'];
|
||||||
|
|
||||||
|
// Fetch competitions, participations, and submissions
|
||||||
try {
|
try {
|
||||||
$pdo = db();
|
$pdo = db();
|
||||||
$stmt = $pdo->query('SELECT title, description, start_date FROM competitions ORDER BY start_date ASC');
|
|
||||||
|
// 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);
|
$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) {
|
} catch (PDOException $e) {
|
||||||
// Handle DB error
|
|
||||||
$competitions = [];
|
$competitions = [];
|
||||||
$db_error = "Failed to load competitions.";
|
$db_error = "Failed to load competitions.";
|
||||||
}
|
}
|
||||||
@ -65,6 +79,35 @@ try {
|
|||||||
<h5 class="card-title"><?php echo htmlspecialchars($competition['title']); ?></h5>
|
<h5 class="card-title"><?php echo htmlspecialchars($competition['title']); ?></h5>
|
||||||
<p class="card-text"><?php echo htmlspecialchars($competition['description']); ?></p>
|
<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>
|
<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>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user