siakad-itm/app/Services/Admin/Manage/Course/ClassEnrollmentService.php
Yoga Pangestu 353829e053
Some checks failed
tests / ci (pull_request) Has been cancelled
feat: Refactor Admin Manage structure into sub-namespaces for better organization
- Created sub-namespaces under Admin\Manage for Course, Academic, Announcement, Finance, and Service.
- Added new FormRequest classes for handling validation in each sub-namespace.
- Implemented Service classes for managing business logic related to each resource.
- Updated routes to reflect the new sub-namespace structure.
- Enhanced code organization and maintainability by grouping related functionalities.
2026-08-25 21:17:48 +07:00

44 lines
1.2 KiB
PHP

<?php
namespace App\Services\Admin\Manage\Course;
use App\Models\ClassEnrollment;
use App\Models\CourseClass;
use App\Models\Student;
use Illuminate\Database\Eloquent\Collection;
class ClassEnrollmentService
{
public function forClass(CourseClass $courseClass): Collection
{
return $courseClass->enrollments()
->with(['student.user.profile', 'student.department'])
->latest('enrolled_at')
->get();
}
public function availableStudents(CourseClass $courseClass): Collection
{
return Student::query()
->where('department_id', $courseClass->course->department_id)
->whereDoesntHave('enrollments', fn ($q) => $q->where('course_class_id', $courseClass->id))
->with(['user.profile', 'department'])
->get();
}
public function enrollMany(CourseClass $courseClass, array $studentIds): void
{
foreach ($studentIds as $studentId) {
$courseClass->enrollments()->firstOrCreate(
['student_id' => $studentId],
['enrolled_at' => now()],
);
}
}
public function unenroll(ClassEnrollment $enrollment): bool
{
return $enrollment->delete();
}
}