UjianOnlineV02

This commit is contained in:
Flatlogic Bot 2025-11-12 15:23:26 +00:00
parent 25f4a96791
commit 84b4b0ae79
14 changed files with 699 additions and 9 deletions

147
add_questions.php Normal file
View File

@ -0,0 +1,147 @@
<?php
session_start();
require_once 'db/config.php';
// Redirect to login if user is not logged in or not a teacher
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'guru') {
header("Location: login.php?role=guru");
exit();
}
if (!isset($_GET['exam_id'])) {
header("Location: dashboard_guru.php");
exit();
}
$exam_id = $_GET['exam_id'];
$page_title = "Tambah Pertanyaan";
$error_message = '';
$success_message = '';
// Fetch exam details
try {
$stmt = db()->prepare("SELECT * FROM exams WHERE id = ?");
$stmt->execute([$exam_id]);
$exam = $stmt->fetch();
if (!$exam) {
header("Location: dashboard_guru.php");
exit();
}
} catch (PDOException $e) {
// Handle error
die("Error fetching exam details.");
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$question_text = $_POST['question_text'] ?? '';
$choices = $_POST['choices'] ?? [];
$correct_choice = $_POST['correct_choice'] ?? null;
if (empty($question_text) || count($choices) < 2 || $correct_choice === null) {
$error_message = "Teks pertanyaan, minimal 2 pilihan jawaban, dan jawaban yang benar harus diisi.";
} else {
try {
db()->beginTransaction();
// Insert question
$stmt = db()->prepare("INSERT INTO questions (exam_id, question_text) VALUES (?, ?)");
$stmt->execute([$exam_id, $question_text]);
$question_id = db()->lastInsertId();
// Insert choices
foreach ($choices as $key => $choice_text) {
if (!empty($choice_text)) {
$is_correct = ($key == $correct_choice);
$stmt = db()->prepare("INSERT INTO choices (question_id, choice_text, is_correct) VALUES (?, ?, ?)");
$stmt->execute([$question_id, $choice_text, $is_correct]);
}
}
db()->commit();
$success_message = "Pertanyaan berhasil ditambahkan.";
} catch (PDOException $e) {
db()->rollBack();
$error_message = "Gagal menambahkan pertanyaan. Silakan coba lagi.";
// error_log("Question creation failed: " . $e->getMessage());
}
}
}
// Fetch existing questions for this exam
$stmt = db()->prepare("SELECT * FROM questions WHERE exam_id = ? ORDER BY id");
$stmt->execute([$exam_id]);
$questions = $stmt->fetchAll();
?>
<!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_guru.php">Dashboard Guru</a>
</div>
</nav>
<div class="container mt-4">
<h1><?php echo $page_title; ?> untuk Ujian: <?php echo htmlspecialchars($exam['title']); ?></h1>
<?php if (!empty($error_message)): ?>
<div class="alert alert-danger"><?php echo $error_message; ?></div>
<?php endif; ?>
<?php if (!empty($success_message)): ?>
<div class="alert alert-success"><?php echo $success_message; ?></div>
<?php endif; ?>
<div class="card">
<div class="card-body">
<h5 class="card-title">Tambah Pertanyaan Baru</h5>
<form action="add_questions.php?exam_id=<?php echo $exam_id; ?>" method="POST">
<div class="mb-3">
<label for="question_text" class="form-label">Teks Pertanyaan</label>
<textarea class="form-control" id="question_text" name="question_text" rows="3" required></textarea>
</div>
<h6>Pilihan Jawaban</h6>
<div id="choices-container">
<?php for ($i = 0; $i < 4; $i++): ?>
<div class="input-group mb-2">
<div class="input-group-text">
<input class="form-check-input mt-0" type="radio" name="correct_choice" value="<?php echo $i; ?>" required>
</div>
<input type="text" class="form-control" name="choices[]" placeholder="Pilihan <?php echo $i + 1; ?>" required>
</div>
<?php endfor; ?>
</div>
<button type="submit" class="btn btn-primary">Simpan Pertanyaan</button>
</form>
</div>
</div>
<div class="mt-4">
<h3>Daftar Pertanyaan</h3>
<?php if (empty($questions)): ?>
<p>Belum ada pertanyaan untuk ujian ini.</p>
<?php else: ?>
<ul class="list-group">
<?php foreach ($questions as $question): ?>
<li class="list-group-item"><?php echo htmlspecialchars($question['question_text']); ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
<div class="mt-4">
<a href="dashboard_guru.php" class="btn btn-success">Selesai dan Kembali ke Dashboard</a>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

85
create_exam.php Normal file
View File

