Skip to content

Instantly share code, notes, and snippets.

@untillnesss
Created November 30, 2024 17:01
Show Gist options
  • Save untillnesss/4afe0b63d7814dfe8475c84a6e7192b4 to your computer and use it in GitHub Desktop.
Save untillnesss/4afe0b63d7814dfe8475c84a6e7192b4 to your computer and use it in GitHub Desktop.
<?php
namespace App\Filament\Clusters\BuLek\Resources\LatihanSoalResource\RelationManagers;
use App\Models\Topic;
use App\Tables\Columns\TestOptionColumn;
use App\Utils\FilamentUtils;
use Filament\Forms;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Columns\Summarizers\Sum;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
class LatihanSoalTopicsRelationManager extends RelationManager
{
protected static string $relationship = 'latihanSoalTopics';
protected static ?string $title = 'Pengaturan Soal';
public function form(Form $form): Form
{
return $form
->schema([
Select::make('topic_id')
->label('Topik')
->searchable()
->required()
->preload()
->live()
->optionsLimit(25)
->afterStateUpdated(function (?string $state, ?string $old, $record) {})
->options(
fn() => Topic::getTopicByRole()
->withCount('soals')
->latest()
->get()
->map(function ($data) {
$data['label_formated'] = $data->name . ' - ' . $data->soals_count . ' Soal';
return $data;
})
->pluck('label_formated', 'id')
),
TextInput::make('item_soal')
->label('Jumlah Soal')
->validationAttribute('Jumlah Soal')
->required()
->numeric()
->minValue(1),
Section::make('Opsi Soal')
->schema([
Checkbox::make('is_random_question')
->label('Acak Soal')
->validationAttribute('Acak Soal'),
Checkbox::make('is_random_answer')
->label('Acak Jawaban')
->validationAttribute('Acak Jawaban'),
])
->columns(2)
->columnSpanFull(),
]);
}
public function table(Table $table): Table
{
return $table
->modelLabel('Pengaturan Topik')
->modifyQueryUsing(function ($query) {
return $query->with([
'topic',
]);
})
->recordTitleAttribute('Topik Soal')
->columns([
Tables\Columns\TextColumn::make('topic.name')
->label('Topik Soal')
->sortable()
->toggleable()
->searchable(),
Tables\Columns\TextColumn::make('item_soal')
->label('Jumlah Soal yang Diambil')
->toggleable()
->sortable()
->searchable()
->summarize(Sum::make()
->label('Jumlah Soal'))
->formatStateUsing(function ($state) {
return $state . ' Soal';
}),
TestOptionColumn::make('is_random_question')
->toggleable()
->label('Opsi'),
FilamentUtils::columnUpdatedAt(),
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make()
->afterFormValidated(function ($data, $action) {
dd($data);
$topik = Topic::query()
->where('id', $data['topic_id'])
->withCount('soals')
->first();
if ($data['item_soal'] > $topik->soals_count) {
Notification::make()
->danger()
->title('Gagal!')
->body('Jumlah soal yang dimasukkan lebih besar dari pada Soal yang tersedia, Maksimal ' . $topik->soals_count . ' Soal.')
->send();
$action->halt();
}
}),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment