Files
alrahma_sunday_school/app/Helpers/student_status_helper.php
root 889c037660
Deploy to Shared Hosting / Shared hosting deploy (push) Failing after 48s
Tests / PHPUnit (push) Failing after 1m22s
fix is_new issue with enrollment fixes
2026-08-20 19:57:25 -04:00

87 lines
3.4 KiB
PHP

<?php
if (!function_exists('student_normalized_enrollment_status')) {
function student_normalized_enrollment_status(array $student, ?string $schoolYear = null): string
{
$studentId = (int)($student['student_id'] ?? $student['id'] ?? 0);
$status = strtolower(trim((string)($student['enrollment_status'] ?? '')));
if ($status === 'withdrawn student' || (int)($student['is_withdrawn'] ?? 0) === 1) {
$status = 'withdrawn';
}
if ($studentId > 0) {
$schoolYear = trim((string)($schoolYear ?? ''));
if ($schoolYear === '') {
try {
$schoolYear = (string)((new \App\Models\ConfigurationModel())->getConfig('school_year') ?? '');
} catch (\Throwable $e) {
$schoolYear = '';
}
}
static $statusCache = [];
$cacheKey = $studentId . ':' . $schoolYear;
if (!array_key_exists($cacheKey, $statusCache)) {
$statusCache[$cacheKey] = '';
if ($schoolYear !== '') {
try {
$row = db_connect()->table('enrollments')
->select('enrollment_status, is_withdrawn')
->where('student_id', $studentId)
->where('school_year', $schoolYear)
->orderBy('updated_at', 'DESC')
->orderBy('enrollment_date', 'DESC')
->orderBy('id', 'DESC')
->get(1)
->getRowArray();
$rowStatus = strtolower(trim((string)($row['enrollment_status'] ?? '')));
if ($rowStatus === 'withdrawn student' || (int)($row['is_withdrawn'] ?? 0) === 1) {
$rowStatus = 'withdrawn';
}
$statusCache[$cacheKey] = $rowStatus;
} catch (\Throwable $e) {
$statusCache[$cacheKey] = '';
}
}
}
if (($statusCache[$cacheKey] ?? '') !== '') {
$status = $statusCache[$cacheKey];
}
}
return $status;
}
}
if (!function_exists('student_is_non_active_for_editing')) {
function student_is_non_active_for_editing(array $student, ?string $schoolYear = null): bool
{
$status = student_normalized_enrollment_status($student, $schoolYear);
if (in_array($status, ['denied', 'withdrawn', 'waitlist'], true)) {
return true;
}
return array_key_exists('is_active', $student) && (int)$student['is_active'] !== 1;
}
}
if (!function_exists('student_enrollment_status_button')) {
function student_enrollment_status_button(array $student, ?string $schoolYear = null): string
{
$status = student_normalized_enrollment_status($student, $schoolYear);
$classes = [
'denied' => 'btn-outline-danger',
'withdrawn' => 'btn-outline-secondary',
'waitlist' => 'btn-outline-warning',
];
if (!isset($classes[$status])) {
return '';
}
return '<div class="mt-1"><span class="btn btn-sm ' . esc($classes[$status], 'attr') . ' disabled py-0 px-2 student-enrollment-status" aria-disabled="true">' . esc(ucwords($status)) . '</span></div>';
}
}