ReportPayloadDto.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\DTO;
  3. /**
  4. * 报告负载数据传输对象
  5. * 用于PDF生成时的数据封装
  6. */
  7. class ReportPayloadDto
  8. {
  9. public function __construct(
  10. public readonly array $paper,
  11. public readonly array $student,
  12. public readonly array $teacher,
  13. public readonly array $questions,
  14. public readonly array $mastery,
  15. public readonly array $parentMasteryLevels, // 新增:父节点掌握度数据
  16. public readonly array $questionInsights,
  17. public readonly array $recommendations,
  18. public readonly array $analysisData = []
  19. ) {}
  20. /**
  21. * 从ExamAnalysisDataDto创建
  22. */
  23. public static function fromExamAnalysisDataDto(ExamAnalysisDataDto $dto): self
  24. {
  25. return new self(
  26. paper: $dto->paper,
  27. student: $dto->student,
  28. teacher: $dto->teacher,
  29. questions: $dto->questions,
  30. mastery: $dto->mastery,
  31. parentMasteryLevels: $dto->parentMasteryLevels, // 新增:父节点掌握度数据
  32. questionInsights: $dto->insights,
  33. recommendations: $dto->recommendations,
  34. // 必须透传原始 analysis_data,模板依赖 question_analysis/knowledge_point_analysis 原始结构
  35. analysisData: $dto->rawAnalysisData
  36. );
  37. }
  38. /**
  39. * 转换为数组
  40. */
  41. public function toArray(): array
  42. {
  43. return [
  44. 'paper' => $this->paper,
  45. 'student' => $this->student,
  46. 'teacher' => $this->teacher,
  47. 'questions' => $this->questions,
  48. 'mastery' => $this->mastery,
  49. 'parent_mastery_levels' => $this->parentMasteryLevels, // 新增:父节点掌握度数据
  50. 'question_insights' => $this->questionInsights,
  51. 'recommendations' => $this->recommendations,
  52. 'analysis_data' => $this->analysisData,
  53. ];
  54. }
  55. /**
  56. * 按题型分组题目
  57. */
  58. public function getQuestionsByType(): array
  59. {
  60. $grouped = [
  61. 'choice' => [],
  62. 'fill' => [],
  63. 'answer' => [],
  64. ];
  65. foreach ($this->questions as $question) {
  66. $type = $question['question_type'] ?? 'answer';
  67. $normalizedType = $this->normalizeQuestionType($type);
  68. $grouped[$normalizedType][] = $question;
  69. }
  70. return $grouped;
  71. }
  72. /**
  73. * 标准化题型名称
  74. */
  75. private function normalizeQuestionType(string $type): string
  76. {
  77. $t = strtolower(trim($type));
  78. return match (true) {
  79. str_contains($t, 'choice') || str_contains($t, '选择') => 'choice',
  80. str_contains($t, 'fill') || str_contains($t, 'blank') || str_contains($t, '填空') => 'fill',
  81. default => 'answer',
  82. };
  83. }
  84. /**
  85. * 获取按卷面顺序排列的题目
  86. */
  87. public function getOrderedQuestions(): array
  88. {
  89. $grouped = $this->getQuestionsByType();
  90. $ordered = array_merge($grouped['choice'], $grouped['fill'], $grouped['answer']);
  91. // 添加显示序号
  92. foreach ($ordered as $i => &$question) {
  93. $question['display_number'] = $i + 1;
  94. }
  95. unset($question);
  96. return $ordered;
  97. }
  98. /**
  99. * 获取知识点名称映射
  100. */
  101. public function getKnowledgePointNameMap(): array
  102. {
  103. $map = [];
  104. foreach ($this->questions as $question) {
  105. $kpCode = $question['knowledge_point'] ?? $question['kp_code'] ?? '';
  106. $kpName = $question['knowledge_point_name'] ?? $kpCode;
  107. if ($kpCode && $kpName) {
  108. $map[$kpCode] = $kpName;
  109. }
  110. }
  111. return $map;
  112. }
  113. }