Files
alrahma_sunday_school/app/Services/StudentYearStatusService.php
T
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

179 lines
4.7 KiB
PHP

<?php
namespace App\Services;
use App\Models\StudentYearStatusModel;
use CodeIgniter\Database\BaseConnection;
class StudentYearStatusService
{
public function __construct(
private BaseConnection $db,
private StudentYearStatusModel $yearStatusModel,
) {
}
public function isNew(int $studentId, string $schoolYear): bool
{
if ($studentId <= 0) {
return true;
}
$schoolYear = trim($schoolYear);
if (! preg_match('/^\d{4}-\d{4}$/', $schoolYear)) {
return true;
}
if (! $this->db->tableExists('student_year_status')) {
return true;
}
$row = $this->yearStatusModel
->where('student_id', $studentId)
->where('school_year', $schoolYear)
->first();
if (! is_array($row)) {
return true;
}
return (int) ($row['is_new'] ?? 1) === 1;
}
public function upsert(int $studentId, string $schoolYear, bool $isNew): void
{
if ($studentId <= 0) {
return;
}
$schoolYear = trim($schoolYear);
if (! preg_match('/^\d{4}-\d{4}$/', $schoolYear) || ! $this->db->tableExists('student_year_status')) {
return;
}
$flag = $isNew ? 1 : 0;
$existing = $this->yearStatusModel
->where('student_id', $studentId)
->where('school_year', $schoolYear)
->first();
if (is_array($existing) && isset($existing['id'])) {
$this->yearStatusModel->update((int) $existing['id'], [
'is_new' => $flag,
]);
return;
}
$this->yearStatusModel->insert([
'student_id' => $studentId,
'school_year' => $schoolYear,
'is_new' => $flag,
]);
}
/**
* @param list<array<string, mixed>> $students
*/
public function attachToStudents(array &$students, string $schoolYear): void
{
$schoolYear = trim($schoolYear);
if ($students === [] || ! preg_match('/^\d{4}-\d{4}$/', $schoolYear)) {
return;
}
$ids = [];
foreach ($students as $student) {
$id = (int) ($student['id'] ?? $student['student_id'] ?? 0);
if ($id > 0) {
$ids[$id] = true;
}
}
$flags = $this->flagsForStudents(array_keys($ids), $schoolYear);
foreach ($students as &$student) {
$id = (int) ($student['id'] ?? $student['student_id'] ?? 0);
if ($id <= 0) {
continue;
}
$student['is_new'] = $flags[$id] ?? 1;
}
unset($student);
}
/**
* @param list<int> $studentIds
* @return array<int, int>
*/
public function flagsForStudents(array $studentIds, string $schoolYear): array
{
$studentIds = array_values(array_unique(array_filter(
array_map('intval', $studentIds),
static fn(int $id): bool => $id > 0
)));
$schoolYear = trim($schoolYear);
if ($studentIds === [] || ! preg_match('/^\d{4}-\d{4}$/', $schoolYear)) {
return [];
}
$flags = [];
foreach ($studentIds as $studentId) {
$flags[$studentId] = 1;
}
if (! $this->db->tableExists('student_year_status')) {
return $flags;
}
$rows = $this->yearStatusModel
->select('student_id, is_new')
->whereIn('student_id', $studentIds)
->where('school_year', $schoolYear)
->findAll();
foreach ($rows as $row) {
$id = (int) ($row['student_id'] ?? 0);
if ($id > 0) {
$flags[$id] = (int) ($row['is_new'] ?? 1) === 1 ? 1 : 0;
}
}
return $flags;
}
public function activeSchoolYear(): ?string
{
if ($this->db->tableExists('school_years')) {
$row = $this->db->table('school_years')
->select('name')
->where('status', 'active')
->orderBy('id', 'DESC')
->get(1)
->getRowArray();
$name = trim((string) ($row['name'] ?? ''));
if (preg_match('/^\d{4}-\d{4}$/', $name)) {
return $name;
}
}
if (! $this->db->tableExists('configuration')) {
return null;
}
$row = $this->db->table('configuration')
->select('config_value')
->where('config_key', 'school_year')
->orderBy('id', 'DESC')
->get(1)
->getRowArray();
$name = trim((string) ($row['config_value'] ?? ''));
return preg_match('/^\d{4}-\d{4}$/', $name) ? $name : null;
}
}