130 lines
3.7 KiB
PHP
130 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources\AssessmentResource\Pages;
|
|
|
|
use App\Filament\Resources\AssessmentResource;
|
|
use App\Models\Assessment;
|
|
use App\Models\Attendances;
|
|
use Filament\Actions;
|
|
use Filament\Resources\Pages\page;
|
|
use App\Models\Student;
|
|
use App\Models\TeacherSubject;
|
|
use Filament\Forms;
|
|
use Filament\Notifications\Notification;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class MultipleAssessments extends page
|
|
{
|
|
protected static string $resource = AssessmentResource::class;
|
|
|
|
protected static string $view = 'filament.resources.assessment-resource.pages.multiple-assessment';
|
|
|
|
use Forms\Concerns\InteractsWithForms;
|
|
|
|
public ?int $teacherSubjectId = null;
|
|
|
|
public array $students = [];
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->form->fill();
|
|
}
|
|
|
|
protected function getFormSchema(): array
|
|
{
|
|
return [
|
|
Forms\Components\Select::make('teacherSubjectId')
|
|
->label('Teacher Subject')
|
|
->options(
|
|
TeacherSubject::with(['teacher', 'subject', 'class'])->get()->mapWithKeys(function ($item) {
|
|
return [
|
|
$item->id => "{$item->teacher->name} - {$item->subject->name} - {$item->class->class_name}"
|
|
];
|
|
})->toArray()
|
|
)
|
|
->searchable()
|
|
->reactive()
|
|
->afterStateUpdated(fn ($state) => $this->loadStudents()),
|
|
];
|
|
}
|
|
|
|
public function loadStudents(): void
|
|
{
|
|
if (!$this->teacherSubjectId) {
|
|
$this->students = [];
|
|
return;
|
|
}
|
|
|
|
$teacherSubject = TeacherSubject::find($this->teacherSubjectId);
|
|
|
|
if (!$teacherSubject) {
|
|
$this->students = [];
|
|
|
|
Notification::make()
|
|
->title('Not Found')
|
|
->body('Selected teacher subject not found.')
|
|
->danger()
|
|
->send();
|
|
|
|
return;
|
|
}
|
|
|
|
$this->students = Student::where('class_id', $teacherSubject->class_id)
|
|
->get()
|
|
->map(function ($student) {
|
|
$existingAssessment = Assessment::where('student_id', $student->id)
|
|
->where('teacher_subject_id', $this->teacherSubjectId)
|
|
->first();
|
|
|
|
return [
|
|
'id' => $student->id,
|
|
'name' => $student->full_name,
|
|
'nis' => $student->nis,
|
|
'score' => $existingAssessment ? $existingAssessment->score : null,
|
|
];
|
|
})->toArray();
|
|
}
|
|
|
|
public function submit(): void
|
|
{
|
|
if (!$this->teacherSubjectId || empty($this->students)) {
|
|
Notification::make()
|
|
->title('Error')
|
|
->body('Please select a teacher subject and enter student scores.')
|
|
->danger()
|
|
->send();
|
|
return;
|
|
}
|
|
|
|
foreach ($this->students as $student) {
|
|
if (!isset($student['score'])) continue;
|
|
|
|
Assessment::updateOrCreate(
|
|
[
|
|
'student_id' => $student['id'],
|
|
'teacher_subject_id' => $this->teacherSubjectId,
|
|
],
|
|
[
|
|
'score' => $student['score'],
|
|
]
|
|
);
|
|
}
|
|
|
|
Notification::make()
|
|
->title('Success')
|
|
->body('Assessments saved successfully.')
|
|
->success()
|
|
->send();
|
|
}
|
|
|
|
protected function getHeaderActions(): array
|
|
{
|
|
return [
|
|
Actions\Action::make('back')
|
|
->label('Back to Assessments')
|
|
->url(static::getResource()::getUrl('index')),
|
|
];
|
|
}
|
|
|
|
}
|