e0dfc3ec82
API CI/CD / Validate (composer + pint) (push) Successful in 3m15s
API CI/CD / Test (PHPUnit) (push) Failing after 5m4s
API CI/CD / Build frontend assets (push) Successful in 1m3s
API CI/CD / Security audit (push) Failing after 49s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
55 lines
1.4 KiB
PHP
55 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Assignment;
|
|
|
|
use App\Models\ClassSection;
|
|
|
|
class AssignmentSectionService
|
|
{
|
|
public function __construct(private ClassSection $classSection) {}
|
|
|
|
public function getSectionNamesMap(array $sectionIds): array
|
|
{
|
|
if (empty($sectionIds)) {
|
|
return [];
|
|
}
|
|
|
|
$map = [];
|
|
|
|
$this->classSection
|
|
->newQuery()
|
|
->whereIn('class_section_id', $sectionIds)
|
|
->get()
|
|
->each(function ($section) use (&$map) {
|
|
$map[(int) $section->class_section_id] = $this->sectionName($section);
|
|
});
|
|
|
|
$missing = collect($sectionIds)
|
|
->map(fn ($id) => (int) $id)
|
|
->reject(fn ($id) => array_key_exists($id, $map))
|
|
->values()
|
|
->all();
|
|
|
|
if (! empty($missing)) {
|
|
$this->classSection
|
|
->newQuery()
|
|
->whereIn('id', $missing)
|
|
->get()
|
|
->each(function ($section) use (&$map) {
|
|
$map[(int) $section->id] = $this->sectionName($section);
|
|
});
|
|
}
|
|
|
|
return $map;
|
|
}
|
|
|
|
private function sectionName(ClassSection $section): string
|
|
{
|
|
return (string) (collect([
|
|
$section->class_section_name ?? null,
|
|
$section->section_name ?? null,
|
|
$section->name ?? null,
|
|
])->first(fn ($value) => trim((string) $value) !== '') ?? '');
|
|
}
|
|
}
|