36352-vm/attendance.php
Flatlogic Bot a2059511fc t4
2025-11-27 10:08:20 +00:00

118 lines
5.0 KiB
PHP

<?php
session_start();
//if (!isset($_SESSION['user_id']) || !in_array($_SESSION['role'], ['teacher', 'admin'])) {
// header("Location: login.php");
// exit();
//}
require_once 'db/config.php';
try {
$pdoconn = db();
// Create attendance table
$pdoconn->exec("CREATE TABLE IF NOT EXISTS attendance (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
login_time DATETIME NOT NULL,
logout_time DATETIME DEFAULT NULL,
ip_address VARCHAR(45),
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");
// Fetch online users
$online_users_stmt = $pdoconn->prepare("SELECT u.username FROM attendance a JOIN users u ON a.user_id = u.id WHERE a.logout_time IS NULL");
$online_users_stmt->execute();
$online_users = $online_users_stmt->fetchAll(PDO::FETCH_ASSOC);
// Fetch attendance history
$history_stmt = $pdoconn->prepare("SELECT u.username, a.login_time, a.logout_time, a.ip_address FROM attendance a JOIN users u ON a.user_id = u.id ORDER BY a.login_time DESC");
$history_stmt->execute();
$history = $history_stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Could not connect to the database :" . $e->getMessage());
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Attendance</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body>
<div class="container">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="index.php">School Admin</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item"><a class="nav-link" href="index.php">Home</a></li>
<li class="nav-item"><a class="nav-link" href="users.php">Users</a></li>
<li class="nav-item"><a class="nav-link" href="roles.php">Roles</a></li>
<li class="nav-item"><a class="nav-link" href="activities.php">Activities</a></li>
<li class="nav-item"><a class="nav-link" href="exams.php">Exams</a></li>
<li class="nav-item active"><a class="nav-link" href="attendance.php">Attendance</a></li>
</ul>
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a href="logout.php" class="btn btn-danger">Logout</a>
</li>
</ul>
</div>
</nav>
<main class="py-5">
<h1>Attendance</h1>
<div class="card my-4">
<div class="card-header">
Currently Online
</div>
<div class="card-body">
<?php if (count($online_users) > 0): ?>
<ul class="list-group">
<?php foreach ($online_users as $user): ?>
<li class="list-group-item"><?php echo htmlspecialchars($user['username']); ?></li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>No users are currently online.</p>
<?php endif; ?>
</div>
</div>
<div class="card">
<div class="card-header">
Attendance History
</div>
<div class="card-body">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Username</th>
<th>Login Time</th>
<th>Logout Time</th>
<th>IP Address</th>
</tr>
</thead>
<tbody>
<?php foreach ($history as $record): ?>
<tr>
<td><?php echo htmlspecialchars($record['username']); ?></td>
<td><?php echo $record['login_time']; ?></td>
<td><?php echo $record['logout_time'] ?? '<i>Still logged in</i>'; ?></td>
<td><?php echo htmlspecialchars($record['ip_address']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</main>
</div>
</body>
</html>