update controllers logic
This commit is contained in:
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace App\Services\ClassProgress;
|
||||
|
||||
use App\Models\Configuration;
|
||||
use App\Models\SubjectCurriculum;
|
||||
use App\Services\SemesterRangeService;
|
||||
|
||||
class ClassProgressMetaService
|
||||
{
|
||||
@@ -16,22 +18,126 @@ class ClassProgressMetaService
|
||||
return (array) config('progress.status_options', []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fallback: next N Sundays from today (CI `buildUpcomingSundayOptions`).
|
||||
*
|
||||
* @return list<string>
|
||||
*/
|
||||
public function sundayOptions(int $count = 12): array
|
||||
{
|
||||
$start = now();
|
||||
if ((int) $start->format('w') !== 0) {
|
||||
$start = $start->next('Sunday');
|
||||
return $this->buildUpcomingSundayOptions($count);
|
||||
}
|
||||
|
||||
/**
|
||||
* CI-aligned Sundays within semester/school-year range when configuration allows.
|
||||
*
|
||||
* @return list<string>
|
||||
*/
|
||||
public function sundayOptionsAlignedWithCi(int $count = 12): array
|
||||
{
|
||||
$range = $this->resolveProgressDateRange();
|
||||
if ($range === null) {
|
||||
return $this->buildUpcomingSundayOptions($count);
|
||||
}
|
||||
|
||||
[$rangeStart, $rangeEnd] = $range;
|
||||
|
||||
try {
|
||||
$start = new \DateTime($rangeStart);
|
||||
$end = new \DateTime($rangeEnd);
|
||||
} catch (\Exception $e) {
|
||||
return $this->buildUpcomingSundayOptions($count);
|
||||
}
|
||||
|
||||
if ($end < $start) {
|
||||
[$start, $end] = [$end, $start];
|
||||
}
|
||||
|
||||
$weekday = (int) $start->format('w');
|
||||
if ($weekday !== 0) {
|
||||
$start->modify('next sunday');
|
||||
}
|
||||
|
||||
$options = [];
|
||||
while ($start <= $end) {
|
||||
$options[] = $start->format('Y-m-d');
|
||||
$start->modify('+7 days');
|
||||
}
|
||||
|
||||
return $options !== [] ? $options : $this->buildUpcomingSundayOptions($count);
|
||||
}
|
||||
|
||||
/** CI `pickDefaultWeekStart`. */
|
||||
public function pickDefaultWeekStart(array $options): string
|
||||
{
|
||||
if ($options === []) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$today = date('Y-m-d');
|
||||
$default = '';
|
||||
foreach ($options as $option) {
|
||||
if ($option <= $today) {
|
||||
$default = $option;
|
||||
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return $default !== '' ? $default : $options[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
private function buildUpcomingSundayOptions(int $count): array
|
||||
{
|
||||
$start = new \DateTime('today');
|
||||
$weekday = (int) $start->format('w');
|
||||
if ($weekday !== 0) {
|
||||
$start->modify('next sunday');
|
||||
}
|
||||
|
||||
$options = [];
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$options[] = $start->format('Y-m-d');
|
||||
$start = $start->addDays(7);
|
||||
$start->modify('+7 days');
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* CI `resolveProgressDateRange`.
|
||||
*
|
||||
* @return array{0:string,1:string}|null
|
||||
*/
|
||||
private function resolveProgressDateRange(): ?array
|
||||
{
|
||||
$schoolYear = (string) (Configuration::getConfig('school_year') ?? '');
|
||||
if ($schoolYear === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var SemesterRangeService $svc */
|
||||
$svc = app(SemesterRangeService::class);
|
||||
|
||||
$semester = $svc->normalizeSemester((string) (Configuration::getConfig('semester') ?? ''));
|
||||
if ($semester === '') {
|
||||
$semester = $svc->getSemesterForDate();
|
||||
}
|
||||
|
||||
if ($semester !== '') {
|
||||
$range = $svc->getSemesterRange($schoolYear, $semester);
|
||||
if ($range !== null) {
|
||||
return $range;
|
||||
}
|
||||
}
|
||||
|
||||
return $svc->getSchoolYearRange($schoolYear);
|
||||
}
|
||||
|
||||
public function curriculumOptions(?int $classId): array
|
||||
{
|
||||
if (!$classId) {
|
||||
|
||||
Reference in New Issue
Block a user