Skip to content

Instantly share code, notes, and snippets.

@daniels-martins
Created July 7, 2025 11:09
Show Gist options
  • Select an option

  • Save daniels-martins/611aca6aa3c2c48de9f7c94b3e184576 to your computer and use it in GitHub Desktop.

Select an option

Save daniels-martins/611aca6aa3c2c48de9f7c94b3e184576 to your computer and use it in GitHub Desktop.
<?php
namespace App\Livewire\Admin;
use App\Models\AcademicSession;
use App\Models\StudentResult;
use App\Models\SubGrade;
use App\Models\Term;
use App\Models\User;
use InvalidArgumentException;
use Livewire\Attributes\On;
use Livewire\Component;
class ResultSummary extends Component
{
// for presentation
public string $classArm;
public string $currentTerm;
// for data manipulation
public int $currentTermId;
public int $classArmId;
public int $studentCount;
public $classAvg;
// for table data manipulation and generation (avg)
// public $relatedAssessmentsIds;
public function render()
{
return view('livewire.admin.result-summary');
}
public function mount()
{
$this->populateCurrentTerm();
}
#[On('plsUpdateTotalResultAvgForSubgradeInUI')]
public function computeTotalClassAvgForTerm($studentResult)
{
$this->classAvg = StudentResult::getAvgOfResultForAnAcademicSet($studentResult);
}
#[On('updatedSelectedGrade')]
#[On('updatedSelectedSubGrade')]
public function populateClassArm($subgradeId, $type)
{
$idFieldMap = [
'App\Models\Grade' => 'gradeId',
'App\Models\SubGrade' => 'subgradeId',
];
$idField = $idFieldMap[$type] ?? null;
if ($idField === null) {
throw new InvalidArgumentException("Invalid type provided: $type ...... expected types include only 'App\Models\Grade' OR 'App\Models\SubGrade' ");
}
// Determine which event was triggered
if ($idField == 'gradeId' || ! ($this->currentTerm && $subgradeId)) {
$this->dispatch('codeUpdatedStudentCount', []);
return 0;
}
// this means we got ($idField == 'subgradeId')
$subGrade = SubGrade::find($subgradeId);
$this->classArmId = $subgradeId;
$this->classArm = $subGrade->grade->name.'-'.$subGrade->name;
// DECLARATIONS FOR DATA TRANSFER TO ANOTHER COMPONENT
$assocAcademicSession = AcademicSession::find(Term::getCurrentRecord()->academic_session_id);
$assocStudents = User::fetchStudentsV2([
'academic_session_id' => $assocAcademicSession->id,
'sub_grade_id' => $subgradeId,
])->get();
// ===eol===DECLARATIONS FOR DATA TRANSFER ====================
$data = [
'subgrade' => $subGrade,
'selectedTerm' => Term::getCurrentRecord(),
'assocAcademicSession' => $assocAcademicSession,
'assocStudents' => $assocStudents,
];
$this->studentCount = $assocStudents->count();
$this->dispatch('codeUpdatedStudentCount', $data);
return 0;
}
// updatedCurrentTerm
public function populateCurrentTerm()
{
$this->currentTermId = (int) Term::getCurrentRecord()->id;
$assocAcademicSession = AcademicSession::find(Term::getCurrentRecord()->academic_session_id);
$this->currentTerm = $assocAcademicSession->name.' '.Term::getCurrentRecord()->name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment