reconstruction of the project
This commit is contained in:
+69
-50
@@ -3,14 +3,18 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class Homework extends BaseModel
|
||||
{
|
||||
protected $table = 'homework';
|
||||
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. If you want Laravel to auto-manage, tell me.
|
||||
*/
|
||||
public $timestamps = true;
|
||||
|
||||
protected $fillable = [
|
||||
'student_id',
|
||||
'school_id',
|
||||
@@ -25,60 +29,75 @@ class Homework 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',
|
||||
'homework_index' => 'integer',
|
||||
|
||||
protected $casts = [];
|
||||
protected $castHandlers = [];
|
||||
// keep numeric; change to 'integer' if your scores are whole numbers
|
||||
'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;
|
||||
/* Optional relationships */
|
||||
public function student()
|
||||
{
|
||||
return $this->belongsTo(Student::class, 'student_id');
|
||||
}
|
||||
|
||||
// Callbacks
|
||||
protected $allowCallbacks = true;
|
||||
protected $beforeInsert = [];
|
||||
protected $afterInsert = [];
|
||||
protected $beforeUpdate = [];
|
||||
protected $afterUpdate = [];
|
||||
protected $beforeFind = [];
|
||||
protected $afterFind = [];
|
||||
protected $beforeDelete = [];
|
||||
protected $afterDelete = [];
|
||||
public function classSection()
|
||||
{
|
||||
return $this->belongsTo(ClassSection::class, 'class_section_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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')
|
||||
/**
|
||||
* Laravel equivalent of CI getAverageHomeworkScore()
|
||||
* Computes average based only on non-blank numeric scores.
|
||||
*/
|
||||
public static function getAverageHomeworkScore(
|
||||
int $studentId,
|
||||
string $semester,
|
||||
string $schoolYear,
|
||||
?int $classSectionId = null
|
||||
): ?float {
|
||||
$q = DB::table('homework')
|
||||
->select('score')
|
||||
->where('student_id', $studentId)
|
||||
->where('semester', $semester)
|
||||
->where('school_year', $schoolYear)
|
||||
->where('score IS NOT NULL'); // Only include records with actual scores
|
||||
->where('school_year', $schoolYear);
|
||||
|
||||
// Execute the query
|
||||
$result = $builder->get()->getRow();
|
||||
if ($classSectionId !== null) {
|
||||
$q->where('class_section_id', $classSectionId);
|
||||
}
|
||||
|
||||
// Return the average or 0.0 if no results
|
||||
return $result ? (float)$result->average_score : 0.0;
|
||||
}
|
||||
$rows = $q->get();
|
||||
if ($rows->isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$total = 0.0;
|
||||
$count = 0;
|
||||
|
||||
}
|
||||
foreach ($rows as $r) {
|
||||
$score = $r->score ?? null;
|
||||
|
||||
if ($score === null) continue;
|
||||
if (is_string($score) && trim($score) === '') continue;
|
||||
if ($score === '') continue;
|
||||
if (!is_numeric($score)) continue;
|
||||
|
||||
$total += (float) $score;
|
||||
$count++;
|
||||
}
|
||||
|
||||
if ($count === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return round($total / $count, 2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user