Spaces:
Sleeping
Sleeping
File size: 9,466 Bytes
102fe5c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | <?php
// 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();
?> |