diff --git a/app/Exports/StudentsExport.php b/app/Exports/StudentsExport.php index 6a8d846..01b7085 100644 --- a/app/Exports/StudentsExport.php +++ b/app/Exports/StudentsExport.php @@ -18,6 +18,7 @@ class StudentsExport implements FromCollection, ShouldAutoSize, WithEvents, With use StyledHeadingRow; public function __construct( + private readonly User $user, private readonly string $search = '', private readonly ?string $gender = null, private readonly ?int $departmentId = null, @@ -32,7 +33,7 @@ public function title(): string public function collection(): Enumerable { - return app(StudentService::class)->forExport($this->search, $this->gender, $this->departmentId, $this->status, $this->enrollmentYear); + return app(StudentService::class)->forExport($this->user, $this->search, $this->gender, $this->departmentId, $this->status, $this->enrollmentYear); } public function headings(): array diff --git a/app/Http/Controllers/Admin/Users/StudentController.php b/app/Http/Controllers/Admin/Users/StudentController.php index 17e4c4a..5cff4a1 100644 --- a/app/Http/Controllers/Admin/Users/StudentController.php +++ b/app/Http/Controllers/Admin/Users/StudentController.php @@ -31,6 +31,7 @@ public function index(PaginatedRequest $request): Response { return Inertia::render('admin/users/students/index', [ 'students' => $this->service->paginated( + $request->user(), ...$request->validatedWithDefaults(), gender: $request->validated('gender'), departmentId: $request->validated('department_id'), @@ -112,6 +113,7 @@ public function updateUserStatus(UpdateStatusRequest $request, User $user): Redi public function export(PaginatedRequest $request): BinaryFileResponse { return Excel::download(new StudentsExport( + user: $request->user(), search: $request->validated('search') ?? '', gender: $request->validated('gender'), departmentId: $request->validated('department_id'), diff --git a/app/Services/Admin/Users/StudentService.php b/app/Services/Admin/Users/StudentService.php index 4f0acff..5cbb9d6 100644 --- a/app/Services/Admin/Users/StudentService.php +++ b/app/Services/Admin/Users/StudentService.php @@ -14,9 +14,9 @@ class StudentService { - public function paginated(int $perPage = 25, string $search = '', ?string $gender = null, ?int $departmentId = null, ?string $status = null, ?int $enrollmentYear = null): LengthAwarePaginator + public function paginated(User $user, int $perPage = 25, string $search = '', ?string $gender = null, ?int $departmentId = null, ?string $status = null, ?int $enrollmentYear = null): LengthAwarePaginator { - return $this->filteredQuery($search, $gender, $departmentId, $status, $enrollmentYear) + return $this->filteredQuery($user, $search, $gender, $departmentId, $status, $enrollmentYear) ->latest() ->paginate($perPage, ['id', 'username', 'email', 'is_active']); } @@ -109,9 +109,9 @@ public function updateUserStatus(User $user, bool $isActive): void $user->update(['is_active' => $isActive]); } - public function forExport(string $search = '', ?string $gender = null, ?int $departmentId = null, ?string $status = null, ?int $enrollmentYear = null): Collection + public function forExport(User $user, string $search = '', ?string $gender = null, ?int $departmentId = null, ?string $status = null, ?int $enrollmentYear = null): Collection { - return $this->filteredQuery($search, $gender, $departmentId, $status, $enrollmentYear) + return $this->filteredQuery($user, $search, $gender, $departmentId, $status, $enrollmentYear) ->orderBy('created_at', 'desc') ->get(['id', 'username', 'email', 'is_active']); } @@ -142,14 +142,19 @@ public function getEnrollmentYears(): array ->all(); } - private function filteredQuery(string $search, ?string $gender, ?int $departmentId, ?string $status, ?int $enrollmentYear = null): Builder + private function filteredQuery(User $user, string $search, ?string $gender, ?int $departmentId, ?string $status, ?int $enrollmentYear = null): Builder { + $taughtDepartmentIds = $user->hasRole(UserRole::Dosen->value) + ? $user->lecturer?->departments()->pluck('departments.id')->all() ?? [] + : null; + return User::with([ 'profile:id,user_id,full_name,phone_number,gender,birth_place,birth_date,address', 'student:id,user_id,student_number,department_id,enrollment_year,current_semester,status', 'student.department:id,name', ]) ->whereHas('roles', fn ($q) => $q->where('name', UserRole::Mahasiswa->value)) + ->when($taughtDepartmentIds !== null, fn ($q) => $q->whereHas('student', fn ($q) => $q->whereIn('department_id', $taughtDepartmentIds))) ->when($search, fn ($q) => $q->where(function ($query) use ($search) { $query->where('username', 'like', "%{$search}%") ->orWhere('email', 'like', "%{$search}%")