From e15fa31a200f560c20af56df8db505341039ff16 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Thu, 27 Nov 2025 10:42:12 +0000 Subject: [PATCH] t6 --- activities.php | 116 ++------ attendance.php | 268 ++++++++++++------ database.sql | 34 +++ db/migrate.php | 21 ++ db/migrations/000_drop_tables.sql | 1 + db/migrations/001_create_roles_table.sql | 4 + db/migrations/002_create_users_table.sql | 11 + db/migrations/003_create_attendance_table.sql | 7 + .../004_create_parent_child_table.sql | 7 + exams.php | 67 ++--- parent_dashboard.php | 3 + roles.php | 18 +- student_dashboard.php | 3 + teacher_dashboard.php | 3 + users.php | 82 +++++- 15 files changed, 399 insertions(+), 246 deletions(-) create mode 100644 database.sql create mode 100644 db/migrate.php create mode 100644 db/migrations/000_drop_tables.sql create mode 100644 db/migrations/001_create_roles_table.sql create mode 100644 db/migrations/002_create_users_table.sql create mode 100644 db/migrations/003_create_attendance_table.sql create mode 100644 db/migrations/004_create_parent_child_table.sql diff --git a/activities.php b/activities.php index b27936c..60e59a8 100644 --- a/activities.php +++ b/activities.php @@ -1,110 +1,32 @@ exec($sql); -} catch (PDOException $e) { - die("ERROR: Could not connect. " . $e->getMessage()); +if (!isset($_SESSION['user_id'])) { + header('Location: login.php'); + exit(); } ?> - + 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
-
-
-
- +
+

Activities

+ +
+
+

Activity Management

+

This page will contain student activity information.

+
- + \ No newline at end of file diff --git a/attendance.php b/attendance.php index 646808e..2a55c83 100644 --- a/attendance.php +++ b/attendance.php @@ -1,118 +1,216 @@ 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;"); +$role_name = $_SESSION['role_name']; +$user_id = $_SESSION['user_id']; - // 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); +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(); +} - // 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); +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(); +} -} catch (PDOException $e) { - die("Could not connect to the database :" . $e->getMessage()); +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."; } ?> + Attendance - - + -
- -
-

Attendance

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

No users are currently online.

+
+ +
+
+

Attendance

-
-
- Attendance History + +
+ + + +

Take Attendance

+
+
+ +
-
- +
+ + + + + + + + + + + + + + +
Student NameStatus
+
+ + +
+
+ + +
+
+ + +
+
+ + + +

My Attendance

+ + + + + + + + + + + + + + + +
DateStatus
+ +

My Child's Attendance

+ 0): + ?> +
+
+
+ + +
+
+
+ + + - - - - + + - + - - - - + +
UsernameLogin TimeLogout TimeIP AddressDateStatus
Still logged in'; ?>
-
-
-
-
+ +
Invalid child selected.
+ + + +

You have no children linked to your account.

