StudentKnowledgeGraph.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace App\Livewire;
  3. use Livewire\Component;
  4. use App\Models\Student;
  5. use Illuminate\Support\Facades\Http;
  6. use Illuminate\Support\Facades\DB;
  7. class StudentKnowledgeGraph extends Component
  8. {
  9. public $selectedStudentId = null;
  10. public $selectedStudent = null;
  11. public $knowledgePoints = [];
  12. public $dependencies = [];
  13. public $masteryData = [];
  14. public $statistics = [];
  15. public $learningPath = [];
  16. public $isLoading = false;
  17. public $students = [];
  18. protected $rules = [
  19. 'selectedStudentId' => 'required|exists:students,student_id',
  20. ];
  21. public function mount()
  22. {
  23. $this->loadStudents();
  24. }
  25. public function updatedSelectedStudentId($value)
  26. {
  27. if ($value) {
  28. $this->loadStudentData($value);
  29. } else {
  30. $this->resetData();
  31. }
  32. }
  33. public function loadStudents()
  34. {
  35. $this->students = DB::table('students')
  36. ->select('student_id', 'name', 'grade', 'class_name')
  37. ->orderBy('grade')
  38. ->orderBy('class_name')
  39. ->orderBy('name')
  40. ->get()
  41. ->map(function ($student) {
  42. return [
  43. 'id' => $student->student_id,
  44. 'label' => "{$student->name} ({$student->grade}-{$student->class_name})",
  45. ];
  46. })
  47. ->toArray();
  48. }
  49. public function loadStudentData($studentId)
  50. {
  51. $this->isLoading = true;
  52. try {
  53. // 获取学生信息
  54. $this->selectedStudent = DB::table('students')
  55. ->where('student_id', $studentId)
  56. ->first();
  57. // 调用LearningAnalytics API获取知识图谱数据
  58. $this->fetchKnowledgeGraphData($studentId);
  59. } catch (\Exception $e) {
  60. session()->flash('error', '加载数据失败:' . $e->getMessage());
  61. \Log::error('加载学生知识图谱失败', [
  62. 'student_id' => $studentId,
  63. 'error' => $e->getMessage(),
  64. ]);
  65. }
  66. $this->isLoading = false;
  67. }
  68. private function fetchKnowledgeGraphData($studentId)
  69. {
  70. $baseUrl = config('services.learning_analytics.url', 'http://localhost:5010');
  71. try {
  72. // 获取掌握度数据
  73. $masteryResponse = Http::timeout(10)->get($baseUrl . '/api/mastery/' . $studentId);
  74. if ($masteryResponse->successful()) {
  75. $this->masteryData = $masteryResponse->json();
  76. }
  77. // 获取依赖关系
  78. $dependencyResponse = Http::timeout(10)->get($baseUrl . '/api/knowledge/dependencies');
  79. if ($dependencyResponse->successful()) {
  80. $this->dependencies = $dependencyResponse->json();
  81. }
  82. // 获取统计信息
  83. $statsResponse = Http::timeout(10)->get($baseUrl . '/api/mastery/' . $studentId . '/statistics');
  84. if ($statsResponse->successful()) {
  85. $this->statistics = $statsResponse->json();
  86. }
  87. // 获取学习路径
  88. $pathResponse = Http::timeout(10)->get($baseUrl . '/api/learning-path/' . $studentId);
  89. if ($pathResponse->successful()) {
  90. $this->learningPath = $pathResponse->json();
  91. }
  92. // 构建知识点图谱数据
  93. $this->buildKnowledgeGraphData();
  94. } catch (\Exception $e) {
  95. \Log::warning('LearningAnalytics API调用失败,使用本地数据', [
  96. 'error' => $e->getMessage(),
  97. ]);
  98. // 如果API调用失败,使用本地模拟数据
  99. $this->loadMockData($studentId);
  100. }
  101. }
  102. private function buildKnowledgeGraphData()
  103. {
  104. $nodes = [];
  105. $links = [];
  106. // 处理掌握度数据,构建节点
  107. if (isset($this->masteryData['masteries'])) {
  108. foreach ($this->masteryData['masteries'] as $mastery) {
  109. $nodes[] = [
  110. 'id' => $mastery['kp_code'],
  111. 'label' => $mastery['kp_code'],
  112. 'mastery' => $mastery['mastery_level'],
  113. 'color' => $this->getMasteryColor($mastery['mastery_level']),
  114. 'size' => $this->getMasterySize($mastery['mastery_level']),
  115. ];
  116. }
  117. }
  118. // 处理依赖关系,构建边
  119. if (isset($this->dependencies['dependencies'])) {
  120. foreach ($this->dependencies['dependencies'] as $dep) {
  121. $links[] = [
  122. 'source' => $dep['prerequisite_kp'],
  123. 'target' => $dep['dependent_kp'],
  124. 'strength' => $dep['influence_weight'],
  125. 'type' => $dep['dependency_type'],
  126. ];
  127. }
  128. }
  129. $this->knowledgePoints = [
  130. 'nodes' => $nodes,
  131. 'links' => $links,
  132. ];
  133. }
  134. private function loadMockData($studentId)
  135. {
  136. // 模拟数据,用于演示
  137. $mockKnowledgePoints = [
  138. 'R01' => ['name' => '有理数', 'mastery' => 0.85],
  139. 'R02' => ['name' => '整式运算', 'mastery' => 0.72],
  140. 'R03' => ['name' => '一元一次方程', 'mastery' => 0.65],
  141. 'R04' => ['name' => '因式分解', 'mastery' => 0.45],
  142. 'R05' => ['name' => '二次方程', 'mastery' => 0.30],
  143. 'R06' => ['name' => '二次函数', 'mastery' => 0.25],
  144. 'R07' => ['name' => '几何图形', 'mastery' => 0.78],
  145. 'R08' => ['name' => '三角形', 'mastery' => 0.68],
  146. ];
  147. $nodes = [];
  148. foreach ($mockKnowledgePoints as $code => $data) {
  149. $nodes[] = [
  150. 'id' => $code,
  151. 'label' => $data['name'],
  152. 'mastery' => $data['mastery'],
  153. 'color' => $this->getMasteryColor($data['mastery']),
  154. 'size' => $this->getMasterySize($data['mastery']),
  155. ];
  156. }
  157. $links = [
  158. ['source' => 'R01', 'target' => 'R02', 'strength' => 0.9, 'type' => 'must'],
  159. ['source' => 'R02', 'target' => 'R03', 'strength' => 0.8, 'type' => 'must'],
  160. ['source' => 'R02', 'target' => 'R04', 'strength' => 0.7, 'type' => 'should'],
  161. ['source' => 'R03', 'target' => 'R05', 'strength' => 0.9, 'type' => 'must'],
  162. ['source' => 'R04', 'target' => 'R05', 'strength' => 0.8, 'type' => 'should'],
  163. ['source' => 'R05', 'target' => 'R06', 'strength' => 0.9, 'type' => 'must'],
  164. ['source' => 'R07', 'target' => 'R08', 'strength' => 0.8, 'type' => 'should'],
  165. ];
  166. $this->knowledgePoints = [
  167. 'nodes' => $nodes,
  168. 'links' => $links,
  169. ];
  170. $this->masteryData = [
  171. 'masteries' => array_map(function ($code, $data) use ($studentId) {
  172. return [
  173. 'student_id' => $studentId,
  174. 'kp_code' => $code,
  175. 'mastery_level' => $data['mastery'],
  176. 'confidence_level' => 0.8,
  177. ];
  178. }, array_keys($mockKnowledgePoints), $mockKnowledgePoints),
  179. ];
  180. $this->statistics = [
  181. 'total_knowledge_points' => count($mockKnowledgePoints),
  182. 'average_mastery' => array_sum(array_column($mockKnowledgePoints, 'mastery')) / count($mockKnowledgePoints),
  183. 'high_mastery_count' => count(array_filter($mockKnowledgePoints, fn($d) => $d['mastery'] >= 0.7)),
  184. 'medium_mastery_count' => count(array_filter($mockKnowledgePoints, fn($d) => $d['mastery'] >= 0.4 && $d['mastery'] < 0.7)),
  185. 'low_mastery_count' => count(array_filter($mockKnowledgePoints, fn($d) => $d['mastery'] < 0.4)),
  186. ];
  187. }
  188. private function getMasteryColor($mastery)
  189. {
  190. if ($mastery >= 0.8) return '#10b981'; // 绿色 - 优秀
  191. if ($mastery >= 0.6) return '#3b82f6'; // 蓝色 - 良好
  192. if ($mastery >= 0.4) return '#f59e0b'; // 黄色 - 中等
  193. if ($mastery >= 0.2) return '#f97316'; // 橙色 - 待提高
  194. return '#ef4444'; // 红色 - 薄弱
  195. }
  196. private function getMasterySize($mastery)
  197. {
  198. return max(10, $mastery * 40); // 最小10px,最大40px
  199. }
  200. private function resetData()
  201. {
  202. $this->selectedStudent = null;
  203. $this->knowledgePoints = [];
  204. $this->dependencies = [];
  205. $this->masteryData = [];
  206. $this->statistics = [];
  207. $this->learningPath = [];
  208. }
  209. public function render()
  210. {
  211. return view('livewire.student-knowledge-graph');
  212. }
  213. }