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('Teacher Subject') ->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('Attendance Date') ->required() ->default(now()) ->live() ->afterStateUpdated(function ($state) { $this->attendanceDate = $state; $this->loadStudents(); }), Select::make('semester') ->label('Semester') ->required() ->options([ 'first' => 'First Semester', 'second' => 'Second Semester' ]) ->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('Attendance saved successfully ' . $this->teacherSubject->id) ->success() ->send(); $this->loadStudents(); } protected function getHeaderActions(): array { return [ Actions\Action::make('back') ->label('Back to Attendances') ->url(static::getResource()::getUrl('index')), ]; } }