This commit is contained in:
Flatlogic Bot 2025-12-07 19:58:10 +00:00
parent e15fa31a20
commit 93f530e4f6
7 changed files with 502 additions and 6 deletions

View File

@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS exams (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
created_by INT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE CASCADE
);

View File

@ -0,0 +1,9 @@
CREATE TABLE IF NOT EXISTS exam_questions (
id INT AUTO_INCREMENT PRIMARY KEY,
exam_id INT NOT NULL,
question_text TEXT NOT NULL,
question_type ENUM('multiple_choice', 'free_text') NOT NULL,
options JSON,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (exam_id) REFERENCES exams(id) ON DELETE CASCADE
);

View File

@ -0,0 +1,12 @@
CREATE TABLE IF NOT EXISTS student_exams (
id INT AUTO_INCREMENT PRIMARY KEY,
exam_id INT NOT NULL,
student_id INT NOT NULL,
status ENUM('assigned', 'in-progress', 'completed') DEFAULT 'assigned',
score INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (exam_id) REFERENCES exams(id) ON DELETE CASCADE,
FOREIGN KEY (student_id) REFERENCES users(id) ON DELETE CASCADE,
UNIQUE KEY (exam_id, student_id)
);

View File

@ -0,0 +1,10 @@
CREATE TABLE IF NOT EXISTS student_answers (
id INT AUTO_INCREMENT PRIMARY KEY,
student_exam_id INT NOT NULL,
question_id INT NOT NULL,
answer_text TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (student_exam_id) REFERENCES student_exams(id) ON DELETE CASCADE,
FOREIGN KEY (question_id) REFERENCES exam_questions(id) ON DELETE CASCADE,
UNIQUE KEY (student_exam_id, question_id)
);

180
exam_questions.php Normal file
View File

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

157
exams.php
View File

