| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace App\DTO;
- /**
- * 报告负载数据传输对象
- * 用于PDF生成时的数据封装
- */
- class ReportPayloadDto
- {
- public function __construct(
- public readonly array $paper,
- public readonly array $student,
- public readonly array $questions,
- public readonly array $mastery,
- public readonly array $questionInsights,
- public readonly array $recommendations,
- public readonly array $analysisData = []
- ) {}
- /**
- * 从ExamAnalysisDataDto创建
- */
- public static function fromExamAnalysisDataDto(ExamAnalysisDataDto $dto): self
- {
- return new self(
- paper: $dto->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;
- }
- }
|