OCRPaperGrading.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Models\OCRRecord;
  4. use App\Services\ExamPaperService;
  5. use App\Services\ChatGPTAnalysisService;
  6. use App\Jobs\RegradeOCRSubmission;
  7. use App\Filament\Traits\HasUserRole;
  8. use Filament\Pages\Page;
  9. use Filament\Notifications\Notification;
  10. use Livewire\Attributes\Computed;
  11. use Illuminate\Support\Facades\Log;
  12. class OCRPaperGrading extends Page
  13. {
  14. use HasUserRole;
  15. protected static ?string $navigationLabel = 'OCR智能阅卷';
  16. protected static ?string $title = 'OCR试卷智能阅卷系统';
  17. protected static ?string $slug = 'ocr-paper-grading';
  18. public static function getNavigationIcon(): ?string
  19. {
  20. return 'heroicon-o-document-duplicate';
  21. }
  22. public static function getNavigationGroup(): ?string
  23. {
  24. return 'OCR+AI系统';
  25. }
  26. protected string $view = 'filament.pages.ocr-paper-grading';
  27. // 选择相关
  28. public ?string $teacherId = null;
  29. public ?string $studentId = null;
  30. public ?string $selectedPaperId = null;
  31. // 处理状态
  32. public ?int $selectedRecordId = null;
  33. public ?OCRRecord $selectedRecord = null;
  34. // ChatGPT识别相关
  35. public bool $useChatGPT = false;
  36. // 获取识别模式文本
  37. public function getRecognitionModeText(): string
  38. {
  39. return $this->useChatGPT ? 'ChatGPT智能识别' : 'OCR识别 + AI判分';
  40. }
  41. public function mount(): void
  42. {
  43. $this->initializeUserRole();
  44. if ($this->isTeacher) {
  45. $teacherId = $this->getCurrentTeacherId();
  46. if ($teacherId) {
  47. $this->teacherId = $teacherId;
  48. }
  49. }
  50. }
  51. #[Computed]
  52. public function teachers(): array
  53. {
  54. return app(ExamPaperService::class)->getTeachers(
  55. $this->isTeacher ? $this->getCurrentTeacherId() : null
  56. );
  57. }
  58. #[Computed]
  59. public function students(): array
  60. {
  61. return app(ExamPaperService::class)->getStudents($this->teacherId);
  62. }
  63. #[Computed]
  64. public function studentPapers(): array
  65. {
  66. return app(ExamPaperService::class)->getStudentPapers($this->studentId);
  67. }
  68. #[Computed]
  69. public function selectedPaperQuestions(): array
  70. {
  71. return app(ExamPaperService::class)->getPaperQuestions($this->selectedPaperId);
  72. }
  73. public function updatedTeacherId($value): void
  74. {
  75. $this->studentId = null;
  76. $this->selectedPaperId = null;
  77. }
  78. public function updatedStudentId($value): void
  79. {
  80. $this->selectedPaperId = null;
  81. }
  82. /**
  83. * 查看OCR记录
  84. */
  85. public function viewRecord(int $recordId): void
  86. {
  87. $this->selectedRecordId = $recordId;
  88. $this->selectedRecord = OCRRecord::with('questions')->findOrFail($recordId);
  89. }
  90. /**
  91. * 手动重新判分
  92. */
  93. public function regrade(int $recordId): void
  94. {
  95. RegradeOCRSubmission::dispatch($recordId);
  96. Notification::make()
  97. ->title('已触发重新判分')
  98. ->body('请稍后刷新查看结果')
  99. ->success()
  100. ->send();
  101. }
  102. /**
  103. * 获取最近的OCR记录
  104. */
  105. public function getRecentRecords(): \Illuminate\Database\Eloquent\Collection
  106. {
  107. return OCRRecord::with('questions')
  108. ->orderBy('created_at', 'desc')
  109. ->limit(10)
  110. ->get();
  111. }
  112. /**
  113. * 切换识别模式
  114. */
  115. public function updatedUseChatGPT($value): void
  116. {
  117. if ($value) {
  118. Notification::make()
  119. ->title('ChatGPT识别模式')
  120. ->body('将使用ChatGPT进行试卷智能分析,无需OCR识别')
  121. ->info()
  122. ->send();
  123. }
  124. }
  125. }