QuestionImportedDifficultyTune.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Models\Question;
  4. use App\Services\QuestionTemReviewService;
  5. use BackedEnum;
  6. use Filament\Notifications\Notification;
  7. use Filament\Pages\Page;
  8. use Filament\Support\Enums\Width;
  9. use Illuminate\Support\Facades\Schema;
  10. use Livewire\Attributes\Computed;
  11. use UnitEnum;
  12. /**
  13. * 展示本会话内从待入库页成功写入的题目,便于事后统一微调 difficulty(0.00~0.90)。
  14. */
  15. class QuestionImportedDifficultyTune extends Page
  16. {
  17. protected static ?string $title = '入库题目调难度';
  18. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-adjustments-horizontal';
  19. protected static ?string $navigationLabel = '入库题目调难度';
  20. protected static string|UnitEnum|null $navigationGroup = '题库管理';
  21. protected static ?int $navigationSort = 5;
  22. protected Width|string|null $maxContentWidth = Width::Full;
  23. protected string $view = 'filament.pages.question-imported-difficulty-tune';
  24. public ?int $selectedQuestionId = null;
  25. public string $difficultyInput = '0.50';
  26. #[Computed(cache: false)]
  27. public function tuningQuestionIds(): array
  28. {
  29. $raw = session(QuestionTemReviewService::SESSION_TUNING_QUESTION_IDS, []);
  30. return is_array($raw) ? array_values(array_unique(array_filter(array_map('intval', $raw)))) : [];
  31. }
  32. #[Computed(cache: false)]
  33. public function selectedQuestion(): ?Question
  34. {
  35. if (! $this->selectedQuestionId || ! Schema::hasTable('questions')) {
  36. return null;
  37. }
  38. return Question::query()->find($this->selectedQuestionId);
  39. }
  40. public function selectQuestion(int $id): void
  41. {
  42. if ($id <= 0) {
  43. return;
  44. }
  45. if (! in_array($id, $this->tuningQuestionIds, true)) {
  46. Notification::make()->title('该题不在当前列表中')->warning()->send();
  47. return;
  48. }
  49. $this->selectedQuestionId = $id;
  50. $q = Question::query()->find($id);
  51. if ($q) {
  52. $d = max(0.0, min(0.9, round((float) ($q->difficulty ?? 0.5), 2)));
  53. $this->difficultyInput = number_format($d, 2, '.', '');
  54. }
  55. }
  56. public function saveDifficulty(): void
  57. {
  58. if (! $this->selectedQuestionId) {
  59. Notification::make()->title('请先选择题目')->warning()->send();
  60. return;
  61. }
  62. if (! in_array($this->selectedQuestionId, $this->tuningQuestionIds, true)) {
  63. Notification::make()->title('该题不在当前列表中')->warning()->send();
  64. return;
  65. }
  66. $raw = trim($this->difficultyInput);
  67. if ($raw === '' || ! is_numeric($raw)) {
  68. Notification::make()->title('难度格式不正确')->danger()->send();
  69. return;
  70. }
  71. $value = round((float) $raw, 2);
  72. if ($value < 0.0 || $value > 0.9) {
  73. Notification::make()->title('难度须在 0.00~0.90 之间')->danger()->send();
  74. return;
  75. }
  76. Question::query()->where('id', $this->selectedQuestionId)->update([
  77. 'difficulty' => $value,
  78. ]);
  79. $this->difficultyInput = number_format($value, 2, '.', '');
  80. Notification::make()
  81. ->title('已更新 difficulty')
  82. ->body(sprintf('question_id: %d · difficulty: %s', $this->selectedQuestionId, $this->difficultyInput))
  83. ->success()
  84. ->send();
  85. $this->dispatch('$refresh');
  86. }
  87. public function clearTuningList(): void
  88. {
  89. session()->forget(QuestionTemReviewService::SESSION_TUNING_QUESTION_IDS);
  90. $this->selectedQuestionId = null;
  91. $this->difficultyInput = '0.50';
  92. Notification::make()->title('已清空列表记录(仅本会话)')->success()->send();
  93. $this->dispatch('$refresh');
  94. }
  95. public function removeFromList(int $questionId): void
  96. {
  97. $ids = array_values(array_filter($this->tuningQuestionIds, fn (int $x) => $x !== $questionId));
  98. session([QuestionTemReviewService::SESSION_TUNING_QUESTION_IDS => $ids]);
  99. if ($this->selectedQuestionId === $questionId) {
  100. $this->selectedQuestionId = null;
  101. $this->difficultyInput = '0.50';
  102. }
  103. $this->dispatch('$refresh');
  104. }
  105. }