update controllers logic
This commit is contained in:
@@ -4,6 +4,9 @@ namespace App\Services\ClassProgress;
|
||||
|
||||
class ClassProgressRuleService
|
||||
{
|
||||
/** CI `ClassProgressController::CUSTOM_UNIT_ROW_LABEL` — must match teacher form JS. */
|
||||
public const CUSTOM_UNIT_ROW_LABEL = 'Custom';
|
||||
|
||||
public function defaultStatus(): string
|
||||
{
|
||||
return 'on_track';
|
||||
@@ -15,6 +18,12 @@ class ClassProgressRuleService
|
||||
return $values === [] ? null : $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* CI `buildUnitChapterSummary` — Custom / typed chapter rows preserved.
|
||||
*
|
||||
* @param array<int|string, mixed> $unitValues
|
||||
* @param array<int|string, mixed> $chapterValues
|
||||
*/
|
||||
public function buildUnitChapterSummary(array $unitValues, array $chapterValues): ?string
|
||||
{
|
||||
$parts = [];
|
||||
@@ -25,9 +34,12 @@ class ClassProgressRuleService
|
||||
if ($unit === '' && $chapter === '') {
|
||||
continue;
|
||||
}
|
||||
if (strcasecmp($unit, self::CUSTOM_UNIT_ROW_LABEL) === 0 && $chapter !== '') {
|
||||
$unit = self::CUSTOM_UNIT_ROW_LABEL;
|
||||
}
|
||||
$segment = $unit;
|
||||
if ($chapter !== '') {
|
||||
$segment = $segment !== '' ? $segment . ' / ' . $chapter : $chapter;
|
||||
$segment = $segment !== '' ? $unit.' / '.$chapter : $chapter;
|
||||
}
|
||||
if ($segment === '') {
|
||||
continue;
|
||||
@@ -40,9 +52,25 @@ class ClassProgressRuleService
|
||||
}
|
||||
|
||||
$summary = implode(' ; ', $parts);
|
||||
|
||||
return mb_strlen($summary) > 120 ? mb_substr($summary, 0, 120) : $summary;
|
||||
}
|
||||
|
||||
/** CI `hasIslamicUnitSelection`. */
|
||||
public function hasIslamicUnitSelection(array $payload): bool
|
||||
{
|
||||
$unitValues = array_map('trim', (array) ($payload['unit_islamic'] ?? []));
|
||||
$chapterValues = array_map('trim', (array) ($payload['chapter_islamic'] ?? []));
|
||||
$count = max(count($unitValues), count($chapterValues));
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
if (($unitValues[$i] ?? '') !== '' || ($chapterValues[$i] ?? '') !== '') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function ensureWeekEnd(string $weekStart, ?string $weekEnd): string
|
||||
{
|
||||
if ($weekEnd) {
|
||||
@@ -62,4 +90,47 @@ class ClassProgressRuleService
|
||||
{
|
||||
return strtotime($weekEnd) >= strtotime($weekStart);
|
||||
}
|
||||
|
||||
/**
|
||||
* CI `parseUnitChapterSummary` — inverse of {@see buildUnitChapterSummary()} for edit forms.
|
||||
*
|
||||
* @return array{units: list<string>, chapters: list<string>}
|
||||
*/
|
||||
public function parseUnitChapterSummary(string $summary): array
|
||||
{
|
||||
$summary = trim($summary);
|
||||
if ($summary === '') {
|
||||
return ['units' => [], 'chapters' => []];
|
||||
}
|
||||
|
||||
$units = [];
|
||||
$chapters = [];
|
||||
$segments = preg_split('/\s*;\s*/', $summary, -1, PREG_SPLIT_NO_EMPTY);
|
||||
foreach ($segments as $segment) {
|
||||
$segment = trim((string) $segment);
|
||||
if ($segment === '') {
|
||||
continue;
|
||||
}
|
||||
if (preg_match('/^Custom\s*\/\s*(.+)$/iu', $segment, $m)) {
|
||||
$units[] = self::CUSTOM_UNIT_ROW_LABEL;
|
||||
$chapters[] = trim($m[1]);
|
||||
|
||||
continue;
|
||||
}
|
||||
$parts = preg_split('/\s*\/\s*/', $segment, 2);
|
||||
if (count($parts) === 2) {
|
||||
$u = trim($parts[0]);
|
||||
if (strcasecmp($u, self::CUSTOM_UNIT_ROW_LABEL) === 0) {
|
||||
$u = self::CUSTOM_UNIT_ROW_LABEL;
|
||||
}
|
||||
$units[] = $u;
|
||||
$chapters[] = trim($parts[1]);
|
||||
} else {
|
||||
$units[] = $segment;
|
||||
$chapters[] = '';
|
||||
}
|
||||
}
|
||||
|
||||
return ['units' => $units, 'chapters' => $chapters];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user