Sfoglia il codice sorgente

fix restore choice options in local PDF rendering

Backfill choice options from question bank when paper question rows do not carry options, so local Blade rendering shows A/B/C/D choices correctly.

Made-with: Cursor
yemeishu 3 settimane fa
parent
commit
98c1e0b851
1 ha cambiato i file con 31 aggiunte e 1 eliminazioni
  1. 31 1
      app/Services/ExamPdfExportService.php

+ 31 - 1
app/Services/ExamPdfExportService.php

@@ -2010,9 +2010,39 @@ class ExamPdfExportService
 
             // 构造视图需要的变量
             $questions = ['choice' => [], 'fill' => [], 'answer' => []];
+            $questionBankIds = $paper->questions
+                ->pluck('question_bank_id')
+                ->filter()
+                ->unique()
+                ->values();
+            $optionsByBankId = [];
+            if ($questionBankIds->isNotEmpty()) {
+                $optionsByBankId = Question::whereIn('id', $questionBankIds)
+                    ->pluck('options', 'id')
+                    ->toArray();
+            }
             foreach ($paper->questions as $pq) {
                 $qType = $this->normalizeQuestionType($pq->question_type ?? 'answer');
-                $questions[$qType][] = $this->normalizeAnswerFieldForPdf($pq);
+                $normalizedQuestion = $this->normalizeAnswerFieldForPdf($pq);
+
+                // 本地渲染链路下,paper_questions 通常不带 options,需要从题库回填选择题选项。
+                if ($qType === 'choice') {
+                    $options = $normalizedQuestion->options ?? null;
+                    if (empty($options) && ! empty($normalizedQuestion->question_bank_id)) {
+                        $options = $optionsByBankId[$normalizedQuestion->question_bank_id] ?? null;
+                    }
+                    if (is_string($options)) {
+                        $decoded = json_decode($options, true);
+                        if (json_last_error() === JSON_ERROR_NONE) {
+                            $options = $decoded;
+                        }
+                    }
+                    if (is_array($options)) {
+                        $normalizedQuestion->options = $options;
+                    }
+                }
+
+                $questions[$qType][] = $normalizedQuestion;
             }
 
             $studentModel = \App\Models\Student::find($paper->student_id);