37970-vm/teacher.php
2026-01-30 14:32:19 +00:00

179 lines
7.6 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
session_start();
// Auth Check
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'Teacher') {
header('Location: login.php');
exit;
}
$db = db();
$school_id = $_SESSION['school_id'];
$date = date('Y-m-d');
$message = '';
$pageTitle = 'Teacher Dashboard | SOMS';
// Handle Attendance Submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['attendance'])) {
try {
$db->beginTransaction();
$stmt = $db->prepare("INSERT INTO attendance (learner_id, status, date, school_id)
VALUES (:learner_id, :status, :date, :school_id)
ON DUPLICATE KEY UPDATE status = VALUES(status)");
foreach ($_POST['attendance'] as $learner_id => $status) {
$stmt->execute([
'learner_id' => $learner_id,
'status' => $status,
'date' => $date,
'school_id' => $school_id
]);
}
$db->commit();
$message = "Attendance saved successfully for $date.";
} catch (Exception $e) {
$db->rollBack();
$message = "Error: " . $e->getMessage();
}
}
// Fetch Learners for this specific school and their current attendance for today
$query = "SELECT l.*, a.status as today_status
FROM learners l
LEFT JOIN attendance a ON l.id = a.learner_id AND a.date = :date
WHERE l.school_id = :school_id
ORDER BY l.full_name ASC";
$stmt = $db->prepare($query);
$stmt->execute(['date' => $date, 'school_id' => $school_id]);
$learners = $stmt->fetchAll();
// Stats
$total_learners = count($learners);
$present_count = 0;
foreach ($learners as $l) {
if ($l['today_status'] === 'present') $present_count++;
}
$presence_rate = $total_learners > 0 ? round(($present_count / $total_learners) * 100) : 0;
include 'includes/header.php';
?>
<div class="container pb-5">
<div class="row mb-4">
<div class="col-12">
<h2 class="h4 mb-1">Teacher Dashboard</h2>
<p class="text-muted small">Daily attendance for <strong><?= date('l, d F Y') ?></strong></p>
</div>
</div>
<?php if ($message): ?>
<div class="alert alert-info alert-dismissible fade show" role="alert">
<?= htmlspecialchars($message) ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<!-- Stats Row -->
<div class="row g-3 mb-4">
<div class="col-md-4">
<div class="card p-3 stats-card shadow-sm">
<p class="text-muted mb-1">Total Learners</p>
<h3 class="mb-0"><?= $total_learners ?></h3>
</div>
</div>
<div class="col-md-4">
<div class="card p-3 stats-card shadow-sm" style="border-left: 4px solid #2E7D32;">
<p class="text-muted mb-1">Presence Rate</p>
<h3 class="mb-0"><?= $presence_rate ?>%</h3>
</div>
</div>
<div class="col-md-4">
<div class="card p-3 stats-card shadow-sm" style="border-left: 4px solid var(--secondary-color);">
<p class="text-muted mb-1">Active Class</p>
<h3 class="mb-0">Grade <?= $total_learners > 0 ? htmlspecialchars($learners[0]['grade']) : 'N/A' ?></h3>
</div>
</div>
</div>
<!-- Attendance Table -->
<div class="card shadow-sm border-0">
<div class="card-header bg-white py-3 d-flex justify-content-between align-items-center">
<h5 class="mb-0 fw-bold">Class Register</h5>
<div class="input-group input-group-sm w-auto">
<span class="input-group-text bg-white border-end-0"><i class="bi bi-search"></i></span>
<input type="text" id="learnerSearch" class="form-control border-start-0" placeholder="Search learner...">
</div>
</div>
<div class="card-body p-0">
<form method="POST">
<div class="table-responsive">
<table class="table table-hover mb-0" id="learnersTable">
<thead class="bg-light">
<tr>
<th class="ps-4">Learner Name</th>
<th>Grade</th>
<th>Student ID</th>
<th class="text-end pe-4">Status</th>
</tr>
</thead>
<tbody>
<?php if (empty($learners)): ?>
<tr>
<td colspan="4" class="text-center py-4 text-muted">No learners found for your school. Please contact your Admin.</td>
</tr>
<?php endif; ?>
<?php foreach ($learners as $learner): ?>
<tr>
<td class="ps-4">
<strong><?= htmlspecialchars($learner['full_name']) ?></strong>
</td>
<td><?= htmlspecialchars($learner['grade']) ?></td>
<td><span class="badge bg-light text-dark border"><?= htmlspecialchars($learner['student_id']) ?></span></td>
<td class="text-end pe-4">
<div class="btn-group btn-group-sm" role="group">
<input type="radio" class="btn-check" name="attendance[<?= $learner['id'] ?>]"
id="pres_<?= $learner['id'] ?>" value="present"
<?= $learner['today_status'] === 'present' ? 'checked' : '' ?> required>
<label class="btn btn-outline-success" for="pres_<?= $learner['id'] ?>">Present</label>
<input type="radio" class="btn-check" name="attendance[<?= $learner['id'] ?>]"
id="abs_<?= $learner['id'] ?>" value="absent"
<?= $learner['today_status'] === 'absent' ? 'checked' : '' ?>>
<label class="btn btn-outline-danger" for="abs_<?= $learner['id'] ?>">Absent</label>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<div class="p-3 text-end border-top">
<button type="submit" class="btn btn-primary px-4 fw-bold">
<i class="bi bi-cloud-upload me-2"></i> Save & Sync Register
</button>
</div>
</form>
</div>
</div>
</div>
<script>
// Simple Search Filter
document.getElementById('learnerSearch').addEventListener('keyup', function() {
let filter = this.value.toUpperCase();
let rows = document.querySelector("#learnersTable tbody").rows;
for (let i = 0; i < rows.length; i++) {
if (rows[i].cells.length < 3) continue;
let nameCol = rows[i].cells[0].textContent.toUpperCase();
let idCol = rows[i].cells[2].textContent.toUpperCase();
if (nameCol.indexOf(filter) > -1 || idCol.indexOf(filter) > -1) {
rows[i].style.display = "";
} else {
rows[i].style.display = "none";
}
}
});
</script>
<?php include 'includes/footer.php'; ?>