reconstruction of the project
This commit is contained in:
+322
-291
@@ -2,15 +2,16 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AttendanceData extends Model
|
||||
class AttendanceData extends BaseModel
|
||||
{
|
||||
protected $table = 'attendance_data';
|
||||
protected $primaryKey = 'id';
|
||||
public $timestamps = true;
|
||||
|
||||
public $timestamps = true; // uses created_at / updated_at
|
||||
|
||||
protected $fillable = [
|
||||
'class_id',
|
||||
'class_section_id',
|
||||
@@ -33,277 +34,268 @@ class AttendanceData extends Model
|
||||
'class_section_id' => 'integer',
|
||||
'school_id' => 'integer',
|
||||
'student_id' => 'integer',
|
||||
'reported' => 'string',
|
||||
'is_reported' => 'string',
|
||||
'is_notified' => 'string',
|
||||
'date' => 'date',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime'
|
||||
'modified_by' => 'integer',
|
||||
'date' => 'datetime', // ✅ DATETIME column
|
||||
];
|
||||
|
||||
private ?array $reportedColumns = null;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Relationships
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/* =========================
|
||||
* Relationships (optional)
|
||||
* ========================= */
|
||||
public function student()
|
||||
{
|
||||
return $this->belongsTo(Student::class, 'student_id');
|
||||
}
|
||||
|
||||
public function class()
|
||||
{
|
||||
return $this->belongsTo(ClassModel::class, 'class_id');
|
||||
}
|
||||
|
||||
public function section()
|
||||
public function classSection()
|
||||
{
|
||||
return $this->belongsTo(ClassSection::class, 'class_section_id');
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Methods Converted from CI4
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/** CI => getAttendanceByClass */
|
||||
public function getAttendanceByClass(int $classId, int $classSectionId, string $date)
|
||||
/* =========================
|
||||
* DATETIME helpers
|
||||
* ========================= */
|
||||
private static function dayRange(string $day): array
|
||||
{
|
||||
return self::where('class_id', $classId)
|
||||
$day = substr($day, 0, 10); // YYYY-MM-DD
|
||||
$start = Carbon::parse($day)->startOfDay();
|
||||
$endEx = Carbon::parse($day)->addDay()->startOfDay(); // exclusive
|
||||
return [$start, $endEx];
|
||||
}
|
||||
|
||||
/* =========================
|
||||
* CI method equivalents
|
||||
* ========================= */
|
||||
|
||||
/**
|
||||
* Retrieve attendance by class and section for a specific date (DATETIME-safe).
|
||||
*/
|
||||
public static function getAttendanceByClass(int $classId, int $classSectionId, string $date)
|
||||
{
|
||||
[$start, $endEx] = static::dayRange($date);
|
||||
|
||||
return static::query()
|
||||
->where('class_id', $classId)
|
||||
->where('class_section_id', $classSectionId)
|
||||
->where('date', $date)
|
||||
->get()
|
||||
->toArray();
|
||||
->where('date', '>=', $start)
|
||||
->where('date', '<', $endEx)
|
||||
->get();
|
||||
}
|
||||
|
||||
/** CI => unreportedTermRows */
|
||||
public function unreportedTermRows(array $studentIds, string $schoolYear, ?string $semester): array
|
||||
/**
|
||||
* Update attendance record by PK.
|
||||
*/
|
||||
public static function updateAttendance(int $attendanceId, array $data): bool
|
||||
{
|
||||
$qb = DB::table($this->table)
|
||||
->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 !== '') {
|
||||
$qb->where('semester', $semester);
|
||||
}
|
||||
if (!empty($studentIds)) {
|
||||
$qb->whereIn('student_id', $studentIds);
|
||||
}
|
||||
|
||||
$this->applyUnreportedFilter($qb);
|
||||
|
||||
return $qb->orderBy('date', 'DESC')
|
||||
->get()
|
||||
->map(static function ($row) {
|
||||
return (array) $row;
|
||||
})
|
||||
->all();
|
||||
$affected = static::query()->whereKey($attendanceId)->update($data);
|
||||
return $affected >= 0;
|
||||
}
|
||||
|
||||
/** CI => updateAttendance */
|
||||
public function updateAttendance(int $id, array $data)
|
||||
/**
|
||||
* Retrieve attendance for a specific student, class section, and date (DATETIME-safe).
|
||||
*/
|
||||
public static function getAttendance(int $studentId, int $classSectionId, string $date): ?self
|
||||
{
|
||||
return self::where('id', $id)->update($data) > 0;
|
||||
[$start, $endEx] = static::dayRange($date);
|
||||
|
||||
return static::query()
|
||||
->where('student_id', $studentId)
|
||||
->where('class_section_id', $classSectionId)
|
||||
->where('date', '>=', $start)
|
||||
->where('date', '<', $endEx)
|
||||
->first();
|
||||
}
|
||||
|
||||
/** CI => getAttendance */
|
||||
public function getAttendance(int $studentId, int $classSectionId, string $date)
|
||||
/**
|
||||
* Retrieve reported attendance for a specific date or range (DATETIME-safe).
|
||||
*/
|
||||
public static function getReportedAttendance(string $startDate, ?string $endDate = null)
|
||||
{
|
||||
$row = self::where([
|
||||
'student_id' => $studentId,
|
||||
'class_section_id' => $classSectionId,
|
||||
'date' => $date
|
||||
])->first();
|
||||
$q = static::query();
|
||||
static::applyReportedFilter($q);
|
||||
|
||||
return $row ? $row->toArray() : null;
|
||||
}
|
||||
|
||||
/** CI => getReportedAttendance */
|
||||
public function getReportedAttendance(string $startDate, ?string $endDate = null)
|
||||
{
|
||||
$query = self::where(function ($q) {
|
||||
$q->where('is_reported', 1)
|
||||
->orWhere('is_reported', 'yes');
|
||||
});
|
||||
[$start, ] = static::dayRange($startDate);
|
||||
|
||||
if ($endDate) {
|
||||
$query->whereBetween('date', [$startDate, $endDate]);
|
||||
[, $endEx] = static::dayRange($endDate); // end exclusive of next day
|
||||
$q->where('date', '>=', $start)->where('date', '<', $endEx);
|
||||
} else {
|
||||
$query->where('date', $startDate);
|
||||
[, $endEx] = static::dayRange($startDate);
|
||||
$q->where('date', '>=', $start)->where('date', '<', $endEx);
|
||||
}
|
||||
|
||||
return $query->get()->toArray();
|
||||
return $q->get();
|
||||
}
|
||||
|
||||
/** CI => getTodayAbsentees */
|
||||
public function getTodayAbsentees(?int $classSectionId = null, ?string $schoolYear = null, ?string $semester = null)
|
||||
public static function getTodayAbsentees(?int $classSectionId = null, ?string $schoolYear = null, ?string $semester = null)
|
||||
{
|
||||
$query = self::where('status', 'Absent')
|
||||
->where('date', now()->toDateString());
|
||||
[$start, $endEx] = [now()->startOfDay(), now()->addDay()->startOfDay()];
|
||||
|
||||
if ($classSectionId !== null) {
|
||||
$query->where('class_section_id', $classSectionId);
|
||||
}
|
||||
if ($schoolYear !== null) {
|
||||
$query->where('school_year', $schoolYear);
|
||||
}
|
||||
if ($semester !== null) {
|
||||
$query->where('semester', $semester);
|
||||
}
|
||||
$q = static::query()
|
||||
->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%'");
|
||||
});
|
||||
|
||||
return $query->get()->toArray();
|
||||
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();
|
||||
}
|
||||
|
||||
/** CI => getTodayLates */
|
||||
public function getTodayLates(?int $classSectionId = null, ?string $schoolYear = null, ?string $semester = null)
|
||||
public static function getTodayLates(?int $classSectionId = null, ?string $schoolYear = null, ?string $semester = null)
|
||||
{
|
||||
$query = self::where('status', 'Late')
|
||||
->where('date', now()->toDateString());
|
||||
[$start, $endEx] = [now()->startOfDay(), now()->addDay()->startOfDay()];
|
||||
|
||||
if ($classSectionId !== null) {
|
||||
$query->where('class_section_id', $classSectionId);
|
||||
}
|
||||
if ($schoolYear !== null) {
|
||||
$query->where('school_year', $schoolYear);
|
||||
}
|
||||
if ($semester !== null) {
|
||||
$query->where('semester', $semester);
|
||||
}
|
||||
$q = static::query()
|
||||
->where('date', '>=', $start)
|
||||
->where('date', '<', $endEx)
|
||||
->where(function ($w) {
|
||||
$w->whereIn(DB::raw("UPPER(TRIM(status))"), ['LATE', 'L'])
|
||||
->orWhereRaw("UPPER(TRIM(status)) LIKE 'LATE%'");
|
||||
});
|
||||
|
||||
return $query->get()->toArray();
|
||||
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();
|
||||
}
|
||||
|
||||
/** CI => getAbsencesByClassSection */
|
||||
public function getAbsencesByClassSection($classSectionId, $schoolYear, $semester)
|
||||
public static function getAbsencesByClassSection(int $classSectionId, string $schoolYear, string $semester)
|
||||
{
|
||||
return self::where('status', 'absent')
|
||||
return static::query()
|
||||
->where('class_section_id', $classSectionId)
|
||||
->where('school_year', $schoolYear)
|
||||
->where('semester', $semester)
|
||||
->orderBy('date', 'DESC')
|
||||
->get()
|
||||
->toArray();
|
||||
->where(function ($w) {
|
||||
$w->whereIn(DB::raw("UPPER(TRIM(status))"), ['ABSENT', 'ABS', 'A'])
|
||||
->orWhereRaw("UPPER(TRIM(status)) LIKE 'ABS%'");
|
||||
})
|
||||
->orderByDesc('date')
|
||||
->get();
|
||||
}
|
||||
|
||||
/** CI => getStudentAttendance */
|
||||
public function getStudentAttendance(
|
||||
/**
|
||||
* Fetch attendance rows for a student, optionally filtered by date range.
|
||||
* Note: Uses inclusive date range by day using datetime bounds.
|
||||
*/
|
||||
public static function getStudentAttendance(
|
||||
int $studentId,
|
||||
?string $startDate = null,
|
||||
?string $endDate = null,
|
||||
?string $endDate = null,
|
||||
?string $schoolYear = null,
|
||||
?string $semester = null
|
||||
): array {
|
||||
$query = self::where('student_id', $studentId);
|
||||
?string $semester = null
|
||||
) {
|
||||
$q = static::query()->where('student_id', $studentId);
|
||||
|
||||
if ($startDate) {
|
||||
$query->where('date', '>=', $startDate);
|
||||
if (!empty($startDate)) {
|
||||
[$start, ] = static::dayRange($startDate);
|
||||
$q->where('date', '>=', $start);
|
||||
}
|
||||
if ($endDate) {
|
||||
$query->where('date', '<=', $endDate);
|
||||
}
|
||||
if ($schoolYear) {
|
||||
$query->where('school_year', $schoolYear);
|
||||
}
|
||||
if ($semester) {
|
||||
$query->where('semester', $semester);
|
||||
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);
|
||||
|
||||
return $query->orderBy('date', 'DESC')->get()->toArray();
|
||||
return $q->orderByDesc('date')->get();
|
||||
}
|
||||
|
||||
/** CI => getStudentAttendanceOnDate */
|
||||
public function getStudentAttendanceOnDate(int $studentId, string $date): ?array
|
||||
/**
|
||||
* Convenience: attendance for an exact date (DATETIME-safe).
|
||||
*/
|
||||
public static function getStudentAttendanceOnDate(int $studentId, string $date): ?self
|
||||
{
|
||||
$row = self::where('student_id', $studentId)
|
||||
->where('date', $date)
|
||||
[$start, $endEx] = static::dayRange($date);
|
||||
|
||||
return static::query()
|
||||
->where('student_id', $studentId)
|
||||
->where('date', '>=', $start)
|
||||
->where('date', '<', $endEx)
|
||||
->first();
|
||||
|
||||
return $row ? $row->toArray() : null;
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| CI Complex Function => getUnreportedAbsencesAndLates()
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
public function getUnreportedAbsencesAndLates(?string $schoolYear = null, ?string $semester = null): array
|
||||
/**
|
||||
* Unreported rows for students in term.
|
||||
*/
|
||||
public static function unreportedTermRows(array $studentIds, string $schoolYear, ?string $semester)
|
||||
{
|
||||
$query = DB::table($this->table)
|
||||
->select('id', 'student_id', 'class_id', 'class_section_id', 'date', 'status', 'reason', 'school_year', 'semester', 'is_reported')
|
||||
->whereNotNull('student_id');
|
||||
$q = static::query()
|
||||
->select(['id','student_id','class_id','class_section_id','date','status','reason','school_year','semester','is_reported'])
|
||||
->where('school_year', $schoolYear);
|
||||
|
||||
$this->applyUnreportedFilter($query);
|
||||
|
||||
$query->where(function ($q) {
|
||||
$q->where('is_notified', 0)
|
||||
->orWhere('is_notified', '0')
|
||||
->orWhereNull('is_notified')
|
||||
->orWhereRaw("LOWER(TRIM(COALESCE(is_notified, ''))) != 'yes'");
|
||||
});
|
||||
|
||||
if ($schoolYear) {
|
||||
$query->where('school_year', $schoolYear);
|
||||
if ($semester !== null && $semester !== '') {
|
||||
$q->where('semester', $semester);
|
||||
}
|
||||
if (!empty($studentIds)) {
|
||||
$q->whereIn('student_id', array_map('intval', $studentIds));
|
||||
}
|
||||
|
||||
if ($semester) {
|
||||
$query->where('semester', $semester);
|
||||
}
|
||||
static::applyUnreportedFilter($q);
|
||||
|
||||
// Status filter
|
||||
$query->where(function ($q) {
|
||||
$q->whereIn('status', ['ABS', 'ABSENT', 'LATE', 'A', 'L'])
|
||||
->orWhere('status', 'like', 'ABS%')
|
||||
->orWhere('status', 'like', 'LATE%');
|
||||
return $q->orderByDesc('date')->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* NOT-REPORTED absences/lates for ALL students in optional term filters.
|
||||
* Returns grouped array: [student_id => ['absences'=>[], 'lates'=>[], 'counts'=>...]]
|
||||
*/
|
||||
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']);
|
||||
|
||||
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'");
|
||||
});
|
||||
|
||||
$rows = $query->orderBy('student_id', 'ASC')
|
||||
->orderBy('date', 'DESC')
|
||||
->get()
|
||||
->toArray();
|
||||
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%'");
|
||||
});
|
||||
|
||||
$rows = $q->orderBy('student_id')->orderByDesc('date')->get()->toArray();
|
||||
|
||||
// Now group output like original CI function
|
||||
$out = [];
|
||||
|
||||
foreach ($rows as $r) {
|
||||
$sid = (int) $r->student_id;
|
||||
$sid = (int) ($r['student_id'] ?? 0);
|
||||
|
||||
if (!isset($out[$sid])) {
|
||||
$out[$sid] = [
|
||||
'absences' => [],
|
||||
'lates' => [],
|
||||
'counts' => ['absences' => 0, 'lates' => 0, 'total' => 0],
|
||||
'lates' => [],
|
||||
'counts' => ['absences' => 0, 'lates' => 0, 'total' => 0],
|
||||
];
|
||||
}
|
||||
|
||||
$status = strtoupper($r->status);
|
||||
$isAbs = $status === 'ABS' || $status === 'ABSENT' || $status === 'A' || str_starts_with($status, 'ABS');
|
||||
$isLate = $status === 'LATE' || $status === 'L' || str_starts_with($status, 'LATE');
|
||||
$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,
|
||||
'date' => $r->date,
|
||||
'status' => $r->status,
|
||||
'reason' => $r->reason,
|
||||
'class_id' => $r->class_id,
|
||||
'class_section_id' => $r->class_section_id,
|
||||
'school_year' => $r->school_year,
|
||||
'semester' => $r->semester,
|
||||
'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,
|
||||
];
|
||||
|
||||
if ($isAbs) {
|
||||
@@ -321,76 +313,80 @@ class AttendanceData extends Model
|
||||
return $out;
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| CI => markRuleAsNotifiedData()
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
/**
|
||||
* Mark rule as notified for a student on a specific day (DATETIME-safe).
|
||||
*/
|
||||
public static function markRuleAsNotifiedData(
|
||||
int $studentId,
|
||||
string $date,
|
||||
?string $semester = null,
|
||||
?string $schoolYear = null
|
||||
): bool {
|
||||
[$start, $endEx] = static::dayRange($date);
|
||||
|
||||
public function markRuleAsNotifiedData(int $studentId, string $date, ?string $semester = null, ?string $schoolYear = null): bool
|
||||
{
|
||||
$day = substr($date, 0, 10);
|
||||
$start = $day . ' 00:00:00';
|
||||
$endEx = date('Y-m-d', strtotime($day . ' +1 day')) . ' 00:00:00';
|
||||
$query = DB::table($this->table)
|
||||
$q = static::query()
|
||||
->where('student_id', $studentId)
|
||||
->where('date >=', $start)
|
||||
->where('date <', $endEx);
|
||||
->where('date', '>=', $start)
|
||||
->where('date', '<', $endEx);
|
||||
|
||||
if (!empty($semester)) {
|
||||
$query->where('semester', $semester);
|
||||
}
|
||||
if (!empty($semester)) $q->where('semester', $semester);
|
||||
if (!empty($schoolYear)) $q->where('school_year', $schoolYear);
|
||||
|
||||
if (!empty($schoolYear)) {
|
||||
$query->where('school_year', $schoolYear);
|
||||
}
|
||||
|
||||
return $query->update([
|
||||
$affected = $q->update([
|
||||
'is_notified' => 'yes',
|
||||
'updated_at' => utc_now(),
|
||||
]) > 0;
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
return $affected >= 0;
|
||||
}
|
||||
|
||||
public function markReportedAndNotified(int $studentId, array $dates, ?string $semester = null, ?string $schoolYear = null): bool
|
||||
/**
|
||||
* Mark specific attendance dates as reported + notified for a student (DATETIME index-friendly).
|
||||
*/
|
||||
public static function markReportedAndNotified(int $studentId, array $dates, ?string $semester = null, ?string $schoolYear = null): bool
|
||||
{
|
||||
$dates = array_values(array_unique(array_filter($dates, static function ($d) {
|
||||
return is_string($d) && preg_match('/^\d{4}-\d{2}-\d{2}$/', $d);
|
||||
})));
|
||||
if (empty($dates)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$builder = DB::table($this->table)
|
||||
if (empty($dates)) return true;
|
||||
|
||||
$q = static::query()
|
||||
->where('student_id', $studentId)
|
||||
->whereIn('date', $dates)
|
||||
->whereRaw("LOWER(TRIM(status)) IN ('absent','late')");
|
||||
|
||||
if (!empty($semester)) {
|
||||
$builder->where('semester', $semester);
|
||||
}
|
||||
if (!empty($schoolYear)) {
|
||||
$builder->where('school_year', $schoolYear);
|
||||
}
|
||||
if (!empty($semester)) $q->where('semester', $semester);
|
||||
if (!empty($schoolYear)) $q->where('school_year', $schoolYear);
|
||||
|
||||
$cols = $this->reportedColumns();
|
||||
$updates = [
|
||||
'updated_at' => utc_now(),
|
||||
// OR day ranges instead of DATE(date)
|
||||
$q->where(function ($w) use ($dates) {
|
||||
foreach ($dates as $d) {
|
||||
[$start, $endEx] = static::dayRange($d);
|
||||
$w->orWhere(function ($x) use ($start, $endEx) {
|
||||
$x->where('date', '>=', $start)->where('date', '<', $endEx);
|
||||
});
|
||||
$w->orWhere('date', $d);
|
||||
}
|
||||
});
|
||||
|
||||
$payload = [
|
||||
'updated_at' => now(),
|
||||
'is_notified' => 'yes',
|
||||
];
|
||||
|
||||
if (!empty($cols['is_reported'])) {
|
||||
$updates['is_reported'] = 'yes';
|
||||
}
|
||||
if (!empty($cols['reported'])) {
|
||||
$updates['reported'] = 1;
|
||||
}
|
||||
if (Schema::hasColumn($this->table, 'is_notified')) {
|
||||
$updates['is_notified'] = 'yes';
|
||||
}
|
||||
$cols = static::reportedColumns();
|
||||
if (!empty($cols['is_reported'])) $payload['is_reported'] = 'yes';
|
||||
if (!empty($cols['reported'])) $payload['reported'] = 1;
|
||||
|
||||
return $builder->update($updates) > 0;
|
||||
$affected = $q->update($payload);
|
||||
|
||||
return $affected >= 0;
|
||||
}
|
||||
|
||||
public function getRecentUnreportedDates(
|
||||
/**
|
||||
* Fetch unreported dates for a student within N days for the given status set (DATETIME-safe).
|
||||
*/
|
||||
public static function getRecentUnreportedDates(
|
||||
int $studentId,
|
||||
array $statuses,
|
||||
?string $incidentDate = null,
|
||||
@@ -398,64 +394,99 @@ class AttendanceData extends Model
|
||||
?string $semester = null,
|
||||
?string $schoolYear = null
|
||||
): array {
|
||||
if ($studentId <= 0 || empty($statuses)) {
|
||||
return [];
|
||||
}
|
||||
$statuses = array_map('strtolower', $statuses);
|
||||
$day = $incidentDate && preg_match('/^\d{4}-\d{2}-\d{2}$/', $incidentDate)
|
||||
? $incidentDate
|
||||
: date('Y-m-d');
|
||||
$start = date('Y-m-d', strtotime($day . ' -' . max(0, $lookbackDays) . ' days'));
|
||||
if ($studentId <= 0 || empty($statuses)) return [];
|
||||
|
||||
$placeholders = implode(',', array_fill(0, count($statuses), '?'));
|
||||
$qb = DB::table($this->table)
|
||||
$statuses = array_map('strtolower', $statuses);
|
||||
|
||||
$day = ($incidentDate && preg_match('/^\d{4}-\d{2}-\d{2}$/', $incidentDate))
|
||||
? $incidentDate
|
||||
: now()->toDateString();
|
||||
|
||||
$startDay = Carbon::parse($day)->subDays(max(0, $lookbackDays))->toDateString();
|
||||
[$start, ] = static::dayRange($startDay);
|
||||
[, $endEx] = static::dayRange($day);
|
||||
|
||||
$q = static::query()
|
||||
->select('date')
|
||||
->where('student_id', $studentId)
|
||||
->where('date >=', $start)
|
||||
->where('date <=', $day)
|
||||
->whereRaw("LOWER(TRIM(status)) IN ({$placeholders})", $statuses);
|
||||
$this->applyUnreportedFilter($qb);
|
||||
->where('date', '>=', $start)
|
||||
->where('date', '<', $endEx)
|
||||
->whereIn(DB::raw("LOWER(TRIM(status))"), $statuses);
|
||||
|
||||
if (!empty($semester)) {
|
||||
$qb->where('semester', $semester);
|
||||
}
|
||||
if (!empty($schoolYear)) {
|
||||
$qb->where('school_year', $schoolYear);
|
||||
}
|
||||
static::applyUnreportedFilter($q);
|
||||
|
||||
$rows = $qb->orderBy('date', 'DESC')->get()->toArray();
|
||||
return array_values(array_unique(array_map(static function ($r) {
|
||||
return substr((string) ($r->date ?? ''), 0, 10);
|
||||
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
|
||||
}, $rows)));
|
||||
}
|
||||
|
||||
private function reportedColumns(): array
|
||||
{
|
||||
if ($this->reportedColumns !== null) {
|
||||
return $this->reportedColumns;
|
||||
}
|
||||
/* =========================
|
||||
* Filters / schema helpers
|
||||
* ========================= */
|
||||
|
||||
$this->reportedColumns = [
|
||||
'is_reported' => Schema::hasColumn($this->table, 'is_reported'),
|
||||
'reported' => Schema::hasColumn($this->table, 'reported'),
|
||||
private static function reportedColumns(): array
|
||||
{
|
||||
static $cache = null;
|
||||
if ($cache !== null) return $cache;
|
||||
|
||||
$table = (new static)->getTable();
|
||||
|
||||
$cache = [
|
||||
'is_reported' => static::columnExists($table, 'is_reported'),
|
||||
'reported' => static::columnExists($table, 'reported'),
|
||||
];
|
||||
|
||||
return $this->reportedColumns;
|
||||
return $cache;
|
||||
}
|
||||
|
||||
private function applyUnreportedFilter($qb): void
|
||||
private static function columnExists(string $table, string $column): bool
|
||||
{
|
||||
$cols = $this->reportedColumns();
|
||||
if (!empty($cols['is_reported'])) {
|
||||
$qb->where(function ($q) {
|
||||
$q->where('is_reported', 'no')
|
||||
->orWhere('is_reported', 0)
|
||||
->orWhere('is_reported', '0')
|
||||
->orWhereNull('is_reported');
|
||||
});
|
||||
}
|
||||
if (!empty($cols['reported'])) {
|
||||
$qb->whereRaw("LOWER(TRIM(COALESCE(reported, ''))) NOT IN ('yes','1','true','y','on')");
|
||||
try {
|
||||
return DB::connection()
|
||||
->getSchemaBuilder()
|
||||
->hasColumn($table, $column);
|
||||
} catch (\Throwable) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Not-reported filter:
|
||||
* - is_reported = 'no' OR 0 OR NULL (if column exists)
|
||||
* - legacy 'reported' column: treat values yes/1/true/on as reported, so exclude those
|
||||
*/
|
||||
private static function applyUnreportedFilter($q): void
|
||||
{
|
||||
$cols = static::reportedColumns();
|
||||
|
||||
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'");
|
||||
});
|
||||
}
|
||||
|
||||
if (!empty($cols['reported'])) {
|
||||
$q->whereRaw("(LOWER(TRIM(COALESCE(reported, ''))) NOT IN ('yes','1','true','y','on'))");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reported filter best-effort (used by getReportedAttendance()).
|
||||
*/
|
||||
private static function applyReportedFilter($q): void
|
||||
{
|
||||
$q->where(function ($w) {
|
||||
$w->where('is_reported', 1)
|
||||
->orWhere('is_reported', '1')
|
||||
->orWhereRaw("LOWER(TRIM(COALESCE(is_reported, ''))) = 'yes'");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user