<?php
// Start session
session_start();
// Include database configuration
require_once '../config/database.php';
// Check if user is logged in and has admin role
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
header('Location: login.php');
exit;
}
// Initialize messages from session (if any)
$success_message = isset($_SESSION['success_message']) ? $_SESSION['success_message'] : '';
$error_message = isset($_SESSION['error_message']) ? $_SESSION['error_message'] : '';
// Clear session messages after retrieving them
unset($_SESSION['success_message']);
unset($_SESSION['error_message']);
// Handle exam deletion
if (isset($_GET['delete']) && is_numeric($_GET['delete'])) {
$exam_id = $_GET['delete'];
try {
// Delete the 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_message'] = "Exam schedule deleted successfully.";
} else {
$_SESSION['error_message'] = "Failed to delete exam schedule.";
}
// Redirect to prevent resubmission
header('Location: schedules.php');
exit;
} catch (Exception $e) {
$_SESSION['error_message'] = "Error: " . $e->getMessage();
header('Location: schedules.php');
exit;
}
}
// Handle form submission for adding/editing exam schedule
if (isset($_POST['submit_exam'])) {
$exam_id = isset($_POST['exam_id']) ? $_POST['exam_id'] : null;
$title = $_POST['title'];
$description = $_POST['description'];
$course_id = $_POST['course_id'];
$exam_date = $_POST['exam_date'];
$start_time = $_POST['start_time'];
$end_time = $_POST['end_time'];
$duration_minutes = $_POST['duration_minutes'];
$location = $_POST['location'];
$passing_percentage = $_POST['passing_percentage'];
$is_active = isset($_POST['is_active']) ? 1 : 0;
// Validate inputs
if (empty($title) || empty($course_id) || empty($exam_date) || empty($start_time) || empty($end_time)) {
$_SESSION['error_message'] = "Please fill in all required fields.";
header('Location: schedules.php' . ($exam_id ? "?edit={$exam_id}" : ''));
exit;
} else {
try {
// Check if it's an update or new entry
if ($exam_id) {
// Update existing exam schedule
$query = "UPDATE exam_schedules SET
title = ?,
description = ?,
course_id = ?,
exam_date = ?,
start_time = ?,
end_time = ?,
duration_minutes = ?,
location = ?,
passing_percentage = ?,
is_active = ?
WHERE id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("ssisssisdii", $title, $description, $course_id, $exam_date, $start_time, $end_time, $duration_minutes, $location, $passing_percentage, $is_active, $exam_id);
if ($stmt->execute()) {
$_SESSION['success_message'] = "Exam schedule updated successfully.";
} else {
$_SESSION['error_message'] = "Failed to update exam schedule.";
}
} else {
// Insert new exam schedule
$query = "INSERT INTO exam_schedules (title, description, course_id, exam_date, start_time, end_time, duration_minutes, location, passing_percentage, is_active)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $conn->prepare($query);
$stmt->bind_param("ssisssisdi", $title, $description, $course_id, $exam_date, $start_time, $end_time, $duration_minutes, $location, $passing_percentage, $is_active);
if ($stmt->execute()) {
$_SESSION['success_message'] = "Exam schedule added successfully.";
} else {
$_SESSION['error_message'] = "Failed to add exam schedule.";
}
}
// Redirect to prevent form resubmission
header('Location: schedules.php');
exit;
} catch (Exception $e) {
$_SESSION['error_message'] = "Error: " . $e->getMessage();
header('Location: schedules.php' . ($exam_id ? "?edit={$exam_id}" : ''));
exit;
}
}
}
// Get exam details for editing
$edit_exam = null;
if (isset($_GET['edit']) && is_numeric($_GET['edit'])) {
$exam_id = $_GET['edit'];
$edit_query = "SELECT * FROM exam_schedules WHERE id = ?";
$stmt = $conn->prepare($edit_query);
$stmt->bind_param("i", $exam_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$edit_exam = $result->fetch_assoc();
}
}
// Get all courses for dropdown
$courses_query = "SELECT id, title FROM courses ORDER BY title";
$courses_result = $conn->query($courses_query);
$courses = [];
if ($courses_result->num_rows > 0) {
while ($row = $courses_result->fetch_assoc()) {
$courses[] = $row;
}
}
// Get exam schedules with course titles
$exams_query = "SELECT es.*, c.title as course_title
FROM exam_schedules es
JOIN courses c ON es.course_id = c.id
ORDER BY es.exam_date DESC";
$exams_result = $conn->query($exams_query);
// Include header
include_once 'includes/header.php';
?>
<div class="container-fluid">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h3 text-gray-800">Exam Schedule Management</h1>
<div class="btn-group">
<a href="schedules.php" class="btn btn-primary active">
<i class="fas fa-calendar-alt"></i> Basic Schedule
</a>
<a href="manage_exams.php" class="btn btn-outline-primary">
<i class="fas fa-tasks"></i> Advanced Exam Management
</a>
</div>
</div>
<?php if ($error_message): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<?php echo $error_message; ?>
<button type="button" class="close btn-close" data-dismiss="alert" data-bs-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<?php endif; ?>
<?php if ($success_message): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<?php echo $success_message; ?>
<button type="button" class="close btn-close" data-dismiss="alert" data-bs-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<?php endif; ?>
<!-- Exam Schedule Form -->
<div class="card shadow mb-4">
<div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
<h6 class="m-0 font-weight-bold text-primary">
<?php echo $edit_exam ? 'Edit Exam Schedule' : 'Add New Exam Schedule'; ?>
</h6>
</div>
<div class="card-body">
<form action="schedules.php" method="POST">
<?php if ($edit_exam): ?>
<input type="hidden" name="exam_id" value="<?php echo $edit_exam['id']; ?>">
<?php endif; ?>
<div class="row">
<div class="col-md-6 mb-3">
<label for="title">Exam Title <span class="text-danger">*</span></label>
<input type="text" class="form-control" id="title" name="title" required
value="<?php echo $edit_exam ? htmlspecialchars($edit_exam['title']) : ''; ?>">
</div>
<div class="col-md-6 mb-3">
<label for="course_id">Course <span class="text-danger">*</span></label>
<select class="form-control" id="course_id" name="course_id" required>
<option value="">-- Select Course --</option>
<?php foreach ($courses as $course): ?>
<option value="<?php echo $course['id']; ?>"
<?php echo ($edit_exam && $edit_exam['course_id'] == $course['id']) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($course['title']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="mb-3">
<label for="description">Description</label>
<textarea class="form-control" id="description" name="description" rows="3"><?php echo $edit_exam ? htmlspecialchars($edit_exam['description']) : ''; ?></textarea>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label for="exam_date">Exam Date <span class="text-danger">*</span></label>
<input type="date" class="form-control" id="exam_date" name="exam_date" required
value="<?php echo $edit_exam ? $edit_exam['exam_date'] : ''; ?>">
<small class="text-muted">Date when the exam is available</small>
</div>
<div class="col-md-6 mb-3">
<label for="duration_minutes">Duration (minutes) <span class="text-danger">*</span></label>
<input type="number" class="form-control" id="duration_minutes" name="duration_minutes" required
value="<?php echo $edit_exam ? $edit_exam['duration_minutes'] : '60'; ?>" min="1">
<small class="text-muted">How long students have to complete the exam</small>
</div>
</div>
<!-- Hidden fields for start/end time -->
<input type="hidden" id="start_time" name="start_time" value="<?php echo $edit_exam ? $edit_exam['start_time'] : '09:00'; ?>">
<input type="hidden" id="end_time" name="end_time" value="<?php echo $edit_exam ? $edit_exam['end_time'] : '18:00'; ?>">
<div class="row">
<div class="col-md-6 mb-3">
<label for="location">Location</label>
<input type="text" class="form-control" id="location" name="location"
value="<?php echo $edit_exam ? htmlspecialchars($edit_exam['location']) : ''; ?>">
</div>
<div class="col-md-6 mb-3">
<label for="passing_percentage">Passing Percentage <span class="text-danger">*</span></label>
<input type="number" class="form-control" id="passing_percentage" name="passing_percentage" required
value="<?php echo $edit_exam ? $edit_exam['passing_percentage'] : '40'; ?>" min="1" max="100">
</div>
</div>
<div class="mb-3">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="is_active" name="is_active"
<?php echo (!$edit_exam || $edit_exam['is_active']) ? 'checked' : ''; ?>>
<label class="form-check-label" for="is_active">
Active
</label>
</div>
</div>
<div class="form-group">
<button type="submit" name="submit_exam" class="btn btn-primary">
<?php echo $edit_exam ? 'Update Exam' : 'Add Exam'; ?>
</button>
<?php if ($edit_exam): ?>
<a href="schedules.php" class="btn btn-secondary">Cancel</a>
<?php endif; ?>
</div>
</form>
</div>
</div>
<!-- Exam Schedules Table -->
<div class="card shadow mb-4">
<div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
<h6 class="m-0 font-weight-bold text-primary">Exam Schedules</h6>
<div>
<a href="add_exam.php" class="btn btn-sm btn-success">
<i class="fas fa-plus"></i> Advanced Exam Creation
</a>
</div>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Title</th>
<th>Course</th>
<th>Date</th>
<th>Time</th>
<th>Duration</th>
<th>Location</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if ($exams_result && $exams_result->num_rows > 0): ?>
<?php while ($exam = $exams_result->fetch_assoc()): ?>
<tr>
<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'])); ?> -
<?php echo date('h:i A', strtotime($exam['end_time'])); ?>
</td>
<td>
<?php
$duration = $exam['duration_minutes'];
$hours = floor($duration / 60);
$minutes = $duration % 60;
if ($hours > 0) {
echo $hours . 'h ';
}
if ($minutes > 0 || $hours == 0) {
echo $minutes . 'm';
}
?>
</td>
<td><?php echo htmlspecialchars($exam['location'] ?? 'N/A'); ?></td>
<td>
<?php if ($exam['is_active']): ?>
<span class="badge badge-success">Active</span>
<?php else: ?>
<span class="badge badge-secondary">Inactive</span>
<?php endif; ?>
</td>
<td>
<div class="btn-group">
<a href="map_questions.php?exam_id=<?php echo $exam['id']; ?>" class="btn btn-success btn-sm">
<i class="fas fa-question-circle"></i> Map Questions
</a>
<button type="button" class="btn btn-primary btn-sm dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="sr-only">Toggle Dropdown</span>
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="schedules.php?edit=<?php echo $exam['id']; ?>">
<i class="fas fa-edit"></i> Basic Edit
</a>
<a class="dropdown-item" href="edit_exam.php?id=<?php echo $exam['id']; ?>">
<i class="fas fa-edit"></i> Advanced Edit
</a>
<a class="dropdown-item" href="exam_questions.php?exam_id=<?php echo $exam['id']; ?>">
<i class="fas fa-list"></i> Manage Questions
</a>
<a class="dropdown-item" href="exam_results.php?exam_id=<?php echo $exam['id']; ?>">
<i class="fas fa-chart-bar"></i> View Results
</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item text-danger" href="schedules.php?delete=<?php echo $exam['id']; ?>"
onclick="return confirm('Are you sure you want to delete this exam schedule?');">
<i class="fas fa-trash"></i> Delete
</a>
</div>
</div>
</td>
</tr>
<?php endwhile; ?>
<?php else: ?>
<tr>
<td colspan="8" class="text-center">No exam schedules found</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script>
// Initialize DataTable
$(document).ready(function() {
$('#dataTable').DataTable({
order: [[2, 'desc']] // Sort by date by default (descending)
});
// Format the duration for display
if (document.getElementById('duration_minutes')) {
const duration = parseInt(document.getElementById('duration_minutes').value);
if (duration) {
document.getElementById('duration_minutes').addEventListener('change', function() {
const duration = parseInt(this.value);
if (duration < 1) {
this.value = 1;
}
});
}
}
});
</script>
<?php
// Include footer
include_once 'includes/footer.php';
?>