92 lines
3.0 KiB
PHP
92 lines
3.0 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
// Make sure the user is a logged-in student
|
|
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'siswa') {
|
|
header("Location: login.php");
|
|
exit();
|
|
}
|
|
|
|
// Ensure it's a POST request
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
header("Location: dashboard_siswa.php");
|
|
exit();
|
|
}
|
|
|
|
$exam_id = $_POST['exam_id'] ?? null;
|
|
$answers = $_POST['answers'] ?? [];
|
|
$user_id = $_SESSION['user_id'];
|
|
|
|
if (!$exam_id || empty($answers)) {
|
|
// Redirect if data is incomplete
|
|
header("Location: dashboard_siswa.php");
|
|
exit();
|
|
}
|
|
|
|
$total_questions = count($answers);
|
|
$correct_answers = 0;
|
|
|
|
// Loop through submitted answers and check correctness
|
|
foreach ($answers as $question_id => $choice_id) {
|
|
$stmt = db()->prepare("SELECT is_correct FROM choices WHERE id = ? AND question_id = ?");
|
|
$stmt->execute([$choice_id, $question_id]);
|
|
$choice = $stmt->fetch();
|
|
|
|
if ($choice && $choice['is_correct']) {
|
|
$correct_answers++;
|
|
}
|
|
}
|
|
|
|
// Calculate score
|
|
$score = ($total_questions > 0) ? ($correct_answers / $total_questions) * 100 : 0;
|
|
|
|
// Save the result to the database
|
|
$insert_stmt = db()->prepare("INSERT INTO exam_results (exam_id, user_id, score) VALUES (?, ?, ?)");
|
|
$insert_stmt->execute([$exam_id, $user_id, $score]);
|
|
|
|
// Fetch exam details for display
|
|
$exam_stmt = db()->prepare("SELECT title FROM exams WHERE id = ?");
|
|
$exam_stmt->execute([$exam_id]);
|
|
$exam = $exam_stmt->fetch();
|
|
|
|
$page_title = "Hasil Ujian";
|
|
?>
|
|
<!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-5">
|
|
<div class="card text-center">
|
|
<div class="card-header">
|
|
<h3>Hasil Ujian: <?php echo htmlspecialchars($exam['title']); ?></h3>
|
|
</div>
|
|
<div class="card-body">
|
|
<h1 class="display-4">Skor Anda:</h1>
|
|
<h2 class="display-1 text-success"><?php echo round($score, 2); ?></h2>
|
|
<p class="lead">
|
|
Anda menjawab <?php echo $correct_answers; ?> dari <?php echo $total_questions; ?> pertanyaan dengan benar.
|
|
</p>
|
|
</div>
|
|
<div class="card-footer text-muted">
|
|
<a href="dashboard_siswa.php" class="btn btn-primary">Kembali ke Dashboard</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|