35665-vm/take_exam.php
2025-11-12 15:23:26 +00:00

95 lines
3.6 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
// Redirect to login if user is not logged in or not a student
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'siswa') {
header("Location: login.php?role=siswa");
exit();
}
// Check if exam_id is provided
if (!isset($_GET['exam_id'])) {
header("Location: dashboard_siswa.php");
exit();
}
$exam_id = $_GET['exam_id'];
// Fetch exam details
$exam_stmt = db()->prepare("SELECT * FROM exams WHERE id = ?");
$exam_stmt->execute([$exam_id]);
$exam = $exam_stmt->fetch();
if (!$exam) {
// Exam not found
header("Location: dashboard_siswa.php");
exit();
}
// Fetch questions and choices
$questions_stmt = db()->prepare("SELECT * FROM questions WHERE exam_id = ? ORDER BY id");
$questions_stmt->execute([$exam_id]);
$questions = $questions_stmt->fetchAll();
$page_title = "Kerjakan Ujian: " . htmlspecialchars($exam['title']);
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $page_title; ?> - Ulangan Harian Online</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<div class="container">
<a class="navbar-brand" href="dashboard_siswa.php">Ulangan Harian Online</a>
</div>
</nav>
<div class="container mt-4">
<h1 class="mb-4"><?php echo htmlspecialchars($exam['title']); ?></h1>
<p><?php echo htmlspecialchars($exam['description']); ?></p>
<form action="submit_exam.php" method="post">
<input type="hidden" name="exam_id" value="<?php echo $exam_id; ?>">
<?php foreach ($questions as $q_index => $question): ?>
<div class="card mb-4">
<div class="card-header">
Pertanyaan #<?php echo $q_index + 1; ?>
</div>
<div class="card-body">
<p class="card-text"><?php echo htmlspecialchars($question['question_text']); ?></p>
<?php
// Fetch choices for this question
$choices_stmt = db()->prepare("SELECT * FROM choices WHERE question_id = ? ORDER BY id");
$choices_stmt->execute([$question['id']]);
$choices = $choices_stmt->fetchAll();
?>
<?php foreach ($choices as $choice): ?>
<div class="form-check">
<input class="form-check-input" type="radio" name="answers[<?php echo $question['id']; ?>]" id="choice-<?php echo $choice['id']; ?>" value="<?php echo $choice['id']; ?>" required>
<label class="form-check-label" for="choice-<?php echo $choice['id']; ?>">
<?php echo htmlspecialchars($choice['choice_text']); ?>
</label>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
<button type="submit" class="btn btn-primary btn-lg">Selesaikan Ujian</button>
</form>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>