add controllers, servoices
This commit is contained in:
@@ -2,38 +2,26 @@
|
||||
|
||||
namespace App\Services\Assignment;
|
||||
|
||||
use App\Models\ClassSection;
|
||||
use App\Models\Configuration;
|
||||
use App\Models\StudentClass;
|
||||
use App\Models\TeacherClass;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class AssignmentService
|
||||
{
|
||||
public function __construct(
|
||||
protected Configuration $configurationModel,
|
||||
protected TeacherClass $teacherClassModel,
|
||||
protected StudentClass $studentClassModel,
|
||||
protected ClassSection $classSectionModel,
|
||||
protected AssignmentContextService $contextService,
|
||||
protected AssignmentDataLoaderService $dataLoader,
|
||||
protected AssignmentSectionService $sectionService,
|
||||
protected AssignmentMetaService $metaService
|
||||
) {}
|
||||
|
||||
public function getCurrentSemester(): string
|
||||
{
|
||||
return (string) ($this->getConfigValue('semester') ?? '');
|
||||
return $this->contextService->getCurrentSemester();
|
||||
}
|
||||
|
||||
public function getCurrentSchoolYear(): string
|
||||
{
|
||||
return (string) ($this->getConfigValue('school_year') ?? '');
|
||||
}
|
||||
|
||||
protected function getConfigValue(string $key): mixed
|
||||
{
|
||||
return $this->configurationModel
|
||||
->newQuery()
|
||||
->where('config_key', $key)
|
||||
->value('config_value');
|
||||
return $this->contextService->getCurrentSchoolYear();
|
||||
}
|
||||
|
||||
public function getAssignmentsOverview(?string $schoolYear = null, ?string $semester = null): array
|
||||
@@ -44,8 +32,8 @@ class AssignmentService
|
||||
$selectedSemester = (string) ($semester ?? $currentSemester);
|
||||
$selectedYear = (string) ($schoolYear ?? $currentSchoolYear);
|
||||
|
||||
$teacherClasses = $this->loadTeacherClasses($selectedYear);
|
||||
$studentClasses = $this->loadStudentClasses($selectedYear);
|
||||
$teacherClasses = $this->dataLoader->loadTeacherClasses($selectedYear);
|
||||
$studentClasses = $this->dataLoader->loadStudentClasses($selectedYear);
|
||||
|
||||
$teacherBySection = $teacherClasses->groupBy('class_section_id');
|
||||
$studentBySection = $studentClasses->groupBy('class_section_id');
|
||||
@@ -56,7 +44,7 @@ class AssignmentService
|
||||
->filter()
|
||||
->values();
|
||||
|
||||
$sectionNames = $this->getSectionNamesMap($allSectionIds->all());
|
||||
$sectionNames = $this->sectionService->getSectionNamesMap($allSectionIds->all());
|
||||
|
||||
$classSections = $allSectionIds->map(function ($sectionId) use (
|
||||
$teacherBySection,
|
||||
@@ -86,19 +74,19 @@ class AssignmentService
|
||||
->unique()
|
||||
->values();
|
||||
|
||||
$semesterMeta = $this->firstNonEmpty([
|
||||
$semesterMeta = $this->metaService->firstNonEmpty([
|
||||
$teacherRows->pluck('semester')->first(fn ($v) => filled($v)),
|
||||
$studentRows->pluck('semester')->first(fn ($v) => filled($v)),
|
||||
$currentSemester,
|
||||
]);
|
||||
|
||||
$schoolYearMeta = $this->firstNonEmpty([
|
||||
$schoolYearMeta = $this->metaService->firstNonEmpty([
|
||||
$teacherRows->pluck('school_year')->first(fn ($v) => filled($v)),
|
||||
$studentRows->pluck('school_year')->first(fn ($v) => filled($v)),
|
||||
$currentSchoolYear,
|
||||
]);
|
||||
|
||||
$descriptionMeta = $this->firstNonEmpty([
|
||||
$descriptionMeta = $this->metaService->firstNonEmpty([
|
||||
$teacherRows->pluck('description')->first(fn ($v) => filled($v)),
|
||||
$studentRows->pluck('description')->first(fn ($v) => filled($v)),
|
||||
'',
|
||||
@@ -141,7 +129,7 @@ class AssignmentService
|
||||
|
||||
return [
|
||||
'classSections' => $classSections,
|
||||
'schoolYears' => $this->getSchoolYears($currentSchoolYear),
|
||||
'schoolYears' => $this->metaService->getSchoolYears($currentSchoolYear),
|
||||
'schoolYear' => $selectedYear,
|
||||
'selectedYear' => $selectedYear,
|
||||
'selectedSemester' => $selectedSemester,
|
||||
@@ -171,8 +159,8 @@ class AssignmentService
|
||||
$currentSemester = $this->getCurrentSemester();
|
||||
$currentSchoolYear = $this->getCurrentSchoolYear();
|
||||
|
||||
$teacherClasses = $this->loadTeacherClasses();
|
||||
$studentClasses = $this->loadStudentClasses();
|
||||
$teacherClasses = $this->dataLoader->loadTeacherClasses();
|
||||
$studentClasses = $this->dataLoader->loadStudentClasses();
|
||||
|
||||
$teacherBySection = $teacherClasses->groupBy('class_section_id');
|
||||
$studentBySection = $studentClasses->groupBy('class_section_id');
|
||||
@@ -183,7 +171,7 @@ class AssignmentService
|
||||
->filter()
|
||||
->values();
|
||||
|
||||
$sectionNames = $this->getSectionNamesMap($allSectionIds->all());
|
||||
$sectionNames = $this->sectionService->getSectionNamesMap($allSectionIds->all());
|
||||
|
||||
$classSections = $allSectionIds->map(function ($sectionId) use (
|
||||
$teacherBySection,
|
||||
@@ -213,19 +201,19 @@ class AssignmentService
|
||||
->unique()
|
||||
->values();
|
||||
|
||||
$semesterMeta = $this->firstNonEmpty([
|
||||
$semesterMeta = $this->metaService->firstNonEmpty([
|
||||
$teacherRows->pluck('semester')->first(fn ($v) => filled($v)),
|
||||
$studentRows->pluck('semester')->first(fn ($v) => filled($v)),
|
||||
$currentSemester,
|
||||
]);
|
||||
|
||||
$schoolYearMeta = $this->firstNonEmpty([
|
||||
$schoolYearMeta = $this->metaService->firstNonEmpty([
|
||||
$teacherRows->pluck('school_year')->first(fn ($v) => filled($v)),
|
||||
$studentRows->pluck('school_year')->first(fn ($v) => filled($v)),
|
||||
$currentSchoolYear,
|
||||
]);
|
||||
|
||||
$descriptionMeta = $this->firstNonEmpty([
|
||||
$descriptionMeta = $this->metaService->firstNonEmpty([
|
||||
$teacherRows->pluck('description')->first(fn ($v) => filled($v)),
|
||||
$studentRows->pluck('description')->first(fn ($v) => filled($v)),
|
||||
'',
|
||||
@@ -272,82 +260,4 @@ class AssignmentService
|
||||
'school_year' => $currentSchoolYear,
|
||||
];
|
||||
}
|
||||
|
||||
protected function loadTeacherClasses(?string $schoolYear = null): Collection
|
||||
{
|
||||
return $this->teacherClassModel
|
||||
->newQuery()
|
||||
->with([
|
||||
'teacher:id,firstname,lastname',
|
||||
])
|
||||
->when(filled($schoolYear), fn ($q) => $q->where('school_year', $schoolYear))
|
||||
->get()
|
||||
->map(function ($row) {
|
||||
$row->teacher_full_name = trim(
|
||||
((string) optional($row->teacher)->firstname) . ' ' .
|
||||
((string) optional($row->teacher)->lastname)
|
||||
);
|
||||
|
||||
return $row;
|
||||
});
|
||||
}
|
||||
|
||||
protected function loadStudentClasses(?string $schoolYear = null): Collection
|
||||
{
|
||||
return $this->studentClassModel
|
||||
->newQuery()
|
||||
->with([
|
||||
'student:id,firstname,lastname,age,gender,registration_grade,photo_consent,tuition_paid,school_id,is_active',
|
||||
])
|
||||
->whereHas('student', fn ($q) => $q->where('is_active', 1))
|
||||
->when(filled($schoolYear), fn ($q) => $q->where('school_year', $schoolYear))
|
||||
->get();
|
||||
}
|
||||
|
||||
protected function getSectionNamesMap(array $sectionIds): array
|
||||
{
|
||||
if (empty($sectionIds)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->classSectionModel
|
||||
->newQuery()
|
||||
->whereIn('id', $sectionIds)
|
||||
->get()
|
||||
->mapWithKeys(function ($section) {
|
||||
$name = $section->section_name ?? $section->name ?? '';
|
||||
return [(int) $section->id => (string) $name];
|
||||
})
|
||||
->all();
|
||||
}
|
||||
|
||||
protected function getSchoolYears(string $fallbackYear = ''): array
|
||||
{
|
||||
$years = DB::table('teacher_class')
|
||||
->selectRaw('DISTINCT school_year')
|
||||
->whereNotNull('school_year')
|
||||
->orderByDesc('school_year')
|
||||
->pluck('school_year')
|
||||
->map(fn ($year) => (string) $year)
|
||||
->filter()
|
||||
->values()
|
||||
->all();
|
||||
|
||||
if (empty($years) && $fallbackYear !== '') {
|
||||
$years[] = $fallbackYear;
|
||||
}
|
||||
|
||||
return $years;
|
||||
}
|
||||
|
||||
protected function firstNonEmpty(array $values): mixed
|
||||
{
|
||||
foreach ($values as $value) {
|
||||
if (filled($value)) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user