ReportPayloadDto.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. analysisData: $dto->toArray()
  35. );
  36. }
  37. /**
  38. * 转换为数组
  39. */
  40. public function toArray(): array
  41. {
  42. return [
  43. 'paper' => $this->paper,
  44. 'student' => $this->student,
  45. 'teacher' => $this->teacher,
  46. 'questions' => $this->questions,
  47. 'mastery' => $this->mastery,
  48. 'parent_mastery_levels' => $this->parentMasteryLevels, // 新增:父节点掌握度数据
  49. 'question_insights' => $this->questionInsights,
  50. 'recommendations' => $this->recommendations,
  51. 'analysis_data' => $this->analysisData,
  52. ];
  53. }
  54. /**
  55. * 按题型分组题目
  56. */
  57. public function getQuestionsByType(): array
  58. {
  59. $grouped = [
  60. 'choice' => [],
  61. 'fill' => [],
  62. 'answer' => [],
  63. ];
  64. foreach ($this->questions as $question) {
  65. $type = $question['question_type'] ?? 'answer';
  66. $normalizedType = $this->normalizeQuestionType($type);
  67. $grouped[$normalizedType][] = $question;
  68. }
  69. return $grouped;
  70. }
  71. /**
  72. * 标准化题型名称
  73. */
  74. private function normalizeQuestionType(string $type): string
  75. {
  76. $t = strtolower(trim($type));
  77. return match (true) {
  78. str_contains($t, 'choice') || str_contains($t, '选择') => 'choice',
  79. str_contains($t, 'fill') || str_contains($t, 'blank') || str_contains($t, '填空') => 'fill',
  80. default => 'answer',
  81. };
  82. }
  83. /**
  84. * 获取按卷面顺序排列的题目
  85. */
  86. public function getOrderedQuestions(): array
  87. {
  88. $grouped = $this->getQuestionsByType();
  89. $ordered = array_merge($grouped['choice'], $grouped['fill'], $grouped['answer']);
  90. // 添加显示序号
  91. foreach ($ordered as $i => &$question) {
  92. $question['display_number'] = $i + 1;
  93. }
  94. unset($question);
  95. return $ordered;
  96. }
  97. /**
  98. * 获取知识点名称映射
  99. */
  100. public function getKnowledgePointNameMap(): array
  101. {
  102. $map = [];
  103. foreach ($this->questions as $question) {
  104. $kpCode = $question['knowledge_point'] ?? $question['kp_code'] ?? '';
  105. $kpName = $question['knowledge_point_name'] ?? $kpCode;
  106. if ($kpCode && $kpName) {
  107. $map[$kpCode] = $kpName;
  108. }
  109. }
  110. return $map;
  111. }
  112. }