prepare('SELECT * FROM exams WHERE id = ? AND created_by = ?');
$stmt->execute([$exam_id, $user_id]);
$exam = $stmt->fetch();
if (!$exam) {
echo "Exam not found or you don't have permission to edit it.";
exit();
}
// Handle question form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['question_text'])) {
$question_text = trim($_POST['question_text']);
$question_type = $_POST['question_type'];
$options = null;
if ($question_type === 'multiple_choice') {
$options_raw = trim($_POST['options']);
if (!empty($options_raw)) {
$options = json_encode(array_map('trim', explode("\n", $options_raw)));
}
}
if (!empty($question_text)) {
if (isset($_POST['question_id']) && !empty($_POST['question_id'])) {
// Update question
$stmt = $pdo->prepare('UPDATE exam_questions SET question_text = ?, question_type = ?, options = ? WHERE id = ? AND exam_id = ?');
$stmt->execute([$question_text, $question_type, $options, $_POST['question_id'], $exam_id]);
} else {
// Add new question
$stmt = $pdo->prepare('INSERT INTO exam_questions (exam_id, question_text, question_type, options) VALUES (?, ?, ?, ?)');
$stmt->execute([$exam_id, $question_text, $question_type, $options]);
}
}
header('Location: exam_questions.php?exam_id=' . $exam_id);
exit();
}
// Handle question deletion
if (isset($_GET['delete_question'])) {
$question_id = $_GET['delete_question'];
$stmt = $pdo->prepare('DELETE FROM exam_questions WHERE id = ? AND exam_id = ?');
$stmt->execute([$question_id, $exam_id]);
header('Location: exam_questions.php?exam_id=' . $exam_id);
exit();
}
// Fetch questions for the exam
$stmt = $pdo->prepare('SELECT * FROM exam_questions WHERE exam_id = ? ORDER BY created_at ASC');
$stmt->execute([$exam_id]);
$questions = $stmt->fetchAll();
// Check if we are editing a question
$edit_question = null;
if (isset($_GET['edit_question'])) {
$stmt = $pdo->prepare('SELECT * FROM exam_questions WHERE id = ? AND exam_id = ?');
$stmt->execute([$_GET['edit_question'], $exam_id]);
$edit_question = $stmt->fetch();
}
?>
Manage Questions for
Questions
| Question |
Type |
Actions |
|
|
Edit |
Delete
|
| No questions have been added to this exam yet. |