fix close year and ignore not active students
Deploy to Shared Hosting / Shared hosting deploy (push) Failing after 48s
Tests / PHPUnit (push) Successful in 1m17s

This commit is contained in:
root
2026-08-20 17:27:28 -04:00
parent 2b0206e7f2
commit 127098b87c
4 changed files with 346 additions and 98 deletions
+74 -48
View File
@@ -304,6 +304,12 @@ class ParentController extends BaseController
foreach ($students as &$student) {
$studentId = $student['id'];
$student['age'] = $this->calculateAgeAsOfSchoolYearStartYear($student['dob'] ?? null, $selectedYear);
$student['allergies'] = $this->allergyModel
->where('student_id', (int) $studentId)
->findColumn('allergy') ?? [];
$student['medical_conditions'] = $this->medicalConditionModel
->where('student_id', (int) $studentId)
->findColumn('condition_name') ?? [];
// Get class section info (can be multiple sections like Grade + Arabic)
$classSections = $this->studentClassModel->getClassSectionsByStudentId($studentId, $selectedYear, true);
@@ -816,66 +822,38 @@ class ParentController extends BaseController
continue;
}
$firstName = $this->normalizeEnrollmentStudentName((string) ($fields['firstname'] ?? ''));
$lastName = $this->normalizeEnrollmentStudentName((string) ($fields['lastname'] ?? ''));
$dob = trim((string) ($fields['dob'] ?? ''));
$gender = trim((string) ($fields['gender'] ?? ''));
$registrationGrade = trim((string) ($fields['registration_grade'] ?? ''));
$studentLabel = trim((string) ($existing['firstname'] ?? '') . ' ' . (string) ($existing['lastname'] ?? ''))
?: 'Student ID ' . $studentId;
$photoConsent = (string) ($fields['photo_consent'] ?? '');
$studentLabel = trim($firstName . ' ' . $lastName) ?: 'Student ID ' . $studentId;
try {
$this->validateNames($firstName);
$this->validateNames($lastName);
} catch (InvalidArgumentException $e) {
$errors[] = $studentLabel . ': ' . $e->getMessage();
continue;
}
if (! in_array($gender, ['Male', 'Female'], true)) {
$errors[] = $studentLabel . ': gender is required.';
continue;
}
if ($photoConsent !== '0' && $photoConsent !== '1') {
$errors[] = $studentLabel . ': photo consent is required.';
continue;
}
if ($registrationGrade === '' || mb_strlen($registrationGrade) > 50) {
$errors[] = $studentLabel . ': registration grade is required.';
continue;
}
$dobObj = \DateTimeImmutable::createFromFormat('!Y-m-d', $dob, new \DateTimeZone('UTC'));
$dobErrors = \DateTimeImmutable::getLastErrors();
$dobWarningCount = is_array($dobErrors) ? (int) ($dobErrors['warning_count'] ?? 0) : 0;
$dobErrorCount = is_array($dobErrors) ? (int) ($dobErrors['error_count'] ?? 0) : 0;
if ($dobObj === false || $dobWarningCount > 0 || $dobErrorCount > 0) {
$errors[] = $studentLabel . ': date of birth must use YYYY-MM-DD format.';
continue;
}
$validation = $this->validateDobAge(
$dob,
$this->registrationMinimumAgeDeadline($schoolYear),
5,
18,
$this->schoolYearAgeDeadline($schoolYear)
$medicalConditions = $this->normalizeEnrollmentHealthSelections(
$fields['medical_conditions'] ?? [],
(string) ($fields['medical_condition_other'] ?? '')
);
if (! $validation['isValid']) {
$errors[] = $studentLabel . ': ' . $validation['message'] . '.';
$allergies = $this->normalizeEnrollmentHealthSelections(
$fields['allergies'] ?? [],
(string) ($fields['allergy_other'] ?? '')
);
if ($medicalConditions === []) {
$errors[] = $studentLabel . ': medical conditions are required.';
continue;
}
if ($allergies === []) {
$errors[] = $studentLabel . ': allergies are required.';
continue;
}
$updates[$studentId] = [
'firstname' => $firstName,
'lastname' => $lastName,
'dob' => $dobObj->format('Y-m-d'),
'age' => $this->calculateAgeAsOfSchoolYearStartYear($dobObj->format('Y-m-d'), $schoolYear),
'gender' => $gender,
'registration_grade' => $registrationGrade,
'photo_consent' => (int) $photoConsent,
'medical_conditions' => $medicalConditions,
'allergies' => $allergies,
];
}
@@ -884,14 +862,62 @@ class ParentController extends BaseController
}
foreach ($updates as $studentId => $payload) {
if (! $this->studentModel->update($studentId, $payload)) {
if (! $this->studentModel->update($studentId, ['photo_consent' => $payload['photo_consent']])) {
$errors[] = 'Student ID ' . $studentId . ': student information could not be updated.';
continue;
}
$this->medicalConditionModel->where('student_id', $studentId)->delete();
foreach ($payload['medical_conditions'] as $condition) {
$this->medicalConditionModel->insert([
'student_id' => $studentId,
'condition_name' => $condition,
]);
}
$this->allergyModel->where('student_id', $studentId)->delete();
foreach ($payload['allergies'] as $allergy) {
$this->allergyModel->insert([
'student_id' => $studentId,
'allergy' => $allergy,
]);
}
}
return $errors;
}
/**
* @param mixed $selected
* @return list<string>
*/
private function normalizeEnrollmentHealthSelections($selected, string $otherText): array
{
$values = [];
foreach ((array) $selected as $value) {
$value = trim((string) $value);
if ($value === '') {
continue;
}
$values[] = $value;
}
$otherText = trim($otherText);
if (in_array('Other', $values, true)) {
$values = array_values(array_filter($values, static fn(string $value): bool => $value !== 'Other'));
if ($otherText !== '') {
$values[] = mb_substr($otherText, 0, 100);
}
}
$unique = [];
foreach ($values as $value) {
$unique[$value] = $value;
}
return array_values($unique);
}
private function normalizeEnrollmentStudentName(string $name): string
{
$name = trim(preg_replace('/\s+/', ' ', $name) ?? '');