36352-vm/exam_questions.php
Flatlogic Bot 93f530e4f6 t7
2025-12-07 19:58:10 +00:00

181 lines
7.2 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit();
}
$user_id = $_SESSION['user_id'];
$role = $_SESSION['role'];
if ($role !== 'teacher') {
header('Location: ' . $role . '_dashboard.php');
exit();
}
if (!isset($_GET['exam_id'])) {
header('Location: exams.php');
exit();
}
$exam_id = $_GET['exam_id'];
$pdo = db();
// Verify the teacher owns the exam
$stmt = $pdo->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();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Manage Questions for <?php echo htmlspecialchars($exam['name']); ?></title>
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<header>
<h1>Manage Questions for "<?php echo htmlspecialchars($exam['name']); ?>"</h1>
<nav>
<ul>
<li><a href="exams.php">Back to Exams</a></li>
<li><a href="teacher_dashboard.php">Dashboard</a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
</nav>
</header>
<main>
<section>
<h2><?php echo $edit_question ? 'Edit' : 'Add'; ?> Question</h2>
<form action="exam_questions.php?exam_id=<?php echo $exam_id; ?>" method="POST">
<?php if ($edit_question): ?>
<input type="hidden" name="question_id" value="<?php echo htmlspecialchars($edit_question['id']); ?>">
<?php endif; ?>
<div>
<label for="question_text">Question:</label>
<textarea id="question_text" name="question_text" rows="3" required><?php echo $edit_question ? htmlspecialchars($edit_question['question_text']) : ''; ?></textarea>
</div>
<div>
<label for="question_type">Question Type:</label>
<select id="question_type" name="question_type">
<option value="free_text" <?php echo ($edit_question && $edit_question['question_type'] === 'free_text') ? 'selected' : ''; ?>>Free Text</option>
<option value="multiple_choice" <?php echo ($edit_question && $edit_question['question_type'] === 'multiple_choice') ? 'selected' : ''; ?>>Multiple Choice</option>
</select>
</div>
<div id="mc_options_container">
<label for="options">Options (one per line):</label>
<textarea id="options" name="options" rows="5"><?php echo ($edit_question && $edit_question['options']) ? htmlspecialchars(implode("\n", json_decode($edit_question['options']))) : ''; ?></textarea>
</div>
<button type="submit"><?php echo $edit_question ? 'Update' : 'Add'; ?> Question</button>
<?php if ($edit_question): ?>
<a href="exam_questions.php?exam_id=<?php echo $exam_id; ?>">Cancel Edit</a>
<?php endif; ?>
</form>
</section>
<section>
<h2>Questions</h2>
<table>
<thead>
<tr>
<th>Question</th>
<th>Type</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($questions as $question):
?>
<tr>
<td><?php echo nl2br(htmlspecialchars($question['question_text'])); ?></td>
<td><?php echo htmlspecialchars(str_replace('_', ' ', $question['question_type'])); ?></td>
<td>
<a href="exam_questions.php?exam_id=<?php echo $exam_id; ?>&edit_question=<?php echo $question['id']; ?>">Edit</a> |
<a href="exam_questions.php?exam_id=<?php echo $exam_id; ?>&delete_question=<?php echo $question['id']; ?>" onclick="return confirm('Are you sure you want to delete this question?');">Delete</a>
</td>
</tr>
<?php endforeach; ?>
<?php if (empty($questions)):
?>
<tr>
<td colspan="3">No questions have been added to this exam yet.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</section>
</main>
<script>
// Show/hide multiple choice options based on question type selection
const questionType = document.getElementById('question_type');
const mcOptionsContainer = document.getElementById('mc_options_container');
function toggleMcOptions() {
mcOptionsContainer.style.display = questionType.value === 'multiple_choice' ? 'block' : 'none';
}
toggleMcOptions(); // Initial check
questionType.addEventListener('change', toggleMcOptions);
</script>
</body>
</html>