@ -1,32 +1,177 @@
<?php <?php
session_start(); session_start();
require_once 'db/config.php';
if (!isset($_SESSION['user_id'])) { if (!isset($_SESSION['user_id'])) {
header('Location: login.php'); header('Location: login.php');
exit(); exit();
} }
$user_id = $_SESSION['user_id'];
$role = $_SESSION['role'];
$pdo = db();
$page_title = 'Exams';
$header_links = '<a href="' . $role . '_dashboard.php">Dashboard</a>';
// Role-based logic
if ($role === 'teacher') {
$page_title = 'Manage Exams';
// Handle form submissions for creating/editing exams
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['exam_name'])) {
$exam_name = trim($_POST['exam_name']);
if (!empty($exam_name)) {
if (isset($_POST['exam_id']) && !empty($_POST['exam_id'])) {
$stmt = $pdo->prepare('UPDATE exams SET name = ? WHERE id = ? AND created_by = ?');
$stmt->execute([$exam_name, $_POST['exam_id'], $user_id]);
} else {
$stmt = $pdo->prepare('INSERT INTO exams (name, created_by) VALUES (?, ?)');
$stmt->execute([$exam_name, $user_id]);
}
}
header('Location: exams.php');
exit();
}
// Handle exam deletion
if (isset($_GET['delete_exam'])) {
$stmt = $pdo->prepare('DELETE FROM exams WHERE id = ? AND created_by = ?');
$stmt->execute([$_GET['delete_exam'], $user_id]);
header('Location: exams.php');
exit();
}
// Fetch exams for the teacher view
$stmt = $pdo->prepare('SELECT * FROM exams WHERE created_by = ? ORDER BY created_at DESC');
$stmt->execute([$user_id]);
$exams = $stmt->fetchAll();
// Check if we are editing an exam
$edit_exam = null;
if (isset($_GET['edit_exam'])) {
$stmt = $pdo->prepare('SELECT * FROM exams WHERE id = ? AND created_by = ?');
$stmt->execute([$_GET['edit_exam'], $user_id]);
$edit_exam = $stmt->fetch();
}
} elseif ($role === 'student') {
$page_title = 'Your Exams';
// Fetch assigned exams for the student view
$stmt = $pdo->prepare('
SELECT e.name, se.status, se.score, se.id as student_exam_id
FROM student_exams se
JOIN exams e ON se.exam_id = e.id
WHERE se.student_id = ?
ORDER BY e.created_at DESC
');
$stmt->execute([$user_id]);
$assigned_exams = $stmt->fetchAll();
} else {
// Redirect other roles to their dashboard
header('Location: ' . $role . '_dashboard.php');
exit();
}
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Exams</title> <title><?php echo $page_title; ?></title>
<link rel="stylesheet" href="assets/css/custom.css"> <link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head> </head>
<body> <body>
<header> <header>
<h1>Exams</h1> <h1><?php echo $page_title; ?></h1>
<nav> <nav>
<ul> <ul>
<li><a href="teacher_dashboard.php">Dashboard</a></li> <li><?php echo $header_links; ?></li>
<li><a href="logout.php">Logout</a></li> <li><a href="logout.php">Logout</a></li>
</ul> </ul>
</nav> </nav>
</header> </header>
<main> <main>
<h2>Exam Management</h2> <?php if ($role === 'teacher'): ?>
<p>This page will contain student exam information.</p> <section>
<h2><?php echo $edit_exam ? 'Edit' : 'Create'; ?> Exam</h2>
<form action="exams.php" method="POST">
<?php if ($edit_exam): ?>
<input type="hidden" name="exam_id" value="<?php echo htmlspecialchars($edit_exam['id']); ?>">
<?php endif; ?>
<div>
<label for="exam_name">Exam Name:</label>
<input type="text" id="exam_name" name="exam_name" value="<?php echo $edit_exam ? htmlspecialchars($edit_exam['name']) : ''; ?>" required>
</div>
<button type="submit"><?php echo $edit_exam ? 'Update' : 'Create'; ?> Exam</button>
<?php if ($edit_exam): ?>
<a href="exams.php">Cancel Edit</a>
<?php endif; ?>
</form>
</section>
<section>
<h2>Your Exams</h2>
<table>
<thead>
<tr>
<th>Exam Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($exams as $exam): ?>
<tr>
<td><?php echo htmlspecialchars($exam['name']); ?></td>
<td>
<a href="view_submissions.php?exam_id=<?php echo $exam['id']; ?>">View Submissions</a> |
<a href="exams.php?edit_exam=<?php echo $exam['id']; ?>">Edit</a> |
<a href="exam_questions.php?exam_id=<?php echo $exam['id']; ?>">Manage Questions</a> |
<a href="exams.php?delete_exam=<?php echo $exam['id']; ?>" onclick="return confirm('Are you sure you want to delete this exam?');">Delete</a>
</td>
</tr>
<?php endforeach; ?>
<?php if (empty($exams)): ?>
<tr><td colspan="2">You have not created any exams yet.</td></tr>
<?php endif; ?>
</tbody>
</table>
</section>
<?php elseif ($role === 'student'): ?>
<section>
<h2>Assigned Exams</h2>
<table>
<thead>
<tr>
<th>Exam Name</th>
<th>Status</th>
<th>Score</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($assigned_exams as $exam): ?>
<tr>
<td><?php echo htmlspecialchars($exam['name']); ?></td>
<td><?php echo htmlspecialchars(ucfirst($exam['status'])); ?></td>
<td><?php echo $exam['score'] !== null ? $exam['score'] . '%' : 'Not graded'; ?></td>
<td>
<?php if ($exam['status'] === 'assigned' || $exam['status'] === 'in-progress'): ?>
<a href="take_exam.php?student_exam_id=<?php echo $exam['student_exam_id']; ?>">Take Exam</a>
<?php elseif ($exam['status'] === 'completed'): ?>
<a href="view_results.php?student_exam_id=<?php echo $exam['student_exam_id']; ?>">View Results</a>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
<?php if (empty($assigned_exams)): ?>
<tr><td colspan="4">You have no assigned exams.</td></tr>
<?php endif; ?>
</tbody>
</table>
</section>
<?php endif; ?>
</main> </main>
</body> </body>
</html> </html>

133
take_exam.php Normal file
View File

@ -0,0 +1,133 @@
<?php
session_start();
require_once 'db/config.php';
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'student') {
header('Location: login.php');
exit();
}
$user_id = $_SESSION['user_id'];
$pdo = db();
if (!isset($_GET['student_exam_id'])) {
header('Location: exams.php');
exit();
}
$student_exam_id = $_GET['student_exam_id'];
// Verify student is assigned to this exam
$stmt = $pdo->prepare('SELECT * FROM student_exams WHERE id = ? AND student_id = ?');
$stmt->execute([$student_exam_id, $user_id]);
$student_exam = $stmt->fetch();
if (!$student_exam) {
echo "You are not assigned to this exam.";
exit();
}
// Prevent re-taking a completed exam
if ($student_exam['status'] === 'completed') {
echo "You have already completed this exam.";
// Maybe redirect to a results page in the future
echo '<br><a href="exams.php">Back to Exams</a>';
exit();
}
// Fetch exam details
$stmt = $pdo->prepare('SELECT * FROM exams WHERE id = ?');
$stmt->execute([$student_exam['exam_id']]);
$exam = $stmt->fetch();
// Fetch exam questions
$stmt = $pdo->prepare('SELECT * FROM exam_questions WHERE exam_id = ? ORDER BY id ASC');
$stmt->execute([$exam['id']]);
$questions = $stmt->fetchAll();
// Handle exam submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['answers'])) {
$answers = $_POST['answers'];
$pdo->beginTransaction();
try {
foreach ($answers as $question_id => $answer_text) {
// Use INSERT ... ON DUPLICATE KEY UPDATE to prevent duplicate answer submissions
$sql = 'INSERT INTO student_answers (student_exam_id, question_id, answer_text) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE answer_text = VALUES(answer_text)';
$stmt = $pdo->prepare($sql);
$stmt->execute([$student_exam_id, $question_id, trim($answer_text)]);
}
// Mark exam as completed
$stmt = $pdo->prepare('UPDATE student_exams SET status = \'completed\' WHERE id = ?');
$stmt->execute([$student_exam_id]);
$pdo->commit();
header('Location: exams.php');
exit();
} catch (Exception $e) {
$pdo->rollBack();
// Log error properly in a real application
die("An error occurred while submitting your exam. Please try again. Error: " . $e->getMessage());
}
}
// If student is starting the exam, mark it as 'in-progress'
if ($student_exam['status'] === 'assigned') {
$stmt = $pdo->prepare('UPDATE student_exams SET status = \'in-progress\' WHERE id = ?');
$stmt->execute([$student_exam_id]);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Take Exam: <?php echo htmlspecialchars($exam['name']); ?></title>
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<header>
<h1><?php echo htmlspecialchars($exam['name']); ?></h1>
<nav>
<ul>
<li><a href="exams.php">Back to Exams</a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
</nav>
</header>
<main>
<form action="take_exam.php?student_exam_id=<?php echo $student_exam_id; ?>" method="POST">
<?php foreach ($questions as $index => $q): ?>
<div class="question-block">
<p><strong>Question <?php echo $index + 1; ?>:</strong> <?php echo nl2br(htmlspecialchars($q['question_text'])); ?></p>
<?php if ($q['question_type'] === 'multiple_choice'): ?>
<?php $options = json_decode($q['options']); ?>
<?php if($options):
foreach ($options as $option):
?>
<div>
<input type="radio" id="q_<?php echo $q['id'] . '_' . htmlspecialchars($option); ?>" name="answers[<?php echo $q['id']; ?>]" value="<?php echo htmlspecialchars($option); ?>" required>
<label for="q_<?php echo $q['id'] . '_' . htmlspecialchars($option); ?>"><?php echo htmlspecialchars($option); ?></label>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php else: // free_text ?>
<textarea name="answers[<?php echo $q['id']; ?>]" rows="5" required></textarea>
<?php endif; ?>
</div>
<?php endforeach; ?>
<?php if (empty($questions)): ?>
<p>This exam has no questions.</p>
<?php else: ?>
<button type="submit" onclick="return confirm('Are you sure you want to submit your answers?');">Submit Exam</button>
<?php endif; ?>
</form>
</main>
</body>
</html>