reconstruction of the project
This commit is contained in:
+53
-42
@@ -7,10 +7,13 @@ use App\Models\BaseModel;
|
||||
class MidtermExam extends BaseModel
|
||||
{
|
||||
protected $table = 'midterm_exam';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $useSoftDeletes = false;
|
||||
protected $protectFields = true;
|
||||
|
||||
/**
|
||||
* CI: 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 = true;
|
||||
|
||||
protected $fillable = [
|
||||
'student_id',
|
||||
'school_id',
|
||||
@@ -24,46 +27,54 @@ class MidtermExam extends BaseModel
|
||||
'updated_at',
|
||||
];
|
||||
|
||||
protected bool $allowEmptyInserts = false;
|
||||
protected bool $updateOnlyChanged = true;
|
||||
protected $casts = [
|
||||
'student_id' => 'integer',
|
||||
'school_id' => 'integer',
|
||||
'class_section_id' => 'integer',
|
||||
'updated_by' => 'integer',
|
||||
|
||||
protected $casts = [];
|
||||
protected $castHandlers = [];
|
||||
// score can be decimal; change to 'integer' if it's whole number
|
||||
'score' => 'decimal:2',
|
||||
|
||||
// Dates
|
||||
public $timestamps = true;
|
||||
protected $dateFormat = 'datetime';
|
||||
const CREATED_AT = 'created_at';
|
||||
const UPDATED_AT = 'updated_at';
|
||||
protected $deletedField = 'deleted_at';
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
];
|
||||
|
||||
// Validation
|
||||
protected $validationRules = [];
|
||||
protected $validationMessages = [];
|
||||
protected $skipValidation = false;
|
||||
protected $cleanValidationRules = true;
|
||||
|
||||
// Callbacks
|
||||
protected $allowCallbacks = true;
|
||||
protected $beforeInsert = [];
|
||||
protected $afterInsert = [];
|
||||
protected $beforeUpdate = [];
|
||||
protected $afterUpdate = [];
|
||||
protected $beforeFind = [];
|
||||
protected $afterFind = [];
|
||||
protected $beforeDelete = [];
|
||||
protected $afterDelete = [];
|
||||
|
||||
// Function to get the final exam score for a specific student, semester, and school year
|
||||
public function getMidtermExamScore($studentId, $schoolYear)
|
||||
/* Optional relationships */
|
||||
public function student()
|
||||
{
|
||||
// Query the final_score table for the final exam score based on student ID, semester, and school year
|
||||
$result = $this->select('score') // We only need the score
|
||||
->where('student_id', $studentId)
|
||||
->where('school_year', $schoolYear)
|
||||
->first(); // Get the first matching result
|
||||
|
||||
// Return the score, or null if no result is found
|
||||
return $result ? $result['score'] : null;
|
||||
return $this->belongsTo(Student::class, 'student_id');
|
||||
}
|
||||
}
|
||||
|
||||
public function classSection()
|
||||
{
|
||||
return $this->belongsTo(ClassSection::class, 'class_section_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Equivalent of CI 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user