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

63 lines
1.8 KiB
PHP

<?php
namespace App\Models;
use App\Models\BaseModel;
class Project extends BaseModel
{
protected $table = 'project';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $fillable = [
'student_id',
'school_id',
'class_section_id',
'updated_by',
'project_index',
'score',
'comment',
'semester',
'school_year',
'created_at',
'updated_at',
];
public $timestamps = true;
/**
* Calculate the average project score for a given student, semester, and school year.
*
* @param int $studentId The student's ID.
* @param string $semester The semester (e.g., 'Fall', 'Spring').
* @param string $schoolYear The school year (e.g., '2024-2025').
* @return float The average project score, or 0 if no scores are found.
*/
public function getAverageProjectScore(int $studentId, string $semester, string $schoolYear): float
{
// Fetch the project scores for the given student, semester, and school year
$builder = $this->builder();
$builder->select('score')
->where('student_id', $studentId)
->where('semester', $semester)
->where('school_year', $schoolYear);
// Execute the query and get the results
$query = $builder->get();
$results = $query->getResultArray();
// If there are no scores, return 0
if (empty($results)) {
return 0.0;
}
// Calculate the average score
$totalScore = 0;
$scoreCount = count($results);
foreach ($results as $result) {
$totalScore += $result['score'];
}
return $totalScore / $scoreCount;
}
}