| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace App\Filament\Pages;
- use App\Models\Question;
- use App\Services\QuestionTemReviewService;
- use BackedEnum;
- use Filament\Notifications\Notification;
- use Filament\Pages\Page;
- use Filament\Support\Enums\Width;
- use Illuminate\Support\Facades\Schema;
- use Livewire\Attributes\Computed;
- use UnitEnum;
- /**
- * 展示本会话内从待入库页成功写入的题目,便于事后统一微调 difficulty(0.00~0.90)。
- */
- class QuestionImportedDifficultyTune extends Page
- {
- protected static ?string $title = '入库题目调难度';
- protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-adjustments-horizontal';
- protected static ?string $navigationLabel = '入库题目调难度';
- protected static string|UnitEnum|null $navigationGroup = '题库管理';
- protected static ?int $navigationSort = 5;
- protected Width|string|null $maxContentWidth = Width::Full;
- protected string $view = 'filament.pages.question-imported-difficulty-tune';
- public ?int $selectedQuestionId = null;
- public string $difficultyInput = '0.50';
- #[Computed(cache: false)]
- public function tuningQuestionIds(): array
- {
- $raw = session(QuestionTemReviewService::SESSION_TUNING_QUESTION_IDS, []);
- return is_array($raw) ? array_values(array_unique(array_filter(array_map('intval', $raw)))) : [];
- }
- #[Computed(cache: false)]
- public function selectedQuestion(): ?Question
- {
- if (! $this->selectedQuestionId || ! Schema::hasTable('questions')) {
- return null;
- }
- return Question::query()->find($this->selectedQuestionId);
- }
- public function selectQuestion(int $id): void
- {
- if ($id <= 0) {
- return;
- }
- if (! in_array($id, $this->tuningQuestionIds, true)) {
- Notification::make()->title('该题不在当前列表中')->warning()->send();
- return;
- }
- $this->selectedQuestionId = $id;
- $q = Question::query()->find($id);
- if ($q) {
- $d = max(0.0, min(0.9, round((float) ($q->difficulty ?? 0.5), 2)));
- $this->difficultyInput = number_format($d, 2, '.', '');
- }
- }
- public function saveDifficulty(): void
- {
- if (! $this->selectedQuestionId) {
- Notification::make()->title('请先选择题目')->warning()->send();
- return;
- }
- if (! in_array($this->selectedQuestionId, $this->tuningQuestionIds, true)) {
- Notification::make()->title('该题不在当前列表中')->warning()->send();
- return;
- }
- $raw = trim($this->difficultyInput);
- if ($raw === '' || ! is_numeric($raw)) {
- Notification::make()->title('难度格式不正确')->danger()->send();
- return;
- }
- $value = round((float) $raw, 2);
- if ($value < 0.0 || $value > 0.9) {
- Notification::make()->title('难度须在 0.00~0.90 之间')->danger()->send();
- return;
- }
- Question::query()->where('id', $this->selectedQuestionId)->update([
- 'difficulty' => $value,
- ]);
- $this->difficultyInput = number_format($value, 2, '.', '');
- Notification::make()
- ->title('已更新 difficulty')
- ->body(sprintf('question_id: %d · difficulty: %s', $this->selectedQuestionId, $this->difficultyInput))
- ->success()
- ->send();
- $this->dispatch('$refresh');
- }
- public function clearTuningList(): void
- {
- session()->forget(QuestionTemReviewService::SESSION_TUNING_QUESTION_IDS);
- $this->selectedQuestionId = null;
- $this->difficultyInput = '0.50';
- Notification::make()->title('已清空列表记录(仅本会话)')->success()->send();
- $this->dispatch('$refresh');
- }
- public function removeFromList(int $questionId): void
- {
- $ids = array_values(array_filter($this->tuningQuestionIds, fn (int $x) => $x !== $questionId));
- session([QuestionTemReviewService::SESSION_TUNING_QUESTION_IDS => $ids]);
- if ($this->selectedQuestionId === $questionId) {
- $this->selectedQuestionId = null;
- $this->difficultyInput = '0.50';
- }
- $this->dispatch('$refresh');
- }
- }
|