File: //var/www/w230/html/attendance/manage_teachers.php
<?php
session_start();
include 'db.php';
$full_name = $_SESSION["full_name"];
$role = $_SESSION["role"];
//echo '<pre>';
//print_r($_POST);
//echo '<hr>';
//print_r($_FILES);
//exit;
// ตรวจสอบสิทธิ์
/*if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
header("Location: dashboard.php");
exit();
}*/
// Handle Add/Edit Teacher Form Submit
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$full_name = trim($_POST['full_name']);
$roles = trim($_POST['roles']);
if ($id > 0) {
// Edit
if ($password != '') {
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$stmt = $conn->prepare("UPDATE teachers SET username=?, password=?, full_name=?, role=? WHERE id=?");
$stmt->bind_param("ssssi", $username, $hashed_password, $full_name,$roles, $id);
} else {
// ไม่เปลี่ยนรหัสผ่าน
$stmt = $conn->prepare("UPDATE teachers SET username=?, full_name=?, role=? WHERE id=?");
$stmt->bind_param("sssi", $username, $full_name, $roles, $id);
}
$stmt->execute();
$stmt->close();
$_SESSION['message'] = "แก้ไขข้อมูลเรียบร้อยแล้ว";
} else {
// Add
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$stmt = $conn->prepare("INSERT INTO teachers (username, password, full_name, role) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $username, $hashed_password, $full_name,$roles);
$stmt->execute();
$stmt->close();
$_SESSION['message'] = "เพิ่มข้อมูลเรียบร้อยแล้ว";
}
header('Location: manage_teachers.php');
exit();
}
// Handle Delete
if (isset($_GET['delete'])) {
$id = intval($_GET['delete']);
$stmt = $conn->prepare("DELETE FROM teachers WHERE id=?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->close();
$_SESSION['message'] = "ลบข้อมูลเรียบร้อยแล้ว";
header('Location: manage_teachers.php');
exit();
}
// Fetch all teachers for listing
$result = $conn->query("SELECT id, username, full_name,role FROM teachers ORDER BY id DESC");
// Fetch teacher to edit (if edit_id present)
$edit_teacher = null;
if (isset($_GET['edit'])) {
$edit_id = intval($_GET['edit']);
$stmt = $conn->prepare("SELECT id, username, full_name, role FROM teachers WHERE id=?");
$stmt->bind_param("i", $edit_id);
$stmt->execute();
$res = $stmt->get_result();
$edit_teacher = $res->fetch_assoc();
$stmt->close();
}
?>
<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8" />
<title>จัดการครูเวร</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- Bootstrap 5 CSS CDN -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" href="plugins/fontawesome-free/css/all.min.css">
<link rel="stylesheet" href="plugins/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<link href="https://fonts.googleapis.com/css2?family=Sarabun:wght@400;600;700&display=swap" rel="stylesheet">
<style>
body {
min-height: 100vh;
background: linear-gradient(135deg,rgb(254, 255, 254),rgb(255, 255, 255));
color: #333;
font-family: 'Sarabun', sans-serif;
}
.container {
max-width: 900px;
margin-top: 0px; /* เดิม: 40px */
margin-bottom: 40px;
background: #fff;
border-radius: 15px;
box-shadow: 0 8px 24px rgba(0,0,0,0.2);
padding: 30px;
}
h2 {
font-weight: 700;
margin-bottom: 25px;
color:rgb(0, 60, 165);
text-align: center;
}
.form-label {
font-weight: 600;
color: #444;
}
.btn-primary {
background-color: #2575fc;
border-color: #2575fc;
}
.btn-primary:hover {
background-color: #1a52d1;
border-color: #1a52d1;
}
table thead {
background-color: #2575fc;
color: #fff;
}
table tbody tr:hover {
background-color: #f1f9ff;
}
.message {
margin-bottom: 20px;
}
.btn-cancel {
background-color: #6c757d;
border-color: #6c757d;
}
.btn-cancel:hover {
background-color: #5a6268;
border-color: #545b62;
}
.logout-btn {
background-color:rgb(255, 255, 255);
border-color:rgb(255, 255, 255);
position: absolute;
top: 20px;
right: 30px;
}
@media (max-width: 576px) {
.logout-btn {
top: 10px;
right: 15px;
font-size: 14px;
}
.form-container {
margin-top: 100px;
padding: 15px;
}
}
header
{
background: #4facfe;
color: white;
padding: 20px;
display: flex;
justify-content: space-between;
align-items: center;
font-family: 'Sarabun', sans-serif;
}
header h2 {
margin: 0;
font-weight: normal;
}
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;
}
</style>
</head>
<body>
<header>
<h4>ระบบเช็กชื่อนักเรียน - คุณครู <?= htmlspecialchars($full_name) ?></h4>
<nav>
<?php
if ($role == 'admin') {
echo '<a href="manage_teachers.php"><i class="fas fa-user-cog fa-2x"></i>เพิ่ม/จัดการผู้ใช้</a>';
echo '<a href="telegram_config.php"><i class="fa-brands fa-telegram fa-2x"></i>ตั้งค่าเชื่อมต่อ Telegram</a>';
}
?>
<a href="dashboard.php"><i class="fa fa-id-badge fa-2x" aria-hidden="true"></i></i>บันทึกสถิติมาเรียน</a>
<a href="attendance_report.php"><i class="fa fa-bar-chart 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">
<h2>จัดการครูเวร</h2>
<?php if (isset($_SESSION['message'])): ?>
<div class="alert alert-success alert-dismissible fade show message" role="alert">
<?=htmlspecialchars($_SESSION['message'])?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php unset($_SESSION['message']); ?>
<?php endif; ?>
<form method="POST" action="manage_teachers.php" class="mb-4">
<input type="hidden" name="id" value="<?= $edit_teacher ? $edit_teacher['id'] : '' ?>" />
<div class="mb-3">
<label for="username" class="form-label">Username:</label>
<input type="text" id="username" name="username" class="form-control" required
value="<?= $edit_teacher ? htmlspecialchars($edit_teacher['username']) : '' ?>" />
</div>
<div class="mb-3">
<label for="password" class="form-label">Password: <?= $edit_teacher ? '<small class="text-muted">(กรอกเมื่อเปลี่ยนรหัสผ่าน)</small>' : '' ?></label>
<input type="password" id="password" name="password" class="form-control" <?= $edit_teacher ? '' : 'required' ?> />
</div>
<div class="mb-3">
<label for="full_name" class="form-label">ชื่อ-สกุล:</label>
<input type="text" id="full_name" name="full_name" class="form-control" required
value="<?= $edit_teacher ? htmlspecialchars($edit_teacher['full_name']) : '' ?>" />
</div>
<div class="mb-3">
<label for="roles" class="form-label">กำหนดสิทธิ์:</label>
<select class="form-control" name="roles" id="roles"required>
<option value="">-- กำหนดสิทธิ์ --</option>
<option value="teacher">ครูเวร(teacher)</option>
<option value="admin">ผู้ดูแลระบบ(admin)</option>
</select>
</div>
<button type="submit" class="btn btn-primary">
<?= $edit_teacher ? 'บันทึกการแก้ไข' : 'เพิ่มครูเวร' ?>
</button>
<?php if ($edit_teacher): ?>
<a href="manage_teachers.php" class="btn btn-cancel ms-2">ยกเลิก</a>
<?php endif; ?>
</form>
<table class="table table-striped table-hover align-middle">
<thead>
<tr>
<th scope="col">ลำดับ</th>
<th scope="col">Username</th>
<th scope="col">ชื่อ-สกุล</th>
<th scope="col">สิทธิ์</th>
<th scope="col" style="width: 140px;">จัดการ</th>
</tr>
</thead>
<tbody>
<?php $i = 1; while ($row = $result->fetch_assoc()): ?>
<tr>
<th scope="row"><?= $i++ ?></th>
<td><?= htmlspecialchars($row['username']) ?></td>
<td><?= htmlspecialchars($row['full_name']) ?></td>
<td><?= htmlspecialchars($row['role']) ?></td>
<td>
<a href="manage_teachers.php?edit=<?= $row['id'] ?>" class="btn btn-sm btn-primary">แก้ไข</a>
<a href="manage_teachers.php?delete=<?= $row['id'] ?>" class="btn btn-sm btn-danger"
onclick="return confirm('ยืนยันการลบครูเวรนี้?')">ลบ</a>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
<!-- Bootstrap 5 JS Bundle -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>