Fix Pint formatting
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
@@ -26,11 +25,11 @@ class AttendanceTracking extends BaseModel
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'student_id' => 'integer',
|
||||
'date' => 'datetime', // legacy dateFormat = datetime
|
||||
'is_reported' => 'integer',
|
||||
'is_notified' => 'integer',
|
||||
'notif_counter' => 'integer',
|
||||
'student_id' => 'integer',
|
||||
'date' => 'datetime', // legacy dateFormat = datetime
|
||||
'is_reported' => 'integer',
|
||||
'is_notified' => 'integer',
|
||||
'notif_counter' => 'integer',
|
||||
];
|
||||
|
||||
/* ----------------------- Helpers ----------------------- */
|
||||
@@ -42,7 +41,7 @@ class AttendanceTracking extends BaseModel
|
||||
return $date;
|
||||
}
|
||||
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) {
|
||||
return $date . ' 00:00:00';
|
||||
return $date.' 00:00:00';
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -61,6 +60,7 @@ class AttendanceTracking extends BaseModel
|
||||
if ($schoolYear !== null && $schoolYear !== '') {
|
||||
$q->where('school_year', $schoolYear);
|
||||
}
|
||||
|
||||
return $q;
|
||||
}
|
||||
|
||||
@@ -77,8 +77,8 @@ class AttendanceTracking extends BaseModel
|
||||
$y = (int) $d->format('Y');
|
||||
|
||||
$semester = ($m >= 8 && $m <= 12) ? 'Fall' : 'Spring';
|
||||
$startY = ($m >= 8) ? $y : $y - 1;
|
||||
$endY = $startY + 1;
|
||||
$startY = ($m >= 8) ? $y : $y - 1;
|
||||
$endY = $startY + 1;
|
||||
|
||||
return [$semester, sprintf('%d-%d', $startY, $endY)];
|
||||
}
|
||||
@@ -89,6 +89,7 @@ class AttendanceTracking extends BaseModel
|
||||
$day = substr($ymd, 0, 10);
|
||||
$start = Carbon::parse($day, 'UTC')->startOfDay();
|
||||
$endEx = Carbon::parse($day, 'UTC')->addDay()->startOfDay(); // exclusive
|
||||
|
||||
return [$start, $endEx];
|
||||
}
|
||||
|
||||
@@ -111,19 +112,19 @@ class AttendanceTracking extends BaseModel
|
||||
|
||||
if ($semester === null || $schoolYear === null) {
|
||||
[$sem, $yr] = static::deriveTermFromDate($dt);
|
||||
$semester = $semester ?? $sem;
|
||||
$semester = $semester ?? $sem;
|
||||
$schoolYear = $schoolYear ?? $yr;
|
||||
}
|
||||
|
||||
$row = static::create([
|
||||
'student_id' => $studentId,
|
||||
'date' => $dt,
|
||||
'is_reported' => $isExcused ? 1 : 0,
|
||||
'reason' => $reason,
|
||||
'is_notified' => 0,
|
||||
'notif_counter' => 0,
|
||||
'semester' => $semester,
|
||||
'school_year' => $schoolYear,
|
||||
'student_id' => $studentId,
|
||||
'date' => $dt,
|
||||
'is_reported' => $isExcused ? 1 : 0,
|
||||
'reason' => $reason,
|
||||
'is_notified' => 0,
|
||||
'notif_counter' => 0,
|
||||
'semester' => $semester,
|
||||
'school_year' => $schoolYear,
|
||||
]);
|
||||
|
||||
return (int) $row->id;
|
||||
@@ -144,7 +145,7 @@ class AttendanceTracking extends BaseModel
|
||||
|
||||
if ($startDate) {
|
||||
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $startDate)) {
|
||||
[$start, ] = static::dayRange($startDate);
|
||||
[$start] = static::dayRange($startDate);
|
||||
$q->where('date', '>=', $start);
|
||||
} else {
|
||||
$q->where('date', '>=', static::normalizeDateTime($startDate));
|
||||
@@ -201,7 +202,7 @@ class AttendanceTracking extends BaseModel
|
||||
?string $schoolYear = null
|
||||
): bool {
|
||||
$startDay = Carbon::now('UTC')->subDays($days)->toDateString();
|
||||
[$start, ] = static::dayRange($startDay);
|
||||
[$start] = static::dayRange($startDay);
|
||||
|
||||
$q = static::query()
|
||||
->where('student_id', $studentId)
|
||||
@@ -231,13 +232,17 @@ class AttendanceTracking extends BaseModel
|
||||
|
||||
$absences = $q->orderByDesc('date')->limit($threshold)->get()->toArray();
|
||||
|
||||
if (count($absences) < $threshold) return false;
|
||||
if (count($absences) < $threshold) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for ($i = 0; $i < $threshold - 1; $i++) {
|
||||
$d1 = new \DateTime($absences[$i]['date']);
|
||||
$d2 = new \DateTime($absences[$i + 1]['date']);
|
||||
$diffDays = (int) $d2->diff($d1)->format('%a');
|
||||
if ($diffDays !== 7) return false;
|
||||
if ($diffDays !== 7) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -264,11 +269,13 @@ class AttendanceTracking extends BaseModel
|
||||
|
||||
$records = $q->orderByDesc('date')->limit($weeksStreak)->get()->toArray();
|
||||
|
||||
if (count($records) < $weeksStreak) return false;
|
||||
if (count($records) < $weeksStreak) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$count = 0;
|
||||
foreach ($records as $r) {
|
||||
if ((int)($r['is_reported'] ?? 0) === 0 && (int)($r['is_notified'] ?? 0) === 0) {
|
||||
if ((int) ($r['is_reported'] ?? 0) === 0 && (int) ($r['is_notified'] ?? 0) === 0) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
@@ -297,9 +304,9 @@ class AttendanceTracking extends BaseModel
|
||||
$last = $q3->orderByDesc('date')->first();
|
||||
|
||||
return [
|
||||
'total_absences' => $total,
|
||||
'total_absences' => $total,
|
||||
'unexcused_absences' => $unexcused,
|
||||
'last_absence' => $last ? $last->toArray() : null,
|
||||
'last_absence' => $last ? $last->toArray() : null,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -313,7 +320,10 @@ class AttendanceTracking extends BaseModel
|
||||
) {
|
||||
$q = static::query();
|
||||
static::applyTermFilters($q, $semester, $schoolYear);
|
||||
if ($limit > 0) $q->limit($limit);
|
||||
if ($limit > 0) {
|
||||
$q->limit($limit);
|
||||
}
|
||||
|
||||
return $q->orderByDesc('date')->get();
|
||||
}
|
||||
|
||||
@@ -335,8 +345,8 @@ class AttendanceTracking extends BaseModel
|
||||
$students = (int) $q3->distinct('student_id')->count('student_id');
|
||||
|
||||
return [
|
||||
'total_absences' => $total,
|
||||
'unexcused_absences' => $unexcused,
|
||||
'total_absences' => $total,
|
||||
'unexcused_absences' => $unexcused,
|
||||
'students_with_absences' => $students,
|
||||
];
|
||||
}
|
||||
@@ -352,26 +362,28 @@ class AttendanceTracking extends BaseModel
|
||||
foreach ($studentsWithViolations as $student) {
|
||||
if (empty($student['absences'])) {
|
||||
$results['skipped']++;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($student['absences'] as $absence) {
|
||||
$ymd = substr((string)($absence['date'] ?? now()->toDateString()), 0, 10);
|
||||
$ymd = substr((string) ($absence['date'] ?? now()->toDateString()), 0, 10);
|
||||
[$start, $endEx] = static::dayRange($ymd);
|
||||
|
||||
$data = [
|
||||
'student_id' => (int)($student['id'] ?? 0),
|
||||
'date' => static::normalizeDateTime($ymd),
|
||||
'is_reported' => 0,
|
||||
'reason' => (string)($student['violation'] ?? ''),
|
||||
'is_notified' => 0,
|
||||
'notif_counter' => 0,
|
||||
'semester' => $semester,
|
||||
'school_year' => $schoolYear,
|
||||
'student_id' => (int) ($student['id'] ?? 0),
|
||||
'date' => static::normalizeDateTime($ymd),
|
||||
'is_reported' => 0,
|
||||
'reason' => (string) ($student['violation'] ?? ''),
|
||||
'is_notified' => 0,
|
||||
'notif_counter' => 0,
|
||||
'semester' => $semester,
|
||||
'school_year' => $schoolYear,
|
||||
];
|
||||
|
||||
if ($data['student_id'] <= 0) {
|
||||
$results['skipped']++;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -391,8 +403,8 @@ class AttendanceTracking extends BaseModel
|
||||
$existing->fill($data)->save();
|
||||
$results['updated']++;
|
||||
|
||||
if (!empty($existing->is_notified)) {
|
||||
static::resetNotification((int)$existing->id);
|
||||
if (! empty($existing->is_notified)) {
|
||||
static::resetNotification((int) $existing->id);
|
||||
$results['notified']++;
|
||||
}
|
||||
} else {
|
||||
@@ -404,7 +416,7 @@ class AttendanceTracking extends BaseModel
|
||||
|
||||
// Only match standalone "4"
|
||||
if (preg_match('/\b4\b/', $data['reason'])) {
|
||||
static::markAsNotified((int)$new->id);
|
||||
static::markAsNotified((int) $new->id);
|
||||
$results['notified']++;
|
||||
}
|
||||
}
|
||||
@@ -427,8 +439,8 @@ class AttendanceTracking extends BaseModel
|
||||
->whereKey($id)
|
||||
->update([
|
||||
'notif_counter' => DB::raw('COALESCE(notif_counter,0)+1'),
|
||||
'is_notified' => 1,
|
||||
'updated_at' => now(),
|
||||
'is_notified' => 1,
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
return $affected > 0;
|
||||
@@ -439,9 +451,9 @@ class AttendanceTracking extends BaseModel
|
||||
$affected = static::query()
|
||||
->whereKey($id)
|
||||
->update([
|
||||
'is_notified' => 0,
|
||||
'is_notified' => 0,
|
||||
'notif_counter' => 0,
|
||||
'updated_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
return $affected >= 0;
|
||||
@@ -463,7 +475,7 @@ class AttendanceTracking extends BaseModel
|
||||
// Here we require term to be passed or derived from date.
|
||||
if ($semester === null || $schoolYear === null) {
|
||||
[$sem, $yr] = static::deriveTermFromDate($ymd);
|
||||
$semester = $semester ?? $sem;
|
||||
$semester = $semester ?? $sem;
|
||||
$schoolYear = $schoolYear ?? $yr;
|
||||
}
|
||||
|
||||
@@ -475,33 +487,33 @@ class AttendanceTracking extends BaseModel
|
||||
->where('semester', $semester)
|
||||
->where('school_year', $schoolYear)
|
||||
->where('date', '>=', $start)
|
||||
->where('date', '<', $endEx)
|
||||
->where('date', '<', $endEx)
|
||||
->where('reason', 'like', "%{$code}%")
|
||||
->orderByDesc('date')
|
||||
->first();
|
||||
|
||||
// 2) Fallback: match by day only
|
||||
if (!$row) {
|
||||
if (! $row) {
|
||||
$row = static::query()
|
||||
->where('student_id', $studentId)
|
||||
->where('semester', $semester)
|
||||
->where('school_year', $schoolYear)
|
||||
->where('date', '>=', $start)
|
||||
->where('date', '<', $endEx)
|
||||
->where('date', '<', $endEx)
|
||||
->orderByDesc('date')
|
||||
->first();
|
||||
}
|
||||
|
||||
if ($row) {
|
||||
$counter = ((int)($row->notif_counter ?? 0)) + 1;
|
||||
$counter = ((int) ($row->notif_counter ?? 0)) + 1;
|
||||
|
||||
$affected = static::query()
|
||||
->whereKey((int)$row->id)
|
||||
->whereKey((int) $row->id)
|
||||
->update([
|
||||
'is_notified' => 1,
|
||||
'is_notified' => 1,
|
||||
'notif_counter' => $counter,
|
||||
'is_reported' => 0,
|
||||
'updated_at' => now(),
|
||||
'is_reported' => 0,
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
return $affected > 0;
|
||||
@@ -509,18 +521,18 @@ class AttendanceTracking extends BaseModel
|
||||
|
||||
// Insert a row so future runs see it as notified
|
||||
$created = static::create([
|
||||
'student_id' => $studentId,
|
||||
'date' => static::normalizeDateTime($ymd),
|
||||
'is_reported' => 0,
|
||||
'reason' => $code,
|
||||
'is_notified' => 1,
|
||||
'notif_counter' => 1,
|
||||
'semester' => $semester,
|
||||
'school_year' => $schoolYear,
|
||||
'note' => null,
|
||||
'student_id' => $studentId,
|
||||
'date' => static::normalizeDateTime($ymd),
|
||||
'is_reported' => 0,
|
||||
'reason' => $code,
|
||||
'is_notified' => 1,
|
||||
'notif_counter' => 1,
|
||||
'semester' => $semester,
|
||||
'school_year' => $schoolYear,
|
||||
'note' => null,
|
||||
]);
|
||||
|
||||
return !empty($created->id);
|
||||
return ! empty($created->id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -538,11 +550,11 @@ class AttendanceTracking extends BaseModel
|
||||
->where('student_id', $studentId)
|
||||
->where('school_year', $schoolYear);
|
||||
|
||||
if (!empty($semester)) {
|
||||
if (! empty($semester)) {
|
||||
$q->where('semester', $semester);
|
||||
}
|
||||
|
||||
if (!empty($lastDate)) {
|
||||
if (! empty($lastDate)) {
|
||||
$q->where('date', '<=', static::normalizeDateTime($lastDate));
|
||||
}
|
||||
|
||||
@@ -550,18 +562,20 @@ class AttendanceTracking extends BaseModel
|
||||
->orderByDesc('date')
|
||||
->first();
|
||||
|
||||
if (!$row) return 0;
|
||||
if (! $row) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$counter = ((int)($row->notif_counter ?? 0)) + 1;
|
||||
$counter = ((int) ($row->notif_counter ?? 0)) + 1;
|
||||
|
||||
$affected = static::query()
|
||||
->whereKey((int)$row->id)
|
||||
->whereKey((int) $row->id)
|
||||
->update([
|
||||
'is_notified' => 1,
|
||||
'is_notified' => 1,
|
||||
'notif_counter' => $counter,
|
||||
'updated_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
return $affected > 0 ? 1 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user