add test batches

This commit is contained in:
root
2026-06-08 23:45:55 -04:00
parent c792b8be05
commit 8d4d610b82
1480 changed files with 22587 additions and 10762 deletions
+73 -117
View File
@@ -2,13 +2,13 @@
namespace App\Models;
use App\Models\BaseModel;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
class AttendanceData extends BaseModel
{
protected $table = 'attendance_data';
public $timestamps = true; // uses created_at / updated_at
protected $fillable = [
@@ -30,12 +30,12 @@ class AttendanceData extends BaseModel
];
protected $casts = [
'class_id' => 'integer',
'class_id' => 'integer',
'class_section_id' => 'integer',
'school_id' => 'integer',
'student_id' => 'integer',
'modified_by' => 'integer',
'date' => 'datetime', // ✅ DATETIME column
'school_id' => 'integer',
'student_id' => 'integer',
'modified_by' => 'integer',
'date' => 'datetime', // ✅ DATETIME column
];
/* =========================
@@ -59,7 +59,6 @@ class AttendanceData extends BaseModel
$day = substr($day, 0, 10); // YYYY-MM-DD
$start = Carbon::parse($day)->startOfDay();
$endEx = Carbon::parse($day)->addDay()->startOfDay(); // exclusive
return [$start, $endEx];
}
@@ -88,7 +87,6 @@ class AttendanceData extends BaseModel
public static function updateAttendance(int $attendanceId, array $data): bool
{
$affected = static::query()->whereKey($attendanceId)->update($data);
return $affected >= 0;
}
@@ -115,7 +113,7 @@ class AttendanceData extends BaseModel
$q = static::query();
static::applyReportedFilter($q);
[$start] = static::dayRange($startDate);
[$start, ] = static::dayRange($startDate);
if ($endDate) {
[, $endEx] = static::dayRange($endDate); // end exclusive of next day
@@ -136,19 +134,13 @@ class AttendanceData extends BaseModel
->where('date', '>=', $start)
->where('date', '<', $endEx)
->where(function ($w) {
$w->whereIn(DB::raw('UPPER(TRIM(status))'), ['ABSENT', 'ABS', 'A'])
->orWhereRaw("UPPER(TRIM(status)) LIKE 'ABS%'");
$w->whereIn(DB::raw("UPPER(TRIM(status))"), ['ABSENT', 'ABS', 'A'])
->orWhereRaw("UPPER(TRIM(status)) LIKE 'ABS%'");
});
if ($classSectionId !== null) {
$q->where('class_section_id', $classSectionId);
}
if ($schoolYear !== null) {
$q->where('school_year', $schoolYear);
}
if ($semester !== null) {
$q->where('semester', $semester);
}
if ($classSectionId !== null) $q->where('class_section_id', $classSectionId);
if ($schoolYear !== null) $q->where('school_year', $schoolYear);
if ($semester !== null) $q->where('semester', $semester);
return $q->get();
}
@@ -161,19 +153,13 @@ class AttendanceData extends BaseModel
->where('date', '>=', $start)
->where('date', '<', $endEx)
->where(function ($w) {
$w->whereIn(DB::raw('UPPER(TRIM(status))'), ['LATE', 'L'])
->orWhereRaw("UPPER(TRIM(status)) LIKE 'LATE%'");
$w->whereIn(DB::raw("UPPER(TRIM(status))"), ['LATE', 'L'])
->orWhereRaw("UPPER(TRIM(status)) LIKE 'LATE%'");
});
if ($classSectionId !== null) {
$q->where('class_section_id', $classSectionId);
}
if ($schoolYear !== null) {
$q->where('school_year', $schoolYear);
}
if ($semester !== null) {
$q->where('semester', $semester);
}
if ($classSectionId !== null) $q->where('class_section_id', $classSectionId);
if ($schoolYear !== null) $q->where('school_year', $schoolYear);
if ($semester !== null) $q->where('semester', $semester);
return $q->get();
}
@@ -185,8 +171,8 @@ class AttendanceData extends BaseModel
->where('school_year', $schoolYear)
->where('semester', $semester)
->where(function ($w) {
$w->whereIn(DB::raw('UPPER(TRIM(status))'), ['ABSENT', 'ABS', 'A'])
->orWhereRaw("UPPER(TRIM(status)) LIKE 'ABS%'");
$w->whereIn(DB::raw("UPPER(TRIM(status))"), ['ABSENT', 'ABS', 'A'])
->orWhereRaw("UPPER(TRIM(status)) LIKE 'ABS%'");
})
->orderByDesc('date')
->get();
@@ -199,26 +185,22 @@ class AttendanceData extends BaseModel
public static function getStudentAttendance(
int $studentId,
?string $startDate = null,
?string $endDate = null,
?string $endDate = null,
?string $schoolYear = null,
?string $semester = null
?string $semester = null
) {
$q = static::query()->where('student_id', $studentId);
if (! empty($startDate)) {
[$start] = static::dayRange($startDate);
if (!empty($startDate)) {
[$start, ] = static::dayRange($startDate);
$q->where('date', '>=', $start);
}
if (! empty($endDate)) {
if (!empty($endDate)) {
[, $endEx] = static::dayRange($endDate);
$q->where('date', '<', $endEx);
}
if (! empty($schoolYear)) {
$q->where('school_year', $schoolYear);
}
if (! empty($semester)) {
$q->where('semester', $semester);
}
if (!empty($schoolYear)) $q->where('school_year', $schoolYear);
if (!empty($semester)) $q->where('semester', $semester);
return $q->orderByDesc('date')->get();
}
@@ -243,13 +225,13 @@ class AttendanceData extends BaseModel
public static function unreportedTermRows(array $studentIds, string $schoolYear, ?string $semester)
{
$q = static::query()
->select(['id', 'student_id', 'class_id', 'class_section_id', 'date', 'status', 'reason', 'school_year', 'semester', 'is_reported'])
->select(['id','student_id','class_id','class_section_id','date','status','reason','school_year','semester','is_reported'])
->where('school_year', $schoolYear);
if ($semester !== null && $semester !== '') {
$q->where('semester', $semester);
}
if (! empty($studentIds)) {
if (!empty($studentIds)) {
$q->whereIn('student_id', array_map('intval', $studentIds));
}
@@ -265,30 +247,26 @@ class AttendanceData extends BaseModel
public static function getUnreportedAbsencesAndLates(?string $schoolYear = null, ?string $semester = null): array
{
$q = static::query()
->select(['id', 'student_id', 'class_id', 'class_section_id', 'date', 'status', 'reason', 'school_year', 'semester', 'is_reported']);
->select(['id','student_id','class_id','class_section_id','date','status','reason','school_year','semester','is_reported']);
static::applyUnreportedFilter($q);
// Not notified yet: supports 0/'0'/NULL and string enums like 'yes'
$q->where(function ($w) {
$w->whereNull('is_notified')
->orWhere('is_notified', 0)
->orWhere('is_notified', '0')
->orWhereRaw("LOWER(TRIM(COALESCE(is_notified, ''))) != 'yes'");
->orWhere('is_notified', 0)
->orWhere('is_notified', '0')
->orWhereRaw("LOWER(TRIM(COALESCE(is_notified, ''))) != 'yes'");
});
if (! empty($schoolYear)) {
$q->where('school_year', $schoolYear);
}
if (! empty($semester)) {
$q->where('semester', $semester);
}
if (!empty($schoolYear)) $q->where('school_year', $schoolYear);
if (!empty($semester)) $q->where('semester', $semester);
// Status filter: exact + prefixes (ABS_3, LATE_2, etc.)
$q->where(function ($w) {
$w->whereIn(DB::raw('UPPER(TRIM(status))'), ['ABS', 'ABSENT', 'LATE', 'A', 'L'])
->orWhereRaw("UPPER(TRIM(status)) LIKE 'ABS%'")
->orWhereRaw("UPPER(TRIM(status)) LIKE 'LATE%'");
$w->whereIn(DB::raw("UPPER(TRIM(status))"), ['ABS','ABSENT','LATE','A','L'])
->orWhereRaw("UPPER(TRIM(status)) LIKE 'ABS%'")
->orWhereRaw("UPPER(TRIM(status)) LIKE 'LATE%'");
});
$rows = $q->orderBy('student_id')->orderByDesc('date')->get()->toArray();
@@ -297,27 +275,27 @@ class AttendanceData extends BaseModel
foreach ($rows as $r) {
$sid = (int) ($r['student_id'] ?? 0);
if (! isset($out[$sid])) {
if (!isset($out[$sid])) {
$out[$sid] = [
'absences' => [],
'lates' => [],
'counts' => ['absences' => 0, 'lates' => 0, 'total' => 0],
'lates' => [],
'counts' => ['absences' => 0, 'lates' => 0, 'total' => 0],
];
}
$st = strtoupper(trim((string) ($r['status'] ?? '')));
$isAbs = ($st === 'ABS' || $st === 'ABSENT' || $st === 'A' || str_starts_with($st, 'ABS'));
$st = strtoupper(trim((string)($r['status'] ?? '')));
$isAbs = ($st === 'ABS' || $st === 'ABSENT' || $st === 'A' || str_starts_with($st, 'ABS'));
$isLate = ($st === 'LATE' || $st === 'L' || str_starts_with($st, 'LATE'));
$rec = [
'id' => $r['id'] ?? null,
'date' => $r['date'] ?? null,
'status' => $r['status'] ?? null,
'reason' => $r['reason'] ?? null,
'class_id' => $r['class_id'] ?? null,
'id' => $r['id'] ?? null,
'date' => $r['date'] ?? null,
'status' => $r['status'] ?? null,
'reason' => $r['reason'] ?? null,
'class_id' => $r['class_id'] ?? null,
'class_section_id' => $r['class_section_id'] ?? null,
'school_year' => $r['school_year'] ?? null,
'semester' => $r['semester'] ?? null,
'school_year' => $r['school_year'] ?? null,
'semester' => $r['semester'] ?? null,
];
if ($isAbs) {
@@ -351,16 +329,12 @@ class AttendanceData extends BaseModel
->where('date', '>=', $start)
->where('date', '<', $endEx);
if (! empty($semester)) {
$q->where('semester', $semester);
}
if (! empty($schoolYear)) {
$q->where('school_year', $schoolYear);
}
if (!empty($semester)) $q->where('semester', $semester);
if (!empty($schoolYear)) $q->where('school_year', $schoolYear);
$affected = $q->update([
'is_notified' => 'yes',
'updated_at' => now(),
'updated_at' => now(),
]);
return $affected >= 0;
@@ -375,20 +349,14 @@ class AttendanceData extends BaseModel
return is_string($d) && preg_match('/^\d{4}-\d{2}-\d{2}$/', $d);
})));
if (empty($dates)) {
return true;
}
if (empty($dates)) return true;
$q = static::query()
->where('student_id', $studentId)
->whereRaw("LOWER(TRIM(status)) IN ('absent','late')");
if (! empty($semester)) {
$q->where('semester', $semester);
}
if (! empty($schoolYear)) {
$q->where('school_year', $schoolYear);
}
if (!empty($semester)) $q->where('semester', $semester);
if (!empty($schoolYear)) $q->where('school_year', $schoolYear);
// OR day ranges instead of DATE(date)
$q->where(function ($w) use ($dates) {
@@ -402,17 +370,13 @@ class AttendanceData extends BaseModel
});
$payload = [
'updated_at' => now(),
'updated_at' => now(),
'is_notified' => 'yes',
];
$cols = static::reportedColumns();
if (! empty($cols['is_reported'])) {
$payload['is_reported'] = 'yes';
}
if (! empty($cols['reported'])) {
$payload['reported'] = 1;
}
if (!empty($cols['is_reported'])) $payload['is_reported'] = 'yes';
if (!empty($cols['reported'])) $payload['reported'] = 1;
$affected = $q->update($payload);
@@ -430,9 +394,7 @@ class AttendanceData extends BaseModel
?string $semester = null,
?string $schoolYear = null
): array {
if ($studentId <= 0 || empty($statuses)) {
return [];
}
if ($studentId <= 0 || empty($statuses)) return [];
$statuses = array_map('strtolower', $statuses);
@@ -441,7 +403,7 @@ class AttendanceData extends BaseModel
: now()->toDateString();
$startDay = Carbon::parse($day)->subDays(max(0, $lookbackDays))->toDateString();
[$start] = static::dayRange($startDay);
[$start, ] = static::dayRange($startDay);
[, $endEx] = static::dayRange($day);
$q = static::query()
@@ -449,21 +411,17 @@ class AttendanceData extends BaseModel
->where('student_id', $studentId)
->where('date', '>=', $start)
->where('date', '<', $endEx)
->whereIn(DB::raw('LOWER(TRIM(status))'), $statuses);
->whereIn(DB::raw("LOWER(TRIM(status))"), $statuses);
static::applyUnreportedFilter($q);
if (! empty($semester)) {
$q->where('semester', $semester);
}
if (! empty($schoolYear)) {
$q->where('school_year', $schoolYear);
}
if (!empty($semester)) $q->where('semester', $semester);
if (!empty($schoolYear)) $q->where('school_year', $schoolYear);
$rows = $q->orderByDesc('date')->pluck('date')->all();
return array_values(array_unique(array_map(static function ($d) {
return substr((string) $d, 0, 10); // YYYY-MM-DD
return substr((string)$d, 0, 10); // YYYY-MM-DD
}, $rows)));
}
@@ -474,15 +432,13 @@ class AttendanceData extends BaseModel
private static function reportedColumns(): array
{
static $cache = null;
if ($cache !== null) {
return $cache;
}
if ($cache !== null) return $cache;
$table = (new static)->getTable();
$cache = [
'is_reported' => static::columnExists($table, 'is_reported'),
'reported' => static::columnExists($table, 'reported'),
'reported' => static::columnExists($table, 'reported'),
];
return $cache;
@@ -508,16 +464,16 @@ class AttendanceData extends BaseModel
{
$cols = static::reportedColumns();
if (! empty($cols['is_reported'])) {
if (!empty($cols['is_reported'])) {
$q->where(function ($w) {
$w->whereNull('is_reported')
->orWhere('is_reported', 0)
->orWhere('is_reported', '0')
->orWhereRaw("LOWER(TRIM(COALESCE(is_reported, ''))) = 'no'");
->orWhere('is_reported', 0)
->orWhere('is_reported', '0')
->orWhereRaw("LOWER(TRIM(COALESCE(is_reported, ''))) = 'no'");
});
}
if (! empty($cols['reported'])) {
if (!empty($cols['reported'])) {
$q->whereRaw("(LOWER(TRIM(COALESCE(reported, ''))) NOT IN ('yes','1','true','y','on'))");
}
}
@@ -529,8 +485,8 @@ class AttendanceData extends BaseModel
{
$q->where(function ($w) {
$w->where('is_reported', 1)
->orWhere('is_reported', '1')
->orWhereRaw("LOWER(TRIM(COALESCE(is_reported, ''))) = 'yes'");
->orWhere('is_reported', '1')
->orWhereRaw("LOWER(TRIM(COALESCE(is_reported, ''))) = 'yes'");
});
}
}