HandlesMindmapDetails.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. $type = $typeMap[$question['question_type'] ?? 'answer'] ?? '解答题';
  101. $stem = $question['stem'] ?? $question['content'] ?? '练习题';
  102. $recommendations[] = [
  103. 'id' => $question['id'] ?? $question['question_code'] ?? uniqid(),
  104. 'title' => mb_strimwidth($stem, 0, 50, '...'),
  105. 'difficulty' => $difficulty,
  106. 'type' => $type,
  107. ];
  108. }
  109. return $recommendations;
  110. } catch (\Exception $e) {
  111. Log::error('Failed to fetch recommendations', [
  112. 'node_id' => $nodeId,
  113. 'error' => $e->getMessage(),
  114. ]);
  115. return $this->getMockRecommendations($nodeId);
  116. }
  117. }
  118. protected function getMockRecommendations(string $nodeId): array
  119. {
  120. return [
  121. [
  122. 'id' => 1,
  123. 'title' => '基础练习:' . $nodeId,
  124. 'difficulty' => '简单',
  125. 'type' => '选择题',
  126. ],
  127. [
  128. 'id' => 2,
  129. 'title' => '进阶练习:' . $nodeId,
  130. 'difficulty' => '中等',
  131. 'type' => '填空题',
  132. ],
  133. [
  134. 'id' => 3,
  135. 'title' => '综合应用:' . $nodeId,
  136. 'difficulty' => '困难',
  137. 'type' => '解答题',
  138. ],
  139. ];
  140. }
  141. protected function getActiveMasteryData(): array
  142. {
  143. if (property_exists($this, 'masteryData')) {
  144. return $this->masteryData ?? [];
  145. }
  146. if (property_exists($this, 'mindmapMasteryData')) {
  147. return $this->mindmapMasteryData ?? [];
  148. }
  149. return [];
  150. }
  151. }