This commit is contained in:
Flatlogic Bot 2025-11-27 10:42:12 +00:00
parent 082d14fa79
commit e15fa31a20
15 changed files with 399 additions and 246 deletions

View File

@ -1,110 +1,32 @@
<?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());
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Activities</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>
<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>
<header>
<h1>Activities</h1>
<nav>
<ul>
<li><a href="teacher_dashboard.php">Dashboard</a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
</nav>
</header>
<main>
<h2>Activity Management</h2>
<p>This page will contain student activity information.</p>
</main>
</body>
</html>

View File

@ -1,118 +1,216 @@
<?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();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit();
}
// 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;");
$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.";
}
?>
<!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 rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="assets/css/custom.css">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</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>
<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>
<div class="card">
<div class="card-header">
Attendance History
<?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>
<div class="card-body">
<table class="table table-striped table-bordered">
<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>Username</th>
<th>Login Time</th>
<th>Logout Time</th>
<th>IP Address</th>
<th>Date</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php foreach ($history as $record): ?>
<?php
$attendance_records = get_student_attendance($child_id);
foreach ($attendance_records 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>
<td><?php echo htmlspecialchars($record['attendance_date']); ?></td>
<td><?php echo htmlspecialchars($record['status']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</main>
</div>
<?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>

34
database.sql Normal file
View File

@ -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)
);

21
db/migrate.php Normal file
View File

@ -0,0 +1,21 @@
<?php
require_once __DIR__ . '/config.php';
function run_migrations() {
$pdo = db();
$migrations_dir = __DIR__ . '/migrations';
$files = glob($migrations_dir . '/*.sql');
foreach ($files as $file) {
$sql = file_get_contents($file);
try {
$pdo->exec($sql);
echo "Migration from $file executed successfully.\n";
} catch (PDOException $e) {
echo "Error executing migration from $file: " . $e->getMessage() . "\n";
}
}
}
run_migrations();

View File

@ -0,0 +1 @@
DROP TABLE IF EXISTS attendance, parent_child, users, roles;

View File

@ -0,0 +1,4 @@
CREATE TABLE IF NOT EXISTS roles (
id INT AUTO_INCREMENT PRIMARY KEY,
role_name VARCHAR(255) NOT NULL UNIQUE
);

View File

@ -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
);

View File

@ -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)
);

View File

@ -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)
);

View File

@ -1,61 +1,32 @@
<?php
require_once 'db/config.php';
session_start();
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());
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Exams</title>
<link rel="stylesheet" href="assets/css/custom.css">
</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>
<header>
<h1>Exams</h1>
<nav>
<ul>
<li><a href="teacher_dashboard.php">Dashboard</a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
</nav>
</header>
<main>
<h2>Exam Management</h2>
<p>This page will contain student exam information.</p>
</main>
</body>
</html>

View File

