reconstruction of the project
This commit is contained in:
+123
-30
@@ -2,13 +2,20 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Project extends BaseModel
|
||||
{
|
||||
/**
|
||||
* Table name (your CI table is singular "project").
|
||||
*/
|
||||
protected $table = 'project';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
|
||||
/**
|
||||
* Mass assignment.
|
||||
*/
|
||||
protected $fillable = [
|
||||
'student_id',
|
||||
'school_id',
|
||||
@@ -22,42 +29,128 @@ class Project extends BaseModel
|
||||
'created_at',
|
||||
'updated_at',
|
||||
];
|
||||
|
||||
/**
|
||||
* If your table has created_at/updated_at columns (it does),
|
||||
* keep timestamps enabled (default = true).
|
||||
*/
|
||||
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.
|
||||
* Helpful casts (adjust types if your DB columns differ).
|
||||
*/
|
||||
public function getAverageProjectScore(int $studentId, string $semester, string $schoolYear): float
|
||||
protected $casts = [
|
||||
'student_id' => 'integer',
|
||||
'school_id' => 'integer',
|
||||
'class_section_id' => 'integer',
|
||||
'updated_by' => 'integer',
|
||||
'project_index' => 'integer',
|
||||
'score' => 'decimal:2', // or 'float' if you prefer
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
];
|
||||
|
||||
/* ============================================================
|
||||
* Relationships (optional but useful)
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
public function student(): BelongsTo
|
||||
{
|
||||
// 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();
|
||||
return $this->belongsTo(Student::class, 'student_id');
|
||||
}
|
||||
|
||||
// If there are no scores, return 0
|
||||
if (empty($results)) {
|
||||
return 0.0;
|
||||
}
|
||||
public function classSection(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(ClassSection::class, 'class_section_id');
|
||||
}
|
||||
|
||||
// Calculate the average score
|
||||
$totalScore = 0;
|
||||
$scoreCount = count($results);
|
||||
public function school(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(School::class, 'school_id');
|
||||
}
|
||||
|
||||
foreach ($results as $result) {
|
||||
$totalScore += $result['score'];
|
||||
}
|
||||
/* ============================================================
|
||||
* Query Scopes (reusable filters)
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
return $totalScore / $scoreCount;
|
||||
public function scopeForStudent(Builder $q, int $studentId): Builder
|
||||
{
|
||||
return $q->where('student_id', $studentId);
|
||||
}
|
||||
|
||||
public function scopeForTerm(Builder $q, string $semester, string $schoolYear): Builder
|
||||
{
|
||||
return $q->where('semester', $semester)
|
||||
->where('school_year', $schoolYear);
|
||||
}
|
||||
|
||||
public function scopeForClassSection(Builder $q, ?int $classSectionId): Builder
|
||||
{
|
||||
return $classSectionId !== null
|
||||
? $q->where('class_section_id', $classSectionId)
|
||||
: $q;
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* Business: Average score
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
/**
|
||||
* Calculate average project score for a student + term (+ optional class section).
|
||||
*
|
||||
* Enhancements vs CI version:
|
||||
* - Uses SQL AVG for performance
|
||||
* - Ignores NULL, empty string, and non-numeric (best-effort) values
|
||||
* - Returns float|null
|
||||
*/
|
||||
public static function averageScore(
|
||||
int $studentId,
|
||||
string $semester,
|
||||
string $schoolYear,
|
||||
?int $classSectionId = null
|
||||
): ?float {
|
||||
$avg = static::query()
|
||||
->forStudent($studentId)
|
||||
->forTerm($semester, $schoolYear)
|
||||
->forClassSection($classSectionId)
|
||||
// ignore NULL and blank
|
||||
->whereNotNull('score')
|
||||
->whereRaw("TRIM(COALESCE(score, '')) <> ''")
|
||||
/**
|
||||
* If score is stored as VARCHAR and may contain junk,
|
||||
* this helps filter numeric-only values in MySQL.
|
||||
* If score is a numeric column, you can remove this line.
|
||||
*/
|
||||
->when(static::scoreMightBeString(), function (Builder $q) {
|
||||
$q->whereRaw("TRIM(score) REGEXP '^-?[0-9]+(\\.[0-9]+)?$'");
|
||||
})
|
||||
->avg('score');
|
||||
|
||||
return $avg === null ? null : (float) $avg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Instance method (so you can keep a similar calling style to CI).
|
||||
*/
|
||||
public function getAverageProjectScore(
|
||||
int $studentId,
|
||||
string $semester,
|
||||
string $schoolYear,
|
||||
?int $classSectionId = null
|
||||
): ?float {
|
||||
return static::averageScore($studentId, $semester, $schoolYear, $classSectionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle the REGEXP filter depending on your schema.
|
||||
* If your `score` column is DECIMAL/INT/FLOAT, return false.
|
||||
*/
|
||||
protected static function scoreMightBeString(): bool
|
||||
{
|
||||
// Safe default if you're unsure. Set to false if score is numeric.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user