feat: implement search functionality for assignments with course name filtering and update permissions
This commit is contained in:
parent
4a421c6981
commit
15b9ebc084
@ -13,13 +13,19 @@
|
|||||||
use App\Models\AssignmentPin;
|
use App\Models\AssignmentPin;
|
||||||
use App\Settings\GeneralSettings;
|
use App\Settings\GeneralSettings;
|
||||||
use Filament\Actions\Action;
|
use Filament\Actions\Action;
|
||||||
|
use Filament\Forms\Components\TextInput;
|
||||||
use Filament\Resources\Pages\Page;
|
use Filament\Resources\Pages\Page;
|
||||||
|
use Filament\Schemas\Schema;
|
||||||
use Illuminate\Database\Eloquent\Collection;
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
use Livewire\Attributes\Computed;
|
use Livewire\Attributes\Computed;
|
||||||
|
use Livewire\Attributes\Url;
|
||||||
|
|
||||||
class ListAssignments extends Page
|
class ListAssignments extends Page
|
||||||
{
|
{
|
||||||
|
#[Url]
|
||||||
|
public ?string $search = '';
|
||||||
|
|
||||||
protected static string $resource = AssignmentResource::class;
|
protected static string $resource = AssignmentResource::class;
|
||||||
|
|
||||||
protected string $view = 'filament.resources.learning.assignments.index';
|
protected string $view = 'filament.resources.learning.assignments.index';
|
||||||
@ -28,6 +34,32 @@ class ListAssignments extends Page
|
|||||||
|
|
||||||
protected static ?string $recordTitleAttribute = 'title';
|
protected static ?string $recordTitleAttribute = 'title';
|
||||||
|
|
||||||
|
public static function getPagePermission(): string
|
||||||
|
{
|
||||||
|
return 'View:CourseSchedule';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function mount(): void
|
||||||
|
{
|
||||||
|
$this->form->fill();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function form(Schema $schema): Schema
|
||||||
|
{
|
||||||
|
return $schema
|
||||||
|
->schema([
|
||||||
|
TextInput::make('search')
|
||||||
|
->label('Cari')
|
||||||
|
->placeholder('Cari Judul Tugas atau Mata Kuliah...')
|
||||||
|
->autocomplete(false)
|
||||||
|
->live(debounce: 500)
|
||||||
|
->afterStateUpdated(fn ($state) => $this->search = $state),
|
||||||
|
])
|
||||||
|
->columns([
|
||||||
|
'sm' => 1,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
protected function getHeaderActions(): array
|
protected function getHeaderActions(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
@ -90,6 +122,12 @@ public function assignments(): Collection
|
|||||||
$query->whereHas('students', fn ($q) => $q->whereKey($studentProfile->id))
|
$query->whereHas('students', fn ($q) => $q->whereKey($studentProfile->id))
|
||||||
->orWhereHas('studyGroups', fn ($q) => $q->where('leader_id', $studentProfile->id)->orWhereHas('students', fn ($sq) => $sq->whereKey($studentProfile->id)));
|
->orWhereHas('studyGroups', fn ($q) => $q->where('leader_id', $studentProfile->id)->orWhereHas('students', fn ($sq) => $sq->whereKey($studentProfile->id)));
|
||||||
})
|
})
|
||||||
|
->when($this->search, function ($query) {
|
||||||
|
$query->where(function ($q) {
|
||||||
|
$q->where('title', 'like', "%{$this->search}%")
|
||||||
|
->orWhereHas('course', fn ($cq) => $cq->where('name', 'like', "%{$this->search}%"));
|
||||||
|
});
|
||||||
|
})
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
return $assignments->sort(function ($a, $b) use ($pinnedIds) {
|
return $assignments->sort(function ($a, $b) use ($pinnedIds) {
|
||||||
|
|||||||
@ -6,6 +6,12 @@
|
|||||||
<x-slot name="heading">{{ $this->heading }}</x-slot>
|
<x-slot name="heading">{{ $this->heading }}</x-slot>
|
||||||
<x-slot name="description">{{ $this->description }}</x-slot>
|
<x-slot name="description">{{ $this->description }}</x-slot>
|
||||||
|
|
||||||
|
<div class="flex items-center justify-between gap-4 mb-6">
|
||||||
|
<div class="w-full max-w-xl">
|
||||||
|
{{ $this->form }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="space-y-4">
|
<div class="space-y-4">
|
||||||
@forelse ($this->assignmentCards as $card)
|
@forelse ($this->assignmentCards as $card)
|
||||||
|
|
||||||
@ -107,7 +113,7 @@ class="{{ $card->indicator_icon_classes }}"
|
|||||||
</div>
|
</div>
|
||||||
@endif
|
@endif
|
||||||
</div>
|
</div>
|
||||||
@empty
|
@empty
|
||||||
<x-filament::empty-state icon="heroicon-o-clipboard-document-list"
|
<x-filament::empty-state icon="heroicon-o-clipboard-document-list"
|
||||||
heading="{{ \App\Filament\Support\SystemNotification::getMessage('Hore, Belum Ada Tugas! 🎊✨', 'Tidak ada data yang ditemukan') }}"
|
heading="{{ \App\Filament\Support\SystemNotification::getMessage('Hore, Belum Ada Tugas! 🎊✨', 'Tidak ada data yang ditemukan') }}"
|
||||||
description="{{ \App\Filament\Support\SystemNotification::getMessage('Belum ada tugas baru yang perlu dikerjakan. Hidup tenang tanpa beban tugas itu asik ya! 😊🌈', 'Belum ada tugas baru yang perlu dikerjakan untuk saat ini. Tetap semangat!') }}"
|
description="{{ \App\Filament\Support\SystemNotification::getMessage('Belum ada tugas baru yang perlu dikerjakan. Hidup tenang tanpa beban tugas itu asik ya! 😊🌈', 'Belum ada tugas baru yang perlu dikerjakan untuk saat ini. Tetap semangat!') }}"
|
||||||
|
|||||||
@ -34,6 +34,7 @@
|
|||||||
'Create:Assignment',
|
'Create:Assignment',
|
||||||
'Update:Assignment',
|
'Update:Assignment',
|
||||||
'Delete:Assignment',
|
'Delete:Assignment',
|
||||||
|
'View:CourseSchedule',
|
||||||
];
|
];
|
||||||
|
|
||||||
foreach ($permissions as $permission) {
|
foreach ($permissions as $permission) {
|
||||||
@ -48,11 +49,11 @@
|
|||||||
it('restricts actions to Developer and Kosma, while allowing students to view and submit', function () {
|
it('restricts actions to Developer and Kosma, while allowing students to view and submit', function () {
|
||||||
$developer = User::factory()->create();
|
$developer = User::factory()->create();
|
||||||
$developer->assignRole(RoleEnum::Developer);
|
$developer->assignRole(RoleEnum::Developer);
|
||||||
$developer->givePermissionTo(['ViewAny:Assignment', 'Create:Assignment']);
|
$developer->givePermissionTo(['ViewAny:Assignment', 'Create:Assignment', 'View:CourseSchedule']);
|
||||||
|
|
||||||
$studentUser = User::factory()->create();
|
$studentUser = User::factory()->create();
|
||||||
$studentUser->assignRole(RoleEnum::Student);
|
$studentUser->assignRole(RoleEnum::Student);
|
||||||
$studentUser->givePermissionTo(['ViewAny:Assignment']);
|
$studentUser->givePermissionTo(['ViewAny:Assignment', 'View:CourseSchedule']);
|
||||||
$student = Student::factory()->create(['user_id' => $studentUser->id]);
|
$student = Student::factory()->create(['user_id' => $studentUser->id]);
|
||||||
|
|
||||||
$this->actingAs($developer);
|
$this->actingAs($developer);
|
||||||
@ -81,7 +82,7 @@
|
|||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
$user = User::factory()->create();
|
$user = User::factory()->create();
|
||||||
$user->assignRole(RoleEnum::Developer);
|
$user->assignRole(RoleEnum::Developer);
|
||||||
$user->givePermissionTo(['ViewAny:Assignment', 'Create:Assignment', 'Update:Assignment', 'Delete:Assignment']);
|
$user->givePermissionTo(['ViewAny:Assignment', 'Create:Assignment', 'Update:Assignment', 'Delete:Assignment', 'View:CourseSchedule']);
|
||||||
$this->actingAs($user);
|
$this->actingAs($user);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -126,7 +127,7 @@
|
|||||||
it('allows individual submission', function () {
|
it('allows individual submission', function () {
|
||||||
$user = User::factory()->create();
|
$user = User::factory()->create();
|
||||||
$user->assignRole(RoleEnum::Student);
|
$user->assignRole(RoleEnum::Student);
|
||||||
$user->givePermissionTo(['ViewAny:Assignment']);
|
$user->givePermissionTo(['ViewAny:Assignment', 'View:CourseSchedule']);
|
||||||
$student = Student::factory()->create(['user_id' => $user->id]);
|
$student = Student::factory()->create(['user_id' => $user->id]);
|
||||||
|
|
||||||
$assignment = Assignment::factory()->create([
|
$assignment = Assignment::factory()->create([
|
||||||
@ -156,12 +157,12 @@
|
|||||||
|
|
||||||
$leaderUser = User::factory()->create();
|
$leaderUser = User::factory()->create();
|
||||||
$leaderUser->assignRole(RoleEnum::Student);
|
$leaderUser->assignRole(RoleEnum::Student);
|
||||||
$leaderUser->givePermissionTo(['ViewAny:Assignment']);
|
$leaderUser->givePermissionTo(['ViewAny:Assignment', 'View:CourseSchedule']);
|
||||||
$leader = Student::factory()->create(['user_id' => $leaderUser->id]);
|
$leader = Student::factory()->create(['user_id' => $leaderUser->id]);
|
||||||
|
|
||||||
$memberUser = User::factory()->create();
|
$memberUser = User::factory()->create();
|
||||||
$memberUser->assignRole(RoleEnum::Student);
|
$memberUser->assignRole(RoleEnum::Student);
|
||||||
$memberUser->givePermissionTo(['ViewAny:Assignment']);
|
$memberUser->givePermissionTo(['ViewAny:Assignment', 'View:CourseSchedule']);
|
||||||
$member = Student::factory()->create(['user_id' => $memberUser->id]);
|
$member = Student::factory()->create(['user_id' => $memberUser->id]);
|
||||||
|
|
||||||
$group = StudyGroup::factory()->create([
|
$group = StudyGroup::factory()->create([
|
||||||
@ -195,7 +196,7 @@
|
|||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
$user = User::factory()->create();
|
$user = User::factory()->create();
|
||||||
$user->assignRole(RoleEnum::Student);
|
$user->assignRole(RoleEnum::Student);
|
||||||
$user->givePermissionTo(['ViewAny:Assignment']);
|
$user->givePermissionTo(['ViewAny:Assignment', 'View:CourseSchedule']);
|
||||||
$this->studentProfile = Student::factory()->create(['user_id' => $user->id]);
|
$this->studentProfile = Student::factory()->create(['user_id' => $user->id]);
|
||||||
$this->actingAs($user);
|
$this->actingAs($user);
|
||||||
$this->currentSemester = app(GeneralSettings::class)->current_semester;
|
$this->currentSemester = app(GeneralSettings::class)->current_semester;
|
||||||
@ -224,11 +225,86 @@
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Assignment Search', function () {
|
||||||
|
beforeEach(function () {
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$user->assignRole(RoleEnum::Student);
|
||||||
|
$user->givePermissionTo(['ViewAny:Assignment', 'View:CourseSchedule']);
|
||||||
|
$this->studentProfile = Student::factory()->create(['user_id' => $user->id]);
|
||||||
|
$this->actingAs($user);
|
||||||
|
$this->currentSemester = app(GeneralSettings::class)->current_semester;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can search assignments by title', function () {
|
||||||
|
$course = Course::factory()->create(['semester' => $this->currentSemester]);
|
||||||
|
|
||||||
|
$assignment1 = Assignment::factory()->create([
|
||||||
|
'title' => 'Tugas Matematika 1',
|
||||||
|
'course_id' => $course->id,
|
||||||
|
'due_date' => now()->addDays(1),
|
||||||
|
]);
|
||||||
|
$assignment2 = Assignment::factory()->create([
|
||||||
|
'title' => 'Tugas Biologi 2',
|
||||||
|
'course_id' => $course->id,
|
||||||
|
'due_date' => now()->addDays(1),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$assignment1->students()->attach($this->studentProfile->id);
|
||||||
|
$assignment2->students()->attach($this->studentProfile->id);
|
||||||
|
|
||||||
|
Livewire::test(ListAssignments::class)
|
||||||
|
->set('search', 'Matematika')
|
||||||
|
->assertSee('Tugas Matematika 1')
|
||||||
|
->assertDontSee('Tugas Biologi 2');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can search assignments by course name', function () {
|
||||||
|
$course1 = Course::factory()->create(['name' => 'Fisika Dasar', 'semester' => $this->currentSemester]);
|
||||||
|
$course2 = Course::factory()->create(['name' => 'Sejarah Indonesia', 'semester' => $this->currentSemester]);
|
||||||
|
|
||||||
|
$assignment1 = Assignment::factory()->create([
|
||||||
|
'title' => 'Laporan Praktikum',
|
||||||
|
'course_id' => $course1->id,
|
||||||
|
'due_date' => now()->addDays(1),
|
||||||
|
]);
|
||||||
|
$assignment2 = Assignment::factory()->create([
|
||||||
|
'title' => 'Makalah',
|
||||||
|
'course_id' => $course2->id,
|
||||||
|
'due_date' => now()->addDays(1),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$assignment1->students()->attach($this->studentProfile->id);
|
||||||
|
$assignment2->students()->attach($this->studentProfile->id);
|
||||||
|
|
||||||
|
Livewire::test(ListAssignments::class)
|
||||||
|
->set('search', 'Fisika')
|
||||||
|
->assertSee('Laporan Praktikum')
|
||||||
|
->assertSee('Fisika Dasar')
|
||||||
|
->assertDontSee('Makalah')
|
||||||
|
->assertDontSee('Sejarah Indonesia');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('shows empty state when no assignments found with search', function () {
|
||||||
|
$course = Course::factory()->create(['semester' => $this->currentSemester]);
|
||||||
|
$assignment = Assignment::factory()->create([
|
||||||
|
'title' => 'Tugas Wajib',
|
||||||
|
'course_id' => $course->id,
|
||||||
|
'due_date' => now()->addDays(1),
|
||||||
|
]);
|
||||||
|
$assignment->students()->attach($this->studentProfile->id);
|
||||||
|
|
||||||
|
Livewire::test(ListAssignments::class)
|
||||||
|
->set('search', 'Random string yang ga ada')
|
||||||
|
->assertDontSee('Tugas Wajib')
|
||||||
|
->assertSee('Hore, Belum Ada Tugas!');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('Assignment Details and Preview', function () {
|
describe('Assignment Details and Preview', function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
$user = User::factory()->create();
|
$user = User::factory()->create();
|
||||||
$user->assignRole(RoleEnum::Developer);
|
$user->assignRole(RoleEnum::Developer);
|
||||||
$user->givePermissionTo(['ViewAny:Assignment', 'View:Assignment']);
|
$user->givePermissionTo(['ViewAny:Assignment', 'View:Assignment', 'View:CourseSchedule']);
|
||||||
$this->actingAs($user);
|
$this->actingAs($user);
|
||||||
$this->currentSemester = app(GeneralSettings::class)->current_semester;
|
$this->currentSemester = app(GeneralSettings::class)->current_semester;
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user