[ Mini Kiebo ]
Server: Windows NT AKFAR_SERVER 10.0 build 26200 (Windows 10) AMD64
Path:
D:
/
xampp
/
htdocs
/
repository_afbs
/
admin
/
[
Home
]
File: users.php
<?php session_start(); require_once '../includes/config.php'; require_once '../includes/database.php'; require_once '../includes/functions.php'; require_once '../includes/auth.php'; if (!isAdmin()) { header('Location: ../login.php'); exit(); } $db = new Database(); $conn = $db->getConnection(); // Handle hapus user if (isset($_GET['delete'])) { $user_id = $_GET['delete']; // Cek apakah user memiliki thesis $check_query = "SELECT COUNT(*) as total FROM theses WHERE author_id = :user_id"; $check_stmt = $conn->prepare($check_query); $check_stmt->bindParam(':user_id', $user_id); $check_stmt->execute(); $total_theses = $check_stmt->fetch(PDO::FETCH_ASSOC)['total']; if ($total_theses > 0) { $_SESSION['error'] = "User tidak dapat dihapus karena memiliki $total_theses tugas akhir. Archive user terlebih dahulu."; } else { $delete_query = "DELETE FROM users WHERE id = :id AND role != 'admin'"; $delete_stmt = $conn->prepare($delete_query); $delete_stmt->bindParam(':id', $user_id); if ($delete_stmt->execute()) { $_SESSION['success'] = "User berhasil dihapus"; } else { $_SESSION['error'] = "Gagal menghapus user"; } } header('Location: users.php'); exit(); } // Filter dan pagination $role = isset($_GET['role']) ? $_GET['role'] : ''; $status = isset($_GET['status']) ? $_GET['status'] : ''; $search = isset($_GET['search']) ? $_GET['search'] : ''; $page = isset($_GET['page']) ? (int)$_GET['page'] : 1; $limit = 20; $offset = ($page - 1) * $limit; // Build query $query = "SELECT u.*, (SELECT COUNT(*) FROM theses WHERE author_id = u.id) as total_theses, (SELECT COUNT(*) FROM downloads_log WHERE user_id = u.id) as total_downloads FROM users u WHERE 1=1"; $countQuery = "SELECT COUNT(*) as total FROM users WHERE 1=1"; $params = []; if (!empty($role)) { $query .= " AND u.role = :role"; $countQuery .= " AND role = :role"; $params[':role'] = $role; } if ($status !== '') { $query .= " AND u.is_active = :status"; $countQuery .= " AND is_active = :status"; $params[':status'] = $status == 'active' ? 1 : 0; } if (!empty($search)) { $query .= " AND (u.full_name LIKE :search OR u.email LIKE :search OR u.nim LIKE :search)"; $countQuery .= " AND (full_name LIKE :search OR email LIKE :search OR nim LIKE :search)"; $params[':search'] = "%$search%"; } $query .= " ORDER BY u.created_at DESC LIMIT :limit OFFSET :offset"; // Get total records $stmt = $conn->prepare($countQuery); foreach($params as $key => $value) { $stmt->bindValue($key, $value); } $stmt->execute(); $totalRecords = $stmt->fetch(PDO::FETCH_ASSOC)['total']; $totalPages = ceil($totalRecords / $limit); // Get users $stmt = $conn->prepare($query); foreach($params as $key => $value) { $stmt->bindValue($key, $value); } $stmt->bindValue(':limit', $limit, PDO::PARAM_INT); $stmt->bindValue(':offset', $offset, PDO::PARAM_INT); $stmt->execute(); $users = $stmt->fetchAll(PDO::FETCH_ASSOC); $page_title = 'Manajemen Pengguna'; include '../templates/header.php'; include '../templates/admin-navbar.php'; ?> <div class="container-fluid"> <div class="row"> <?php include '../templates/admin-sidebar.php'; ?> <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4"> <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom"> <h1 class="h2">Manajemen Pengguna</h1> <div class="btn-toolbar mb-2 mb-md-0"> <a href="user-add.php" class="btn btn-sm btn-primary"> <i class="fas fa-plus"></i> Tambah User </a> </div> </div> <?php if(isset($_SESSION['success'])): ?> <div class="alert alert-success alert-dismissible fade show"> <?php echo $_SESSION['success']; unset($_SESSION['success']); ?> <button type="button" class="btn-close" data-bs-dismiss="alert"></button> </div> <?php endif; ?> <?php if(isset($_SESSION['error'])): ?> <div class="alert alert-danger alert-dismissible fade show"> <?php echo $_SESSION['error']; unset($_SESSION['error']); ?> <button type="button" class="btn-close" data-bs-dismiss="alert"></button> </div> <?php endif; ?> <!-- Filter --> <div class="card mb-4"> <div class="card-body"> <form method="GET" class="row g-3"> <div class="col-md-3"> <select name="role" class="form-select"> <option value="">Semua Role</option> <option value="mahasiswa" <?php echo $role == 'mahasiswa' ? 'selected' : ''; ?>>Mahasiswa</option> <option value="dosen" <?php echo $role == 'dosen' ? 'selected' : ''; ?>>Dosen</option> <option value="admin" <?php echo $role == 'admin' ? 'selected' : ''; ?>>Admin</option> <option value="umum" <?php echo $role == 'umum' ? 'selected' : ''; ?>>Umum</option> </select> </div> <div class="col-md-3"> <select name="status" class="form-select"> <option value="">Semua Status</option> <option value="active" <?php echo $status == 'active' ? 'selected' : ''; ?>>Active</option> <option value="inactive" <?php echo $status == 'inactive' ? 'selected' : ''; ?>>Inactive</option> </select> </div> <div class="col-md-4"> <input type="text" name="search" class="form-control" placeholder="Cari nama, email, NIM..." value="<?php echo htmlspecialchars($search); ?>"> </div> <div class="col-md-2"> <button type="submit" class="btn btn-primary w-100">Filter</button> </div> </form> </div> </div> <!-- Tabel Users --> <div class="card"> <div class="card-body"> <div class="table-responsive"> <table class="table table-hover"> <thead> <tr> <th>ID</th> <th>Nama</th> <th>Email</th> <th>NIM</th> <th>Role</th> <th>Status</th> <th>Total TA</th> <th>Downloads</th> <th>Bergabung</th> <th>Aksi</th> </tr> </thead> <tbody> <?php foreach($users as $user): ?> <tr> <td><?php echo $user['id']; ?></td> <td><?php echo htmlspecialchars($user['full_name']); ?></td> <td><?php echo $user['email']; ?></td> <td><?php echo $user['nim'] ?? '-'; ?></td> <td> <?php $role_class = [ 'admin' => 'danger', 'dosen' => 'primary', 'mahasiswa' => 'success', 'umum' => 'secondary' ]; ?> <span class="badge bg-<?php echo $role_class[$user['role']]; ?>"> <?php echo $user['role']; ?> </span> </td> <td> <?php if($user['is_active']): ?> <span class="badge bg-success">Active</span> <?php else: ?> <span class="badge bg-secondary">Inactive</span> <?php endif; ?> </td> <td><?php echo $user['total_theses']; ?></td> <td><?php echo $user['total_downloads']; ?></td> <td><?php echo date('d/m/Y', strtotime($user['created_at'])); ?></td> <td> <div class="btn-group"> <a href="user-edit.php?id=<?php echo $user['id']; ?>" class="btn btn-sm btn-warning"> <i class="fas fa-edit"></i> </a> <?php if($user['role'] != 'admin'): ?> <a href="?delete=<?php echo $user['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Hapus user <?php echo $user['full_name']; ?>?')"> <i class="fas fa-trash"></i> </a> <?php endif; ?> </div> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> <!-- Pagination --> <?php if($totalPages > 1): ?> <nav aria-label="Page navigation" class="mt-4"> <ul class="pagination justify-content-center"> <li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>"> <a class="page-link" href="?page=<?php echo $page-1; ?>&role=<?php echo $role; ?>&status=<?php echo $status; ?>&search=<?php echo urlencode($search); ?>"> Previous </a> </li> <?php for($i = 1; $i <= $totalPages; $i++): ?> <?php if($i >= $page-2 && $i <= $page+2): ?> <li class="page-item <?php echo $i == $page ? 'active' : ''; ?>"> <a class="page-link" href="?page=<?php echo $i; ?>&role=<?php echo $role; ?>&status=<?php echo $status; ?>&search=<?php echo urlencode($search); ?>"> <?php echo $i; ?> </a> </li> <?php endif; ?> <?php endfor; ?> <li class="page-item <?php echo $page >= $totalPages ? 'disabled' : ''; ?>"> <a class="page-link" href="?page=<?php echo $page+1; ?>&role=<?php echo $role; ?>&status=<?php echo $status; ?>&search=<?php echo urlencode($search); ?>"> Next </a> </li> </ul> </nav> <?php endif; ?> </div> </div> </main> </div> </div> <?php include '../templates/footer.php'; ?>