ReportPayloadDto.php 3.4 KB

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