148 lines
5.5 KiB
PHP
148 lines
5.5 KiB
PHP
<?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>
|