| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- <?php
- namespace App\Livewire\UploadExam;
- use Livewire\Component;
- use App\Services\LearningAnalyticsService;
- use Filament\Notifications\Notification;
- class GradingPanel extends Component
- {
- public ?string $teacherId = null;
- public ?string $studentId = null;
- public ?string $selectedPaperId = null;
- public array $questions = [];
- public array $gradingData = [];
- public ?string $paperName = null;
- public ?string $paperClass = null;
- public ?string $paperStudent = null;
- public ?string $paperDate = null;
- protected $listeners = [
- 'loadPaper' => '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 [];
- }
- }
- }
|