HandlesMindmapDetails.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace App\Filament\Traits;
  3. use Illuminate\Support\Facades\Log;
  4. trait HandlesMindmapDetails
  5. {
  6. protected function getNodeDetails(string $nodeId, ?array $masteryData = null): array
  7. {
  8. $treePath = public_path('data/tree.json');
  9. $edgesPath = public_path('data/edges.json');
  10. if (!file_exists($treePath)) {
  11. return ['error' => 'Tree data not found'];
  12. }
  13. $tree = json_decode(file_get_contents($treePath), true);
  14. $edges = file_exists($edgesPath) ? json_decode(file_get_contents($edgesPath), true) : [];
  15. $node = $this->findNodeInTree($tree, $nodeId);
  16. if (!$node) {
  17. return ['error' => 'Node not found'];
  18. }
  19. $masteryMap = $masteryData ?? $this->getActiveMasteryData();
  20. $masteryInfo = $masteryMap[$nodeId] ?? null;
  21. $masteryLevel = $masteryInfo ? ($masteryInfo['mastery_level'] ?? 0) : 0;
  22. $totalAttempts = $masteryInfo ? ($masteryInfo['total_attempts'] ?? 0) : 0;
  23. $accuracy = $masteryInfo ? ($masteryInfo['accuracy_rate'] ?? 0) : 0;
  24. $prerequisites = [];
  25. $successors = [];
  26. foreach ($edges as $edge) {
  27. if (($edge['target'] ?? null) === $nodeId && ($edge['type'] ?? '') === 'prerequisite') {
  28. $prereqNode = $this->findNodeInTree($tree, $edge['source']);
  29. if ($prereqNode) {
  30. $prerequisites[] = [
  31. 'id' => $edge['source'],
  32. 'name' => $prereqNode['name'] ?? $prereqNode['label'] ?? $edge['source'],
  33. 'mastery' => $masteryMap[$edge['source']]['mastery_level'] ?? 0,
  34. ];
  35. }
  36. }
  37. if (($edge['source'] ?? null) === $nodeId && ($edge['type'] ?? '') === 'successor') {
  38. $succNode = $this->findNodeInTree($tree, $edge['target']);
  39. if ($succNode) {
  40. $successors[] = [
  41. 'id' => $edge['target'],
  42. 'name' => $succNode['name'] ?? $succNode['label'] ?? $edge['target'],
  43. 'mastery' => $masteryMap[$edge['target']]['mastery_level'] ?? 0,
  44. ];
  45. }
  46. }
  47. }
  48. $recommendations = $this->getRecommendations($nodeId);
  49. return [
  50. 'id' => $nodeId,
  51. 'name' => $node['name'] ?? $node['label'] ?? $nodeId,
  52. 'code' => $node['code'] ?? $nodeId,
  53. 'mastery_level' => $masteryLevel,
  54. 'total_attempts' => $totalAttempts,
  55. 'accuracy_rate' => $accuracy,
  56. 'error_rate' => $totalAttempts > 0 ? (1 - $accuracy) : 0,
  57. 'prerequisites' => $prerequisites,
  58. 'successors' => $successors,
  59. 'recommendations' => $recommendations,
  60. 'skills' => $node['skills'] ?? [],
  61. ];
  62. }
  63. protected function findNodeInTree(array $node, string $targetId): ?array
  64. {
  65. $nodeId = $node['code'] ?? $node['id'] ?? $node['label'] ?? null;
  66. if ($nodeId === $targetId) {
  67. return $node;
  68. }
  69. if (isset($node['children'])) {
  70. foreach ($node['children'] as $child) {
  71. $found = $this->findNodeInTree($child, $targetId);
  72. if ($found) {
  73. return $found;
  74. }
  75. }
  76. }
  77. return null;
  78. }
  79. protected function getRecommendations(string $nodeId): array
  80. {
  81. try {
  82. $questionBankService = app(\App\Services\QuestionBankService::class);
  83. $response = $questionBankService->getQuestionsByKpCode($nodeId, 3);
  84. if (empty($response['data'])) {
  85. return $this->getMockRecommendations($nodeId);
  86. }
  87. $recommendations = [];
  88. foreach ($response['data'] as $question) {
  89. $difficultyMap = [
  90. 0.3 => '简单',
  91. 0.6 => '中等',
  92. 0.9 => '困难',
  93. ];
  94. $difficulty = $difficultyMap[$question['difficulty'] ?? 0.6] ?? '中等';
  95. $typeMap = [
  96. 'choice' => '选择题',
  97. 'fill' => '填空题',
  98. 'answer' => '解答题',
  99. ];
  100. $rawType = strtolower((string) ($question['question_type'] ?? 'answer'));
  101. $normalizedType = match (true) {
  102. str_contains($rawType, 'choice') || str_contains($rawType, '选择') => 'choice',
  103. str_contains($rawType, 'fill') || str_contains($rawType, 'blank') || str_contains($rawType, '填空') => 'fill',
  104. default => 'answer',
  105. };
  106. $type = $typeMap[$normalizedType] ?? '解答题';
  107. $stem = $question['stem'] ?? $question['content'] ?? '练习题';
  108. $recommendations[] = [
  109. 'id' => $question['id'] ?? $question['question_code'] ?? uniqid(),
  110. 'title' => mb_strimwidth($stem, 0, 50, '...'),
  111. 'difficulty' => $difficulty,
  112. 'type' => $type,
  113. ];
  114. }
  115. return $recommendations;
  116. } catch (\Exception $e) {
  117. Log::error('Failed to fetch recommendations', [
  118. 'node_id' => $nodeId,
  119. 'error' => $e->getMessage(),
  120. ]);
  121. return $this->getMockRecommendations($nodeId);
  122. }
  123. }
  124. protected function getMockRecommendations(string $nodeId): array
  125. {
  126. return [
  127. [
  128. 'id' => 1,
  129. 'title' => '基础练习:' . $nodeId,
  130. 'difficulty' => '简单',
  131. 'type' => '选择题',
  132. ],
  133. [
  134. 'id' => 2,
  135. 'title' => '进阶练习:' . $nodeId,
  136. 'difficulty' => '中等',
  137. 'type' => '填空题',
  138. ],
  139. [
  140. 'id' => 3,
  141. 'title' => '综合应用:' . $nodeId,
  142. 'difficulty' => '困难',
  143. 'type' => '解答题',
  144. ],
  145. ];
  146. }
  147. protected function getActiveMasteryData(): array
  148. {
  149. if (property_exists($this, 'masteryData')) {
  150. return $this->masteryData ?? [];
  151. }
  152. if (property_exists($this, 'mindmapMasteryData')) {
  153. return $this->mindmapMasteryData ?? [];
  154. }
  155. return [];
  156. }
  157. }