From a2059511fcd4f597eb93d678f1a1e0c35690d4d1 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Thu, 27 Nov 2025 10:08:20 +0000 Subject: [PATCH] t4 --- activities.php | 110 +++++++++++++++++++++++++++++++++++++++++++++ attendance.php | 118 +++++++++++++++++++++++++++++++++++++++++++++++++ exams.php | 61 +++++++++++++++++++++++++ index.php | 3 ++ login.php | 11 +++++ logout.php | 12 +++++ roles.php | 22 +++++++++ users.php | 22 +++++++++ 8 files changed, 359 insertions(+) create mode 100644 activities.php create mode 100644 attendance.php create mode 100644 exams.php diff --git a/activities.php b/activities.php new file mode 100644 index 0000000..b27936c --- /dev/null +++ b/activities.php @@ -0,0 +1,110 @@ +exec($sql); +} catch (PDOException $e) { + die("ERROR: Could not connect. " . $e->getMessage()); +} +?> + + + + + + Activities + + + + + + +
+
+
+ +

Activity log:

+ 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); + ?> + + + + + + + + + + + + + + + + + + + +
IDUserActionTimestamp
+
+
+
+ + + diff --git a/attendance.php b/attendance.php new file mode 100644 index 0000000..646808e --- /dev/null +++ b/attendance.php @@ -0,0 +1,118 @@ +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()); +} +?> + + + + + Attendance + + + + +
+ +
+

Attendance

+ +
+
+ Currently Online +
+
+ 0): ?> +
    + +
  • + +
+ +

No users are currently online.

+ +
+
+ +
+
+ Attendance History +
+
+ + + + + + + + + + + + + + + + + + + +
UsernameLogin TimeLogout TimeIP Address
Still logged in'; ?>
+
+
+
+
+ + \ No newline at end of file diff --git a/exams.php b/exams.php new file mode 100644 index 0000000..832c4d4 --- /dev/null +++ b/exams.php @@ -0,0 +1,61 @@ +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()); +} +?> + + + + + + + Exams + + +

Exams

+ +

Exam management page.

+ + diff --git a/index.php b/index.php index 94f8a65..5652639 100644 --- a/index.php +++ b/index.php @@ -39,6 +39,9 @@
  • خانه
  • مدیریت نقش‌ها
  • مدیریت کاربران
  • + Activities + Exams + Attendance
    diff --git a/login.php b/login.php index 3885ff3..99512da 100644 --- a/login.php +++ b/login.php @@ -28,6 +28,17 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $_SESSION['user_id'] = $user['id']; $_SESSION['username'] = $username; $_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"); exit(); } else { diff --git a/logout.php b/logout.php index e4bc3fa..afbb213 100644 --- a/logout.php +++ b/logout.php @@ -1,5 +1,17 @@ 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_destroy(); header("Location: login.php"); diff --git a/roles.php b/roles.php index 9bff98a..93fa978 100644 --- a/roles.php +++ b/roles.php @@ -18,6 +18,13 @@ try { 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 if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_POST['add_role'])) { @@ -25,6 +32,8 @@ try { if (!empty($name)) { $stmt = $pdo->prepare("INSERT INTO roles (name) VALUES (: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'])) { $id = $_POST['role_id']; @@ -32,6 +41,7 @@ try { if (!empty($name) && !empty($id)) { $stmt = $pdo->prepare("UPDATE roles SET name = :name WHERE id = :id"); $stmt->execute(['name' => $name, 'id' => $id]); + log_activity($_SESSION['user_id'], "Updated role {$name} (ID: {$id})"); } } header("Location: roles.php"); @@ -41,8 +51,17 @@ try { // Handle Delete if (isset($_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->execute(['id' => $id]); + + if ($deleted_role) { + log_activity($_SESSION['user_id'], "Deleted role {$deleted_role['name']} (ID: {$id})"); + } header("Location: roles.php"); exit; } @@ -86,6 +105,9 @@ try {
  • خانه
  • مدیریت نقش‌ها
  • مدیریت کاربران
  • +
  • Activities
  • +
  • Exams
  • +
  • Attendance
  • diff --git a/users.php b/users.php index f775aba..b3d162f 100644 --- a/users.php +++ b/users.php @@ -26,6 +26,13 @@ try { // Fetch all roles for the dropdown $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 if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_POST['add_user'])) { @@ -43,6 +50,8 @@ try { 'password' => $hashed_password, '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'])) { $id = $_POST['user_id']; @@ -71,6 +80,7 @@ try { 'id' => $id ]); } + log_activity($_SESSION['user_id'], "Updated user {$username} (ID: {$id})"); } } header("Location: users.php"); @@ -80,8 +90,17 @@ try { // Handle Delete if (isset($_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->execute(['id' => $id]); + + if ($deleted_user) { + log_activity($_SESSION['user_id'], "Deleted user {$deleted_user['username']} (ID: {$id})"); + } header("Location: users.php"); exit; } @@ -130,6 +149,9 @@ try {
  • خانه
  • مدیریت نقش‌ها
  • مدیریت کاربران
  • +
  • Activities
  • +
  • Exams
  • +
  • Attendance