Files
alrahma_sunday_school_api/app/Models/Homework.php
T
2026-03-05 12:29:37 -05:00

85 lines
2.4 KiB
PHP
Executable File

<?php
namespace App\Models;
use App\Models\BaseModel;
class Homework extends BaseModel
{
protected $table = 'homework';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $fillable = [
'student_id',
'school_id',
'class_section_id',
'updated_by',
'homework_index',
'score',
'comment',
'semester',
'school_year',
'created_at',
'updated_at',
];
protected bool $allowEmptyInserts = false;
protected bool $updateOnlyChanged = true;
protected $casts = [];
protected $castHandlers = [];
// Dates
public $timestamps = true;
protected $dateFormat = 'datetime';
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
protected $deletedField = 'deleted_at';
// 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 = [];
/**
* Calculates the average homework score for a student
*
* @param int $studentId The student ID
* @param string $semester The semester (e.g., 'Fall', 'Spring')
* @param string $schoolYear The school year (e.g., '2023-2024')
* @return float The average score (0.0 if no homework found)
*/
public function getAverageHomeworkScore(int $studentId, string $semester, string $schoolYear): float
{
// Build the query using Query Builder
$builder = $this->db->table('homework');
$builder->selectAvg('score', 'average_score')
->where('student_id', $studentId)
->where('semester', $semester)
->where('school_year', $schoolYear)
->where('score IS NOT NULL'); // Only include records with actual scores
// Execute the query
$result = $builder->get()->getRow();
// Return the average or 0.0 if no results
return $result ? (float)$result->average_score : 0.0;
}
}