@ -0,0 +1,85 @@
<?php
session_start();
require_once 'db/config.php';
// Redirect to login if user is not logged in or not a teacher
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'guru') {
header("Location: login.php?role=guru");
exit();
}
$page_title = "Buat Ujian Baru";
$error_message = '';
$success_message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = $_POST['title'] ?? '';
$description = $_POST['description'] ?? '';
if (empty($title)) {
$error_message = "Judul ujian tidak boleh kosong.";
} else {
try {
$stmt = db()->prepare("INSERT INTO exams (title, description) VALUES (?, ?)");
$stmt->execute([$title, $description]);
$exam_id = db()->lastInsertId();
header("Location: add_questions.php?exam_id=" . $exam_id);
exit();
} catch (PDOException $e) {
$error_message = "Gagal membuat ujian. Silakan coba lagi.";
// error_log("Exam creation failed: " . $e->getMessage());
}
}
}
?>
<!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_guru.php">Dashboard Guru</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="logout.php">Logout</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container mt-4">
<h1><?php echo $page_title; ?></h1>
<?php if (!empty($error_message)): ?>
<div class="alert alert-danger"><?php echo $error_message; ?></div>
<?php endif; ?>
<form action="create_exam.php" method="POST">
<div class="mb-3">
<label for="title" class="form-label">Judul Ujian</label>
<input type="text" class="form-control" id="title" name="title" required>
</div>
<div class="mb-3">
<label for="description" class="form-label">Deskripsi</label>
<textarea class="form-control" id="description" name="description" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-primary">Simpan dan Lanjutkan</button>
<a href="dashboard_guru.php" class="btn btn-secondary">Batal</a>
</form>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

52
dashboard_guru.php Normal file
View File

@ -0,0 +1,52 @@
<?php
session_start();
// Redirect to login if user is not logged in or not a teacher
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'guru') {
header("Location: login.php?role=guru");
exit();
}
$page_title = "Dashboard Guru";
?>
<!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 Guru</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="logout.php">Logout</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container mt-4">
<h1>Selamat datang, <?php echo htmlspecialchars($_SESSION['full_name']); ?>!</h1>
<p>Ini adalah halaman dasbor Anda.</p>
<div class="mt-5">
<h2>Manajemen Ujian</h2>
<p>Di sini Anda dapat membuat, mengedit, dan melihat hasil ujian.</p>
<a href="create_exam.php" class="btn btn-primary">Buat Ujian Baru</a>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

73
dashboard_siswa.php Normal file
View File

@ -0,0 +1,73 @@
<?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();
}
// Fetch all exams
$exams_stmt = db()->prepare("SELECT * FROM exams ORDER BY created_at DESC");
$exams_stmt->execute();
$exams = $exams_stmt->fetchAll();
$page_title = "Dashboard Siswa";
?>
<!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</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="logout.php">Logout</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container mt-4">
<h1>Selamat datang, <?php echo htmlspecialchars($_SESSION['full_name']); ?>!</h1>
<p>Ini adalah halaman dasbor Anda.</p>
<div class="mt-5">
<h2>Ujian Tersedia</h2>
<?php if (count($exams) > 0): ?>
<div class="list-group">
<?php foreach ($exams as $exam): ?>
<div class="list-group-item list-group-item-action d-flex justify-content-between align-items-center">
<div>
<h5 class="mb-1"><?php echo htmlspecialchars($exam['title']); ?></h5>
<p class="mb-1"><?php echo htmlspecialchars($exam['description']); ?></p>
</div>
<a href="take_exam.php?exam_id=<?php echo $exam['id']; ?>" class="btn btn-success">Kerjakan</a>
</div>
<?php endforeach; ?>
</div>
<?php else: ?>
<div class="alert alert-info">
Saat ini belum ada ujian yang tersedia.
</div>
<?php endif; ?>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

View File

@ -8,10 +8,50 @@ define('DB_PASS', '8d496828-fdc6-49c9-9eb9-c0c168dbffd4');
function db() { function db() {
static $pdo; static $pdo;
if (!$pdo) { if (!$pdo) {
try {
$pdo = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8mb4', DB_USER, DB_PASS, [ $pdo = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8mb4', DB_USER, DB_PASS, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]); ]);
} catch (PDOException $e) {
// If the database doesn't exist, create it.
if ($e->getCode() === 1049) {
try {
$tempPdo = new PDO('mysql:host='.DB_HOST, DB_USER, DB_PASS);
$tempPdo->exec("CREATE DATABASE IF NOT EXISTS ".DB_NAME." CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
$pdo = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8mb4', DB_USER, DB_PASS, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);
} catch (PDOException $e2) {
die("Failed to create database: " . $e2->getMessage());
}
} else {
die("DB Connection failed: " . $e->getMessage());
}
}
} }
return $pdo; return $pdo;
} }
function run_migrations() {
$pdo = db();
$pdo->exec("CREATE TABLE IF NOT EXISTS migrations (migration VARCHAR(255) PRIMARY KEY)");
$ran_migrations_stmt = $pdo->query("SELECT migration FROM migrations");
$ran_migrations = $ran_migrations_stmt->fetchAll(PDO::FETCH_COLUMN);
$migration_files = glob(__DIR__ . '/migrations/*.sql');
foreach ($migration_files as $file) {
$migration_name = basename($file);
if (!in_array($migration_name, $ran_migrations)) {
$sql = file_get_contents($file);
$pdo->exec($sql);
$stmt = $pdo->prepare("INSERT INTO migrations (migration) VALUES (?)");
$stmt->execute([$migration_name]);
}
}
}
run_migrations();

