KnowledgeGraphVisualization.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Services\KnowledgeGraphService;
  4. use App\Services\LearningAnalyticsService;
  5. use Filament\Pages\Page;
  6. class KnowledgeGraphVisualization extends Page
  7. {
  8. protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-share';
  9. protected static string|\UnitEnum|null $navigationGroup = null;
  10. protected static ?int $navigationSort = 3;
  11. protected static bool $shouldRegisterNavigation = false;
  12. protected static ?string $navigationLabel = '知识图谱可视化';
  13. protected static ?string $title = '知识图谱可视化';
  14. protected string $view = 'filament.pages.knowledge-graph-visualization-simple';
  15. public array $graphData = [];
  16. public ?string $selectedStudentId = null;
  17. public array $studentMasteryData = [];
  18. public function mount(KnowledgeGraphService $service, LearningAnalyticsService $learningService): void
  19. {
  20. $this->graphData = $service->exportGraph();
  21. // 如果有选中的学生,获取掌握度数据
  22. if ($this->selectedStudentId) {
  23. $this->loadStudentMasteryData($learningService);
  24. }
  25. }
  26. public function updatedSelectedStudentId($value)
  27. {
  28. if ($value) {
  29. $learningService = app(LearningAnalyticsService::class);
  30. $this->loadStudentMasteryData($learningService);
  31. } else {
  32. $this->studentMasteryData = [];
  33. }
  34. }
  35. protected function loadStudentMasteryData(LearningAnalyticsService $learningService)
  36. {
  37. try {
  38. // 从MySQL查询学生掌握度数据
  39. $masteryRecords = \Illuminate\Support\Facades\DB::connection('remote_mysql')
  40. ->table('student_mastery')
  41. ->where('student_id', $this->selectedStudentId)
  42. ->select(['kp', 'mastery', 'stability'])
  43. ->get()
  44. ->toArray();
  45. $this->studentMasteryData = array_map(function ($record) {
  46. return [
  47. 'kp_code' => $record->kp,
  48. 'mastery' => (float) $record->mastery,
  49. 'stability' => (float) $record->stability
  50. ];
  51. }, $masteryRecords);
  52. } catch (\Exception $e) {
  53. \Illuminate\Support\Facades\Log::error('获取学生掌握度数据失败', [
  54. 'student_id' => $this->selectedStudentId,
  55. 'error' => $e->getMessage()
  56. ]);
  57. $this->studentMasteryData = [];
  58. }
  59. }
  60. public function getNodeMastery(string $kpCode): ?float
  61. {
  62. foreach ($this->studentMasteryData as $mastery) {
  63. if ($mastery['kp_code'] === $kpCode) {
  64. return $mastery['mastery'];
  65. }
  66. }
  67. return null;
  68. }
  69. public function getMasteryColor(?float $mastery): string
  70. {
  71. if ($mastery === null) {
  72. return '#d1d5db'; // gray-300 - 未学习
  73. }
  74. if ($mastery >= 0.9) return '#10b981'; // emerald-500 - 优秀
  75. if ($mastery >= 0.8) return '#34d399'; // emerald-400 - 良好
  76. if ($mastery >= 0.7) return '#fbbf24'; // amber-400 - 中等
  77. if ($mastery >= 0.6) return '#fb923c'; // orange-400 - 及格
  78. return '#ef4444'; // red-500 - 需提升
  79. }
  80. public function getMasteryLevel(?float $mastery): string
  81. {
  82. if ($mastery === null) return '未学习';
  83. if ($mastery >= 0.9) return '优秀';
  84. if ($mastery >= 0.8) return '良好';
  85. if ($mastery >= 0.7) return '中等';
  86. if ($mastery >= 0.6) return '及格';
  87. return '需提升';
  88. }
  89. public function getStudents(): array
  90. {
  91. try {
  92. return \Illuminate\Support\Facades\DB::connection('remote_mysql')
  93. ->table('students')
  94. ->select(['student_id', 'name'])
  95. ->limit(100)
  96. ->get()
  97. ->toArray();
  98. } catch (\Exception $e) {
  99. \Illuminate\Support\Facades\Log::error('获取学生列表失败', [
  100. 'error' => $e->getMessage()
  101. ]);
  102. return [];
  103. }
  104. }
  105. }