36352-vm/attendance.php
Flatlogic Bot e15fa31a20 t6
2025-11-27 10:42:12 +00:00

217 lines
10 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit();
}
$role_name = $_SESSION['role_name'];
$user_id = $_SESSION['user_id'];
function get_students() {
$pdo = db();
$stmt = $pdo->prepare("SELECT u.id, u.first_name, u.last_name FROM users u JOIN roles r ON u.role_id = r.id WHERE r.role_name = 'student'");
$stmt->execute();
return $stmt->fetchAll();
}
function get_children_for_parent($parent_id) {
$pdo = db();
$stmt = $pdo->prepare("SELECT u.id, u.first_name, u.last_name FROM users u JOIN parent_child pc ON u.id = pc.child_id WHERE pc.parent_id = ?");
$stmt->execute([$parent_id]);
return $stmt->fetchAll();
}
function get_student_attendance($student_id) {
$pdo = db();
$stmt = $pdo->prepare("SELECT attendance_date, status FROM attendance WHERE student_id = ? ORDER BY attendance_date DESC");
$stmt->execute([$student_id]);
return $stmt->fetchAll();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $role_name === 'teacher') {
$attendance_date = $_POST['attendance_date'];
$students = $_POST['students'];
$pdo = db();
$stmt = $pdo->prepare("INSERT INTO attendance (student_id, attendance_date, status) VALUES (?, ?, ?)");
foreach ($students as $student_id => $status) {
$stmt->execute([$student_id, $attendance_date, $status]);
}
$success_message = "Attendance for $attendance_date has been saved.";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Attendance</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<header class="p-3 mb-3 border-bottom sticky-top bg-light">
<div class="container">
<div class="d-flex flex-wrap align-items-center justify-content-center justify-content-lg-start">
<a href="/" class="d-flex align-items-center mb-2 mb-lg-0 text-dark text-decoration-none">
<span class="fs-4">School Management</span>
</a>
<ul class="nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0">
<?php if ($role_name === 'teacher'): ?>
<li><a href="teacher_dashboard.php" class="nav-link px-2 link-dark">Dashboard</a></li>
<li><a href="attendance.php" class="nav-link px-2 link-secondary">Attendance</a></li>
<li><a href="exams.php" class="nav-link px-2 link-dark">Exams</a></li>
<li><a href="activities.php" class="nav-link px-2 link-dark">Activities</a></li>
<?php elseif ($role_name === 'student'): ?>
<li><a href="student_dashboard.php" class="nav-link px-2 link-dark">Dashboard</a></li>
<li><a href="attendance.php" class="nav-link px-2 link-secondary">Attendance</a></li>
<li><a href="exams.php" class="nav-link px-2 link-dark">Exams</a></li>
<li><a href="activities.php" class="nav-link px-2 link-dark">Activities</a></li>
<?php elseif ($role_name === 'parent'): ?>
<li><a href="parent_dashboard.php" class="nav-link px-2 link-dark">Dashboard</a></li>
<li><a href="attendance.php" class="nav-link px-2 link-secondary">Attendance</a></li>
<li><a href="exams.php" class="nav-link px-2 link-dark">Exams</a></li>
<li><a href="activities.php" class="nav-link px-2 link-dark">Activities</a></li>
<?php endif; ?>
</ul>
<div class="text-end">
<a href="logout.php" class="btn btn-outline-primary">Logout</a>
</div>
</div>
</div>
</header>
<main class="container py-5">
<h1>Attendance</h1>
<?php if (isset($success_message)): ?>
<div class="alert alert-success"><?php echo $success_message; ?></div>
<?php endif; ?>
<?php if ($role_name === 'teacher'): ?>
<h2>Take Attendance</h2>
<form method="POST">
<div class="mb-3">
<label for="attendance_date" class="form-label">Date</label>
<input type="date" class="form-control" id="attendance_date" name="attendance_date" value="<?php echo date('Y-m-d'); ?>" required>
</div>
<table class="table">
<thead>
<tr>
<th>Student Name</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
$students = get_students();
foreach ($students as $student):
?>
<tr>
<td><?php echo htmlspecialchars($student['first_name'] . ' ' . $student['last_name']); ?></td>
<td>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="students[<?php echo $student['id']; ?>]" id="present_<?php echo $student['id']; ?>" value="present" checked>
<label class="form-check-label" for="present_<?php echo $student['id']; ?>">Present</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="students[<?php echo $student['id']; ?>]" id="absent_<?php echo $student['id']; ?>" value="absent">
<label class="form-check-label" for="absent_<?php echo $student['id']; ?>">Absent</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="students[<?php echo $student['id']; ?>]" id="late_<?php echo $student['id']; ?>" value="late">
<label class="form-check-label" for="late_<?php echo $student['id']; ?>">Late</label>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<button type="submit" class="btn btn-primary">Submit Attendance</button>
</form>
<?php elseif ($role_name === 'student'): ?>
<h2>My Attendance</h2>
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
$attendance_records = get_student_attendance($user_id);
foreach ($attendance_records as $record):
?>
<tr>
<td><?php echo htmlspecialchars($record['attendance_date']); ?></td>
<td><?php echo htmlspecialchars($record['status']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php elseif ($role_name === 'parent'): ?>
<h2>My Child's Attendance</h2>
<?php
$children = get_children_for_parent($user_id);
if (count($children) > 0):
?>
<form method="GET" class="mb-3">
<div class="row">
<div class="col-md-4">
<label for="child_id" class="form-label">Select Child</label>
<select class="form-select" id="child_id" name="child_id" onchange="this.form.submit()">
<option value="">Select a child</option>
<?php foreach ($children as $child): ?>
<option value="<?php echo $child['id']; ?>" <?php echo (isset($_GET['child_id']) && $_GET['child_id'] == $child['id']) ? 'selected' : ''; ?>><?php echo htmlspecialchars($child['first_name'] . ' ' . $child['last_name']); ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
</form>
<?php if (isset($_GET['child_id'])):
$child_id = $_GET['child_id'];
// Make sure the selected child belongs to the parent
$is_child_of_parent = false;
foreach ($children as $child) {
if ($child['id'] == $child_id) {
$is_child_of_parent = true;
break;
}
}
if ($is_child_of_parent):
?>
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
$attendance_records = get_student_attendance($child_id);
foreach ($attendance_records as $record):
?>
<tr>
<td><?php echo htmlspecialchars($record['attendance_date']); ?></td>
<td><?php echo htmlspecialchars($record['status']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<div class="alert alert-danger">Invalid child selected.</div>
<?php endif; ?>
<?php endif; ?>
<?php else: ?>
<p>You have no children linked to your account.</p>
<?php endif; ?>
<?php endif; ?>
</main>
</body>
</html>