36352-vm/take_exam.php
Flatlogic Bot 93f530e4f6 t7
2025-12-07 19:58:10 +00:00

134 lines
4.9 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'student') {
header('Location: login.php');
exit();
}
$user_id = $_SESSION['user_id'];
$pdo = db();
if (!isset($_GET['student_exam_id'])) {
header('Location: exams.php');
exit();
}
$student_exam_id = $_GET['student_exam_id'];
// Verify student is assigned to this exam
$stmt = $pdo->prepare('SELECT * FROM student_exams WHERE id = ? AND student_id = ?');
$stmt->execute([$student_exam_id, $user_id]);
$student_exam = $stmt->fetch();
if (!$student_exam) {
echo "You are not assigned to this exam.";
exit();
}
// Prevent re-taking a completed exam
if ($student_exam['status'] === 'completed') {
echo "You have already completed this exam.";
// Maybe redirect to a results page in the future
echo '<br><a href="exams.php">Back to Exams</a>';
exit();
}
// Fetch exam details
$stmt = $pdo->prepare('SELECT * FROM exams WHERE id = ?');
$stmt->execute([$student_exam['exam_id']]);
$exam = $stmt->fetch();
// Fetch exam questions
$stmt = $pdo->prepare('SELECT * FROM exam_questions WHERE exam_id = ? ORDER BY id ASC');
$stmt->execute([$exam['id']]);
$questions = $stmt->fetchAll();
// Handle exam submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['answers'])) {
$answers = $_POST['answers'];
$pdo->beginTransaction();
try {
foreach ($answers as $question_id => $answer_text) {
// Use INSERT ... ON DUPLICATE KEY UPDATE to prevent duplicate answer submissions
$sql = 'INSERT INTO student_answers (student_exam_id, question_id, answer_text) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE answer_text = VALUES(answer_text)';
$stmt = $pdo->prepare($sql);
$stmt->execute([$student_exam_id, $question_id, trim($answer_text)]);
}
// Mark exam as completed
$stmt = $pdo->prepare('UPDATE student_exams SET status = \'completed\' WHERE id = ?');
$stmt->execute([$student_exam_id]);
$pdo->commit();
header('Location: exams.php');
exit();
} catch (Exception $e) {
$pdo->rollBack();
// Log error properly in a real application
die("An error occurred while submitting your exam. Please try again. Error: " . $e->getMessage());
}
}
// If student is starting the exam, mark it as 'in-progress'
if ($student_exam['status'] === 'assigned') {
$stmt = $pdo->prepare('UPDATE student_exams SET status = \'in-progress\' WHERE id = ?');
$stmt->execute([$student_exam_id]);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Take Exam: <?php echo htmlspecialchars($exam['name']); ?></title>
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<header>
<h1><?php echo htmlspecialchars($exam['name']); ?></h1>
<nav>
<ul>
<li><a href="exams.php">Back to Exams</a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
</nav>
</header>
<main>
<form action="take_exam.php?student_exam_id=<?php echo $student_exam_id; ?>" method="POST">
<?php foreach ($questions as $index => $q): ?>
<div class="question-block">
<p><strong>Question <?php echo $index + 1; ?>:</strong> <?php echo nl2br(htmlspecialchars($q['question_text'])); ?></p>
<?php if ($q['question_type'] === 'multiple_choice'): ?>
<?php $options = json_decode($q['options']); ?>
<?php if($options):
foreach ($options as $option):
?>
<div>
<input type="radio" id="q_<?php echo $q['id'] . '_' . htmlspecialchars($option); ?>" name="answers[<?php echo $q['id']; ?>]" value="<?php echo htmlspecialchars($option); ?>" required>
<label for="q_<?php echo $q['id'] . '_' . htmlspecialchars($option); ?>"><?php echo htmlspecialchars($option); ?></label>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php else: // free_text ?>
<textarea name="answers[<?php echo $q['id']; ?>]" rows="5" required></textarea>
<?php endif; ?>
</div>
<?php endforeach; ?>
<?php if (empty($questions)): ?>
<p>This exam has no questions.</p>
<?php else: ?>
<button type="submit" onclick="return confirm('Are you sure you want to submit your answers?');">Submit Exam</button>
<?php endif; ?>
</form>
</main>
</body>
</html>