OCRPaperGrading.php 3.7 KB

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