File: /var/www/w230/html/attendance/dashboard.php
<?php
session_start();
require 'db.php';
// ตรวจสอบสิทธิ์
/*if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
header("Location: dashboard.php");
exit();
}*/
if (!isset($_SESSION["teacher_id"])) {
header("Location: login.php");
exit;
}
$teacher_name = $_SESSION["teacher_name"];
$teacher_id = $_SESSION["teacher_id"];
$full_name = $_SESSION["full_name"];
$role = $_SESSION["role"];
//echo"$teacher_name .$teacher_id .$full_name .$role";
$selected_class = $_GET['class'] ?? '';
$students = [];
if ($selected_class) {
$stmt = $conn->prepare("SELECT * FROM students WHERE class_level = ? ORDER BY fullname");
$stmt->bind_param("s", $selected_class);
$stmt->execute();
$students = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
}
?>
<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8">
<title>แดชบอร์ดครูเวร</title>
<link href="https://fonts.googleapis.com/css2?family=Sarabun&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
* { box-sizing: border-box; font-family: 'Sarabun', sans-serif; }
body {
margin: 0;
background: #f4f6f9;
}
header {
background: #4facfe;
color: white;
padding: 20px;
display: flex;
justify-content: space-between;
align-items: center;
}
header h2 {
margin: 0;
font-weight: 600;
font-size: 1.2rem;
display: flex;
gap: 10px;
align-items: center; /* ให้ข้อความอยู่กึ่งกลาง */
}
header img {
height: 80px;
width: auto;
border-radius: 8px;
align-self: flex-start; /* ทำให้โลโก้ชิดบน */
}
nav a {
margin-left: 20px;
color: white;
text-decoration: none;
font-weight: bold;
}
.container {
padding: 30px;
}
.card {
background: white;
padding: 25px;
border-radius: 12px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
margin-bottom: 30px;
}
select, input[type="submit"] {
padding: 10px;
font-size: 16px;
border-radius: 8px;
border: 1px solid #ccc;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
padding: 10px;
border-bottom: 1px solid #eee;
text-align: center;
}
input[type="radio"] {
transform: scale(1.2);
}
.btn-submit {
background: #4facfe;
color: white;
border: none;
margin-top: 20px;
cursor: pointer;
}
.btn-submit:hover {
background: #00c6ff;
}
@media (max-width: 600px) {
table th, table td {
font-size: 14px;
}
}
.status-btn {
width: 40px;
height: 40px;
border-radius: 50%;
border: none;
margin: 2px;
cursor: pointer;
transition: 0.2s;
font-weight: bold;
color: white;
font-size: 14px;
background-color: #bdc3c7; /* สีเริ่มต้น */
}
.status-btn.active[data-status="มา"] {
background-color: #2ecc71;
}
.status-btn.active[data-status="สาย"] {
background-color: #f1c40f;
}
.status-btn.active[data-status="ขาด"] {
background-color: #e74c3c;
}
.status-btn.active[data-status="ลา"] {
background-color: #9b59b6;
}
.status-btn.active[data-status="ป่วย"] {
background-color: #3498db;
}
.btn-mark-all {
padding: 8px 16px;
background: #00b894;
color: white;
border: none;
border-radius: 12px;
cursor: pointer;
font-size: 14px;
}
.btn-submit {
padding: 10px 20px;
background: #0984e3;
color: white;
border: none;
border-radius: 8px;
font-size: 16px;
margin-top: 20px;
cursor: pointer;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 15px;
}
th, td {
padding: 8px;
border-bottom: 1px solid #ccc;
text-align: center;
}
th {
background: #ecf0f1;
}
</style>
</head>
<body>
<header>
<h2><img src="logo.png">ระบบเช็กชื่อนักเรียน - คุณครู <?= htmlspecialchars($full_name) ?>
</h2>
<nav>
<?php
if ($role == 'admin') {
echo '<a href="manage_teachers.php"><i class="fas fa-user-cog me-1 fa-2x"></i>เพิ่ม/จัดการผู้ใช้</a>';
echo '<a href="telegram_config.php"><i class="fa-brands fa-telegram me-1 fa-2x"></i>ตั้งค่าเชื่อมต่อ Telegram</a>';
}
?>
<a href="dashboard.php"><i class="fa fa-id-badge me-1 fa-2x" aria-hidden="true"></i></i> บันทึกสถิติมาเรียน</a>
<a href="attendance_report.php"><i class="fa fa-bar-chart me-1 fa-2x" aria-hidden="true"></i> รายงานสถิติมาเรียน</a>
<a href="logout.php"><i class="fa fa-sign-out fa-2x" aria-hidden="true"></i> ออกจากระบบ</a>
</nav>
</header>
<div class="container">
<div class="card">
<form method="get">
<label>เลือกชั้นเรียน:</label>
<select name="class" onchange="this.form.submit()">
<option value="">-- เลือกชั้น --</option>
<?php
$res = $conn->query("SELECT DISTINCT class_level FROM students ORDER BY class_level");
while ($row = $res->fetch_assoc()) {
$selected = ($row['class_level'] == $selected_class) ? 'selected' : '';
echo "<option value='{$row['class_level']}' $selected>{$row['class_level']}</option>";
}
?>
</select>
</form>
</div>
<?php if (isset($_SESSION['attendance_success'])):
?>
<div id="alertBox" style="background-color:#fef5e6; padding:16px 20px; color:#e67e22; margin:20px auto; width:90%; max-width:600px; border-radius:8px; position:relative; box-shadow:0 2px 8px rgba(0,0,0,0.05);">
<span style="font-size:20px; margin-right:10px;">ℹ️</span>
<?= $_SESSION['attendance_success'] ?>
<span onclick="document.getElementById('alertBox').style.display='none'" style="position:absolute; right:12px; top:10px; cursor:pointer; font-weight:bold;">×</span>
</div>
<?php unset($_SESSION['attendance_success']); endif; ?>
<?php if ($students): ?>
<div class="card">
<form method="post" action="save_attendance.php">
<input type="hidden" name="class_level" value="<?= htmlspecialchars($selected_class) ?>">
<input type="hidden" name="teacher_id" value="<?=$_SESSION["teacher_id"]?>">
<div style="text-align:right; margin-bottom:10px;">
<button type="button" class="btn-mark-all" onclick="markAll('มา')">✅ มาทั้งหมด</button>
<button type="button" class="btn-mark-all" onclick="clearAll()">🧹 ล้างทั้งหมด</button>
<input type="submit" class="btn-submit" value="✅ บันทึกการเข้าแถว">
</div>
<table>
<tr>
<th>รหัส</th>
<th>ชื่อ-สกุล</th>
<th colspan="5">สถานะ</th>
</tr>
<?php foreach ($students as $student): ?>
<tr>
<td><?= htmlspecialchars($student['student_id']) ?></td>
<td><?= htmlspecialchars($student['fullname']) ?></td>
<td>
<button type="button" class="status-btn" onclick="selectStatus(this, 'มา')" data-id="<?= $student['id'] ?>">มา</button>
</td>
<td>
<button type="button" class="status-btn" onclick="selectStatus(this, 'ลา')" data-id="<?= $student['id'] ?>">ลา</button>
</td>
<td>
<button type="button" class="status-btn" onclick="selectStatus(this, 'ขาด')" data-id="<?= $student['id'] ?>">ขาด</button>
</td>
<td>
<button type="button" class="status-btn" onclick="selectStatus(this, 'สาย')" data-id="<?= $student['id'] ?>">สาย</button>
</td>
<td>
<button type="button" class="status-btn" onclick="selectStatus(this, 'ป่วย')" data-id="<?= $student['id'] ?>">ป่วย</button>
</td>
</tr>
<input type="hidden" name="status[<?= $student['id'] ?>]" id="status_<?= $student['id'] ?>">
<?php endforeach; ?>
</table>
<input type="submit" class="btn-submit" value="✅ บันทึกการเข้าแถว">
</form>
</div>
<?php endif; ?>
</div>
</body>
</html>
<script>
function selectStatus(btn, status) {
const studentId = btn.getAttribute("data-id");
// ลบสถานะปุ่มอื่นในแถวเดียวกัน
const row = btn.parentNode.parentNode;
const buttons = row.querySelectorAll(".status-btn");
buttons.forEach(b => {
b.classList.remove("active");
b.removeAttribute("data-status");
});
// เพิ่มสถานะปุ่มที่เลือก
btn.classList.add("active");
btn.setAttribute("data-status", status);
// กำหนดค่า hidden input
document.getElementById("status_" + studentId).value = status;
}
function markAll(status) {
document.querySelectorAll("input[id^='status_']").forEach(input => {
const studentId = input.id.replace("status_", "");
const btn = document.querySelector(`button[data-id='${studentId}'][onclick*="'${status}'"]`);
selectStatus(btn, status);
});
}
function clearAll() {
document.querySelectorAll("input[id^='status_']").forEach(input => {
const studentId = input.id.replace("status_", "");
// เคลียร์ค่าใน hidden input
input.value = "";
// ลบคลาส active และ data-status จากปุ่มทั้งหมด
const row = document.querySelector(`button[data-id='${studentId}']`).parentNode.parentNode;
const buttons = row.querySelectorAll(".status-btn");
buttons.forEach(b => {
b.classList.remove("active");
b.removeAttribute("data-status");
});
});
}
</script>