Mini Shell
| Direktori : /home/mhcadmin/www/Portal/ |
|
|
| Current File : /home/mhcadmin/www/Portal/get_all_users_by_department.php |
<?php
session_start();
require_once 'config/config.php';
header('Content-Type: application/json');
// Check if the request is POST and has department parameter
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['department'])) {
// Sanitize the department ID
$department_id = mysqli_real_escape_string($conn, $_POST['department']);
// Get department name from Department table
$dept_query = "SELECT Name FROM Department WHERE id = '$department_id' AND Deleted = 'No'";
$dept_result = mysqli_query($conn, $dept_query);
$dept_name = '';
if ($dept_result && mysqli_num_rows($dept_result) > 0) {
$dept_data = mysqli_fetch_assoc($dept_result);
$dept_name = $dept_data['Name'];
}
// Query to get all active users from the selected department
$query = "SELECT id, Full_Name, Position, HOD as is_hod, UserDepartment, Status
FROM admin_accounts
WHERE Status = 'Active'
AND UserDepartment = '$department_id'
ORDER BY HOD DESC, Full_Name ASC";
$result = mysqli_query($conn, $query);
$users = array();
if ($result && mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$users[] = array(
'id' => $row['id'],
'full_name' => $row['Full_Name'],
'position' => $row['Position'] ?? '',
'is_hod' => $row['is_hod'] ?? '0',
'department' => $row['UserDepartment']
);
}
// Return success response with users
echo json_encode(array(
'success' => true,
'users' => $users,
'department_name' => $dept_name,
'total_users' => count($users),
'message' => 'Users retrieved successfully'
));
} else {
// No users found in this department
echo json_encode(array(
'success' => true,
'users' => [],
'department_name' => $dept_name,
'total_users' => 0,
'message' => 'No active users found in this department'
));
}
} else {
// Invalid request
echo json_encode(array(
'success' => false,
'message' => 'Invalid request. Department parameter is required.',
'users' => []
));
}
// Close the database connection
mysqli_close($conn);
?>