paper, student: $dto->student, questions: $dto->questions, mastery: $dto->mastery, questionInsights: $dto->insights, recommendations: $dto->recommendations, analysisData: $dto->toArray() ); } /** * 转换为数组 */ public function toArray(): array { return [ 'paper' => $this->paper, 'student' => $this->student, 'questions' => $this->questions, 'mastery' => $this->mastery, 'question_insights' => $this->questionInsights, 'recommendations' => $this->recommendations, 'analysis_data' => $this->analysisData, ]; } /** * 按题型分组题目 */ public function getQuestionsByType(): array { $grouped = [ 'choice' => [], 'fill' => [], 'answer' => [], ]; foreach ($this->questions as $question) { $type = $question['question_type'] ?? 'answer'; $normalizedType = $this->normalizeQuestionType($type); $grouped[$normalizedType][] = $question; } return $grouped; } /** * 标准化题型名称 */ private function normalizeQuestionType(string $type): string { $t = strtolower(trim($type)); return match (true) { str_contains($t, 'choice') || str_contains($t, '选择') => 'choice', str_contains($t, 'fill') || str_contains($t, 'blank') || str_contains($t, '填空') => 'fill', default => 'answer', }; } /** * 获取按卷面顺序排列的题目 */ public function getOrderedQuestions(): array { $grouped = $this->getQuestionsByType(); $ordered = array_merge($grouped['choice'], $grouped['fill'], $grouped['answer']); // 添加显示序号 foreach ($ordered as $i => &$question) { $question['display_number'] = $i + 1; } unset($question); return $ordered; } /** * 获取知识点名称映射 */ public function getKnowledgePointNameMap(): array { $map = []; foreach ($this->questions as $question) { $kpCode = $question['knowledge_point'] ?? $question['kp_code'] ?? ''; $kpName = $question['knowledge_point_name'] ?? $kpCode; if ($kpCode && $kpName) { $map[$kpCode] = $kpName; } } return $map; } }