| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263 |
- <?php
- namespace App\Filament\Pages;
- use App\Jobs\ProcessOCRRecord;
- use App\Models\OCRRecord;
- use App\Models\Student;
- use App\Models\Teacher;
- use App\Filament\Traits\HasUserRole;
- use BackedEnum;
- use Filament\Notifications\Notification;
- use Filament\Pages\Page;
- use Filament\Forms;
- use Livewire\WithFileUploads;
- use Livewire\Attributes\Computed;
- use Livewire\Attributes\On;
- use Illuminate\Support\Facades\Storage;
- use UnitEnum;
- class UploadExamPaper extends Page
- {
- use HasUserRole, WithFileUploads;
- protected static ?string $title = '上传试卷';
- protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-cloud-arrow-up';
- protected static ?string $navigationLabel = '上传试卷';
- protected static string|UnitEnum|null $navigationGroup = '操作';
- protected static ?int $navigationSort = 2;
- protected static ?string $slug = 'upload-exam-paper';
- protected string $view = 'filament.pages.upload-exam-paper';
- public ?string $teacherId = null;
- public ?string $studentId = null;
- public $uploadedImage = null;
- public bool $isUploading = false;
- public ?string $paperType = null; // 试卷类型:unit_test, midterm, final, homework, quiz, other
- public $form;
- public array $data = [];
- public bool $analyzing = false;
- public ?string $analysisError = null;
- // 新增:模式选择
- public string $mode = 'upload'; // 'upload' 或 'select_paper'
- public ?string $selectedPaperId = null;
- public bool $showGrading = false;
- public array $questions = [];
- public array $gradingData = [];
- public ?string $paperName = null;
- public ?string $paperClass = null;
- public ?string $paperStudent = null;
- public ?string $paperDate = null;
- public array $questionGrades = []; // 存储每道题的评分
- public function mount()
- {
- // 初始化用户角色
- $this->initializeUserRole();
- // 如果是老师,自动选择当前老师
- if ($this->isTeacher) {
- $teacherId = $this->getCurrentTeacherId();
- if ($teacherId) {
- $this->teacherId = $teacherId;
- }
- } else {
- $this->teacherId = null;
- }
- $this->studentId = null;
- $this->uploadedImage = null;
- $this->paperType = null;
- $this->mode = 'upload';
- $this->selectedPaperId = null;
- $this->questionGrades = [];
- }
- public function form(Forms\Form $form): Forms\Form
- {
- return $form
- ->statePath('data')
- ->schema([
- Forms\Components\FileUpload::make('image')
- ->label('上传试卷图片')
- ->image()
- ->multiple()
- ->directory('exam-papers')
- ->acceptedFileTypes(['image/png', 'image/jpeg', 'image/jpg'])
- ->helperText('支持PNG、JPG、JPEG格式,可同时上传多张图片')
- ->maxFiles(10)
- ->required(),
- Forms\Components\TextInput::make('paper_name')
- ->label('试卷名称')
- ->required()
- ->placeholder('例如:数学期末考试'),
- Forms\Components\Select::make('class')
- ->label('班级')
- ->options([
- 'ClassA' => '三年级一班',
- 'ClassB' => '三年级二班',
- 'ClassC' => '四年级一班',
- 'ClassD' => '四年级二班',
- 'ClassE' => '五年级一班',
- 'ClassF' => '五年级二班',
- 'ClassG' => '六年级一班',
- 'ClassH' => '六年级二班',
- ])
- ->required(),
- Forms\Components\TextInput::make('student_name')
- ->label('学生姓名')
- ->required()
- ->placeholder('请输入学生姓名'),
- Forms\Components\Select::make('paper_type')
- ->label('试卷类型')
- ->options([
- 'quiz' => '课堂测验',
- 'midterm' => '期中考试',
- 'final' => '期末考试',
- 'homework' => '家庭作业',
- ])
- ->default('quiz')
- ->required(),
- Forms\Components\TextInput::make('paper_subject')
- ->label('科目')
- ->default('数学')
- ->required(),
- ]);
- }
- #[Computed]
- public function teachers(): array
- {
- try {
- $query = Teacher::query()
- ->leftJoin('users as u', 'teachers.teacher_id', '=', 'u.user_id')
- ->select(
- 'teachers.teacher_id',
- 'teachers.name',
- 'teachers.subject',
- 'u.username',
- 'u.email'
- );
- // 如果是老师,只返回自己
- if ($this->isTeacher) {
- $teacherId = $this->getCurrentTeacherId();
- if ($teacherId) {
- $query->where('teachers.teacher_id', $teacherId);
- }
- }
- $teachers = $query->orderBy('teachers.name')->get();
- // 检查是否有学生没有对应的老师记录
- $teacherIds = $teachers->pluck('teacher_id')->toArray();
- $missingTeacherIds = Student::query()
- ->distinct()
- ->whereNotIn('teacher_id', $teacherIds)
- ->pluck('teacher_id')
- ->toArray();
- $teachersArray = $teachers->all();
- if (!empty($missingTeacherIds)) {
- foreach ($missingTeacherIds as $missingId) {
- $teachersArray[] = (object) [
- 'teacher_id' => $missingId,
- 'name' => '未知老师 (' . $missingId . ')',
- 'subject' => '未知',
- 'username' => null,
- 'email' => null
- ];
- }
- usort($teachersArray, function($a, $b) {
- return strcmp($a->name, $b->name);
- });
- }
- return $teachersArray;
- } catch (\Exception $e) {
- \Illuminate\Support\Facades\Log::error('加载老师列表失败', [
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- #[Computed]
- public function students(): array
- {
- if (empty($this->teacherId)) {
- return [];
- }
- try {
- return Student::query()
- ->leftJoin('users as u', 'students.student_id', '=', 'u.user_id')
- ->where('students.teacher_id', $this->teacherId)
- ->select(
- 'students.student_id',
- 'students.name',
- 'students.grade',
- 'students.class_name',
- 'u.username',
- 'u.email'
- )
- ->orderBy('students.grade')
- ->orderBy('students.class_name')
- ->orderBy('students.name')
- ->get()
- ->all();
- } catch (\Exception $e) {
- \Illuminate\Support\Facades\Log::error('加载学生列表失败', [
- 'teacher_id' => $this->teacherId,
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- #[Computed]
- public function recentRecords(): array
- {
- // 1. 获取OCR记录(图片上传)
- $ocrQuery = OCRRecord::with('student');
- // 如果选择了学生,则筛选该学生的记录
- if (!empty($this->studentId)) {
- $ocrQuery->where('user_id', $this->studentId);
- }
- $ocrRecords = $ocrQuery->latest()->take(5)->get()
- ->map(function($record) {
- $studentName = $record->student?->name ?: ('学生ID: ' . $record->user_id);
- return [
- 'type' => 'ocr_upload',
- 'id' => $record->id,
- 'record_id' => $record->id,
- 'paper_id' => null,
- 'student_id' => $record->user_id,
- 'student_name' => $studentName,
- 'paper_type' => $record->paper_type_label,
- 'paper_name' => $record->image_filename ?: '未命名图片',
- 'status' => $record->status,
- 'total_questions' => $record->total_questions,
- 'processed_questions' => $record->processed_questions ?? 0,
- 'created_at' => $record->created_at->format('Y-m-d H:i'),
- 'is_completed' => $record->status === 'completed',
- ];
- })->toArray();
- // 2. 获取所有Paper记录(包括草稿和已评分)
- $paperQuery = \App\Models\Paper::with('student');
- // 如果选择了学生,则筛选该学生的记录
- if (!empty($this->studentId)) {
- $paperQuery->where('student_id', $this->studentId);
- }
- $allPapers = $paperQuery->latest()->take(5)->get()
- ->map(function($paper) {
- $type = $paper->status === 'completed' ? 'graded_paper' : 'generated';
- $paperType = $paper->status === 'completed' ? '已评分试卷' : '系统生成试卷';
- $iconColor = $paper->status === 'completed' ? 'text-green-500' : 'text-blue-500';
- $studentName = $paper->student?->name ?: ('学生ID: ' . $paper->student_id);
- return [
- 'type' => $type,
- 'id' => $paper->paper_id,
- 'record_id' => null,
- 'paper_id' => $paper->paper_id,
- 'student_id' => $paper->student_id,
- 'student_name' => $studentName,
- 'paper_type' => $paperType,
- 'paper_name' => $paper->paper_name ?? '未命名试卷',
- 'status' => $paper->difficulty_category,
- 'total_questions' => $paper->question_count ?? 0,
- 'created_at' => $paper->created_at->format('Y-m-d H:i'),
- 'is_completed' => $paper->status === 'completed',
- 'icon_color' => $iconColor,
- ];
- })->toArray();
- // 3. 合并并按时间排序
- $allRecords = array_merge($ocrRecords, $allPapers);
- usort($allRecords, function($a, $b) {
- return strcmp($b['created_at'], $a['created_at']);
- });
- return array_slice($allRecords, 0, 10);
- }
-
- /**
- * 获取学生的试卷列表
- */
- #[Computed]
- public function studentPapers(): array
- {
- if (empty($this->studentId)) {
- return [];
- }
- try {
- // 使用 Student 关联查询试卷
- $student = \App\Models\Student::find($this->studentId);
- if (!$student) {
- \Log::warning('未找到指定学生', ['student_id' => $this->studentId]);
- return [];
- }
- return $student->papers()
- ->withCount('questions') // 添加题目计数
- ->orderBy('created_at', 'desc')
- ->take(20)
- ->get()
- ->map(function($paper) {
- return [
- 'paper_id' => $paper->paper_id, // 使用 paper_id 而不是 id
- 'paper_name' => $paper->paper_name ?? '未命名试卷',
- 'total_questions' => $paper->questions_count ?? 0,
- 'total_score' => $paper->total_score ?? 0,
- 'created_at' => $paper->created_at->format('Y-m-d H:i'),
- ];
- })
- ->toArray();
- } catch (\Exception $e) {
- \Log::error('获取学生试卷列表失败', [
- 'student_id' => $this->studentId,
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- #[Computed]
- public function paperTypes(): array
- {
- return [
- '' => '请选择试卷形式',
- 'unit_test' => '单元测试',
- 'midterm' => '期中考试',
- 'final' => '期末考试',
- 'homework' => '家庭作业',
- 'quiz' => '随堂测验',
- 'other' => '其他',
- ];
- }
- /**
- * 获取选中试卷的题目列表
- */
- #[Computed]
- public function selectedPaperQuestions(): array
- {
- if (empty($this->selectedPaperId)) {
- return [];
- }
- try {
- // 首先检查试卷是否存在
- $paper = \App\Models\Paper::where('paper_id', $this->selectedPaperId)->first();
- if (!$paper) {
- \Log::warning('未找到指定试卷', ['paper_id' => $this->selectedPaperId]);
- return [];
- }
- // 使用关联关系查询题目
- $paperWithQuestions = \App\Models\Paper::with(['questions' => function($query) {
- $query->orderBy('question_number');
- }])->where('paper_id', $this->selectedPaperId)->first();
- $questions = $paperWithQuestions ? $paperWithQuestions->questions : collect([]);
- // 处理数据不一致的情况:如果题目为空但试卷显示有题目
- if ($questions->isEmpty() && ($paper->question_count ?? 0) > 0) {
- \Log::warning('试卷显示有题目但实际题目数据缺失', [
- 'paper_id' => $this->selectedPaperId,
- 'expected_questions' => $paper->question_count,
- 'actual_questions' => 0
- ]);
- // 返回占位题目,让用户知道有数据缺失
- return [
- [
- 'id' => 'missing_data',
- 'question_number' => 1,
- 'question_bank_id' => null,
- 'question_type' => 'info',
- 'content' => "⚠️ 数据异常:试卷显示应有 {$paper->question_count} 道题目,但未找到题目数据。这通常是试卷创建过程中断导致的。请联系管理员或重新创建试卷。",
- 'answer' => '',
- 'score' => 0,
- 'is_missing_data' => true
- ]
- ];
- }
- if ($questions->isEmpty()) {
- \Log::info('试卷确实没有题目', ['paper_id' => $this->selectedPaperId]);
- return [
- [
- 'id' => 'no_questions',
- 'question_number' => 1,
- 'question_bank_id' => null,
- 'question_type' => 'info',
- 'content' => '该试卷暂无题目数据',
- 'answer' => '',
- 'score' => 0,
- 'is_empty' => true
- ]
- ];
- }
- // 获取题目详情
- $questionBankService = app(\App\Services\QuestionBankService::class);
- $questionIds = $questions->pluck('question_bank_id')->filter()->unique()->toArray();
- if (empty($questionIds)) {
- \Log::info('题目没有关联题库ID', ['paper_id' => $this->selectedPaperId]);
- // 返回基本的题目信息,不包含题库详情
- return $questions->map(function($q) {
- return [
- 'id' => $q->id,
- 'question_number' => $q->question_number,
- 'question_bank_id' => $q->question_bank_id,
- 'question_type' => $q->question_type,
- 'content' => '题目内容未关联到题库',
- 'answer' => '',
- 'score' => $q->score ?? 5,
- ];
- })->toArray();
- }
- $questionsResponse = $questionBankService->getQuestionsByIds($questionIds);
- $questionDetails = collect($questionsResponse['data'] ?? [])->keyBy('id');
- return $questions->map(function($q) use ($questionDetails) {
- $detail = $questionDetails->get($q->question_bank_id);
- return [
- 'id' => $q->id,
- 'question_number' => $q->question_number,
- 'question_bank_id' => $q->question_bank_id,
- 'question_type' => $q->question_type,
- 'content' => $detail['stem'] ?? '题目内容缺失',
- 'answer' => $detail['answer'] ?? '',
- 'score' => $q->score ?? 5,
- 'kp_code' => $q->knowledge_point, // 从本地数据库获取知识点代码
- ];
- })->toArray();
- } catch (\Exception $e) {
- \Log::error('获取试卷题目失败', [
- 'paper_id' => $this->selectedPaperId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return [
- [
- 'id' => 'error',
- 'question_number' => 1,
- 'question_bank_id' => null,
- 'question_type' => 'error',
- 'content' => '获取题目数据时发生错误:' . $e->getMessage(),
- 'answer' => '',
- 'score' => 0,
- 'is_error' => true
- ]
- ];
- }
- }
- public function updatedTeacherId($value): void
- {
- // 当教师选择变化时,清空之前选择的学生
- $this->studentId = null;
- $this->selectedPaperId = null;
- $this->questionGrades = [];
- }
-
- public function updatedStudentId($value): void
- {
- // 当学生选择变化时,清空已选试卷
- $this->selectedPaperId = null;
- $this->questionGrades = [];
- }
-
- public function updatedMode($value): void
- {
- // 切换模式时重置相关字段
- $this->uploadedImage = null;
- $this->selectedPaperId = null;
- $this->questionGrades = [];
- }
- /**
- * 处理评分完成事件
- */
- #[On('gradingComplete')]
- public function handleGradingComplete(): void
- {
- // 提交完成后跳转到详情页
- if ($this->selectedPaperId && $this->studentId) {
- $url = $this->getViewRecordUrl('graded_paper', $this->selectedPaperId, '', $this->studentId);
- $this->redirect($url, navigate: true);
- }
- }
- /**
- * 处理来自子组件的评分提交事件
- */
- #[On('submitManualGrading')]
- public function handleSubmitFromParent(array $questionGrades, array $gradingData, array $questions): void
- {
- // 从子组件接收数据
- $this->questionGrades = $questionGrades;
- $this->gradingData = $gradingData;
- $this->questions = $questions;
- \Log::info('UploadExamPaper: 接收到子组件提交的评分数据', [
- 'questionGrades_count' => count($questionGrades),
- 'questions_count' => count($questions)
- ]);
- // 调用原有的提交方法
- $this->submitManualGrading();
- // 通知用户提交成功
- Notification::make()
- ->title('评分提交成功')
- ->success()
- ->send();
- // 触发事件处理跳转
- $this->dispatch('gradingComplete');
- }
- public function submitUpload(): void
- {
- if (!$this->teacherId) {
- Notification::make()
- ->title('请选择老师')
- ->danger()
- ->send();
- return;
- }
- if (!$this->studentId) {
- Notification::make()
- ->title('请选择学生')
- ->danger()
- ->send();
- return;
- }
- // 获取表单数据
- $formData = $this->data;
- if (empty($formData['image'])) {
- Notification::make()
- ->title('请上传图片')
- ->danger()
- ->send();
- return;
- }
- if (empty($formData['paper_name'])) {
- Notification::make()
- ->title('请填写试卷名称')
- ->danger()
- ->send();
- return;
- }
- if (empty($formData['class'])) {
- Notification::make()
- ->title('请选择班级')
- ->danger()
- ->send();
- return;
- }
- if (empty($formData['student_name'])) {
- Notification::make()
- ->title('请填写学生姓名')
- ->danger()
- ->send();
- return;
- }
- $this->isUploading = true;
- try {
- // 处理图片(可能是单张或多张)
- $images = $formData['image'];
- if (!is_array($images)) {
- $images = [$images];
- }
- $paths = [];
- foreach ($images as $image) {
- if ($image) {
- $paths[] = storage_path('app/public/' . $image);
- }
- }
- if (empty($paths)) {
- throw new \Exception('图片保存失败');
- }
- $paperId = 'paper_' . time() . '_' . substr(md5(uniqid()), 0, 8);
- // AI分析服务调用
- $response = \Http::timeout(300)
- ->post('http://localhost:5016/analyze-exam', [
- 'paper_id' => $paperId,
- 'paper_name' => $formData['paper_name'],
- 'student_name' => $formData['student_name'],
- 'class_name' => $formData['class'],
- 'paper_type' => $formData['paper_type'],
- 'subject' => $formData['paper_subject'],
- 'image_files' => $paths,
- ]);
- if ($response->successful()) {
- $result = $response->json();
- $this->saveAnalysisResult($result, $paperId);
- $this->analysisResult = $result;
- Notification::make()
- ->title('分析完成')
- ->success()
- ->send();
- } else {
- $this->analysisError = '分析服务响应失败: ' . $response->status();
- Notification::make()
- ->title('分析失败')
- ->body($this->analysisError)
- ->error()
- ->send();
- }
- // 重置表单
- $this->teacherId = null;
- $this->studentId = null;
- $this->uploadedImage = null;
- $this->paperType = null;
- } catch (\Exception $e) {
- Notification::make()
- ->title('上传失败')
- ->body($e->getMessage())
- ->danger()
- ->send();
- } finally {
- $this->isUploading = false;
- $this->analyzing = false;
- }
- }
- #[On('teacherChanged')]
- public function updateTeacherId($teacherId)
- {
- $this->teacherId = $teacherId;
- $this->studentId = null;
- }
- #[On('studentChanged')]
- public function updateStudentId($teacherId, $studentId)
- {
- $this->studentId = $studentId;
- }
- public function removeImage(): void
- {
- $this->uploadedImage = null;
- }
-
- /**
- * 提交手动评分
- */
- public function submitManualGrading(): void
- {
- if (!$this->selectedPaperId) {
- Notification::make()
- ->title('请选择试卷')
- ->danger()
- ->send();
- return;
- }
- // 将 gradingData 转换为 questionGrades 格式
- $this->convertGradingDataToQuestionGrades();
- if (empty($this->questionGrades)) {
- Notification::make()
- ->title('请至少为一道题目评分')
- ->danger()
- ->send();
- return;
- }
- try {
- // 准备数据发送到 LearningAnalytics
- $analyticsData = [];
-
- // 获取题目详情以便查找kp_code
- $questionsMap = collect($this->selectedPaperQuestions)->keyBy('id');
-
- // 收集需要从API补充信息的题目ID
- $missingKpCodeQuestionIds = [];
- foreach ($this->questionGrades as $questionId => $grade) {
- $question = $questionsMap->get($questionId);
- if (!$question) {
- continue;
- }
-
- // 优先使用本地存储的 kp_code
- if (empty($question['kp_code'])) {
- $missingKpCodeQuestionIds[] = $questionId;
- }
- }
-
- // 如果有缺失 kp_code 的题目,尝试从 API 获取
- $apiDetailsMap = collect([]);
- if (!empty($missingKpCodeQuestionIds)) {
- $questionBankIds = collect($missingKpCodeQuestionIds)
- ->map(fn($qId) => $questionsMap->get($qId)['question_bank_id'] ?? null)
- ->filter()
- ->toArray();
-
- if (!empty($questionBankIds)) {
- $questionBankService = app(\App\Services\QuestionBankService::class);
- $questionsDetails = $questionBankService->getQuestionsByIds($questionBankIds);
- $apiDetailsMap = collect($questionsDetails['data'] ?? [])->keyBy('id');
- }
- }
- foreach ($this->questionGrades as $questionId => $grade) {
- $question = $questionsMap->get($questionId);
- if (!$question) {
- continue;
- }
-
- $kpCode = $question['kp_code'];
-
- // 如果本地没有,尝试从API结果中获取
- if (empty($kpCode)) {
- $detail = $apiDetailsMap->get($question['question_bank_id']);
- $kpCode = $detail['kp_code'] ?? $detail['knowledge_point_code'] ?? null;
- }
- // 确保 is_correct 有值(如果为 null,设置为 false)
- $isCorrect = $grade['is_correct'];
- if ($isCorrect === null) {
- $isCorrect = false;
- }
- $analyticsData[] = [
- 'question_bank_id' => $question['question_bank_id'],
- 'student_answer' => $grade['student_answer'] ?? '',
- 'is_correct' => $isCorrect,
- 'score' => $grade['score'] ?? 0,
- 'max_score' => $question['score'] ?? 0,
- 'kp_code' => $kpCode,
- 'ip_address' => '127.0.0.1', // 提供默认IP地址,避免PostgreSQL inet类型错误
- 'device_type' => 'web', // 提供默认设备类型
- 'feedback_provided' => false, // 提供默认反馈状态
- ];
- }
- // 调用 LearningAnalytics 服务
- $learningAnalyticsService = app(\App\Services\LearningAnalyticsService::class);
- // 步骤0: 保存学生答案到本地数据库 (重要:确保数据持久化)
- foreach ($this->questionGrades as $questionId => $grade) {
- // 从数据库获取当前题目的记录(包含满分)
- $paperQuestion = \App\Models\PaperQuestion::where('id', $questionId)->first();
- if (!$paperQuestion) {
- \Log::warning('未找到题目记录', ['question_id' => $questionId]);
- continue;
- }
- // 确保 is_correct 是布尔值(转换字符串 'true'/'false' 为布尔值)
- $isCorrect = $grade['is_correct'];
- if ($isCorrect === 'true' || $isCorrect === true) {
- $isCorrect = true;
- } elseif ($isCorrect === 'false' || $isCorrect === false) {
- $isCorrect = false;
- }
- // 确保 score_obtained 是数字
- $score = $grade['score'];
- if ($score !== null) {
- $score = is_numeric($score) ? (float)$score : 0;
- }
- // **关键修复**:确保 is_correct 和 score 的一致性
- // score 优先级高于 is_correct,根据得分比例动态计算
- $maxScore = $paperQuestion->score ?? 0;
- if ($maxScore > 0) {
- $scoreRatio = $score / $maxScore;
- // 只有达到满分才算完全正确
- if ($scoreRatio >= 1.0) {
- $isCorrect = true;
- } elseif ($scoreRatio > 0) {
- $isCorrect = false; // 部分得分不算完全正确
- } else {
- $isCorrect = false;
- }
- }
- \Log::info('保存评分数据', [
- 'question_id' => $questionId,
- 'max_score' => $maxScore,
- 'score_obtained' => $score,
- 'is_correct' => $isCorrect,
- 'score_ratio' => $maxScore > 0 ? ($score / $maxScore) : 0
- ]);
- \App\Models\PaperQuestion::where('id', $questionId)->update([
- 'student_answer' => $grade['student_answer'] ?? '',
- 'is_correct' => $isCorrect,
- 'score_obtained' => $score ?? 0,
- ]);
- }
- \Log::info('学生答案已保存到数据库', [
- 'student_id' => $this->studentId,
- 'paper_id' => $this->selectedPaperId,
- 'updated_count' => count($this->questionGrades)
- ]);
- // 步骤1: 保存答题记录到 LearningAnalytics
- \Log::info('准备调用submitBatchAttempts API', [
- 'student_id' => $this->studentId,
- 'paper_id' => $this->selectedPaperId,
- 'analytics_data_sample' => array_slice($analyticsData, 0, 2) // 记录前2题的数据作为样本
- ]);
- $result = $learningAnalyticsService->submitBatchAttempts($this->studentId, [
- 'paper_id' => $this->selectedPaperId,
- 'answers' => $analyticsData,
- ]);
- // 检查API返回结果
- if (is_array($result) && isset($result['error']) && $result['error']) {
- throw new \Exception($result['message'] ?? 'API调用失败');
- }
- if ($result === null || (is_array($result) && empty($result))) {
- throw new \Exception('API返回空数据');
- }
- \Log::info('答题记录已保存到学习分析服务', [
- 'student_id' => $this->studentId,
- 'paper_id' => $this->selectedPaperId,
- 'count' => count($analyticsData)
- ]);
- // 步骤2: 触发 AI 分析(包含掌握度更新和学习报告生成)
- try {
- $paper = \App\Models\Paper::find($this->selectedPaperId);
-
- // 构造 AI 分析请求数据
- $analysisQuestions = [];
- foreach ($this->questionGrades as $questionId => $grade) {
- $question = $questionsMap->get($questionId);
- if (!$question) {
- continue;
- }
-
- $kpCode = $question['kp_code'];
- if (empty($kpCode)) {
- $detail = $apiDetailsMap->get($question['question_bank_id']);
- $kpCode = $detail['kp_code'] ?? $detail['knowledge_point_code'] ?? null;
- }
-
- $analysisQuestions[] = [
- 'question_id' => $question['question_bank_id'],
- 'question_number' => (string)$question['question_number'],
- 'question_text' => $question['content'] ?? '',
- 'student_answer' => $grade['student_answer'] ?? '',
- 'correct_answer' => $question['answer'] ?? '',
- 'kp_code' => $kpCode,
- 'score_value' => $grade['score'] ?? 0,
- 'max_score' => $question['score'],
- 'is_correct' => $grade['is_correct'] ?? false,
- 'teacher_validated' => true, // 手动评分即为教师验证
- 'ocr_confidence' => 1.0, // 手动评分置信度为1
- ];
- }
-
- $analysisData = [
- 'exam_id' => $this->selectedPaperId,
- 'student_id' => $this->studentId,
- 'ocr_record_id' => 0, // 系统生成卷子没有OCR记录ID
- 'paper_id' => $this->selectedPaperId,
- 'teacher_name' => auth()->user()->name ?? 'Teacher',
- 'analysis_type' => 'mastery',
- 'questions' => $analysisQuestions,
- ];
-
- // 调用统一的 AI 分析接口
- \Log::info('准备调用submitOCRAnalysis API', [
- 'paper_id' => $this->selectedPaperId,
- 'student_id' => $this->studentId,
- 'analysis_data_sample' => [
- 'question_count' => count($analysisQuestions),
- 'first_question' => $analysisQuestions[0] ?? null
- ]
- ]);
- $analysisResult = $learningAnalyticsService->submitOCRAnalysis($analysisData);
- \Log::info('AI分析已触发', [
- 'paper_id' => $this->selectedPaperId,
- 'student_id' => $this->studentId,
- 'analysis_result_keys' => is_array($analysisResult) ? array_keys($analysisResult) : 'not_array',
- 'analysis_result' => $analysisResult
- ]);
-
- // 保存 analysis_id 到 Paper 表
- if (isset($analysisResult['analysis_id'])) {
- \App\Models\Paper::where('paper_id', $this->selectedPaperId)->update([
- 'analysis_id' => $analysisResult['analysis_id'],
- ]);
-
- \Log::info('已保存 analysis_id', [
- 'paper_id' => $this->selectedPaperId,
- 'analysis_id' => $analysisResult['analysis_id']
- ]);
- }
-
- } catch (\Exception $analysisError) {
- // AI 分析失败不影响主流程
- \Log::warning('触发AI分析失败', [
- 'paper_id' => $this->selectedPaperId,
- 'error' => $analysisError->getMessage()
- ]);
- }
- // 更新Paper表状态为已完成评分
- \App\Models\Paper::where('paper_id', $this->selectedPaperId)->update([
- 'status' => 'completed',
- 'completed_at' => now(),
- ]);
- Notification::make()
- ->title('提交成功')
- ->body('评分已提交,AI分析正在进行中')
- ->success()
- ->send();
- // 刷新最近记录列表
- unset($this->recentRecords);
- // 重置表单
- $this->selectedPaperId = null;
- $this->questionGrades = [];
- } catch (\Exception $e) {
- \Log::error('提交手动评分失败', [
- 'error' => $e->getMessage(),
- 'student_id' => $this->studentId,
- 'paper_id' => $this->selectedPaperId,
- ]);
- Notification::make()
- ->title('提交失败')
- ->body($e->getMessage())
- ->danger()
- ->send();
- }
- }
- /**
- * 将 gradingData 转换为 questionGrades 格式
- * gradingData: 索引数组 [{is_correct: bool, score: float}]
- * questionGrades: 题目ID为键的数组 [questionId => {is_correct: bool, score: float, student_answer: string}]
- */
- private function convertGradingDataToQuestionGrades(): void
- {
- $this->questionGrades = [];
- // 遍历 questions 数组(包含题目信息)
- foreach ($this->questions as $index => $question) {
- // 获取对应索引的 gradingData
- $grading = $this->gradingData[$index] ?? null;
- // 只有当 grading 不为空且有评分数据时才添加
- if ($grading && (
- (isset($grading['is_correct']) && $grading['is_correct'] !== null) ||
- (isset($grading['score']) && $grading['score'] !== null)
- )) {
- $questionId = $question['id'];
- // 处理 is_correct 值(字符串 'true'/'false' 或布尔值)
- $isCorrect = $grading['is_correct'] ?? null;
- if ($isCorrect === 'true') {
- $isCorrect = true;
- } elseif ($isCorrect === 'false') {
- $isCorrect = false;
- }
- // 如果 is_correct 为 null,保持为 null(不要转换为布尔值)
- // 处理 score 值
- $score = $grading['score'] ?? null;
- if ($score !== null && $score !== '') {
- $score = is_numeric($score) ? (float)$score : null;
- }
- // **关键修复**:根据题型处理缺失的字段
- if ($question['question_type'] === 'choice') {
- // 选择题:只有 is_correct,需要自动计算分数
- if ($isCorrect === true) {
- $score = $question['score'] ?? 0; // 正确给满分
- } elseif ($isCorrect === false) {
- $score = 0; // 错误给0分
- }
- } else {
- // 填空/解答题:只有 score,需要自动计算 is_correct
- if ($score !== null) {
- // 动态计算:得分等于满分才算正确
- $maxScore = $question['score'] ?? 0;
- $isCorrect = ($score >= $maxScore && $maxScore > 0);
- }
- }
- // 获取学生答案(优先使用 gradingData 中的值,如果没有则使用题目中的值)
- $studentAnswer = $grading['student_answer'] ?? $question['student_answer'] ?? '';
- // 对于选择题,如果学生答案为空,基于评分推断
- if (empty($studentAnswer) && $question['question_type'] === 'choice') {
- if ($isCorrect === true) {
- // 如果选"正确",学生答案就是正确答案
- $studentAnswer = $question['correct_answer'] ?? '正确答案';
- } elseif ($isCorrect === false) {
- // 如果选"错误",学生答案可以为空或者设置为特殊标记
- $studentAnswer = '错误答案';
- }
- }
- // 转换格式
- $this->questionGrades[$questionId] = [
- 'is_correct' => $isCorrect,
- 'score' => $score,
- 'student_answer' => $studentAnswer,
- ];
- }
- }
- \Log::info('转换评分数据', [
- 'grading_data_count' => count(array_filter($this->gradingData ?? [])),
- 'question_grades_count' => count($this->questionGrades),
- 'questions_count' => count($this->questions ?? []),
- 'sample_question_grades' => array_slice($this->questionGrades, 0, 2, true),
- ]);
- }
- #[Computed]
- public function gradingProgress(): string
- {
- $gradedCount = count(array_filter($this->gradingData ?? []));
- $totalCount = count($this->questions ?? []);
- return "已评分:{$gradedCount}/{$totalCount}题";
- }
- public function startAnalysis(): void
- {
- $this->analyzing = true;
- $this->analysisError = null;
- try {
- $this->submitUpload();
- } catch (\Exception $e) {
- $this->analysisError = $e->getMessage();
- $this->analyzing = false;
- }
- }
- public function saveGrading(): void
- {
- $this->submitManualGrading();
- }
- public function updatedSelectedPaperId($value): void
- {
- if (empty($value)) {
- $this->questions = [];
- $this->gradingData = [];
- $this->showGrading = false;
- return;
- }
- // 加载试卷信息和题目
- $this->loadPaperForGrading($value);
- }
- public function loadPaperForGrading($paperId): void
- {
- try {
- $paper = \App\Models\Paper::where('paper_id', $paperId)->first();
- if (!$paper) {
- Notification::make()
- ->title('试卷不存在')
- ->danger()
- ->send();
- return;
- }
- // 设置试卷信息
- $this->paperName = $paper->paper_name;
- $this->paperClass = $paper->difficulty_category ?? '未设置';
- $this->paperStudent = $paper->student_id;
- $this->paperDate = $paper->created_at->format('Y-m-d H:i');
- // 加载题目
- $paperWithQuestions = \App\Models\Paper::with(['questions' => function($query) {
- $query->orderBy('question_number');
- }])->where('paper_id', $paperId)->first();
- $questions = $paperWithQuestions ? $paperWithQuestions->questions : collect([]);
- // 如果没有正确答案,先尝试从题库API获取
- $apiDetailsMap = new \Illuminate\Support\Collection();
- if (!$questions->isEmpty()) {
- $questionBankIds = $questions->where('question_bank_id', '!=', null)->pluck('question_bank_id')->unique()->toArray();
- if (!empty($questionBankIds)) {
- try {
- $questionBankService = app(\App\Services\QuestionBankService::class);
- $apiResponse = $questionBankService->getQuestionsByIds($questionBankIds);
- if (!empty($apiResponse['data'])) {
- foreach ($apiResponse['data'] as $detail) {
- $apiDetailsMap->put($detail['id'], $detail);
- }
- \Log::info('成功从题库API获取题目详情', [
- 'count' => count($apiResponse['data']),
- 'ids' => array_keys($apiResponse['data'])
- ]);
- }
- } catch (\Exception $e) {
- \Log::warning('获取题库详情失败', ['error' => $e->getMessage()]);
- }
- }
- }
- if ($questions->isEmpty()) {
- $this->questions = [
- [
- 'id' => 'no_questions',
- 'question_number' => 1,
- 'question_type' => 'info',
- 'content' => '该试卷暂无题目数据',
- 'answer' => '',
- 'score' => 0,
- 'is_empty' => true
- ]
- ];
- } else {
- $this->questions = $questions->map(function($question, $index) use ($apiDetailsMap) {
- // 从 API 获取正确答案(优先使用 API 数据)
- $correctAnswer = $question->correct_answer;
- if (empty($correctAnswer) && $question->question_bank_id && $apiDetailsMap->has($question->question_bank_id)) {
- $detail = $apiDetailsMap->get($question->question_bank_id);
- $correctAnswer = $detail['answer'] ?? $detail['correct_answer'] ?? '';
- }
- return [
- 'id' => $question->id,
- 'question_number' => $question->question_number,
- 'question_type' => $question->question_type,
- 'question_text' => $question->question_text,
- 'content' => $question->question_text,
- 'options' => json_decode($question->options, true) ?: [],
- 'answer' => $correctAnswer,
- 'correct_answer' => $correctAnswer,
- 'student_answer' => '', // 学生答案暂不显示,等后续完善
- 'score' => $question->score,
- 'max_score' => $question->score,
- 'question_bank_id' => $question->question_bank_id,
- 'is_empty' => false
- ];
- })->toArray();
- }
- // 初始化评分数据
- $this->gradingData = array_fill(0, count($this->questions), ['score' => null, 'is_correct' => null, 'comment' => '']);
- $this->showGrading = true;
- } catch (\Exception $e) {
- \Log::error('加载试卷题目失败', [
- 'paper_id' => $paperId,
- 'error' => $e->getMessage()
- ]);
- Notification::make()
- ->title('加载失败')
- ->body($e->getMessage())
- ->danger()
- ->send();
- }
- }
- private function saveAnalysisResult(array $result, string $paperId): void
- {
- try {
- \DB::beginTransaction();
- // 保存试卷基本信息
- $examPaper = \App\Models\Paper::create([
- 'paper_id' => $paperId,
- 'paper_name' => $result['paper_name'] ?? '未命名试卷',
- 'student_id' => $this->studentId,
- 'teacher_id' => $this->teacherId,
- 'paper_type' => $result['paper_type'] ?? 'quiz',
- 'question_count' => count($result['questions'] ?? []),
- 'total_score' => $result['total_score'] ?? 0,
- 'status' => 'completed',
- ]);
- // 保存题目信息
- foreach ($result['questions'] ?? [] as $index => $questionData) {
- \App\Models\PaperQuestion::create([
- 'paper_id' => $paperId,
- 'question_number' => $index + 1,
- 'question_text' => $questionData['question_text'] ?? '',
- 'question_type' => $questionData['question_type'] ?? 'choice',
- 'options' => json_encode($questionData['options'] ?? []),
- 'correct_answer' => $questionData['correct_answer'] ?? '',
- 'score' => $questionData['score'] ?? 1,
- ]);
- }
- \DB::commit();
- } catch (\Exception $e) {
- \DB::rollBack();
- \Log::error('保存分析结果失败: ' . $e->getMessage());
- }
- }
- /**
- * 查看记录详情 - 使用页面跳转
- */
- public function getViewRecordUrl(string $type, string $paperId, string $recordId, string $studentId): string
- {
- // 返回ExamAnalysis详情页面URL
- if (in_array($type, ['graded_paper', 'generated'])) {
- // 系统生成或已评分试卷,使用paperId
- return '/admin/exam-analysis?paperId=' . $paperId . '&studentId=' . $studentId;
- } elseif ($type === 'ocr_upload') {
- // OCR上传记录,也跳转到详情页
- return '/admin/exam-analysis?recordId=' . $recordId . '&studentId=' . $studentId;
- }
- return '#';
- }
- }
|