Spaces:
Sleeping
Sleeping
| // Include database connection and authentication check | |
| require_once 'includes/db_connect.php'; | |
| require_once 'includes/auth_check.php'; | |
| // This page is for faculty only | |
| requireFaculty(); | |
| $error = ''; | |
| $success = ''; | |
| // Get all users | |
| $users = []; | |
| $query = "SELECT * FROM users ORDER BY created_at DESC"; | |
| $result = $conn->query($query); | |
| if ($result && $result->num_rows > 0) { | |
| while ($row = $result->fetch_assoc()) { | |
| $users[] = $row; | |
| } | |
| } | |
| // Handle user activation/deactivation | |
| if (isset($_GET['action']) && isset($_GET['id'])) { | |
| $action = $_GET['action']; | |
| $user_id = (int)$_GET['id']; | |
| // Can't modify yourself | |
| if ($user_id === (int)$_SESSION['user_id']) { | |
| $error = "You cannot modify your own account status."; | |
| } else { | |
| if ($action === 'activate') { | |
| $status = 'active'; | |
| $success_msg = "User activated successfully."; | |
| } elseif ($action === 'deactivate') { | |
| $status = 'inactive'; | |
| $success_msg = "User deactivated successfully."; | |
| } else { | |
| $error = "Invalid action."; | |
| } | |
| if (empty($error)) { | |
| $update_query = "UPDATE users SET status = ? WHERE id = ?"; | |
| $update_stmt = $conn->prepare($update_query); | |
| $update_stmt->bind_param("si", $status, $user_id); | |
| if ($update_stmt->execute()) { | |
| $success = $success_msg; | |
| // Refresh user list | |
| $result = $conn->query($query); | |
| $users = []; | |
| if ($result && $result->num_rows > 0) { | |
| while ($row = $result->fetch_assoc()) { | |
| $users[] = $row; | |
| } | |
| } | |
| } else { | |
| $error = "Failed to update user status: " . $conn->error; | |
| } | |
| } | |
| } | |
| } | |
| // Count stats | |
| $totalUsers = count($users); | |
| $activeUsers = 0; | |
| $inactiveUsers = 0; | |
| $studentUsers = 0; | |
| $facultyUsers = 0; | |
| foreach ($users as $user) { | |
| if ($user['status'] === 'active') $activeUsers++; | |
| if ($user['status'] === 'inactive') $inactiveUsers++; | |
| if ($user['user_type'] === 'student') $studentUsers++; | |
| if ($user['user_type'] === 'faculty') $facultyUsers++; | |
| } | |
| // Include header | |
| include 'includes/header.php'; | |
| <div class="row mb-4"> | |
| <div class="col-md-12"> | |
| <nav aria-label="breadcrumb"> | |
| <ol class="breadcrumb"> | |
| <li class="breadcrumb-item"><a href="index.php">Home</a></li> | |
| <li class="breadcrumb-item active">Admin Panel</li> | |
| </ol> | |
| </nav> | |
| <h2><i class="fas fa-cogs me-2"></i> Admin Panel</h2> | |
| <p class="lead">Manage users and system settings</p> | |
| </div> | |
| </div> | |
| <?php if (!empty($error)): ?> | |
| <div class="alert alert-danger alert-dismissible fade show" role="alert"> | |
| <i class="fas fa-exclamation-circle me-2"></i> <?php echo $error; ?> | |
| <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> | |
| </div> | |
| <?php endif; ?> | |
| <?php if (!empty($success)): ?> | |
| <div class="alert alert-success alert-dismissible fade show" role="alert"> | |
| <i class="fas fa-check-circle me-2"></i> <?php echo $success; ?> | |
| <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> | |
| </div> | |
| <?php endif; ?> | |
| <div class="row mb-4"> | |
| <div class="col-md-3"> | |
| <div class="card text-center mb-3 shadow-sm"> | |
| <div class="card-body"> | |
| <i class="fas fa-users fa-3x mb-3 text-primary"></i> | |
| <h5 class="card-title">Total Users</h5> | |
| <p class="card-text display-4"><?php echo $totalUsers; ?></p> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="col-md-3"> | |
| <div class="card text-center mb-3 shadow-sm"> | |
| <div class="card-body"> | |
| <i class="fas fa-user-graduate fa-3x mb-3 text-success"></i> | |
| <h5 class="card-title">Students</h5> | |
| <p class="card-text display-4"><?php echo $studentUsers; ?></p> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="col-md-3"> | |
| <div class="card text-center mb-3 shadow-sm"> | |
| <div class="card-body"> | |
| <i class="fas fa-chalkboard-teacher fa-3x mb-3 text-danger"></i> | |
| <h5 class="card-title">Faculty</h5> | |
| <p class="card-text display-4"><?php echo $facultyUsers; ?></p> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="col-md-3"> | |
| <div class="card text-center mb-3 shadow-sm"> | |
| <div class="card-body"> | |
| <i class="fas fa-toggle-on fa-3x mb-3 text-warning"></i> | |
| <h5 class="card-title">Active/Inactive</h5> | |
| <p class="card-text display-4"><?php echo $activeUsers; ?> / <?php echo $inactiveUsers; ?></p> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="row"> | |
| <div class="col-md-12"> | |
| <div class="card shadow-sm"> | |
| <div class="card-header bg-primary text-white"> | |
| <h5 class="mb-0"><i class="fas fa-users me-2"></i> User Management</h5> | |
| </div> | |
| <div class="card-body"> | |
| <?php if (count($users) > 0): ?> | |
| <div class="table-responsive"> | |
| <table class="table table-striped table-hover"> | |
| <thead class="table-light"> | |
| <tr> | |
| <th>ID</th> | |
| <th>Username</th> | |
| <th>Email</th> | |
| <th>Type</th> | |
| <th>Status</th> | |
| <th>Created</th> | |
| <th>Last Login</th> | |
| <th>Actions</th> | |
| </tr> | |
| </thead> | |
| <tbody> | |
| <?php foreach ($users as $user): ?> | |
| <tr> | |
| <td><?php echo $user['id']; ?></td> | |
| <td><?php echo htmlspecialchars($user['username']); ?></td> | |
| <td><?php echo htmlspecialchars($user['email']); ?></td> | |
| <td> | |
| <span class="badge <?php echo $user['user_type'] === 'faculty' ? 'bg-danger' : 'bg-success'; ?>"> | |
| <?php echo ucfirst($user['user_type']); ?> | |
| </span> | |
| </td> | |
| <td> | |
| <span class="badge <?php echo $user['status'] === 'active' ? 'bg-success' : 'bg-secondary'; ?>"> | |
| <?php echo ucfirst($user['status']); ?> | |
| </span> | |
| </td> | |
| <td><?php echo date('M d, Y', strtotime($user['created_at'])); ?></td> | |
| <td><?php echo $user['last_login'] ? date('M d, Y H:i', strtotime($user['last_login'])) : 'Never'; ?></td> | |
| <td> | |
| <?php if ((int)$user['id'] !== (int)$_SESSION['user_id']): ?> | |
| <?php if ($user['status'] === 'active'): ?> | |
| <a href="admin.php?action=deactivate&id=<?php echo $user['id']; ?>" class="btn btn-sm btn-warning"> | |
| <i class="fas fa-toggle-off"></i> Deactivate | |
| </a> | |
| <?php else: ?> | |
| <a href="admin.php?action=activate&id=<?php echo $user['id']; ?>" class="btn btn-sm btn-success"> | |
| <i class="fas fa-toggle-on"></i> Activate | |
| </a> | |
| <?php endif; ?> | |
| <?php else: ?> | |
| <span class="text-muted">Current User</span> | |
| <?php endif; ?> | |
| </td> | |
| </tr> | |
| <?php endforeach; ?> | |
| </tbody> | |
| </table> | |
| </div> | |
| <?php else: ?> | |
| <div class="alert alert-info"> | |
| <i class="fas fa-info-circle me-2"></i> No users found in the database. | |
| </div> | |
| <?php endif; ?> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <?php | |
| // Include footer | |
| include 'includes/footer.php'; | |
| // Close connection | |
| $conn->close(); | |
| ?> |