reconstruction of the project

This commit is contained in:
root
2026-03-08 16:33:24 -04:00
parent 23b7db1107
commit c8de5f7edc
9157 changed files with 77877 additions and 1073823 deletions
+67 -79
View File
@@ -2,17 +2,16 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\BaseModel;
use Illuminate\Support\Facades\DB;
class AttendanceRecord extends Model
class AttendanceRecord extends BaseModel
{
protected $table = 'attendance_record';
protected $primaryKey = 'id';
// Enable timestamps using custom CI fields
// ✅ CI uses created_at / updated_at
public $timestamps = true;
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
protected $fillable = [
'class_section_id',
'student_id',
@@ -29,25 +28,19 @@ class AttendanceRecord extends Model
];
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',
'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',
];
/*
|--------------------------------------------------------------------------
| Relationships
|--------------------------------------------------------------------------
*/
/* =========================
* Relationships (optional)
* ========================= */
public function student()
{
return $this->belongsTo(Student::class, 'student_id');
@@ -58,97 +51,92 @@ class AttendanceRecord extends Model
return $this->belongsTo(ClassSection::class, 'class_section_id');
}
public function modifier()
{
return $this->belongsTo(User::class, 'modified_by');
}
/*
|--------------------------------------------------------------------------
| Converted CodeIgniter Methods
|--------------------------------------------------------------------------
*/
/* =========================
* CI method equivalents
* ========================= */
/**
* Equivalent to CI getAttendanceRecordByClass()
* Get attendance records by class section, semester, and school year.
*/
public function getAttendanceRecordByClass($classSectionId, $semester, $schoolYear)
public static function getAttendanceRecordByClass(int $classSectionId, string $semester, string $schoolYear)
{
return self::where('class_section_id', $classSectionId)
return static::query()
->where('class_section_id', $classSectionId)
->where('semester', $semester)
->where('school_year', $schoolYear)
->get()
->toArray();
->get();
}
/**
* Equivalent to CI getAttendanceRecord()
* Get attendance record for a specific student in a class section.
*/
public function getAttendanceRecord($studentId, $classSectionId, $semester, $schoolYear)
public static function getAttendanceRecord(int $studentId, int $classSectionId, string $semester, string $schoolYear): ?self
{
return self::where([
'student_id' => $studentId,
'class_section_id' => $classSectionId,
'semester' => $semester,
'school_year' => $schoolYear,
])->first()?->toArray();
return static::query()
->where('student_id', $studentId)
->where('class_section_id', $classSectionId)
->where('semester', $semester)
->where('school_year', $schoolYear)
->first();
}
/**
* Equivalent to CI updateAttendanceRecord()
* Update an existing attendance record by PK.
*/
public function updateAttendanceRecord($attendanceId, $data)
public static function updateAttendanceRecord(int $attendanceId, array $data): bool
{
return self::where('id', $attendanceId)->update($data) > 0;
$affected = static::query()->whereKey($attendanceId)->update($data);
return $affected >= 0;
}
/**
* Laravel-optimized version of CI incrementAttendanceCounters()
* Uses atomic SQL increments.
* Increment attendance counters for a student (atomic, avoids race conditions).
*
* $increments example: ['total_absence' => 1, 'total_presence' => 0]
*/
public function incrementAttendanceCounters(
public static 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();
$allowed = ['total_absence','total_late','total_presence','total_attendance'];
$inc = array_intersect_key($increments, array_flip($allowed));
if (!$record) {
return false;
if (empty($inc)) return true;
// Build atomic update: field = field + X
$update = [];
foreach ($inc as $field => $value) {
$value = (int) $value;
if ($value === 0) continue;
$update[$field] = DB::raw("COALESCE($field,0) + {$value}");
}
foreach ($increments as $field => $value) {
if (in_array($field, [
'total_absence',
'total_late',
'total_presence',
'total_attendance'
])) {
$record->increment($field, $value);
}
}
if (empty($update)) return true;
$record->touch(); // update updated_at
return true;
$affected = static::query()
->where('student_id', $studentId)
->where('class_section_id', $classSectionId)
->where('semester', $semester)
->where('school_year', $schoolYear)
->update($update);
// If no record exists, return false (same idea as your CI code)
return $affected > 0;
}
/**
* Equivalent to CI getTotalAbsences()
* Total absences for a student in a term.
* (Equivalent result, but more efficient than summing in PHP.)
*/
public function getTotalAbsences($studentId, $semester, $schoolYear): int
public static function getTotalAbsences(int $studentId, string $semester, string $schoolYear): int
{
$record = self::where('student_id', $studentId)
return (int) static::query()
->where('student_id', $studentId)
->where('semester', $semester)
->where('school_year', $schoolYear)
->first();
return $record?->total_absence ?? 0;
->sum('total_absence');
}
}
}