227 lines
7.2 KiB
PHP
227 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages;
|
|
|
|
use App\Models\AcademicYear;
|
|
use App\Models\Assessment;
|
|
use App\Models\ClassRoom;
|
|
use App\Models\ClassStudent;
|
|
use App\Models\ClassSubject;
|
|
use App\Models\Student;
|
|
use App\Models\Subject;
|
|
use App\Models\TeacherSubject;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Form;
|
|
use Filament\Pages\Page;
|
|
|
|
class StudentReport extends Page
|
|
{
|
|
|
|
protected static ?string $navigationIcon = 'heroicon-o-document-text';
|
|
|
|
protected static string $view = 'filament.pages.student-report';
|
|
|
|
protected static ?string $navigationGroup = 'Academic Management';
|
|
|
|
public $class_id;
|
|
public $academic_year;
|
|
public $semester;
|
|
public ?array $data;
|
|
|
|
public $list;
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
return auth()->user()->hasAnyRole(['super_admin']);
|
|
}
|
|
|
|
public static function shouldRegisterNavigation(): bool
|
|
{
|
|
return auth()->user()->hasAnyRole(['super_admin']);
|
|
}
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->class = null;
|
|
$this->academicYear = null;
|
|
$this->semester = null;
|
|
$this->data = [];
|
|
}
|
|
|
|
public function form(Form $form): Form
|
|
{
|
|
return $form->schema([
|
|
Select::make('class_id')
|
|
->label('Class')
|
|
->required()
|
|
->options(ClassRoom::pluck('class_name', 'id')->toArray())
|
|
->searchable()
|
|
->reactive()
|
|
->afterStateUpdated(function ($state) {
|
|
$this->class_id = $state;
|
|
$this->data['class_id'] = $state; // Update data array
|
|
$this->loadData();
|
|
}),
|
|
|
|
Select::make('academic_year')
|
|
->label('Academic Year')
|
|
->required()
|
|
->options(AcademicYear::pluck('name', 'id')->toArray())
|
|
->searchable()
|
|
->reactive()
|
|
->afterStateUpdated(function ($state) {
|
|
$this->academic_year = $state;
|
|
$this->data['academic_year'] = $state; // Update data array
|
|
$this->loadData();
|
|
}),
|
|
|
|
Select::make('semester')
|
|
->label('Semester')
|
|
->required()
|
|
->options([
|
|
'first' => 'First Semester',
|
|
'second' => 'Second Semester'
|
|
])
|
|
->reactive()
|
|
->afterStateUpdated(function ($state) {
|
|
$this->semester = $state;
|
|
$this->data['semester'] = $state;
|
|
$this->loadData();
|
|
}),
|
|
])->columns(3);
|
|
}
|
|
|
|
protected function loadData(): void
|
|
{
|
|
if (count($this->data) < 3) {
|
|
$this->list = [];
|
|
return;
|
|
}
|
|
|
|
$groupedAssessment = [];
|
|
|
|
$assessments = Assessment::where('semester', $this->semester)
|
|
->whereHas('teacherSubject', function($query) {
|
|
$query->where('academic_year_id', $this->academic_year)
|
|
->where('class_id', $this->class_id);
|
|
})
|
|
->with('teacherSubject', 'student')
|
|
->get()
|
|
->toArray();
|
|
|
|
$classSubjects = ClassSubject::with(['subject', 'class', 'academicYear'])
|
|
->where('class_room_id', $this->class_id)
|
|
->where('academic_year_id', $this->academic_year)
|
|
->get()
|
|
->sortByDesc(function ($item) {
|
|
return $item->subject->name;
|
|
})
|
|
->toArray();
|
|
|
|
|
|
$header = [];
|
|
|
|
foreach ($classSubjects as $classSubject) {
|
|
$category = strtolower($classSubject['subject']['category']);
|
|
$subjectName = $classSubject['subject']['name'];
|
|
$subjectId = $classSubject['subject']['id'];
|
|
$header[$category][$subjectId] = $subjectName;
|
|
}
|
|
|
|
$students = ClassStudent::with(['class', 'academicYear', 'student'])
|
|
->where('class_room_id', $this->class_id)
|
|
->where('academic_year_id', $this->academic_year)
|
|
->get()
|
|
->map(function($student) {
|
|
return $student['student'];
|
|
})
|
|
->toArray();
|
|
|
|
$finals = [];
|
|
foreach ($students as $student) {
|
|
$studentData = [
|
|
'student_id' => $student['id'],
|
|
'name' => $student['full_name'],
|
|
];
|
|
|
|
foreach ($header as $category => $subjects) {
|
|
foreach ($subjects as $subjectId => $subjectName) {
|
|
$matchingAssessment = collect($assessments)->first(function ($a) use ($student, $subjectId) {
|
|
return $a['student_id'] == $student['id'] && $a['teacher_subject']['subject_id'] == $subjectId;
|
|
});
|
|
|
|
$studentData[$category][$subjectId] = $matchingAssessment['score'] ?? '-'; // atau null
|
|
}
|
|
}
|
|
|
|
$finals[] = $studentData;
|
|
}
|
|
|
|
$result = [];
|
|
|
|
foreach ($finals as $final => $fnl) {
|
|
$existStudent = Student::where('id', $fnl['student_id'])->firstOrFail();
|
|
|
|
$studentData = [
|
|
'name' => $fnl['name'],
|
|
'id' => $fnl['student_id'],
|
|
];
|
|
|
|
$mapel = $fnl['umum'] ?? null;
|
|
|
|
if ($mapel) {
|
|
foreach ($mapel as $key => $value) {
|
|
$existSubject = Subject::where('id', $key)->firstOrFail();
|
|
|
|
if ($existSubject->is_religious) {
|
|
$studentReligion = strtolower($existStudent->religion); // contoh: "islam"
|
|
$subjectName = strtolower($existSubject->name); // contoh: "pendidikan agama islam"
|
|
|
|
if (str_contains($subjectName, $studentReligion)) {
|
|
// Hanya mapel agama yang sesuai dengan agama siswa dimasukkan ke umum[0]
|
|
$studentData['umum'][0] = $value;
|
|
}
|
|
// Mapel agama lain tidak dimasukkan sama sekali
|
|
} else {
|
|
$studentData['umum'][$key] = $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
$studentData['muatan lokal'] = $fnl['muatan lokal'] ?? null;
|
|
$studentData['seni'] = $fnl['seni'] ?? null;
|
|
|
|
$result[] = $studentData;
|
|
}
|
|
|
|
$groupedAssessment["data"] = $result;
|
|
|
|
$groupedSubjectsHeader = [];
|
|
|
|
$groupedSubjectsHeader['name'] = "Nama";
|
|
|
|
$religionAdded = false;
|
|
|
|
foreach ($classSubjects as $classSubject) {
|
|
$category = strtolower($classSubject['subject']['category']);
|
|
$subjectName = $classSubject['subject']['name'];
|
|
$isReligion = $classSubject['subject']['is_religious'];
|
|
$subjectId = $classSubject['subject']['id'];
|
|
|
|
if ($isReligion) {
|
|
if (!$religionAdded) {
|
|
$groupedSubjectsHeader['umum'][0] = 'Pendidikan Agama';
|
|
$religionAdded = true;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
$groupedSubjectsHeader[$category][$subjectId] = $subjectName;
|
|
}
|
|
|
|
$groupedAssessment["header"] = $groupedSubjectsHeader;
|
|
|
|
$this->list = $groupedAssessment;
|
|
}
|
|
}
|