<?php
// Start session
session_start();
// Include database configuration
require_once '../config/database.php';
// Include header and sidebar
include_once 'includes/header.php';
// Check if user is logged in and has admin or director role
if (!isset($_SESSION['user_id']) || ($_SESSION['role'] !== 'admin' && $_SESSION['role'] !== 'director')) {
header('Location: login.php');
exit;
}
// Handle delete exam request
if (isset($_POST['delete_exam'])) {
$exam_id = $_POST['exam_id'];
// Delete exam
$delete_query = "DELETE FROM exam_schedules WHERE id = ?";
$stmt = $conn->prepare($delete_query);
$stmt->bind_param("i", $exam_id);
if ($stmt->execute()) {
$_SESSION['success'] = "Exam deleted successfully";
} else {
$_SESSION['error'] = "Error deleting exam: " . $conn->error;
}
// Redirect to prevent form resubmission
header("Location: " . $_SERVER['PHP_SELF']);
exit;
}
// Handle update exam status request
if (isset($_POST['update_status'])) {
$exam_id = $_POST['exam_id'];
$status = $_POST['status'];
// Update exam status
$update_query = "UPDATE exam_schedules SET status = ? WHERE id = ?";
$stmt = $conn->prepare($update_query);
$stmt->bind_param("si", $status, $exam_id);
if ($stmt->execute()) {
$_SESSION['success'] = "Exam status updated successfully";
} else {
$_SESSION['error'] = "Error updating exam status: " . $conn->error;
}
// Redirect to prevent form resubmission
header("Location: " . $_SERVER['PHP_SELF']);
exit;
}
// Get filter values
$status_filter = isset($_GET['status']) ? $_GET['status'] : '';
$course_filter = isset($_GET['course_id']) ? $_GET['course_id'] : '';
$search_term = isset($_GET['search']) ? $_GET['search'] : '';
// Prepare base query
$query = "
SELECT e.*, c.title as course_title,
(SELECT COUNT(*) FROM exam_question_maps WHERE exam_id = e.id) as question_count,
(SELECT COUNT(*) FROM student_exams WHERE exam_id = e.id) as attempts_count
FROM exam_schedules e
LEFT JOIN courses c ON e.course_id = c.id
WHERE 1=1
";
// Add filters to query
$params = [];
$types = "";
if (!empty($status_filter)) {
$query .= " AND e.status = ?";
$params[] = $status_filter;
$types .= "s";
}
if (!empty($course_filter)) {
$query .= " AND e.course_id = ?";
$params[] = $course_filter;
$types .= "i";
}
if (!empty($search_term)) {
$query .= " AND (e.title LIKE ? OR c.title LIKE ?)";
$search_param = "%" . $search_term . "%";
$params[] = $search_param;
$params[] = $search_param;
$types .= "ss";
}
// Add order by
$query .= " ORDER BY e.exam_date DESC, e.start_time ASC";
// Prepare and execute query
$stmt = $conn->prepare($query);
if (!empty($params)) {
$stmt->bind_param($types, ...$params);
}
$stmt->execute();
$result = $stmt->get_result();
// Get all courses for filter dropdown
$courses_query = "SELECT id, title FROM courses ORDER BY title";
$courses_result = $conn->query($courses_query);
?>
<div class="content-wrapper">
<div class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1 class="m-0">Manage Exams</h1>
</div>
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><a href="index.php">Home</a></li>
<li class="breadcrumb-item active">Exams</li>
</ol>
</div>
</div>
</div>
</div>
<section class="content">
<div class="container-fluid">
<div class="row mb-3">
<div class="col-12">
<a href="add_exam.php" class="btn btn-primary">
<i class="fas fa-plus"></i> Add New Exam
</a>
<a href="manage_question_banks.php" class="btn btn-info ml-2">
<i class="fas fa-book"></i> Manage Question Banks
</a>
</div>
</div>
<?php if (isset($_SESSION['success'])): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<?php
echo $_SESSION['success'];
unset($_SESSION['success']);
?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<?php endif; ?>
<?php if (isset($_SESSION['error'])): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<?php
echo $_SESSION['error'];
unset($_SESSION['error']);
?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<?php endif; ?>
<div class="card">
<div class="card-header">
<h3 class="card-title">Exams List</h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse">
<i class="fas fa-minus"></i>
</button>
</div>
</div>
<div class="card-body">
<!-- Filters -->
<form action="" method="GET" class="mb-4">
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label>Course</label>
<select name="course_id" class="form-control">
<option value="">All Courses</option>
<?php while ($course = $courses_result->fetch_assoc()): ?>
<option value="<?php echo $course['id']; ?>" <?php echo ($course_filter == $course['id']) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($course['title']); ?>
</option>
<?php endwhile; ?>
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Status</label>
<select name="status" class="form-control">
<option value="">All Statuses</option>
<option value="active" <?php echo ($status_filter == 'active') ? 'selected' : ''; ?>>Active</option>
<option value="inactive" <?php echo ($status_filter == 'inactive') ? 'selected' : ''; ?>>Inactive</option>
<option value="completed" <?php echo ($status_filter == 'completed') ? 'selected' : ''; ?>>Completed</option>
</select>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Search</label>
<input type="text" name="search" class="form-control" placeholder="Search..." value="<?php echo htmlspecialchars($search_term); ?>">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label> </label>
<button type="submit" class="btn btn-primary btn-block">Filter</button>
</div>
</div>
</div>
</form>
<!-- Exams Table -->
<div class="table-responsive">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Course</th>
<th>Date</th>
<th>Time</th>
<th>Duration</th>
<th>Questions</th>
<th>Attempts</th>
<!-- <th>Status</th> -->
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if ($result->num_rows > 0): ?>
<?php while ($exam = $result->fetch_assoc()): ?>
<tr>
<td><?php echo $exam['id']; ?></td>
<td><?php echo htmlspecialchars($exam['title']); ?></td>
<td><?php echo htmlspecialchars($exam['course_title']); ?></td>
<td><?php echo date('d-m-Y', strtotime($exam['exam_date'])); ?></td>
<td><?php echo date('h:i A', strtotime($exam['start_time'])) . ' - ' . date('h:i A', strtotime($exam['end_time'])); ?></td>
<td><?php echo $exam['duration_minutes'] . ' mins'; ?></td>
<td>
<span class="badge badge-info"><?php echo $exam['question_count']; ?></span>
<a href="map_questions.php?exam_id=<?php echo $exam['id']; ?>" class="btn btn-xs btn-outline-primary ml-1">
<i class="fas fa-pen"></i> Manage
</a>
</td>
<td>
<span class="badge badge-secondary"><?php echo $exam['attempts_count']; ?></span>
<?php if ($exam['attempts_count'] > 0): ?>
<a href="exam_results.php?exam_id=<?php echo $exam['id']; ?>" class="btn btn-xs btn-outline-info ml-1">
<i class="fas fa-chart-bar"></i> Results
</a>
<?php endif; ?>
</td>
<!-- <td>
<?php
// $status_class = '';
// if ($exam['status'] == 'active') {
// $status_class = 'success';
// } elseif ($exam['status'] == 'inactive') {
// $status_class = 'warning';
// } else {
// $status_class = 'secondary';
// }
// ?>
<span class="badge badge-<?php echo $status_class; ?>">
<?php echo ucfirst($exam['status']); ?>
</span>
<button type="button" class="btn btn-xs btn-outline-secondary dropdown-toggle ml-1" data-toggle="dropdown">
Change
</button>
<div class="dropdown-menu">
<form action="" method="POST">
<input type="hidden" name="exam_id" value="<?php echo $exam['id']; ?>">
<input type="hidden" name="status" value="active">
<button type="submit" name="update_status" class="dropdown-item">
<i class="fas fa-check text-success"></i> Set Active
</button>
</form>
<form action="" method="POST">
<input type="hidden" name="exam_id" value="<?php echo $exam['id']; ?>">
<input type="hidden" name="status" value="inactive">
<button type="submit" name="update_status" class="dropdown-item">
<i class="fas fa-pause text-warning"></i> Set Inactive
</button>
</form>
<form action="" method="POST">
<input type="hidden" name="exam_id" value="<?php echo $exam['id']; ?>">
<input type="hidden" name="status" value="completed">
<button type="submit" name="update_status" class="dropdown-item">
<i class="fas fa-check-double text-info"></i> Set Completed
</button>
</form>
</div>
</td> -->
<td>
<div class="btn-group">
<a href="edit_exam.php?id=<?php echo $exam['id']; ?>" class="btn btn-sm btn-info">
<i class="fas fa-edit"></i>
</a>
<button type="button" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#deleteModal<?php echo $exam['id']; ?>">
<i class="fas fa-trash"></i>
</button>
</div>
<!-- Delete Modal -->
<div class="modal fade" id="deleteModal<?php echo $exam['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header bg-danger">
<h5 class="modal-title" id="deleteModalLabel">Confirm Delete</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Are you sure you want to delete the exam: <strong><?php echo htmlspecialchars($exam['title']); ?></strong>?</p>
<p class="text-danger">This action cannot be undone and will remove all associated student exams and results.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<form action="" method="POST">
<input type="hidden" name="exam_id" value="<?php echo $exam['id']; ?>">
<button type="submit" name="delete_exam" class="btn btn-danger">Delete Exam</button>
</form>
</div>
</div>
</div>
</div>
</td>
</tr>
<?php endwhile; ?>
<?php else: ?>
<tr>
<td colspan="10" class="text-center">No exams found</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</section>
</div>
<?php
// Include footer
include_once 'includes/footer.php';
?>