KnowledgeGraphVisualization.php 4.0 KB

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