Skip to content

Instantly share code, notes, and snippets.

@daniels-martins
Created July 7, 2025 11:15
Show Gist options
  • Save daniels-martins/e58d7c05518e839b6c5f7de8ca53881d to your computer and use it in GitHub Desktop.
Save daniels-martins/e58d7c05518e839b6c5f7de8ca53881d to your computer and use it in GitHub Desktop.
<?php
namespace App\Livewire\Admin;
use App\Models\StudentRecord;
use App\Models\SubgradeSubjectTeacher;
use App\Models\User;
use App\Traits\LivewireTraits\SubjectResultApprovalTrait;
use Livewire\Attributes\On;
use Livewire\Component;
class StudentsResultsTable extends Component
{
use SubjectResultApprovalTrait;
public $studentRecords = [];
/**
* refers to the subjects for this subgrade
*/
public $subjects;
public $subjectNamesArray;
public $reason4Rejection;
// werey dey this one head
// #[On('notifyChangeInResultApprovalStatus')]
public function render()
{
return view('livewire.admin.students-results-table');
}
#[On('codeUpdatedStudentCount')]
public function updateStudentTable($data)
{
if (empty($data)) {
$this->studentRecords = collect([]);
$this->reset('subjects', 'subjectNamesArray');
return 0;
} // $this->students = $data['assocStudents'];
$this->subjects = SubgradeSubjectTeacher::with(['subject', 'teacher'])
->where('sub_grade_id', $data['subgrade']['id'])
->where('term_id', $data['selectedTerm']['id'])
->where('academic_session_id', $data['assocAcademicSession']['id'])
->whereNotNull('subject_id') // Exclude rows with null subject_id
->get();
$this->subjectNamesArray = array_unique($this->subjects->pluck('subject.name', 'subject.id')->toArray());
$studentsInSelectedSubgrade = User::fetchStudentsV2([
'academic_session_id' => $data['assocAcademicSession']['id'],
'sub_grade_id' => $data['subgrade']['id'],
], ['scores.assessment'])->get();
$studentIdsInSelectedSubgrade = array_unique($studentsInSelectedSubgrade->pluck('id')->toArray());
$teacherIdsInThisSubGrade = array_unique($this->subjects->pluck('teacher_id')->toArray());
// dd('students in subgrade : ' ,$studentIdsInSelectedSubgrade, 'teachers in subgrade : ', $teacherIdsInThisSubGrade);//debug
$this->studentRecords = $this->mightyQuery($data, $studentIdsInSelectedSubgrade);
// send Data for mass approval
$this->dispatch('releaseResultsForMassApproval', [
'studentRecords' => $this->studentRecords,
'subjectIdsArray' => array_keys($this->subjectNamesArray),
]);
// this logic is for generating unique links on the blade file to each student result
foreach ($this->studentRecords as $studentRecord) {
$studentRecord->encryptedStudentId = encrypt($studentRecord->student_id);
// this logic keeps updating the subject_iterator (using the latest subject iterator)
session()->put('student_record_for_'.$studentRecord->student_id, $studentRecord); // this works fine : original one to be used by the controller
session()->put('student_record_for_'.$studentRecord->encryptedStudentId, $studentRecord); // encrypted alternative (for the blade file)
}
}
/**
* this computes the assessments to be analyzed for a specific subject
*
* @param mixed $data
* @param mixed $studentIdsInSelectedSubgrade
* @return \Illuminate\Support\Collection represents a joined table of assessmentsToBeAnalized along with their corresponding student information
*/
private function mightyQuery($data, $studentIdsInSelectedSubgrade)
{
return $results = StudentRecord::whereIn('student_id', $studentIdsInSelectedSubgrade)
->with(['student', 'student.studentProfile'])
->withWhereHas('subgradeSubjectTeacher', function ($query) use ($data) {
$query->where('term_id', $data['selectedTerm']['id'])
->where('academic_session_id', $data['assocAcademicSession']['id'])
->with([
'subject' => function ($subjectTable) {
$subjectTable->select('id', 'name'); // Select only id and name from subjects table
},
])
->withWhereHas('presets')
->groupBy('subgrade_subject_teacher.subject_id');
})->get();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment