214 lines
7.3 KiB
PHP

<?php
namespace App\Filament\Resources\AttendancesResource\Pages;
use App\Filament\Resources\AttendancesResource;
use App\Models\Assessment;
use App\Models\Attendances;
use App\Models\ClassRoom;
use App\Models\ClassStudent;
use App\Models\HomeRoomTeacher;
use App\Models\Student;
use App\Models\TeacherSubject;
use Filament\Actions;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\Page;
use Illuminate\Support\Facades\DB;
class MultipleAttendances extends Page
{
protected static string $resource = AttendancesResource::class;
protected static string $view = 'filament.resources.attendances-resource.pages.multiple-attendances';
public ?array $data = [];
public $classroomId;
public $teacherSubjectId;
public $teacherSubject;
public $attendanceDate;
public $semester = 'first';
public $students = [];
public function mount(): void
{
$this->form->fill([
'date' => now()->format('Y-m-d'),
'semester' => 'first',
]);
$this->attendanceDate = now()->format('Y-m-d');
}
public function form(Form $form): Form
{
return $form
->schema([
Select::make('teacher_subject_id')
->label('Guru per-Mapel')
->required()
->options(function () {
$user = auth()->user();
$query = TeacherSubject::with(['teacher', 'subject', 'class', 'academicYear']);
if ($user->hasRole('teacher')) {
// Ambil ID kelas dimana user menjadi wali kelas
$homeRoomClassIds = HomeRoomTeacher::where('teacher_id', $user->id)
->pluck('class_room_id')
->toArray();
$query->where(function($q) use ($user, $homeRoomClassIds) {
// Mata pelajaran yang diajarkan oleh guru ini
$q->where('teacher_id', $user->id)
// ATAU kelas dimana dia menjadi wali kelas
->orWhereIn('class_id', $homeRoomClassIds);
});
}
return $query->get()->mapWithKeys(fn ($item) => [
$item->id => sprintf('%s - %s - %s - %s',
$item->teacher->name,
$item->subject->name,
$item->class->class_name,
$item->academicYear->name
)
]);
})
->searchable()
->live()
->afterStateUpdated(function ($state) {
$this->teacherSubjectId = $state;
$this->loadStudents();
}),
DatePicker::make('date')
->label('Tanggal Absensi')
->required()
->default(now())
->live()
->afterStateUpdated(function ($state) {
$this->attendanceDate = $state;
$this->loadStudents();
}),
Select::make('semester')
->label('Semester')
->required()
->options([
'first' => 'Ganjil',
'second' => 'Genap'
])
->live()
->afterStateUpdated(function ($state) {
$this->semester = $state;
$this->loadStudents();
}),
])
->statePath('data')
->columns(3);
}
protected function loadStudents(): void
{
if (!$this->attendanceDate || !$this->teacherSubjectId) {
$this->students = [];
return;
}
$this->teacherSubject = TeacherSubject::where('id', $this->teacherSubjectId)->firstOrFail();
$classStudents = ClassStudent::where('class_room_id', $this->teacherSubject->class_id)
->where('academic_year_id', $this->teacherSubject->academic_year_id)
->with('student')
->get();
$isSubjectReligion = $this->teacherSubject->subject->is_religious;
$subjectName = strtolower($this->teacherSubject->subject->name);
$result = [];
foreach ($classStudents as $classStudent) {
$student = $classStudent->student;
if (!$student) continue;
if ($isSubjectReligion) {
$studentReligion = strtolower($student->religion ?? '');
if (!str_contains($subjectName, $studentReligion)) {
continue;
}
}
$existingAttendance = Attendances::where('student_id', $student->id)
->where('teacher_subject_id', $this->teacherSubjectId)
->whereDate('date', $this->attendanceDate)
->where('semester', $this->semester)
->first();
$result[] = [
'id' => $student->id,
'name' => $student->full_name,
'nis' => $student->nis,
'status' => $existingAttendance ? $existingAttendance->status : null,
'attendance_id' => $existingAttendance ? $existingAttendance->id : null,
];
}
$this->students = $result;
}
public function markAll($status): void
{
foreach ($this->students as $key => $student) {
$this->students[$key]['status'] = $status;
}
}
public function submit(): void
{
DB::transaction(function () {
foreach ($this->students as $student) {
if ($student['status']) {
if ($student['attendance_id']) {
// Update existing attendance
Attendances::where('id', $student['attendance_id'])
->update([
'status' => $student['status'],
'recorded_by' => auth()->id(),
]);
} else {
// Create new attendance
Attendances::create([
'student_id' => $student['id'],
'teacher_subject_id' => $this->teacherSubject->id,
'date' => $this->attendanceDate,
'status' => $student['status'],
'semester' => $this->semester,
'recorded_by' => auth()->id(),
]);
}
}
}
});
Notification::make()
->title('Absensi berhasil disimpan!')
->success()
->send();
$this->loadStudents();
}
protected function getHeaderActions(): array
{
return [
Actions\Action::make('back')
->label('Kembali ke halaman absensi')
->url(static::getResource()::getUrl('index')),
];
}
}