+ + +
- \ No newline at end of file + diff --git a/database.sql b/database.sql new file mode 100644 index 0000000..a23e35d --- /dev/null +++ b/database.sql @@ -0,0 +1,34 @@ +DROP TABLE IF EXISTS attendance, parent_child, users, roles; + +CREATE TABLE IF NOT EXISTS roles ( + id INT AUTO_INCREMENT PRIMARY KEY, + role_name VARCHAR(255) NOT NULL UNIQUE +); + +CREATE TABLE IF NOT EXISTS users ( + id INT AUTO_INCREMENT PRIMARY KEY, + username VARCHAR(255) NOT NULL UNIQUE, + email VARCHAR(255) NOT NULL UNIQUE, + password VARCHAR(255) NOT NULL, + role_id INT, + first_name VARCHAR(255), + last_name VARCHAR(255), + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE SET NULL +); + +CREATE TABLE IF NOT EXISTS attendance ( + id INT AUTO_INCREMENT PRIMARY KEY, + student_id INT NOT NULL, + attendance_date DATE NOT NULL, + status ENUM('present', 'absent', 'late') NOT NULL, + FOREIGN KEY (student_id) REFERENCES users(id) +); + +CREATE TABLE IF NOT EXISTS parent_child ( + id INT AUTO_INCREMENT PRIMARY KEY, + parent_id INT NOT NULL, + child_id INT NOT NULL, + FOREIGN KEY (parent_id) REFERENCES users(id), + FOREIGN KEY (child_id) REFERENCES users(id) +); diff --git a/db/migrate.php b/db/migrate.php new file mode 100644 index 0000000..d0d5787 --- /dev/null +++ b/db/migrate.php @@ -0,0 +1,21 @@ +exec($sql); + echo "Migration from $file executed successfully.\n"; + } catch (PDOException $e) { + echo "Error executing migration from $file: " . $e->getMessage() . "\n"; + } + } +} + +run_migrations(); + diff --git a/db/migrations/000_drop_tables.sql b/db/migrations/000_drop_tables.sql new file mode 100644 index 0000000..e2eb59f --- /dev/null +++ b/db/migrations/000_drop_tables.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS attendance, parent_child, users, roles; \ No newline at end of file diff --git a/db/migrations/001_create_roles_table.sql b/db/migrations/001_create_roles_table.sql new file mode 100644 index 0000000..b8e6e9c --- /dev/null +++ b/db/migrations/001_create_roles_table.sql @@ -0,0 +1,4 @@ +CREATE TABLE IF NOT EXISTS roles ( + id INT AUTO_INCREMENT PRIMARY KEY, + role_name VARCHAR(255) NOT NULL UNIQUE +); \ No newline at end of file diff --git a/db/migrations/002_create_users_table.sql b/db/migrations/002_create_users_table.sql new file mode 100644 index 0000000..37d9016 --- /dev/null +++ b/db/migrations/002_create_users_table.sql @@ -0,0 +1,11 @@ +CREATE TABLE IF NOT EXISTS users ( + id INT AUTO_INCREMENT PRIMARY KEY, + username VARCHAR(255) NOT NULL UNIQUE, + email VARCHAR(255) NOT NULL UNIQUE, + password VARCHAR(255) NOT NULL, + role_id INT, + first_name VARCHAR(255), + last_name VARCHAR(255), + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE SET NULL +); \ No newline at end of file diff --git a/db/migrations/003_create_attendance_table.sql b/db/migrations/003_create_attendance_table.sql new file mode 100644 index 0000000..30a1f1b --- /dev/null +++ b/db/migrations/003_create_attendance_table.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS attendance ( + id INT AUTO_INCREMENT PRIMARY KEY, + student_id INT NOT NULL, + attendance_date DATE NOT NULL, + status ENUM('present', 'absent', 'late') NOT NULL, + FOREIGN KEY (student_id) REFERENCES users(id) +); \ No newline at end of file diff --git a/db/migrations/004_create_parent_child_table.sql b/db/migrations/004_create_parent_child_table.sql new file mode 100644 index 0000000..00d79e9 --- /dev/null +++ b/db/migrations/004_create_parent_child_table.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS parent_child ( + id INT AUTO_INCREMENT PRIMARY KEY, + parent_id INT NOT NULL, + child_id INT NOT NULL, + FOREIGN KEY (parent_id) REFERENCES users(id), + FOREIGN KEY (child_id) REFERENCES users(id) +); \ No newline at end of file diff --git a/exams.php b/exams.php index 832c4d4..4bed414 100644 --- a/exams.php +++ b/exams.php @@ -1,61 +1,32 @@ 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()); +if (!isset($_SESSION['user_id'])) { + header('Location: login.php'); + exit(); } ?> - Exams + -

Exams

- -

Exam management page.

+
+

Exams

+ +
+
+

Exam Management

+

This page will contain student exam information.

