Просмотр исходного кода

fix: align knowledge exam_content with assembled paper schema

Populate knowledge exam_content with questions and paper_info timestamps, and derive statistics from case items to match status payload expectations.

Co-authored-by: Cursor <cursoragent@cursor.com>
yemeishu 6 дней назад
Родитель
Сommit
83c62a97d9
1 измененных файлов с 56 добавлено и 3 удалено
  1. 56 3
      app/Jobs/GenerateKnowledgeExplanationPdfJob.php

+ 56 - 3
app/Jobs/GenerateKnowledgeExplanationPdfJob.php

@@ -133,6 +133,18 @@ class GenerateKnowledgeExplanationPdfJob implements ShouldQueue
             $displayCode = $paperId;
         }
         $kpCodes = is_array($record->kp_codes) ? array_values($record->kp_codes) : [];
+        $questions = $this->buildKnowledgeQuestions();
+        $knowledgeDistribution = [];
+        foreach ($kpCodes as $kpCode) {
+            $knowledgeDistribution[(string) $kpCode] = 0;
+        }
+        foreach ($questions as $q) {
+            $kp = (string) ($q['knowledge_point'] ?? '');
+            if ($kp === '') {
+                continue;
+            }
+            $knowledgeDistribution[$kp] = (int) ($knowledgeDistribution[$kp] ?? 0) + 1;
+        }
 
         return [
             'paper_info' => [
@@ -140,22 +152,25 @@ class GenerateKnowledgeExplanationPdfJob implements ShouldQueue
                 'paper_name' => '知识点讲解_' . $displayCode,
                 'student_id' => (string) ($record->student_id ?? ''),
                 'teacher_id' => (string) ($record->teacher_id ?? ''),
-                'total_questions' => 0,
+                'total_questions' => count($questions),
                 'total_score' => 0,
                 'difficulty_category' => null,
+                'created_at' => optional($record->created_at)->toISOString(),
+                'updated_at' => optional($record->updated_at)->toISOString(),
                 'exam_code' => $displayCode,
                 'grading_code' => null,
                 'paper_id_num' => $displayCode,
             ],
+            'questions' => $questions,
             'knowledge_points' => $kpCodes,
             'statistics' => [
                 'type_distribution' => [
                     'choice' => 0,
                     'fill' => 0,
-                    'answer' => 0,
+                    'answer' => count($questions),
                 ],
                 'difficulty_distribution' => [],
-                'knowledge_point_distribution' => array_fill_keys($kpCodes, 0),
+                'knowledge_point_distribution' => $knowledgeDistribution,
                 'average_difficulty' => null,
                 'total_estimated_time' => 0,
             ],
@@ -165,4 +180,42 @@ class GenerateKnowledgeExplanationPdfJob implements ShouldQueue
             'source' => 'knowledge_explanation',
         ];
     }
+
+    private function buildKnowledgeQuestions(): array
+    {
+        $questions = [];
+        $seq = 1;
+        foreach ($this->knowledgePoints as $point) {
+            $kpCode = (string) ($point['kp_code'] ?? '');
+            $kpName = (string) ($point['kp_name'] ?? $kpCode);
+            $cases = is_array($point['cases'] ?? null) ? $point['cases'] : [];
+            foreach ($cases as $case) {
+                $questionId = (int) ($case['question_id'] ?? 0);
+                $questions[] = [
+                    'question_number' => $seq++,
+                    'question_id' => $questionId > 0 ? (string) $questionId : '',
+                    'question_bank_id' => $questionId > 0 ? $questionId : null,
+                    'question_type' => (string) ($case['question_type'] ?? 'answer'),
+                    'knowledge_point' => $kpCode,
+                    'knowledge_point_name' => $kpName,
+                    'difficulty' => isset($case['difficulty']) && is_numeric($case['difficulty']) ? (float) $case['difficulty'] : null,
+                    'score' => 0,
+                    'estimated_time' => null,
+                    'stem' => (string) ($case['stem'] ?? ''),
+                    'options' => is_array($case['options'] ?? null) ? $case['options'] : [],
+                    'correct_answer' => (string) ($case['answer'] ?? ''),
+                    'solution' => (string) ($case['solution'] ?? ''),
+                    'metadata' => [
+                        'source_type' => (string) ($case['source_type'] ?? ''),
+                        'source_label' => (string) ($case['source_label'] ?? ''),
+                        'is_wrong_case' => (bool) ($case['is_wrong_case'] ?? false),
+                        'child_kp_code' => $case['child_kp_code'] ?? null,
+                        'child_kp_name' => $case['child_kp_name'] ?? null,
+                    ],
+                ];
+            }
+        }
+
+        return $questions;
+    }
 }