<?php
// Include header
include_once 'includes/header.php';
// Check if user is an admin
if (!isset($_SESSION['user_id']) || ($_SESSION['role'] !== 'admin' && $_SESSION['role'] !== 'director')) {
header('Location: ../login.php');
exit;
}
$success_message = '';
$error_message = '';
// Check if attendance table exists
$check_table_sql = "SHOW TABLES LIKE 'attendance'";
$result = $conn->query($check_table_sql);
$table_exists = ($result->num_rows > 0);
// Handle table creation if not exists
if (isset($_POST['create_table']) && !$table_exists) {
// Create the attendance table
$create_attendance_table_sql = "
CREATE TABLE `attendance` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`enrollment_id` int(11) NOT NULL,
`course_id` int(11) NOT NULL,
`student_id` int(11) NOT NULL,
`date` date NOT NULL,
`status` enum('present','absent','late','excused') NOT NULL DEFAULT 'present',
`marked_by` int(11) DEFAULT NULL,
`notes` text DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT current_timestamp(),
`updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`id`),
KEY `enrollment_id` (`enrollment_id`),
KEY `course_id` (`course_id`),
KEY `student_id` (`student_id`),
KEY `marked_by` (`marked_by`),
KEY `date` (`date`),
CONSTRAINT `attendance_course_id_fk` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE CASCADE,
CONSTRAINT `attendance_enrollment_id_fk` FOREIGN KEY (`enrollment_id`) REFERENCES `enrollments` (`id`) ON DELETE CASCADE,
CONSTRAINT `attendance_student_id_fk` FOREIGN KEY (`student_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
CONSTRAINT `attendance_marked_by_fk` FOREIGN KEY (`marked_by`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
";
// Execute query
if ($conn->query($create_attendance_table_sql) === TRUE) {
$success_message = "Attendance table created successfully!";
$table_exists = true;
} else {
$error_message = "Error creating attendance table: " . $conn->error;
}
}
// Get attendance statistics if table exists
$attendance_stats = [];
if ($table_exists) {
$stats_query = "
SELECT
COUNT(DISTINCT date) as total_days,
COUNT(*) as total_records,
SUM(CASE WHEN status = 'present' THEN 1 ELSE 0 END) as present_count,
SUM(CASE WHEN status = 'absent' THEN 1 ELSE 0 END) as absent_count,
SUM(CASE WHEN status = 'late' THEN 1 ELSE 0 END) as late_count,
SUM(CASE WHEN status = 'excused' THEN 1 ELSE 0 END) as excused_count
FROM attendance
";
$stats_result = $conn->query($stats_query);
if ($stats_result) {
$attendance_stats = $stats_result->fetch_assoc();
}
}
?>
<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">Attendance System Setup</h1>
</div>
<?php if ($success_message): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<i class="fas fa-check-circle me-2"></i> <?php echo $success_message; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<?php if ($error_message): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<i class="fas fa-exclamation-circle me-2"></i> <?php echo $error_message; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<div class="row">
<div class="col-xl-12 col-lg-12">
<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">Attendance System Status</h6>
</div>
<div class="card-body">
<?php if (!$table_exists): ?>
<div class="alert alert-warning">
<i class="fas fa-exclamation-triangle me-2"></i> The attendance table does not exist in the database. You need to create it to enable attendance tracking.
</div>
<p>The attendance system allows instructors to track student attendance for courses. Once created, the following features will be available:</p>
<ul>
<li>Instructors can mark students as present, absent, late, or excused</li>
<li>Students can view their attendance records and percentage</li>
<li>Administrators can generate attendance reports</li>
</ul>
<form method="post" action="" class="mt-4">
<div class="d-grid gap-2 d-md-flex justify-content-md-start">
<button type="submit" name="create_table" class="btn btn-primary">
<i class="fas fa-table me-2"></i> Create Attendance Table
</button>
</div>
</form>
<?php else: ?>
<div class="alert alert-success">
<i class="fas fa-check-circle me-2"></i> The attendance system is active and ready to use.
</div>
<div class="row">
<div class="col-md-6">
<h5 class="mb-3">Table Structure</h5>
<table class="table table-bordered">
<thead>
<tr>
<th>Column</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>id</td>
<td>int</td>
<td>Primary key</td>
</tr>
<tr>
<td>enrollment_id</td>
<td>int</td>
<td>Foreign key to enrollments table</td>
</tr>
<tr>
<td>course_id</td>
<td>int</td>
<td>Foreign key to courses table</td>
</tr>
<tr>
<td>student_id</td>
<td>int</td>
<td>Foreign key to users table</td>
</tr>
<tr>
<td>date</td>
<td>date</td>
<td>Date of attendance</td>
</tr>
<tr>
<td>status</td>
<td>enum</td>
<td>present, absent, late, excused</td>
</tr>
<tr>
<td>marked_by</td>
<td>int</td>
<td>User ID of instructor who marked attendance</td>
</tr>
<tr>
<td>notes</td>
<td>text</td>
<td>Optional notes about attendance</td>
</tr>
<tr>
<td>created_at</td>
<td>timestamp</td>
<td>When record was created</td>
</tr>
<tr>
<td>updated_at</td>
<td>timestamp</td>
<td>When record was last updated</td>
</tr>
</tbody>
</table>
</div>
<div class="col-md-6">
<h5 class="mb-3">Attendance Statistics</h5>
<?php if (empty($attendance_stats['total_records'])): ?>
<div class="alert alert-info">
<i class="fas fa-info-circle me-2"></i> No attendance records have been created yet.
</div>
<?php else: ?>
<div class="row">
<div class="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">
Total Days</div>
<div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $attendance_stats['total_days']; ?></div>
</div>
<div class="col-auto">
<i class="fas fa-calendar fa-2x text-gray-300"></i>
</div>
</div>
</div>
</div>
</div>
<div class="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 Records</div>
<div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $attendance_stats['total_records']; ?></div>
</div>
<div class="col-auto">
<i class="fas fa-clipboard-list fa-2x text-gray-300"></i>
</div>
</div>
</div>
</div>
</div>
</div>
<h6 class="mt-3 mb-2">Status Distribution</h6>
<div class="mb-2">
<div class="d-flex justify-content-between">
<span>Present</span>
<span class="text-success"><?php echo $attendance_stats['present_count']; ?></span>
</div>
<div class="progress mb-2" style="height: 10px;">
<div class="progress-bar bg-success" role="progressbar"
style="width: <?php echo ($attendance_stats['total_records'] > 0) ? round(($attendance_stats['present_count'] / $attendance_stats['total_records']) * 100) : 0; ?>%"
aria-valuenow="<?php echo $attendance_stats['present_count']; ?>" aria-valuemin="0"
aria-valuemax="<?php echo $attendance_stats['total_records']; ?>"></div>
</div>
</div>
<div class="mb-2">
<div class="d-flex justify-content-between">
<span>Absent</span>
<span class="text-danger"><?php echo $attendance_stats['absent_count']; ?></span>
</div>
<div class="progress mb-2" style="height: 10px;">
<div class="progress-bar bg-danger" role="progressbar"
style="width: <?php echo ($attendance_stats['total_records'] > 0) ? round(($attendance_stats['absent_count'] / $attendance_stats['total_records']) * 100) : 0; ?>%"
aria-valuenow="<?php echo $attendance_stats['absent_count']; ?>" aria-valuemin="0"
aria-valuemax="<?php echo $attendance_stats['total_records']; ?>"></div>
</div>
</div>
<div class="mb-2">
<div class="d-flex justify-content-between">
<span>Late</span>
<span class="text-warning"><?php echo $attendance_stats['late_count']; ?></span>
</div>
<div class="progress mb-2" style="height: 10px;">
<div class="progress-bar bg-warning" role="progressbar"
style="width: <?php echo ($attendance_stats['total_records'] > 0) ? round(($attendance_stats['late_count'] / $attendance_stats['total_records']) * 100) : 0; ?>%"
aria-valuenow="<?php echo $attendance_stats['late_count']; ?>" aria-valuemin="0"
aria-valuemax="<?php echo $attendance_stats['total_records']; ?>"></div>
</div>
</div>
<div class="mb-2">
<div class="d-flex justify-content-between">
<span>Excused</span>
<span class="text-info"><?php echo $attendance_stats['excused_count']; ?></span>
</div>
<div class="progress mb-2" style="height: 10px;">
<div class="progress-bar bg-info" role="progressbar"
style="width: <?php echo ($attendance_stats['total_records'] > 0) ? round(($attendance_stats['excused_count'] / $attendance_stats['total_records']) * 100) : 0; ?>%"
aria-valuenow="<?php echo $attendance_stats['excused_count']; ?>" aria-valuemin="0"
aria-valuemax="<?php echo $attendance_stats['total_records']; ?>"></div>
</div>
</div>
<?php endif; ?>
<div class="mt-4">
<a href="attendance_report.php" class="btn btn-primary">
<i class="fas fa-chart-bar me-2"></i> View Attendance Reports
</a>
</div>
</div>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
</div>
<?php
// Include footer
include_once 'includes/footer.php';
?>