218 lines
6.6 KiB
PHP
218 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages;
|
|
|
|
use App\Models\AcademicYear;
|
|
use App\Models\Assessment;
|
|
use App\Models\ClassRoom;
|
|
use App\Models\ClassSubject;
|
|
use App\Models\CompetencyAchievement;
|
|
use App\Models\ExtracurricularAssessment;
|
|
use App\Models\Student;
|
|
use App\Models\SchoolInformation;
|
|
use App\Models\Subject;
|
|
use App\Models\TeacherSubject;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Pages\Page;
|
|
use Carbon\Carbon;
|
|
use function PHPUnit\Framework\isEmpty;
|
|
use function Symfony\Component\String\s;
|
|
|
|
Carbon::setLocale('id');
|
|
|
|
class ReportPreview extends Page
|
|
{
|
|
protected static ?string $navigationIcon = 'heroicon-o-document-text';
|
|
|
|
protected static string $view = 'filament.pages.report-preview';
|
|
|
|
public $student;
|
|
public $class;
|
|
public $class_name;
|
|
public $academic_year;
|
|
public $semester;
|
|
|
|
public $school_information;
|
|
|
|
public $table = [];
|
|
|
|
public $sakit = 0;
|
|
public $izin = 0;
|
|
public $tanpa_keterangan = 0;
|
|
|
|
public function mount()
|
|
{
|
|
$this->loadData();
|
|
$this->loadAssessment();
|
|
}
|
|
|
|
|
|
public function saveAttendance()
|
|
{
|
|
$this->validate([
|
|
'sakit' => 'required|integer|min:0',
|
|
'izin' => 'required|integer|min:0',
|
|
'tanpa_keterangan' => 'required|integer|min:0',
|
|
]);
|
|
|
|
$this->dispatch('notify', [
|
|
'title' => 'Berhasil',
|
|
'message' => 'Data ketidakhadiran diperbarui',
|
|
'type' => 'success'
|
|
]);
|
|
}
|
|
|
|
protected function loadData() : void
|
|
{
|
|
$this->student = Student::find(request()->query('studentId'));
|
|
$this->class = ClassRoom::find(request()->query('classId'));
|
|
$this->academic_year = AcademicYear::find(request()->query('yearId'));
|
|
$this->semester = request()->query('semester');
|
|
$this->school_information = SchoolInformation::first();
|
|
|
|
$this->class_name = $this->toRoman($this->class->class_level) . ' ' . $this->extractClassLetter($this->class->class_name);
|
|
}
|
|
|
|
protected function loadAssessment(): void
|
|
{
|
|
$requiredParams = ['studentId', 'classId', 'semester', 'yearId'];
|
|
|
|
foreach ($requiredParams as $param) {
|
|
if (blank(request()->query($param))) {
|
|
Notification::make()
|
|
->title('Missing Data')
|
|
->body("The parameter '{$param}' is required.")
|
|
->danger()
|
|
->send();
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
$student_id = request()->query('studentId');
|
|
$class_id = request()->query('classId');
|
|
$semester = request()->query('semester');
|
|
$year_id = request()->query('yearId');
|
|
|
|
// Assessment Mapping
|
|
$assessments = Assessment::where('semester', $this->semester)
|
|
->whereHas('teacherSubject', function($query) use ($class_id, $year_id) {
|
|
$query->where('academic_year_id', $year_id)
|
|
->where('class_id', $class_id);
|
|
})
|
|
->where('student_id', $this->student->id)
|
|
->with('teacherSubject.subject', 'student')
|
|
->get()
|
|
->map(function ($as) use ($class_id) {
|
|
$subject = $as->teacherSubject->subject ?? null;
|
|
|
|
if (!$subject) {
|
|
return null;
|
|
}
|
|
|
|
$competencyAchievements = CompetencyAchievement::where('class_room_id', $class_id)
|
|
->where('subject_id', $subject->id)
|
|
->where('min_score', '<=', $as->score)
|
|
->where('max_score', '>=', $as->score)
|
|
->first();
|
|
|
|
return [
|
|
"score" => $as->score,
|
|
"subject" => $subject->name,
|
|
"category" => $subject->category,
|
|
"competency_achievement" => $competencyAchievements ? $this->student->full_name . '' . $competencyAchievements->description : '-',
|
|
];
|
|
})
|
|
->filter() // Hapus null jika ada
|
|
->sortByDesc('subject')
|
|
->sortBy(function ($item) {
|
|
return $item['category'] === 'umum' ? 0 : 1;
|
|
})
|
|
->values()
|
|
->toArray();
|
|
|
|
$this->table["assessments"]['umum'] = array_filter($assessments, function ($item) {
|
|
return $item["category"] === "umum";
|
|
});
|
|
|
|
$this->table["assessments"]['muatan lokal'] = array_filter($assessments, function ($item) {
|
|
return $item["category"] === "muatan lokal";
|
|
});
|
|
|
|
$this->table["assessments"]['seni'] = array_filter($assessments, function ($item) {
|
|
return $item["category"] === "seni";
|
|
});
|
|
|
|
$extracurricular = ExtracurricularAssessment::with('classStudent','extracurricular')
|
|
->where('semester', $this->semester)
|
|
->whereHas('classStudent', function($query) use ($class_id, $year_id) {
|
|
$query->where('academic_year_id', $year_id)
|
|
->where('class_room_id', $class_id)
|
|
->where('student_id', $this->student->id);
|
|
})
|
|
->get()
|
|
->map(function ($as) {
|
|
return [
|
|
"name" => $as->extracurricular->name,
|
|
"predicate" => $as->predicate,
|
|
"description" => $as->description,
|
|
];
|
|
})
|
|
->toArray();
|
|
|
|
$this->table["extracurricular"] = $extracurricular;
|
|
}
|
|
|
|
public function downloadPdf()
|
|
{
|
|
$view = $this->render()->render();
|
|
$pdf = PDF::loadHTML($view)->setPaper('a4', 'landscape');
|
|
return response()->streamDownload(function () use ($pdf) {
|
|
echo $pdf->stream();
|
|
}, $this->student->full_name . '.pdf');
|
|
}
|
|
|
|
public function extractClassLetter($className)
|
|
{
|
|
preg_match('/[A-Z]+$/i', trim($className), $matches);
|
|
return $matches[0] ?? '';
|
|
}
|
|
|
|
public function toRoman($number)
|
|
{
|
|
$map = [
|
|
'M' => 1000,
|
|
'CM' => 900,
|
|
'D' => 500,
|
|
'CD' => 400,
|
|
'C' => 100,
|
|
'XC' => 90,
|
|
'L' => 50,
|
|
'XL' => 40,
|
|
'X' => 10,
|
|
'IX' => 9,
|
|
'V' => 5,
|
|
'IV' => 4,
|
|
'I' => 1,
|
|
];
|
|
|
|
$returnValue = '';
|
|
while ($number > 0) {
|
|
foreach ($map as $roman => $int) {
|
|
if ($number >= $int) {
|
|
$number -= $int;
|
|
$returnValue .= $roman;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $returnValue;
|
|
}
|
|
|
|
public static function shouldRegisterNavigation(): bool
|
|
{
|
|
return false;
|
|
}
|
|
}
|