+
- + \ No newline at end of file diff --git a/parent_dashboard.php b/parent_dashboard.php index 71ecbea..3706def 100644 --- a/parent_dashboard.php +++ b/parent_dashboard.php @@ -25,6 +25,9 @@ if (!isset($_SESSION['user_id']) || empty($_SESSION['role_name']) || $_SESSION['
Logout diff --git a/roles.php b/roles.php index 480500b..080d29b 100644 --- a/roles.php +++ b/roles.php @@ -15,7 +15,7 @@ try { // Create roles table if it doesn't exist $pdo->exec("CREATE TABLE IF NOT EXISTS roles ( id INT AUTO_INCREMENT PRIMARY KEY, - name VARCHAR(255) NOT NULL UNIQUE + role_name VARCHAR(255) NOT NULL UNIQUE )"); // Function to log activity @@ -30,8 +30,8 @@ try { if (isset($_POST['add_role'])) { $name = trim($_POST['role_name']); if (!empty($name)) { - $stmt = $pdo->prepare("INSERT INTO roles (name) VALUES (:name)"); - $stmt->execute(['name' => $name]); + $stmt = $pdo->prepare("INSERT INTO roles (role_name) VALUES (:role_name)"); + $stmt->execute(['role_name' => $name]); $new_role_id = $pdo->lastInsertId(); log_activity($_SESSION['user_id'], "Created role {$name} (ID: {$new_role_id})"); } @@ -39,8 +39,8 @@ try { $id = $_POST['role_id']; $name = trim($_POST['role_name']); if (!empty($name) && !empty($id)) { - $stmt = $pdo->prepare("UPDATE roles SET name = :name WHERE id = :id"); - $stmt->execute(['name' => $name, 'id' => $id]); + $stmt = $pdo->prepare("UPDATE roles SET role_name = :role_name WHERE id = :id"); + $stmt->execute(['role_name' => $name, 'id' => $id]); log_activity($_SESSION['user_id'], "Updated role {$name} (ID: {$id})"); } } @@ -52,7 +52,7 @@ try { 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 = $pdo->prepare("SELECT role_name FROM roles WHERE id = :id"); $stmt->execute(['id' => $id]); $deleted_role = $stmt->fetch(); @@ -60,7 +60,7 @@ try { $stmt->execute(['id' => $id]); if ($deleted_role) { - log_activity($_SESSION['user_id'], "Deleted role {$deleted_role['name']} (ID: {$id})"); + log_activity($_SESSION['user_id'], "Deleted role {$deleted_role['role_name']} (ID: {$id})"); } header("Location: roles.php"); exit; @@ -137,7 +137,7 @@ try { - + ویرایش حذف @@ -156,7 +156,7 @@ try {
- +
diff --git a/student_dashboard.php b/student_dashboard.php index c148d46..9d318b4 100644 --- a/student_dashboard.php +++ b/student_dashboard.php @@ -25,6 +25,9 @@ if (!isset($_SESSION['user_id']) || empty($_SESSION['role_name']) || $_SESSION['
Logout diff --git a/teacher_dashboard.php b/teacher_dashboard.php index 1c60275..6ed6002 100644 --- a/teacher_dashboard.php +++ b/teacher_dashboard.php @@ -25,6 +25,9 @@ if (!isset($_SESSION['user_id']) || empty($_SESSION['role_name']) || $_SESSION['
Logout diff --git a/users.php b/users.php index 2412f87..71a3cea 100644 --- a/users.php +++ b/users.php @@ -19,12 +19,14 @@ try { email VARCHAR(255) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL, role_id INT, + first_name VARCHAR(255), + last_name VARCHAR(255), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE SET NULL )"); // Fetch all roles for the dropdown - $roles = $pdo->query("SELECT * FROM roles ORDER BY name")->fetchAll(); + $roles = $pdo->query("SELECT * FROM roles ORDER BY role_name")->fetchAll(); // Function to log activity function log_activity($user_id, $action) { @@ -40,15 +42,19 @@ try { $email = trim($_POST['email']); $password = $_POST['password']; $role_id = $_POST['role_id']; + $first_name = trim($_POST['first_name']); + $last_name = trim($_POST['last_name']); if (!empty($username) && !empty($email) && !empty($password) && !empty($role_id)) { $hashed_password = password_hash($password, PASSWORD_DEFAULT); - $stmt = $pdo->prepare("INSERT INTO users (username, email, password, role_id) VALUES (:username, :email, :password, :role_id)"); + $stmt = $pdo->prepare("INSERT INTO users (username, email, password, role_id, first_name, last_name) VALUES (:username, :email, :password, :role_id, :first_name, :last_name)"); $stmt->execute([ 'username' => $username, 'email' => $email, 'password' => $hashed_password, - 'role_id' => $role_id + 'role_id' => $role_id, + 'first_name' => $first_name, + 'last_name' => $last_name ]); $new_user_id = $pdo->lastInsertId(); log_activity($_SESSION['user_id'], "Created user {$username} (ID: {$new_user_id})"); @@ -59,29 +65,45 @@ try { $email = trim($_POST['email']); $password = $_POST['password']; $role_id = $_POST['role_id']; + $first_name = trim($_POST['first_name']); + $last_name = trim($_POST['last_name']); if (!empty($id) && !empty($username) && !empty($email) && !empty($role_id)) { if (!empty($password)) { $hashed_password = password_hash($password, PASSWORD_DEFAULT); - $stmt = $pdo->prepare("UPDATE users SET username = :username, email = :email, password = :password, role_id = :role_id WHERE id = :id"); + $stmt = $pdo->prepare("UPDATE users SET username = :username, email = :email, password = :password, role_id = :role_id, first_name = :first_name, last_name = :last_name WHERE id = :id"); $stmt->execute([ 'username' => $username, 'email' => $email, 'password' => $hashed_password, 'role_id' => $role_id, + 'first_name' => $first_name, + 'last_name' => $last_name, 'id' => $id ]); } else { - $stmt = $pdo->prepare("UPDATE users SET username = :username, email = :email, role_id = :role_id WHERE id = :id"); + $stmt = $pdo->prepare("UPDATE users SET username = :username, email = :email, role_id = :role_id, first_name = :first_name, last_name = :last_name WHERE id = :id"); $stmt->execute([ 'username' => $username, 'email' => $email, 'role_id' => $role_id, + 'first_name' => $first_name, + 'last_name' => $last_name, 'id' => $id ]); } log_activity($_SESSION['user_id'], "Updated user {$username} (ID: {$id})"); + } elseif (isset($_POST['link_parent_child'])) { + $parent_id = $_POST['parent_id']; + $child_id = $_POST['child_id']; + + if (!empty($parent_id) && !empty($child_id)) { + $stmt = $pdo->prepare("INSERT INTO parent_child (parent_id, child_id) VALUES (:parent_id, :child_id)"); + $stmt->execute(['parent_id' => $parent_id, 'child_id' => $child_id]); + log_activity($_SESSION['user_id'], "Linked parent (ID: {$parent_id}) to child (ID: {$child_id})"); } + header("Location: users.php?link_success=1"); + exit; } header("Location: users.php"); exit; @@ -107,12 +129,16 @@ try { // Fetch all users with their role names $users = $pdo->query(" - SELECT users.*, roles.name AS role_name + SELECT users.*, roles.role_name AS role_name FROM users LEFT JOIN roles ON users.role_id = roles.id ORDER BY users.id DESC ")->fetchAll(); + // Fetch all parents and students + $parents = $pdo->query("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 = 'parent'")->fetchAll(); + $students = $pdo->query("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'")->fetchAll(); + // Fetch user for editing $editing_user = null; if (isset($_GET['edit_id'])) { @@ -210,6 +236,14 @@ try {
+
+ + +
+
+ + +
> @@ -220,7 +254,7 @@ try { @@ -234,6 +268,40 @@ try {
+ +
+
+

Link Parent to Child

+ +
Parent and child linked successfully.
+ +
+
+
+ + +
+
+ + +
+
+ +
+
+
+
+