sistem-akademik/app/Filament/Resources/AssessmentComponentResource.php
2025-04-11 22:56:42 +07:00

171 lines
5.7 KiB
PHP

<?php
namespace App\Filament\Resources;
use App\Filament\Resources\AssessmentComponentResource\Pages;
use App\Models\AssessmentComponent;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Str;
class AssessmentComponentResource extends Resource
{
protected static ?string $model = AssessmentComponent::class;
protected static ?string $navigationIcon = 'heroicon-o-calculator';
protected static ?string $navigationGroup = 'Data Master';
protected static ?int $navigationSort = 3;
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Section::make('Component Information')
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(100)
->live(onBlur: true)
->afterStateUpdated(function ($set, $state) {
$abbreviation = self::generateAbbreviation($state);
$set('code', $abbreviation);
}),
Forms\Components\TextInput::make('code')
->required()
->maxLength(50)
->unique(ignoreRecord: true)
->disabled()
->dehydrated(),
Forms\Components\TextInput::make('weight')
->required()
->numeric()
->minValue(0)
->maxValue(100)
->suffix('%')
->helperText('Bobot dalam persentase (0-100)'),
Forms\Components\Toggle::make('is_active')
->default(true)
->inline(false)
->onColor('success')
->offColor('danger'),
])->columns(2)
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('code')
->searchable()
->sortable()
->badge()
->color('info'),
Tables\Columns\TextColumn::make('weight')
->numeric()
->suffix('%')
->sortable()
->alignCenter(),
Tables\Columns\IconColumn::make('is_active')
->boolean()
->sortable()
->alignCenter(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
Tables\Filters\SelectFilter::make('is_active')
->options([
true => 'Active',
false => 'Inactive',
])
->label('Status'),
])
->actions([
Tables\Actions\EditAction::make()
->iconButton(),
Tables\Actions\DeleteAction::make()
->iconButton(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
])
->emptyStateActions([
Tables\Actions\CreateAction::make(),
])
->defaultSort('weight', 'desc');
}
public static function getPages(): array
{
return [
'index' => Pages\ListAssessmentComponents::route('/'),
'create' => Pages\CreateAssessmentComponent::route('/create'),
'edit' => Pages\EditAssessmentComponent::route('/{record}/edit'),
];
}
protected static function generateAbbreviation(string $name): string
{
// Pastikan nama tidak kosong
if (empty(trim($name))) {
return 'DFT'; // Default value jika kosong
}
$commonAbbreviations = [
'ulangan harian' => 'UH',
'penilaian tengah semester' => 'PTS',
'penilaian akhir semester' => 'PAS',
'tugas' => 'TGS',
'praktikum' => 'PRK',
'kehadiran' => 'ABS',
'proyek' => 'PRJ',
];
$lowerName = Str::lower(trim($name));
// Cek apakah nama cocok dengan daftar singkatan umum
if (array_key_exists($lowerName, $commonAbbreviations)) {
return $commonAbbreviations[$lowerName];
}
// Logika untuk membuat singkatan dari kata pertama setiap kata
$words = preg_split('/\s+/', $lowerName);
$abbreviation = '';
foreach ($words as $word) {
// Ambil huruf pertama dari setiap kata yang tidak kosong
if (!empty($word)) {
$abbreviation .= strtoupper($word[0]);
}
}
// Jika hasil singkatan terlalu pendek, ambil 3 huruf pertama (tanpa spasi)
if (strlen($abbreviation) < 2) {
$cleaned = preg_replace('/\s+/', '', $name);
$abbreviation = strtoupper(substr($cleaned, 0, 3));
}
return $abbreviation ?: 'DFT'; // Fallback jika masih kosong
}
}