updateUserStatus($user_id, $new_status, $admin_user_id); if ($has_valid_r_flag) { $userManager->updateUserRJupyterAccess($user_id, $requested_r_access === '1', $admin_user_id); if ((int)$user_id === (int)$_SESSION['user_id']) { $_SESSION['can_run_r'] = ($requested_r_access === '1'); } } $message = 'User status updated successfully!'; if ($has_valid_r_flag) { $message = 'User status and R/Jupyter access updated successfully!'; } set_message($message, 'success'); } catch (Exception $e) { set_message('Error updating user status: ' . $e->getMessage(), 'danger'); } } } elseif ($action_type === 'reset_password' && $user_id) { $new_password = $_POST['new_password'] ?? ''; $confirm_password = $_POST['confirm_password'] ?? ''; if (empty($new_password) || empty($confirm_password)) { set_message('Please provide and confirm the new password.', 'danger'); } elseif ($new_password !== $confirm_password) { set_message('Passwords do not match. Please try again.', 'danger'); } elseif (strlen($new_password) < 8) { set_message('Password must be at least 8 characters long.', 'danger'); } else { try { $admin_user_id = (int) $_SESSION['user_id']; $userManager->changePassword((int)$user_id, $new_password, $admin_user_id); set_message('Password reset successfully.', 'success'); } catch (Exception $e) { set_message('Error resetting password: ' . $e->getMessage(), 'danger'); } } } elseif ($action_type === 'add_user') { // --- Handle Add New User Submission --- $id_card = trim($_POST['id_card'] ?? ''); $first_name_en = trim($_POST['first_name_en'] ?? ''); $last_name_en = trim($_POST['last_name_en'] ?? ''); $sex = trim($_POST['sex'] ?? ''); $dob = trim($_POST['dob'] ?? ''); $phone_number = trim($_POST['phone_number'] ?? ''); $email = trim($_POST['email'] ?? ''); $username = trim($_POST['username'] ?? ''); $password = $_POST['password'] ?? ''; $confirm_password = $_POST['confirm_password'] ?? ''; $user_role_new = trim($_POST['user_role_new'] ?? 'Data User'); // Role for new user // Server-side validation for new user if (empty($first_name_en) || empty($last_name_en) || empty($sex) || empty($dob) || empty($username) || empty($password) || empty($confirm_password)) { set_message("All required fields for new user must be filled.", "danger"); } elseif ($password !== $confirm_password) { set_message("Passwords do not match for new user.", "danger"); } elseif (!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL)) { set_message("Invalid email format for new user.", "danger"); } else { // Prepare data for User class $person_data = [ 'id_card' => $id_card, 'first_name_en' => $first_name_en, 'last_name_en' => $last_name_en, 'sex' => $sex, 'dob' => $dob, 'pob' => null, // Add if you collect this 'nationality' => 'Cambodian', // Default or collect 'marital_status' => 'Single', // Default or collect 'phone_number' => $phone_number, 'email' => $email, 'telegram' => null, // Add if you collect this 'note' => null // Add if you collect this ]; $user_data = [ 'username' => $username, 'password' => $password, 'status' => 'Data User', // Default status for new registrations 'can_run_r' => !empty($_POST['user_can_run_r']) ]; try { if ($userManager->registerUser($person_data, $user_data)) { set_message("New user '" . htmlspecialchars($username) . "' registered successfully!", "success"); } else { // This else might be redundant if registerUser always throws on failure set_message("Failed to register new user due to an unknown error.", "danger"); } } catch (Exception $e) { set_message('Error registering new user: ' . $e->getMessage(), 'danger'); } } } // Redirect to self, preserving search/filter parameters if they exist $redirect_url = 'manage_users.php'; $query_params = []; if (!empty($search_query)) { $query_params['search'] = urlencode($search_query); } if (!empty($filter_status)) { $query_params['status_filter'] = urlencode($filter_status); } if (!empty($query_params)) { $redirect_url .= '?' . http_build_query($query_params); } header('Location: ' . $redirect_url); exit(); } // Fetch users based on search and filter parameters // We will modify getAllUsers in classes/User.php to accept these parameters $users = $userManager->getAllUsers($search_query, $filter_status); $totalUsers = count($users); $activeUsers = 0; $inactiveUsers = 0; $dacStaffCount = 0; $ownerCount = 0; $contributorCount = 0; $rAccessCount = 0; foreach ($users as $user) { $status = $user['isu_status'] ?? ''; $isActive = $status !== 'Inactive'; if ($isActive) { $activeUsers++; } else { $inactiveUsers++; } if ($status === 'DAC Staff') { $dacStaffCount++; } elseif ($status === 'Data Owner') { $ownerCount++; } elseif ($status === 'Data Contributor') { $contributorCount++; } if (!empty($user['isu_can_run_r'])) { $rAccessCount++; } } $summaryMetrics = [ [ 'label' => 'Total Users', 'value' => $totalUsers, 'icon' => 'fa-users', 'class' => 'bg-primary-subtle text-primary', 'icon_class' => 'text-primary' ], [ 'label' => 'Active Accounts', 'value' => $activeUsers, 'icon' => 'fa-user-check', 'class' => 'bg-success-subtle text-success', 'icon_class' => 'text-success' ], [ 'label' => 'With R/Jupyter', 'value' => $rAccessCount, 'icon' => 'fa-flask', 'class' => 'bg-info-subtle text-info', 'icon_class' => 'text-info' ], [ 'label' => 'Inactive', 'value' => $inactiveUsers, 'icon' => 'fa-user-slash', 'class' => 'bg-warning-subtle text-warning', 'icon_class' => 'text-warning' ], ]; ?>
| No. | Username | Full Name | Phone | Current Role | R/Jupyter | Actions | |
|---|---|---|---|---|---|---|---|
| Enabled Disabled |
|
||||||
| No users match the current filters. | |||||||