<?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;
}
// Get statistics data
$stats = [];
try {
// Total number of question banks
$banks_query = "SELECT COUNT(*) as count FROM question_banks";
$banks_result = $conn->query($banks_query);
$stats['question_banks'] = $banks_result->fetch_assoc()['count'];
// Total number of questions
$questions_query = "SELECT COUNT(*) as count FROM questions";
$questions_result = $conn->query($questions_query);
$stats['questions'] = $questions_result->fetch_assoc()['count'];
// Total exams
$exams_query = "SELECT COUNT(*) as count FROM exam_schedules";
$exams_result = $conn->query($exams_query);
$stats['exams'] = $exams_result->fetch_assoc()['count'];
// Total exam attempts
$attempts_query = "SELECT COUNT(*) as count FROM student_exams";
$attempts_result = $conn->query($attempts_query);
$stats['attempts'] = $attempts_result->fetch_assoc()['count'];
// Upcoming exams
$upcoming_query = "SELECT es.*, c.title as course_title
FROM exam_schedules es
JOIN courses c ON es.course_id = c.id
WHERE es.exam_date >= CURDATE()
ORDER BY es.exam_date
LIMIT 5";
$upcoming_result = $conn->query($upcoming_query);
$upcoming_exams = [];
if ($upcoming_result->num_rows > 0) {
while ($row = $upcoming_result->fetch_assoc()) {
$upcoming_exams[] = $row;
}
}
// Recent exam attempts
$recent_attempts_query = "SELECT se.*, u.first_name, u.last_name, u.id,
es.title as exam_title, c.title as course_title
FROM student_exams se
JOIN users u ON se.user_id = u.id
JOIN exam_schedules es ON se.exam_id = es.id
JOIN courses c ON es.course_id = c.id
ORDER BY se.created_at DESC
LIMIT 5";
$recent_attempts_result = $conn->query($recent_attempts_query);
$recent_attempts = [];
if ($recent_attempts_result->num_rows > 0) {
while ($row = $recent_attempts_result->fetch_assoc()) {
$recent_attempts[] = $row;
}
}
} catch (Exception $e) {
$error_message = "Error retrieving statistics: " . $e->getMessage();
}
// Include header
include_once 'includes/header.php';
?>
<div class="container-fluid">
<div class="d-sm-flex align-items-center justify-content-between mb-4">
<h1 class="h3 mb-0 text-gray-800">Exam Management Dashboard</h1>
<div>
<a href="schedules.php" class="d-none d-sm-inline-block btn btn-primary shadow-sm mr-2">
<i class="fas fa-calendar fa-sm text-white-50"></i> Manage Exams
</a>
<a href="banks.php" class="d-none d-sm-inline-block btn btn-info shadow-sm">
<i class="fas fa-question-circle fa-sm text-white-50"></i> Question Banks
</a>
</div>
</div>
<?php if (isset($error_message)): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<?php echo $error_message; ?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<?php endif; ?>
<!-- Statistics Cards -->
<div class="row">
<!-- Question Banks Card -->
<div class="col-xl-3 col-md-6 mb-4">
<div class="card border-left-primary shadow h-100 py-2">
<div class="card-body">
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-xs font-weight-bold text-primary text-uppercase mb-1">
Question Banks</div>
<div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo isset($stats['question_banks']) ? number_format($stats['question_banks']) : 0; ?></div>
</div>
<div class="col-auto">
<i class="fas fa-clipboard-list fa-2x text-gray-300"></i>
</div>
</div>
</div>
</div>
</div>
<!-- Questions Card -->
<div class="col-xl-3 col-md-6 mb-4">
<div class="card border-left-success shadow h-100 py-2">
<div class="card-body">
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-xs font-weight-bold text-success text-uppercase mb-1">
Total Questions</div>
<div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo isset($stats['questions']) ? number_format($stats['questions']) : 0; ?></div>
</div>
<div class="col-auto">
<i class="fas fa-question fa-2x text-gray-300"></i>
</div>
</div>
</div>
</div>
</div>
<!-- Exams Card -->
<div class="col-xl-3 col-md-6 mb-4">
<div class="card border-left-info shadow h-100 py-2">
<div class="card-body">
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-xs font-weight-bold text-info text-uppercase mb-1">
Total Exams</div>
<div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo isset($stats['exams']) ? number_format($stats['exams']) : 0; ?></div>
</div>
<div class="col-auto">
<i class="fas fa-calendar-alt fa-2x text-gray-300"></i>
</div>
</div>
</div>
</div>
</div>
<!-- Attempts Card -->
<div class="col-xl-3 col-md-6 mb-4">
<div class="card border-left-warning shadow h-100 py-2">
<div class="card-body">
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-xs font-weight-bold text-warning text-uppercase mb-1">
Exam Attempts</div>
<div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo isset($stats['attempts']) ? number_format($stats['attempts']) : 0; ?></div>
</div>
<div class="col-auto">
<i class="fas fa-users fa-2x text-gray-300"></i>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Quick Actions -->
<div class="row mb-4">
<div class="col-12">
<div class="card shadow">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Quick Actions</h6>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-3 mb-3 text-center">
<a href="schedules.php" class="btn btn-primary btn-icon-split btn-lg">
<span class="icon text-white-50">
<i class="fas fa-calendar-plus"></i>
</span>
<span class="text">Schedule New Exam</span>
</a>
</div>
<div class="col-md-3 mb-3 text-center">
<a href="banks.php" class="btn btn-success btn-icon-split btn-lg">
<span class="icon text-white-50">
<i class="fas fa-folder-plus"></i>
</span>
<span class="text">Create Question Bank</span>
</a>
</div>
<div class="col-md-3 mb-3 text-center">
<a href="add_question.php" class="btn btn-info btn-icon-split btn-lg">
<span class="icon text-white-50">
<i class="fas fa-plus-circle"></i>
</span>
<span class="text">Add Questions</span>
</a>
</div>
<div class="col-md-3 mb-3 text-center">
<a href="settings.php" class="btn btn-warning btn-icon-split btn-lg">
<span class="icon text-white-50">
<i class="fas fa-cog"></i>
</span>
<span class="text">Exam Settings</span>
</a>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Content Row -->
<div class="row">
<!-- Upcoming Exams -->
<div class="col-lg-6 mb-4">
<div class="card shadow">
<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">Upcoming Exams</h6>
<a href="schedules.php" class="btn btn-sm btn-primary">View All</a>
</div>
<div class="card-body">
<?php if (empty($upcoming_exams)): ?>
<div class="alert alert-info">
No upcoming exams scheduled. <a href="schedules.php" class="alert-link">Schedule an exam now</a>.
</div>
<?php else: ?>
<div class="table-responsive">
<table class="table table-bordered" width="100%" cellspacing="0">
<thead>
<tr>
<th>Exam Title</th>
<th>Course</th>
<th>Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($upcoming_exams as $exam): ?>
<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>
<a href="map_questions.php?exam_id=<?php echo $exam['id']; ?>" class="btn btn-sm btn-info">
<i class="fas fa-question-circle"></i> Questions
</a>
<a href="schedules.php?edit=<?php echo $exam['id']; ?>" class="btn btn-sm btn-primary">
<i class="fas fa-edit"></i> Edit
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</div>
<!-- Recent Exam Attempts -->
<div class="col-lg-6 mb-4">
<div class="card shadow">
<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">Recent Exam Attempts</h6>
</div>
<div class="card-body">
<?php if (empty($recent_attempts)): ?>
<div class="alert alert-info">
No recent exam attempts by students.
</div>
<?php else: ?>
<div class="table-responsive">
<table class="table table-bordered" width="100%" cellspacing="0">
<thead>
<tr>
<th>Student</th>
<th>Exam</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($recent_attempts as $attempt): ?>
<tr>
<td>
<?php echo htmlspecialchars($attempt['first_name'] . ' ' . $attempt['last_name']); ?>
<div class="small text-muted"><?php echo htmlspecialchars($attempt['user_id']); ?></div>
</td>
<td>
<?php echo htmlspecialchars($attempt['exam_title']); ?>
<div class="small text-muted"><?php echo htmlspecialchars($attempt['course_title']); ?></div>
</td>
<td>
<?php
$status_badge_class = '';
switch ($attempt['status']) {
case 'pending':
$status_badge_class = 'secondary';
break;
case 'in_progress':
$status_badge_class = 'info';
break;
case 'completed':
$status_badge_class = 'primary';
break;
case 'graded':
$status_badge_class = 'warning';
break;
case 'passed':
$status_badge_class = 'success';
break;
case 'failed':
$status_badge_class = 'danger';
break;
default:
$status_badge_class = 'secondary';
}
?>
<span class="badge badge-<?php echo $status_badge_class; ?>">
<?php echo ucfirst(str_replace('_', ' ', $attempt['status'])); ?>
</span>
</td>
<td>
<a href="student_answers.php?exam_id=<?php echo $attempt['exam_id']; ?>&student_exam_id=<?php echo $attempt['id']; ?>" class="btn btn-sm btn-info">
<i class="fas fa-eye"></i> View
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
</div>
<?php
// Include footer
include_once 'includes/footer.php';
?>