Files
alrahma_sunday_school_api/app/Models/MidtermExam.php
T
2026-06-09 01:25:14 -04:00

87 lines
2.1 KiB
PHP

<?php
namespace App\Models;
class MidtermExam extends BaseModel
{
protected $table = 'midterm_exam';
/**
* legacy: useTimestamps = false (even though created_at/updated_at columns exist).
* Keep it OFF to match behavior (you can still store created_at/updated_at manually).
*/
public $timestamps = false;
protected $fillable = [
'student_id',
'school_id',
'class_section_id',
'updated_by',
'score',
'max_points',
'status',
'excused_reason',
'locked_at',
'locked_by',
'comment',
'semester',
'school_year',
'created_at',
'updated_at',
];
protected $casts = [
'student_id' => 'integer',
'school_id' => 'integer',
'class_section_id' => 'integer',
'updated_by' => 'integer',
// score can be decimal; change to 'integer' if it's whole number
'score' => 'decimal:2',
'max_points' => 'decimal:2',
'locked_by' => 'integer',
'locked_at' => 'datetime',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
/* Optional relationships */
public function student()
{
return $this->belongsTo(Student::class, 'student_id');
}
public function classSection()
{
return $this->belongsTo(ClassSection::class, 'class_section_id');
}
/**
* Equivalent of legacy getMidtermExamScore()
*/
public static function getMidtermExamScore(
int $studentId,
?string $semester,
string $schoolYear,
?int $classSectionId = null
) {
$q = static::query()
->select('score')
->where('student_id', $studentId)
->where('school_year', $schoolYear);
if ($semester !== null && $semester !== '') {
$q->where('semester', $semester);
}
if ($classSectionId !== null) {
$q->where('class_section_id', $classSectionId);
}
$row = $q->first();
return $row?->score;
}
}