@ -25,6 +25,9 @@ if (!isset($_SESSION['user_id']) || empty($_SESSION['role_name']) || $_SESSION['
</a>
<ul class="nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0">
<li><a href="parent_dashboard.php" class="nav-link px-2 link-secondary">Dashboard</a></li>
<li><a href="attendance.php" class="nav-link px-2 link-dark">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>
</ul>
<div class="text-end">
<a href="logout.php" class="btn btn-outline-primary">Logout</a>

View File

@ -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 {
<?php foreach ($roles as $role): ?>
<tr>
<td><?php echo htmlspecialchars($role['id']); ?></td>
<td><?php echo htmlspecialchars($role['name']); ?></td>
<td><?php echo htmlspecialchars($role['role_name']); ?></td>
<td>
<a href="roles.php?edit_id=<?php echo $role['id']; ?>" class="btn btn-sm btn-outline-primary">ویرایش</a>
<a href="roles.php?delete_id=<?php echo $role['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('آیا مطمئن هستید؟')">حذف</a>
@ -156,7 +156,7 @@ try {
<?php endif; ?>
<div class="mb-3">
<label for="role_name" class="form-label">نام نقش</label>
<input type="text" class="form-control" id="role_name" name="role_name" value="<?php echo htmlspecialchars($editing_role['name'] ?? ''); ?>" required>
<input type="text" class="form-control" id="role_name" name="role_name" value="<?php echo htmlspecialchars($editing_role['role_name'] ?? ''); ?>" required>
</div>
<?php if ($editing_role): ?>
<button type="submit" name="update_role" class="btn btn-primary w-100">به‌روزرسانی</button>

View File

@ -25,6 +25,9 @@ if (!isset($_SESSION['user_id']) || empty($_SESSION['role_name']) || $_SESSION['
</a>
<ul class="nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0">
<li><a href="student_dashboard.php" class="nav-link px-2 link-secondary">Dashboard</a></li>
<li><a href="attendance.php" class="nav-link px-2 link-dark">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>
</ul>
<div class="text-end">
<a href="logout.php" class="btn btn-outline-primary">Logout</a>

View File

@ -25,6 +25,9 @@ if (!isset($_SESSION['user_id']) || empty($_SESSION['role_name']) || $_SESSION['
</a>
<ul class="nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0">
<li><a href="teacher_dashboard.php" class="nav-link px-2 link-secondary">Dashboard</a></li>
<li><a href="attendance.php" class="nav-link px-2 link-dark">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>
</ul>
<div class="text-end">
<a href="logout.php" class="btn btn-outline-primary">Logout</a>

View File

@ -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 {
<label for="email" class="form-label">ایمیل</label>
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($editing_user['email'] ?? ''); ?>" required>
</div>
<div class="mb-3">
<label for="first_name" class="form-label">نام</label>
<input type="text" class="form-control" id="first_name" name="first_name" value="<?php echo htmlspecialchars($editing_user['first_name'] ?? ''); ?>">
</div>
<div class="mb-3">
<label for="last_name" class="form-label">نام خانوادگی</label>
<input type="text" class="form-control" id="last_name" name="last_name" value="<?php echo htmlspecialchars($editing_user['last_name'] ?? ''); ?>">
</div>
<div class="mb-3">
<label for="password" class="form-label">رمز عبور <?php echo $editing_user ? '(خالی بگذارید تا بدون تغییر بماند)' : ''; ?></label>
<input type="password" class="form-control" id="password" name="password" <?php echo !$editing_user ? 'required' : ''; ?>>
@ -220,7 +254,7 @@ try {
<option value="">یک نقش انتخاب کنید</option>
<?php foreach ($roles as $role): ?>
<option value="<?php echo $role['id']; ?>" <?php echo (isset($editing_user) && $editing_user['role_id'] == $role['id']) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($role['name']); ?>
<?php echo htmlspecialchars($role['role_name']); ?>
</option>
<?php endforeach; ?>
</select>
@ -234,6 +268,40 @@ try {
</form>
</div>
</div>
<div class="row mt-5">
<div class="col-md-12">
<h2>Link Parent to Child</h2>
<?php if (isset($_GET['link_success'])): ?>
<div class="alert alert-success">Parent and child linked successfully.</div>
<?php endif; ?>
<form method="POST" action="users.php">
<div class="row">
<div class="col-md-5">
<label for="parent_id" class="form-label">Parent</label>
<select class="form-select" id="parent_id" name="parent_id" required>
<option value="">Select a parent</option>
<?php foreach ($parents as $parent): ?>
<option value="<?php echo $parent['id']; ?>"><?php echo htmlspecialchars($parent['first_name'] . ' ' . $parent['last_name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-5">
<label for="child_id" class="form-label">Child</label>
<select class="form-select" id="child_id" name="child_id" required>
<option value="">Select a child</option>
<?php foreach ($students as $student): ?>
<option value="<?php echo $student['id']; ?>"><?php echo htmlspecialchars($student['first_name'] . ' ' . $student['last_name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-2 d-flex align-items-end">
<button type="submit" name="link_parent_child" class="btn btn-primary w-100">Link</button>
</div>
</div>
</form>
</div>
</div>
</main>
<footer class="footer mt-auto py-3 bg-light">