This commit is contained in:
Flatlogic Bot 2025-11-27 10:08:20 +00:00
parent acd454b06b
commit a2059511fc
8 changed files with 359 additions and 0 deletions

110
activities.php Normal file
View File

@ -0,0 +1,110 @@
<?php
session_start();
if (!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true) {
header("location: login.php");
exit;
}
require_once 'db/config.php';
try {
$pdo = db();
$sql = "CREATE TABLE IF NOT EXISTS activities (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
action VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
)";
$pdo->exec($sql);
} catch (PDOException $e) {
die("ERROR: Could not connect. " . $e->getMessage());
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Activities</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">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 active">
<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">
<a class="nav-link" href="attendance.php">Attendance</a>
</li>
</ul>
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<?php if (isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true): ?>
<a href="logout.php" class="btn btn-danger">Logout</a>
<?php else: ?>
<a href="login.php" class="btn btn-primary">Login</a>
<?php endif; ?>
</li>
</ul>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h1>Activities</h1>
</div>
<p>Activity log:</p>
<?php
$stmt = $pdo->query("SELECT a.id, u.username, a.action, a.created_at FROM activities a JOIN users u ON a.user_id = u.id ORDER BY a.created_at DESC");
$activities = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>User</th>
<th>Action</th>
<th>Timestamp</th>
</tr>
</thead>
<tbody>
<?php foreach ($activities as $activity): ?>
<tr>
<td><?php echo $activity["id"]; ?></td>
<td><?php echo htmlspecialchars($activity["username"]); ?></td>
<td><?php echo htmlspecialchars($activity["action"]); ?></td>
<td><?php echo $activity["created_at"]; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>

118
attendance.php Normal file
View File

@ -0,0 +1,118 @@
<?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>

61
exams.php Normal file
View File

@ -0,0 +1,61 @@
<?php
require_once 'db/config.php';
try {
$db = db();
// Create exams table
$db->exec("CREATE TABLE IF NOT EXISTS exams (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT,
teacher_id INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (teacher_id) REFERENCES users(id) ON DELETE SET NULL
)");
// Create exam_questions table
$db->exec("CREATE TABLE IF NOT EXISTS exam_questions (
id INT AUTO_INCREMENT PRIMARY KEY,
exam_id INT,
question TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (exam_id) REFERENCES exams(id) ON DELETE CASCADE
)");
// Create student_exams table
$db->exec("CREATE TABLE IF NOT EXISTS student_exams (
id INT AUTO_INCREMENT PRIMARY KEY,
student_id INT,
exam_id INT,
score INT,
completed_at TIMESTAMP,
FOREIGN KEY (student_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (exam_id) REFERENCES exams(id) ON DELETE CASCADE
)");
} catch (PDOException $e) {
die("Error: " . $e->getMessage());
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Exams</title>
</head>
<body>
<h1>Exams</h1>
<nav>
<a href="index.php">Home</a>
<a href="users.php">Users</a>
<a href="roles.php">Roles</a>
<a href="activities.php">Activities</a>
<a href="exams.php">Exams</a>
<a href="attendance.php">Attendance</a>
</nav>
<p>Exam management page.</p>
</body>
</html>

View File

@ -39,6 +39,9 @@
<li><a href="index.php" class="nav-link px-2 link-secondary">خانه</a></li> <li><a href="index.php" class="nav-link px-2 link-secondary">خانه</a></li>
<li><a href="roles.php" class="nav-link px-2 link-dark">مدیریت نقش‌ها</a></li> <li><a href="roles.php" class="nav-link px-2 link-dark">مدیریت نقش‌ها</a></li>
<li><a href="users.php" class="nav-link px-2 link-dark">مدیریت کاربران</a></li> <li><a href="users.php" class="nav-link px-2 link-dark">مدیریت کاربران</a></li>
<a href="activities.php">Activities</a>
<a href="exams.php">Exams</a>
<a href="attendance.php">Attendance</a>
</ul> </ul>
<div class="text-end"> <div class="text-end">

View File

@ -28,6 +28,17 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$_SESSION['user_id'] = $user['id']; $_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $username; $_SESSION['username'] = $username;
$_SESSION['role_id'] = $user['role_id']; $_SESSION['role_id'] = $user['role_id'];
// Log attendance
$login_time = date('Y-m-d H:i:s');
$ip_address = $_SERVER['REMOTE_ADDR'];
$attendance_stmt = $db->prepare("INSERT INTO attendance (user_id, login_time, ip_address) VALUES (:user_id, :login_time, :ip_address)");
$attendance_stmt->bindParam(':user_id', $user['id']);
$attendance_stmt->bindParam(':login_time', $login_time);
$attendance_stmt->bindParam(':ip_address', $ip_address);
$attendance_stmt->execute();
$_SESSION['attendance_id'] = $db->lastInsertId();
header("Location: index.php"); header("Location: index.php");
exit(); exit();
} else { } else {

View File

@ -1,5 +1,17 @@
<?php <?php
session_start(); session_start();
require_once 'db/config.php';
if (isset($_SESSION['attendance_id'])) {
$db = db();
$logout_time = date('Y-m-d H:i:s');
$attendance_stmt = $db->prepare("UPDATE attendance SET logout_time = :logout_time WHERE id = :id");
$attendance_stmt->bindParam(':logout_time', $logout_time);
$attendance_stmt->bindParam(':id', $_SESSION['attendance_id']);
$attendance_stmt->execute();
}
session_unset(); session_unset();
session_destroy(); session_destroy();
header("Location: login.php"); header("Location: login.php");

View File

@ -18,6 +18,13 @@ try {
name VARCHAR(255) NOT NULL UNIQUE name VARCHAR(255) NOT NULL UNIQUE
)"); )");
// Function to log activity
function log_activity($user_id, $action) {
global $pdo;
$stmt = $pdo->prepare("INSERT INTO activities (user_id, action) VALUES (:user_id, :action)");
$stmt->execute(['user_id' => $user_id, 'action' => $action]);
}
// Handle Create and Update // Handle Create and Update
if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['add_role'])) { if (isset($_POST['add_role'])) {
@ -25,6 +32,8 @@ try {
if (!empty($name)) { if (!empty($name)) {
$stmt = $pdo->prepare("INSERT INTO roles (name) VALUES (:name)"); $stmt = $pdo->prepare("INSERT INTO roles (name) VALUES (:name)");
$stmt->execute(['name' => $name]); $stmt->execute(['name' => $name]);
$new_role_id = $pdo->lastInsertId();
log_activity($_SESSION['user_id'], "Created role {$name} (ID: {$new_role_id})");
} }
} elseif (isset($_POST['update_role'])) { } elseif (isset($_POST['update_role'])) {
$id = $_POST['role_id']; $id = $_POST['role_id'];
@ -32,6 +41,7 @@ try {
if (!empty($name) && !empty($id)) { if (!empty($name) && !empty($id)) {
$stmt = $pdo->prepare("UPDATE roles SET name = :name WHERE id = :id"); $stmt = $pdo->prepare("UPDATE roles SET name = :name WHERE id = :id");
$stmt->execute(['name' => $name, 'id' => $id]); $stmt->execute(['name' => $name, 'id' => $id]);
log_activity($_SESSION['user_id'], "Updated role {$name} (ID: {$id})");
} }
} }
header("Location: roles.php"); header("Location: roles.php");
@ -41,8 +51,17 @@ try {
// Handle Delete // Handle Delete
if (isset($_GET['delete_id'])) { if (isset($_GET['delete_id'])) {
$id = $_GET['delete_id']; $id = $_GET['delete_id'];
// Get role name for logging
$stmt = $pdo->prepare("SELECT name FROM roles WHERE id = :id");
$stmt->execute(['id' => $id]);
$deleted_role = $stmt->fetch();
$stmt = $pdo->prepare("DELETE FROM roles WHERE id = :id"); $stmt = $pdo->prepare("DELETE FROM roles WHERE id = :id");
$stmt->execute(['id' => $id]); $stmt->execute(['id' => $id]);
if ($deleted_role) {
log_activity($_SESSION['user_id'], "Deleted role {$deleted_role['name']} (ID: {$id})");
}
header("Location: roles.php"); header("Location: roles.php");
exit; exit;
} }
@ -86,6 +105,9 @@ try {
<li><a href="index.php" class="nav-link px-2 link-dark">خانه</a></li> <li><a href="index.php" class="nav-link px-2 link-dark">خانه</a></li>
<li><a href="roles.php" class="nav-link px-2 link-secondary">مدیریت نقش‌ها</a></li> <li><a href="roles.php" class="nav-link px-2 link-secondary">مدیریت نقش‌ها</a></li>
<li><a href="users.php" class="nav-link px-2 link-dark">مدیریت کاربران</a></li> <li><a href="users.php" class="nav-link px-2 link-dark">مدیریت کاربران</a></li>
<li><a href="activities.php" class="nav-link px-2 link-dark">Activities</a></li>
<li><a href="exams.php" class="nav-link px-2 link-dark">Exams</a></li>
<li><a href="attendance.php" class="nav-link px-2 link-dark">Attendance</a></li>
</ul> </ul>
<div class="text-end"> <div class="text-end">
<?php if (isset($_SESSION['user_id'])): ?> <?php if (isset($_SESSION['user_id'])): ?>

View File

@ -26,6 +26,13 @@ try {
// Fetch all roles for the dropdown // Fetch all roles for the dropdown
$roles = $pdo->query("SELECT * FROM roles ORDER BY name")->fetchAll(); $roles = $pdo->query("SELECT * FROM roles ORDER BY name")->fetchAll();
// Function to log activity
function log_activity($user_id, $action) {
global $pdo;
$stmt = $pdo->prepare("INSERT INTO activities (user_id, action) VALUES (:user_id, :action)");
$stmt->execute(['user_id' => $user_id, 'action' => $action]);
}
// Handle Create and Update // Handle Create and Update
if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['add_user'])) { if (isset($_POST['add_user'])) {
@ -43,6 +50,8 @@ try {
'password' => $hashed_password, 'password' => $hashed_password,
'role_id' => $role_id 'role_id' => $role_id
]); ]);
$new_user_id = $pdo->lastInsertId();
log_activity($_SESSION['user_id'], "Created user {$username} (ID: {$new_user_id})");
} }
} elseif (isset($_POST['update_user'])) { } elseif (isset($_POST['update_user'])) {
$id = $_POST['user_id']; $id = $_POST['user_id'];
@ -71,6 +80,7 @@ try {
'id' => $id 'id' => $id
]); ]);
} }
log_activity($_SESSION['user_id'], "Updated user {$username} (ID: {$id})");
} }
} }
header("Location: users.php"); header("Location: users.php");
@ -80,8 +90,17 @@ try {
// Handle Delete // Handle Delete
if (isset($_GET['delete_id'])) { if (isset($_GET['delete_id'])) {
$id = $_GET['delete_id']; $id = $_GET['delete_id'];
// Get username for logging
$stmt = $pdo->prepare("SELECT username FROM users WHERE id = :id");
$stmt->execute(['id' => $id]);
$deleted_user = $stmt->fetch();
$stmt = $pdo->prepare("DELETE FROM users WHERE id = :id"); $stmt = $pdo->prepare("DELETE FROM users WHERE id = :id");
$stmt->execute(['id' => $id]); $stmt->execute(['id' => $id]);
if ($deleted_user) {
log_activity($_SESSION['user_id'], "Deleted user {$deleted_user['username']} (ID: {$id})");
}
header("Location: users.php"); header("Location: users.php");
exit; exit;
} }
@ -130,6 +149,9 @@ try {
<li><a href="index.php" class="nav-link px-2 link-dark">خانه</a></li> <li><a href="index.php" class="nav-link px-2 link-dark">خانه</a></li>
<li><a href="roles.php" class="nav-link px-2 link-dark">مدیریت نقش‌ها</a></li> <li><a href="roles.php" class="nav-link px-2 link-dark">مدیریت نقش‌ها</a></li>
<li><a href="users.php" class="nav-link px-2 link-secondary">مدیریت کاربران</a></li> <li><a href="users.php" class="nav-link px-2 link-secondary">مدیریت کاربران</a></li>
<li><a href="activities.php" class="nav-link px-2 link-dark">Activities</a></li>
<li><a href="exams.php" class="nav-link px-2 link-dark">Exams</a></li>
<li><a href="attendance.php" class="nav-link px-2 link-dark">Attendance</a></li>
</ul> </ul>
<div class="text-end"> <div class="text-end">
<?php if (isset($_SESSION['user_id'])): ?> <?php if (isset($_SESSION['user_id'])): ?>