155 lines
4.1 KiB
PHP
Executable File
155 lines
4.1 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class AttendanceRecord extends Model
|
|
{
|
|
protected $table = 'attendance_record';
|
|
protected $primaryKey = 'id';
|
|
|
|
// Enable timestamps using custom CI fields
|
|
public $timestamps = true;
|
|
const CREATED_AT = 'created_at';
|
|
const UPDATED_AT = 'updated_at';
|
|
protected $fillable = [
|
|
'class_section_id',
|
|
'student_id',
|
|
'school_id',
|
|
'total_absence',
|
|
'total_late',
|
|
'total_presence',
|
|
'total_attendance',
|
|
'semester',
|
|
'school_year',
|
|
'modified_by',
|
|
'created_at',
|
|
'updated_at',
|
|
];
|
|
|
|
protected $casts = [
|
|
'class_section_id' => 'integer',
|
|
'student_id' => 'integer',
|
|
'school_id' => 'integer',
|
|
'total_absence' => 'integer',
|
|
'total_late' => 'integer',
|
|
'total_presence' => 'integer',
|
|
'total_attendance' => 'integer',
|
|
'modified_by' => 'integer',
|
|
|
|
'created_at' => 'datetime',
|
|
'updated_at' => 'datetime',
|
|
];
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Relationships
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
public function student()
|
|
{
|
|
return $this->belongsTo(Student::class, 'student_id');
|
|
}
|
|
|
|
public function classSection()
|
|
{
|
|
return $this->belongsTo(ClassSection::class, 'class_section_id');
|
|
}
|
|
|
|
public function modifier()
|
|
{
|
|
return $this->belongsTo(User::class, 'modified_by');
|
|
}
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Converted CodeIgniter Methods
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
/**
|
|
* Equivalent to CI getAttendanceRecordByClass()
|
|
*/
|
|
public function getAttendanceRecordByClass($classSectionId, $semester, $schoolYear)
|
|
{
|
|
return self::where('class_section_id', $classSectionId)
|
|
->where('semester', $semester)
|
|
->where('school_year', $schoolYear)
|
|
->get()
|
|
->toArray();
|
|
}
|
|
|
|
/**
|
|
* Equivalent to CI getAttendanceRecord()
|
|
*/
|
|
public function getAttendanceRecord($studentId, $classSectionId, $semester, $schoolYear)
|
|
{
|
|
return self::where([
|
|
'student_id' => $studentId,
|
|
'class_section_id' => $classSectionId,
|
|
'semester' => $semester,
|
|
'school_year' => $schoolYear,
|
|
])->first()?->toArray();
|
|
}
|
|
|
|
/**
|
|
* Equivalent to CI updateAttendanceRecord()
|
|
*/
|
|
public function updateAttendanceRecord($attendanceId, $data)
|
|
{
|
|
return self::where('id', $attendanceId)->update($data) > 0;
|
|
}
|
|
|
|
/**
|
|
* Laravel-optimized version of CI incrementAttendanceCounters()
|
|
* Uses atomic SQL increments.
|
|
*/
|
|
public function incrementAttendanceCounters(
|
|
int $studentId,
|
|
int $classSectionId,
|
|
string $semester,
|
|
string $schoolYear,
|
|
array $increments
|
|
): bool {
|
|
$record = self::where([
|
|
'student_id' => $studentId,
|
|
'class_section_id' => $classSectionId,
|
|
'semester' => $semester,
|
|
'school_year' => $schoolYear,
|
|
])->first();
|
|
|
|
if (!$record) {
|
|
return false;
|
|
}
|
|
|
|
foreach ($increments as $field => $value) {
|
|
if (in_array($field, [
|
|
'total_absence',
|
|
'total_late',
|
|
'total_presence',
|
|
'total_attendance'
|
|
])) {
|
|
$record->increment($field, $value);
|
|
}
|
|
}
|
|
|
|
$record->touch(); // update updated_at
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Equivalent to CI getTotalAbsences()
|
|
*/
|
|
public function getTotalAbsences($studentId, $semester, $schoolYear): int
|
|
{
|
|
$record = self::where('student_id', $studentId)
|
|
->where('semester', $semester)
|
|
->where('school_year', $schoolYear)
|
|
->first();
|
|
|
|
return $record?->total_absence ?? 0;
|
|
}
|
|
}
|