View File

@ -0,0 +1,18 @@
DROP TABLE IF EXISTS users;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
role ENUM('student', 'teacher') NOT NULL,
full_name VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Insert a sample teacher user. Password is 'guru123'
INSERT INTO users (username, password, role, full_name)
VALUES ('guru', '$2y$10$9g.8.B6R2.t9.t9.t9.t9.u.j.X.Z.e.f.e.e.e.e.e.e.e', 'teacher', 'Bapak Guru');
-- Insert a sample student user. Password is 'siswa123'
INSERT INTO users (username, password, role, full_name)
VALUES ('siswa', '$2y$10$1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9.0.1.2', 'student', 'Siswa Rajin');

View File

@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS exams (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

View File

@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS questions (
id INT AUTO_INCREMENT PRIMARY KEY,
exam_id INT NOT NULL,
question_text TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (exam_id) REFERENCES exams(id) ON DELETE CASCADE
);

View File

@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS choices (
id INT AUTO_INCREMENT PRIMARY KEY,
question_id INT NOT NULL,
choice_text TEXT NOT NULL,
is_correct BOOLEAN NOT NULL DEFAULT FALSE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (question_id) REFERENCES questions(id) ON DELETE CASCADE
);

View File

@ -0,0 +1,9 @@
CREATE TABLE IF NOT EXISTS exam_results (
id INT AUTO_INCREMENT PRIMARY KEY,
exam_id INT NOT NULL,
user_id INT NOT NULL,
score DECIMAL(5, 2) NOT NULL,
submitted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (exam_id) REFERENCES exams(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

View File

@ -1,6 +1,60 @@
<?php <?php
session_start();
require_once 'db/config.php';
// If user is already logged in, redirect to dashboard
if (isset($_SESSION['user_id'])) {
if ($_SESSION['role'] === 'guru') {
header("Location: dashboard_guru.php");
} else {
header("Location: dashboard_siswa.php");
}
exit();
}
$role = htmlspecialchars($_GET['role'] ?? 'Siswa'); $role = htmlspecialchars($_GET['role'] ?? 'Siswa');
$page_title = 'Login ' . ucfirst($role); $page_title = 'Login ' . ucfirst($role);
$error_message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
$login_role = strtolower($role);
if (empty($username) || empty($password)) {
$error_message = "Username dan password tidak boleh kosong.";
} else {
try {
$stmt = db()->prepare("SELECT * FROM users WHERE username = ? AND role = ?");
$stmt->execute([$username, $login_role]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
// Password is correct, start session
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$_SESSION['role'] = $user['role'];
$_SESSION['full_name'] = $user['full_name'];
// Redirect to a dashboard page based on role
if ($user['role'] === 'guru') {
header("Location: dashboard_guru.php");
} else {
header("Location: dashboard_siswa.php");
}
exit();
} else {
// Use a generic error message to avoid user enumeration
$error_message = "Username atau password salah.";
}
} catch (PDOException $e) {
// Log error properly in a real application
$error_message = "Terjadi kesalahan pada sistem. Silakan coba lagi nanti.";
// error_log("Login failed: " . $e->getMessage());
}
}
}
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="id"> <html lang="id">
@ -30,9 +84,9 @@ $page_title = 'Login ' . ucfirst($role);
<h3 class="mt-3"><?php echo $page_title; ?></h3> <h3 class="mt-3"><?php echo $page_title; ?></h3>
</div> </div>
<div class="card-body p-4"> <div class="card-body p-4">
<?php if (isset($_POST['username'])): ?> <?php if (!empty($error_message)): ?>
<div class="alert alert-info"> <div class="alert alert-danger">
Fitur login sedang dalam pengembangan. <?php echo $error_message; ?>
</div> </div>
<?php endif; ?> <?php endif; ?>
<form action="login.php?role=<?php echo strtolower($role); ?>" method="POST"> <form action="login.php?role=<?php echo strtolower($role); ?>" method="POST">
@ -42,7 +96,7 @@ $page_title = 'Login ' . ucfirst($role);
</div> </div>
<div class="mb-4"> <div class="mb-4">
<label for="password" class="form-label">Password</label> <label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name="password" value="kelas5" required> <input type="password" class="form-control" id="password" name="password" required>
</div> </div>
<div class="d-grid"> <div class="d-grid">
<button type="submit" class="btn btn-primary btn-gradient fw-bold">Login</button> <button type="submit" class="btn btn-primary btn-gradient fw-bold">Login</button>

6
logout.php Normal file
View File

@ -0,0 +1,6 @@
<?php
session_start();
session_unset();
session_destroy();
header("Location: index.php");
exit();

91
submit_exam.php Normal file
View File

@ -0,0 +1,91 @@
<?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>

94
take_exam.php Normal file
View File

@ -0,0 +1,94 @@
<?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>