'loadPaper', 'submitManualGrading' => 'handleSubmitFromParent', ]; // 接收从父组件传递的参数 public function mount( ?string $teacherId = null, ?string $studentId = null, ?string $selectedPaperId = null, array $questions = [] ): void { $this->teacherId = $teacherId; $this->studentId = $studentId; $this->selectedPaperId = $selectedPaperId; $this->questions = $questions; } #[On('loadPaper')] public function loadPaper(string $paperId, string $teacherId, string $studentId): void { $this->selectedPaperId = $paperId; $this->teacherId = $teacherId; $this->studentId = $studentId; } public function updatedSelectedPaperId($value): void { if (!empty($value)) { // 清空评分数据 $this->gradingData = []; } } public function setChoiceAnswer(int $index, bool $isCorrect) { if (!isset($this->gradingData[$index])) { $this->gradingData[$index] = []; } $this->gradingData[$index]['is_correct'] = $isCorrect; // 选择题自动计算分数 if ($isCorrect && isset($this->questions[$index]['score'])) { $this->gradingData[$index]['score'] = $this->questions[$index]['score']; } else { $this->gradingData[$index]['score'] = 0; } } public function resetGrading() { $this->gradingData = array_fill(0, count($this->questions), ['score' => null, 'is_correct' => null]); Notification::make() ->title('已重置评分') ->success() ->send(); } public function submitGrading() { try { // 数据验证和处理 $this->convertGradingDataToQuestionGrades(); \Log::info('GradingPanel: 转换后的评分数据', [ 'questionGrades' => $this->questionGrades ]); if (empty($this->questionGrades)) { Notification::make() ->title('请至少为一道题目评分') ->danger() ->send(); return; } // dispatch 事件让父页面处理提交 $this->dispatch('submitManualGrading', questionGrades: $this->questionGrades, gradingData: $this->gradingData, questions: $this->questions ); } catch (\Exception $e) { \Log::error('GradingPanel: 提交失败', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); Notification::make() ->title('提交失败') ->body($e->getMessage()) ->danger() ->send(); } } private function convertGradingDataToQuestionGrades() { $this->questionGrades = []; foreach ($this->questions as $index => $question) { $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']; // 处理评分数据... $this->questionGrades[$questionId] = [ 'is_correct' => $grading['is_correct'] ?? null, 'score' => $grading['score'] ?? null, 'student_answer' => '', ]; } } } public function render() { // 直接复用父页面的逻辑获取题目数据 if (!empty($this->selectedPaperId)) { $this->questions = $this->loadPaperQuestionsDirectly(); } return view('livewire.upload-exam.grading-panel'); } // 直接加载题目数据(复用父页面逻辑) private function loadPaperQuestionsDirectly(): array { try { \Log::info('GradingPanel: 开始加载题目', ['paper_id' => $this->selectedPaperId]); $paper = \App\Models\Paper::where('paper_id', $this->selectedPaperId)->first(); if (!$paper) { \Log::warning('GradingPanel: 试卷不存在', ['paper_id' => $this->selectedPaperId]); return []; } $questions = $paper->questions()->orderBy('question_number')->get(); \Log::info('GradingPanel: 查询到题目', ['count' => $questions->count()]); if ($questions->isEmpty()) { \Log::warning('GradingPanel: 题目为空'); // 直接返回空数组,不要返回提示信息 return []; } // 无论如何都返回题目数据,即使API失败 $processedQuestions = $questions->map(function($q) { return [ 'id' => $q->id, 'question_number' => $q->question_number, 'question_type' => $q->question_type, 'content' => $q->question_text, 'answer' => '', // 可以为空 'score' => $q->score ?? 5, 'question_bank_id' => $q->question_bank_id, 'is_empty' => false ]; })->toArray(); \Log::info('GradingPanel: 处理后的题目', ['count' => count($processedQuestions)]); return $processedQuestions; } catch (\Exception $e) { \Log::error('GradingPanel: 加载失败', ['error' => $e->getMessage()]); return []; } } }