36629-vm/test_results.php
Flatlogic Bot ea73dc7483 v1
2025-12-04 08:49:26 +00:00

67 lines
2.1 KiB
PHP

<?php
require_once 'includes/header.php';
require_once 'db/config.php';
// Redirect if not logged in
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit();
}
// Check for test ID
if (!isset($_GET['test_id']) || !is_numeric($_GET['test_id'])) {
echo "<div class='container mt-5'><div class='alert alert-danger'>Invalid test ID.</div></div>";
require_once 'includes/footer.php';
exit();
}
$test_id = intval($_GET['test_id']);
$user_id = $_SESSION['user_id'];
try {
$db = db();
// Fetch test details
$stmt = $db->prepare("SELECT title FROM tests WHERE id = :id");
$stmt->execute(['id' => $test_id]);
$test = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$test) {
echo "<div class='container mt-5'><div class='alert alert-danger'>Test not found.</div></div>";
require_once 'includes/footer.php';
exit();
}
// Fetch user's score
$scoreStmt = $db->prepare("SELECT score FROM user_tests WHERE user_id = :user_id AND test_id = :test_id");
$scoreStmt->execute(['user_id' => $user_id, 'test_id' => $test_id]);
$result = $scoreStmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Database error: " . $e->getMessage());
}
?>
<div class="container mt-5">
<div class="card text-center">
<div class="card-header">
<h2>Results for <?php echo htmlspecialchars($test['title']); ?></h2>
</div>
<div class="card-body">
<?php if ($result): ?>
<h3 class="card-title">Your Score:</h3>
<p class="display-4 fw-bold"><?php echo htmlspecialchars($result['score']); ?></p>
<p class="text-muted">This score reflects your responses to the test questions.</p>
<?php else: ?>
<div class="alert alert-warning">We couldn't find your results for this test. Please make sure you have completed it.</div>
<?php endif; ?>
</div>
<div class="card-footer text-muted">
<a href="index.php" class="btn btn-primary">Back to Test List</a>
</div>
</div>
</div>
<?php require_once 'includes/footer.php'; ?>