StudentKnowledgeGraph.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. $masteryPayload = [];
  72. try {
  73. // 获取掌握度数据
  74. $masteryResponse = Http::timeout(10)->get($baseUrl . '/api/mastery/' . $studentId);
  75. if ($masteryResponse->successful()) {
  76. $masteryPayload = $masteryResponse->json();
  77. }
  78. // 获取依赖关系
  79. $dependencyResponse = Http::timeout(10)->get($baseUrl . '/api/knowledge/dependencies');
  80. if ($dependencyResponse->successful()) {
  81. $this->dependencies = $dependencyResponse->json();
  82. }
  83. // 获取统计信息
  84. $statsResponse = Http::timeout(10)->get($baseUrl . '/api/mastery/' . $studentId . '/statistics');
  85. if ($statsResponse->successful()) {
  86. $this->statistics = $statsResponse->json();
  87. }
  88. // 获取学习路径
  89. $pathResponse = Http::timeout(10)->get($baseUrl . '/api/learning-path/' . $studentId);
  90. if ($pathResponse->successful()) {
  91. $this->learningPath = $pathResponse->json();
  92. }
  93. $this->setMasteryData($masteryPayload);
  94. // 构建知识点图谱数据
  95. $this->buildKnowledgeGraphData();
  96. } catch (\Exception $e) {
  97. \Log::warning('LearningAnalytics API调用失败,使用本地数据', [
  98. 'error' => $e->getMessage(),
  99. ]);
  100. // 如果API调用失败,使用本地模拟数据
  101. $this->loadMockData($studentId);
  102. }
  103. }
  104. private function buildKnowledgeGraphData()
  105. {
  106. $nodes = [];
  107. $links = [];
  108. $masteries = $this->masteryData['masteries'] ?? [];
  109. // 处理掌握度数据,构建节点
  110. foreach ($masteries as $mastery) {
  111. if (!isset($mastery['mastery_level'])) {
  112. continue;
  113. }
  114. $masteryLevel = (float) $mastery['mastery_level'];
  115. $kpCode = $mastery['kp_code'] ?? null;
  116. if (!$kpCode) {
  117. continue;
  118. }
  119. $nodes[] = [
  120. 'id' => $kpCode,
  121. 'label' => $mastery['kp_name'] ?? $kpCode,
  122. 'mastery' => $masteryLevel,
  123. 'color' => $this->getMasteryColor($masteryLevel),
  124. 'size' => $this->getMasterySize($masteryLevel),
  125. ];
  126. }
  127. // 处理依赖关系,构建边
  128. if (isset($this->dependencies['dependencies'])) {
  129. foreach ($this->dependencies['dependencies'] as $dep) {
  130. $links[] = [
  131. 'source' => $dep['prerequisite_kp'],
  132. 'target' => $dep['dependent_kp'],
  133. 'strength' => $dep['influence_weight'],
  134. 'type' => $dep['dependency_type'],
  135. ];
  136. }
  137. }
  138. $this->knowledgePoints = [
  139. 'nodes' => $nodes,
  140. 'links' => $links,
  141. ];
  142. $this->dispatchGraphUpdated();
  143. }
  144. private function loadMockData($studentId)
  145. {
  146. // 模拟数据,用于演示
  147. $mockKnowledgePoints = [
  148. 'R01' => ['name' => '有理数', 'mastery' => 0.85],
  149. 'R02' => ['name' => '整式运算', 'mastery' => 0.72],
  150. 'R03' => ['name' => '一元一次方程', 'mastery' => 0.65],
  151. 'R04' => ['name' => '因式分解', 'mastery' => 0.45],
  152. 'R05' => ['name' => '二次方程', 'mastery' => 0.30],
  153. 'R06' => ['name' => '二次函数', 'mastery' => 0.25],
  154. 'R07' => ['name' => '几何图形', 'mastery' => 0.78],
  155. 'R08' => ['name' => '三角形', 'mastery' => 0.68],
  156. ];
  157. $nodes = [];
  158. foreach ($mockKnowledgePoints as $code => $data) {
  159. $nodes[] = [
  160. 'id' => $code,
  161. 'label' => $data['name'],
  162. 'mastery' => $data['mastery'],
  163. 'color' => $this->getMasteryColor($data['mastery']),
  164. 'size' => $this->getMasterySize($data['mastery']),
  165. ];
  166. }
  167. $links = [
  168. ['source' => 'R01', 'target' => 'R02', 'strength' => 0.9, 'type' => 'must'],
  169. ['source' => 'R02', 'target' => 'R03', 'strength' => 0.8, 'type' => 'must'],
  170. ['source' => 'R02', 'target' => 'R04', 'strength' => 0.7, 'type' => 'should'],
  171. ['source' => 'R03', 'target' => 'R05', 'strength' => 0.9, 'type' => 'must'],
  172. ['source' => 'R04', 'target' => 'R05', 'strength' => 0.8, 'type' => 'should'],
  173. ['source' => 'R05', 'target' => 'R06', 'strength' => 0.9, 'type' => 'must'],
  174. ['source' => 'R07', 'target' => 'R08', 'strength' => 0.8, 'type' => 'should'],
  175. ];
  176. $this->knowledgePoints = [
  177. 'nodes' => $nodes,
  178. 'links' => $links,
  179. ];
  180. $this->setMasteryData([
  181. 'masteries' => array_map(function ($code, $data) use ($studentId) {
  182. return [
  183. 'student_id' => $studentId,
  184. 'kp_code' => $code,
  185. 'mastery_level' => $data['mastery'],
  186. 'confidence_level' => 0.8,
  187. ];
  188. }, array_keys($mockKnowledgePoints), $mockKnowledgePoints),
  189. ]);
  190. $this->statistics = [
  191. 'total_knowledge_points' => count($mockKnowledgePoints),
  192. 'average_mastery' => array_sum(array_column($mockKnowledgePoints, 'mastery')) / count($mockKnowledgePoints),
  193. 'high_mastery_count' => count(array_filter($mockKnowledgePoints, fn($d) => $d['mastery'] >= 0.7)),
  194. 'medium_mastery_count' => count(array_filter($mockKnowledgePoints, fn($d) => $d['mastery'] >= 0.4 && $d['mastery'] < 0.7)),
  195. 'low_mastery_count' => count(array_filter($mockKnowledgePoints, fn($d) => $d['mastery'] < 0.4)),
  196. ];
  197. $this->dispatchGraphUpdated();
  198. }
  199. private function getMasteryColor($mastery)
  200. {
  201. if ($mastery >= 0.8) return '#10b981'; // 绿色 - 优秀
  202. if ($mastery >= 0.6) return '#3b82f6'; // 蓝色 - 良好
  203. if ($mastery >= 0.4) return '#f59e0b'; // 黄色 - 中等
  204. if ($mastery >= 0.2) return '#f97316'; // 橙色 - 待提高
  205. return '#ef4444'; // 红色 - 薄弱
  206. }
  207. private function getMasterySize($mastery)
  208. {
  209. return max(10, $mastery * 40); // 最小10px,最大40px
  210. }
  211. private function resetData()
  212. {
  213. $this->selectedStudent = null;
  214. $this->knowledgePoints = [];
  215. $this->dependencies = [];
  216. $this->masteryData = [];
  217. $this->statistics = [];
  218. $this->learningPath = [];
  219. $this->dispatchGraphUpdated();
  220. }
  221. private function dispatchGraphUpdated(): void
  222. {
  223. // 通知前端重新渲染图谱(更新节点颜色/大小等)
  224. $this->dispatch('knowledgeGraphUpdated', $this->knowledgePoints);
  225. }
  226. private function setMasteryData(array $payload): void
  227. {
  228. $masteries = $this->normalizeMasteries($payload);
  229. $this->masteryData = array_merge($payload, [
  230. 'masteries' => $masteries,
  231. 'mastery_map' => $this->buildMasteryMap($masteries),
  232. ]);
  233. }
  234. private function normalizeMasteries(array $raw): array
  235. {
  236. if (isset($raw['masteries']) && is_array($raw['masteries'])) {
  237. return array_values($raw['masteries']);
  238. }
  239. $candidates = [];
  240. if (isset($raw['data']) && is_array($raw['data'])) {
  241. $candidates[] = $raw['data'];
  242. }
  243. if (isset($raw['Target']) && is_array($raw['Target'])) {
  244. $candidates[] = $raw['Target'];
  245. }
  246. if ($this->isAssociativeArray($raw) && $this->looksLikeMasteryRecord(reset($raw))) {
  247. $candidates[] = $raw;
  248. }
  249. foreach ($candidates as $candidate) {
  250. $normalized = $this->normalizeCandidateMasteries($candidate);
  251. if (!empty($normalized)) {
  252. return $normalized;
  253. }
  254. }
  255. return [];
  256. }
  257. private function normalizeCandidateMasteries(array $candidate): array
  258. {
  259. $normalized = [];
  260. foreach ($candidate as $key => $entry) {
  261. if (!is_array($entry) || !isset($entry['mastery_level'])) {
  262. continue;
  263. }
  264. if (!isset($entry['kp_code']) && is_string($key)) {
  265. $entry['kp_code'] = $key;
  266. }
  267. if (isset($entry['kp_code'])) {
  268. $normalized[] = $entry;
  269. }
  270. }
  271. return $normalized;
  272. }
  273. private function isAssociativeArray(array $array): bool
  274. {
  275. return array_keys($array) !== range(0, count($array) - 1);
  276. }
  277. private function looksLikeMasteryRecord($entry): bool
  278. {
  279. return is_array($entry) && array_key_exists('mastery_level', $entry);
  280. }
  281. private function buildMasteryMap(array $masteries): array
  282. {
  283. $map = [];
  284. foreach ($masteries as $mastery) {
  285. if (!isset($mastery['kp_code'])) {
  286. continue;
  287. }
  288. $map[$mastery['kp_code']] = $mastery;
  289. }
  290. return $map;
  291. }
  292. public function render()
  293. {
  294. return view('livewire.student-knowledge-graph');
  295. }
  296. }