39037-vm/attendance_detail.php
Flatlogic Bot 463712fc0c SMS
2026-03-07 09:22:35 +00:00

72 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/db_init.php';
$db = db();
$class_id = (int)$_GET['class_id'];
$date = $_GET['date'] ?? date('Y-m-d');
$role = $_GET['role'] ?? 'admin';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$attendance = $_POST['status'] ?? [];
foreach ($attendance as $student_id => $status) {
$stmt = $db->prepare("INSERT INTO attendance (student_id, class_id, status, attendance_date) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE status = ?");
$stmt->execute([(int)$student_id, $class_id, $status, $date, $status]);
}
$message = "Absensi berhasil disimpan untuk tanggal $date.";
}
$class = $db->prepare("SELECT * FROM classes WHERE id = ?");
$class->execute([$class_id]);
$classData = $class->fetch();
$students = $db->prepare("SELECT s.id, s.full_name, a.status FROM students s LEFT JOIN attendance a ON s.id = a.student_id AND a.attendance_date = ? WHERE s.class_id = ?");
$students->execute([$date, $class_id]);
$studentList = $students->fetchAll();
?>
<!doctype html>
<html lang="id">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Absensi — <?= htmlspecialchars($classData['name'] ?? 'Kelas') ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?= time(); ?>">
</head>
<body>
<div class="container py-4">
<nav class="mb-4"><a href="attendance.php?role=<?= htmlspecialchars($role) ?>">← Kembali ke Daftar Kelas</a></nav>
<h1 class="h3 mb-4">Absensi Kelas: <?= htmlspecialchars($classData['name'] ?? 'Unknown') ?></h1>
<?php if (isset($message)): ?><div class="alert alert-success"><?= $message ?></div><?php endif; ?>
<form method="POST">
<table class="table table-bordered">
<thead>
<tr>
<th>Nama Siswa</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php foreach ($studentList as $student): ?>
<tr>
<td><?= htmlspecialchars($student['full_name']) ?></td>
<td>
<select name="status[<?= (int)$student['id'] ?>]" class="form-select form-select-sm">
<option value="hadir" <?= ($student['status'] === 'hadir') ? 'selected' : '' ?>>Hadir</option>
<option value="sakit" <?= ($student['status'] === 'sakit') ? 'selected' : '' ?>>Sakit</option>
<option value="izin" <?= ($student['status'] === 'izin') ? 'selected' : '' ?>>Izin</option>
<option value="alpa" <?= ($student['status'] === 'alpa' || !$student['status']) ? 'selected' : '' ?>>Alpa</option>
</select>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<button type="submit" class="btn btn-primary">Simpan Absensi</button>
</form>
</div>
</body>
</html>