98 lines
3.8 KiB
PHP
98 lines
3.8 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();
|
|
|
|
// Check if user has already taken this test
|
|
$checkStmt = $db->prepare("SELECT COUNT(*) FROM user_tests WHERE user_id = :user_id AND test_id = :test_id");
|
|
$checkStmt->execute(['user_id' => $user_id, 'test_id' => $test_id]);
|
|
if ($checkStmt->fetchColumn() > 0) {
|
|
echo "<div class='container mt-5'><div class='alert alert-info'>You have already completed this test. <a href='test_results.php?test_id=$test_id'>View your results</a>.</div></div>";
|
|
require_once 'includes/footer.php';
|
|
exit();
|
|
}
|
|
|
|
|
|
// Fetch test details
|
|
$stmt = $db->prepare("SELECT title, description 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 questions and options
|
|
$questionsStmt = $db->prepare("
|
|
SELECT q.id AS question_id, q.question_text, o.id AS option_id, o.option_text, o.score
|
|
FROM questions q
|
|
JOIN options o ON q.id = o.question_id
|
|
WHERE q.test_id = :test_id
|
|
ORDER BY q.id, o.id
|
|
");
|
|
$questionsStmt->execute(['test_id' => $test_id]);
|
|
$questionsData = $questionsStmt->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_ASSOC);
|
|
|
|
} catch (PDOException $e) {
|
|
die("Database error: " . $e->getMessage());
|
|
}
|
|
|
|
?>
|
|
|
|
<div class="container mt-5">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h2><?php echo htmlspecialchars($test['title']); ?></h2>
|
|
</div>
|
|
<div class="card-body">
|
|
<p class="lead"><?php echo nl2br(htmlspecialchars($test['description'])); ?></p>
|
|
<hr>
|
|
<?php if (empty($questionsData)): ?>
|
|
<div class="alert alert-warning">This test has no questions yet.</div>
|
|
<?php else: ?>
|
|
<form action="submit_test.php" method="post">
|
|
<input type="hidden" name="test_id" value="<?php echo $test_id; ?>">
|
|
|
|
<?php foreach ($questionsData as $question_id => $options): ?>
|
|
<div class="mb-4">
|
|
<h5><?php echo htmlspecialchars($options[0]['question_text']); ?></h5>
|
|
<?php foreach ($options as $option): ?>
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="radio" name="answers[<?php echo $question_id; ?>]" id="option_<?php echo $option['option_id']; ?>" value="<?php echo $option['option_id']; ?>" required>
|
|
<label class="form-check-label" for="option_<?php echo $option['option_id']; ?>">
|
|
<?php echo htmlspecialchars($option['option_text']); ?>
|
|
</label>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
|
|
<button type="submit" class="btn btn-success">Submit Answers</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once 'includes/footer.php'; ?>
|