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(); ?>
Belum ada pertanyaan untuk ujian ini.