177 lines
7.0 KiB
PHP
177 lines
7.0 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'];
|
|
$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>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title><?php echo $page_title; ?></title>
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body>
|
|
<header>
|
|
<h1><?php echo $page_title; ?></h1>
|
|
<nav>
|
|
<ul>
|
|
<li><?php echo $header_links; ?></li>
|
|
<li><a href="logout.php">Logout</a></li>
|
|
</ul>
|
|
</nav>
|
|
</header>
|
|
<main>
|
|
<?php if ($role === 'teacher'): ?>
|
|
<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>
|
|
</body>
|
|
</html>
|