diff --git a/code/manage_users.php b/code/manage_users.php new file mode 100644 index 0000000000000000000000000000000000000000..c1b839baf396bd43357163a46f06dad5ea1133c5 --- /dev/null +++ b/code/manage_users.php @@ -0,0 +1,157 @@ +<?php +ini_set('display_errors', 1); +ini_set('display_startup_errors', 1); +error_reporting(E_ALL); +include 'config.php'; +session_start(); + +if (!isset($_SESSION['user']) || $_SESSION['user']['role'] !== 'admin') { + header('Location: login.php'); + exit; +} + +$usert = $_SESSION['user']; + +// Changer le statut d'un utilisateur +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['change_status_user_id'])) { + $user_id = $conn->real_escape_string($_POST['change_status_user_id']); + $new_status = $conn->real_escape_string($_POST['new_status']) === 'true' ? 1 : 0; + + $update_status = $conn->prepare("UPDATE User SET status = ? WHERE id = ?"); + $update_status->bind_param("ii", $new_status, $user_id); + if ($update_status->execute()) { + $success = "User status updated successfully!"; + } else { + $error = "Error updating user status: " . $conn->error; + } + $update_status->close(); +} + +// Supprimer un utilisateur +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_user_id'])) { + $user_id = $conn->real_escape_string($_POST['delete_user_id']); + + $delete_user = $conn->prepare("DELETE FROM User WHERE id = ?"); + $delete_user->bind_param("i", $user_id); + if ($delete_user->execute()) { + $success = "User deleted successfully!"; + } else { + $error = "Error deleting user: " . $conn->error; + } + $delete_user->close(); +} + +// Récupérer tous les utilisateurs sauf les administrateurs +$users = []; +$sql = "SELECT id, first_name, last_name, email, role, specialty, status + FROM User + WHERE role != 'admin' AND role != 'student'"; +$result = $conn->query($sql); +while ($row = $result->fetch_assoc()) { + $users[] = $row; +} +?> +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>Manage Users</title> + <link rel="stylesheet" href="style.css"> +</head> +<body> + <div class="sidebar"> + <div> + <h2>Menu</h2> + <?php if ($usert['role'] === 'tutor'): ?> + <a href="create_session.php">Create a session</a> + <a href="prof_create_depot.php">Create a depot</a> + <a href="prof_view_files.php">View depot</a> + <a href="manage_sessions.php">Manage sessions</a> + <a href="view_students.php">View Students</a> + <a href="attendance.php">Call the roll</a> + <a href="planning.php">View Weekly Schedule</a> + <?php elseif ($usert['role'] === 'student'): ?> + <a href="student_upload_file.php">View depot</a> + <a href="join_session.php">Join a session</a> + <a href="view_sessions.php">View sessions</a> + <a href="planning.php">View Weekly Schedule</a> + <?php elseif ($usert['role'] === 'admin'): ?> + <a href="manage_users.php">Gestion utilisateurs</a> + <a href="manage_sessions_admin.php">Gestion de sessions</a> + <a href="planning.php">Programme hebdomadaire</a> + <?php endif; ?> + <a href="logout.php" class="logout">Logout</a> + </div> + <a href="welcome.php"> + <img src="img/but.jpg" alt="Sidebar Image"> + </a> + </div> + <div class="main-content"> + <div class="manage-users"> + <h1>Gestion utilisateurs</h1> + + <?php if (isset($success)): ?> + <p class="success"><?= htmlspecialchars($success) ?></p> + <?php endif; ?> + + <?php if (isset($error)): ?> + <p class="error"><?= htmlspecialchars($error) ?></p> + <?php endif; ?> + + <?php if (!empty($users)): ?> + <div style="overflow-x: auto;"> + <table> + <thead> + <tr> + <th>Prénom</th> + <th>Nom</th> + <th>Email</th> + <th>Role</th> + <th>Specialité</th> + <th>Etat</th> + <th>Actions</th> + <th>Supprimer</th> + </tr> + </thead> + <tbody> + <?php foreach ($users as $user): ?> + <tr> + <td><?= htmlspecialchars($user['first_name']) ?></td> + <td><?= htmlspecialchars($user['last_name']) ?></td> + <td><?= htmlspecialchars($user['email']) ?></td> + <td><?= htmlspecialchars($user['role']) ?></td> + <td><?= htmlspecialchars($user['specialty'] ?? 'N/A') ?></td> + <td><?= $user['status'] ? 'Active' : 'Inactive' ?></td> + <td> + <form method="POST" style="display:inline;"> + <input type="hidden" name="change_status_user_id" value="<?= htmlspecialchars($user['id']) ?>"> + <input type="hidden" name="new_status" value="<?= $user['status'] ? 'false' : 'true' ?>"> + <button type="submit"> + <?= $user['status'] ? 'Désactiver' : 'Activer' ?> + </button> + </form> + </td> + <td> + <form method="POST" style="display:inline;"> + <input type="hidden" name="delete_user_id" value="<?= htmlspecialchars($user['id']) ?>"> + <button type="submit" onclick="return confirm('Voulez-vous vraiment supprimer cet utilisateur?')"> + Supprimer + </button> + </form> + </td> + </tr> + <?php endforeach; ?> + </tbody> + </table> + </div> + <?php else: ?> + <p>No users available.</p> + <?php endif; ?> + + <br> + + </div> + </div> +</body> +</html>