|
|
@@ -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);
|