update policy and fix other issues
This commit is contained in:
@@ -297,6 +297,8 @@ final class EnrollmentTransitionService
|
||||
'initial_transition_applied'
|
||||
);
|
||||
|
||||
service('studentYearStatus')->upsert($studentId, $targetSchoolYear, false);
|
||||
|
||||
if ((int) ($evaluation['assigned_class_section_id'] ?? 0) > 0) {
|
||||
$this->upsertStudentClass($studentId, (int) $evaluation['assigned_class_section_id'], $targetSchoolYear, $performedBy);
|
||||
}
|
||||
|
||||
@@ -279,6 +279,8 @@ final class SchoolYearClosingService
|
||||
'next_school_year_id' => $targetYearId,
|
||||
]);
|
||||
$targetName = (string) ($target['name'] ?? '');
|
||||
$sourceName = (string) ($this->requireYear($sourceYearId)['name'] ?? '');
|
||||
service('studentYearStatus')->markReturningStudentsForTargetYear($sourceName, $targetName);
|
||||
$this->managementService->syncConfigurationForYear($targetYearId);
|
||||
$this->syncActiveYearSession($targetName);
|
||||
$this->managementService->log($sourceYearId, SchoolYearStatus::CLOSING, SchoolYearStatus::CLOSED, 'closing_complete', $userId, [
|
||||
|
||||
@@ -197,6 +197,14 @@ final class SchoolYearManagementService
|
||||
'updated_by' => $userId,
|
||||
'carry_over_balance_behavior' => $year['carry_over_balance_behavior'],
|
||||
]);
|
||||
$previousYearId = (int) ($year['previous_school_year_id'] ?? 0);
|
||||
if ($previousYearId > 0) {
|
||||
$previousYear = $this->schoolYearModel->find($previousYearId);
|
||||
service('studentYearStatus')->markReturningStudentsForTargetYear(
|
||||
(string) ($previousYear['name'] ?? ''),
|
||||
(string) ($year['name'] ?? '')
|
||||
);
|
||||
}
|
||||
$this->syncConfigurationFromSchoolYear($year);
|
||||
$this->syncActiveYearSession((string) $year['name']);
|
||||
$this->log($id, $from, SchoolYearStatus::ACTIVE, 'activate', $userId);
|
||||
|
||||
@@ -72,6 +72,34 @@ class StudentYearStatusService
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark students who existed in a source school year as returning in the target year.
|
||||
*
|
||||
* This is intentionally idempotent so school-year activation/closing and repair
|
||||
* migrations can call it safely.
|
||||
*/
|
||||
public function markReturningStudentsForTargetYear(string $sourceSchoolYear, string $targetSchoolYear): int
|
||||
{
|
||||
$sourceSchoolYear = trim($sourceSchoolYear);
|
||||
$targetSchoolYear = trim($targetSchoolYear);
|
||||
|
||||
if (
|
||||
! preg_match('/^\d{4}-\d{4}$/', $sourceSchoolYear)
|
||||
|| ! preg_match('/^\d{4}-\d{4}$/', $targetSchoolYear)
|
||||
|| $sourceSchoolYear === $targetSchoolYear
|
||||
|| ! $this->db->tableExists('student_year_status')
|
||||
) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$studentIds = $this->returningStudentIds($sourceSchoolYear);
|
||||
foreach ($studentIds as $studentId) {
|
||||
$this->upsert($studentId, $targetSchoolYear, false);
|
||||
}
|
||||
|
||||
return count($studentIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array<string, mixed>> $students
|
||||
*/
|
||||
@@ -144,6 +172,37 @@ class StudentYearStatusService
|
||||
return $flags;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<int>
|
||||
*/
|
||||
private function returningStudentIds(string $sourceSchoolYear): array
|
||||
{
|
||||
$ids = [];
|
||||
|
||||
foreach (['student_year_status', 'student_class', 'enrollments'] as $table) {
|
||||
if (! $this->db->tableExists($table) || ! $this->db->fieldExists('school_year', $table)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$rows = $this->db->table($table)
|
||||
->select('student_id')
|
||||
->where('school_year', $sourceSchoolYear)
|
||||
->where('student_id IS NOT NULL', null, false)
|
||||
->groupBy('student_id')
|
||||
->get()
|
||||
->getResultArray();
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$studentId = (int) ($row['student_id'] ?? 0);
|
||||
if ($studentId > 0) {
|
||||
$ids[$studentId] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return array_keys($ids);
|
||||
}
|
||||
|
||||
public function activeSchoolYear(): ?string
|
||||
{
|
||||
if ($this->db->tableExists('school_years')) {
|
||||
|
||||
Reference in New Issue
Block a user