KnowledgeMindmap.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Filament\Pages;
  3. use BackedEnum;
  4. use Filament\Pages\Page;
  5. use UnitEnum;
  6. class KnowledgeMindmap 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 = 29;
  11. protected static ?string $navigationLabel = '知识图谱脑图';
  12. protected static ?string $slug = 'knowledge-mindmap';
  13. protected static ?string $title = '知识图谱脑图';
  14. protected string $view = 'filament.pages.knowledge-mindmap';
  15. public $teachers = [];
  16. public $students = [];
  17. public $selectedTeacherId = '';
  18. public $selectedStudentId = '';
  19. public $selectedStudentName = '';
  20. public $masteryData = [];
  21. public function mount(): void
  22. {
  23. $this->loadTeachers();
  24. }
  25. public function loadTeachers(): void
  26. {
  27. // Use the same logic as the controller but directly here
  28. $teachers = \App\Models\Teacher::query()
  29. ->select('teacher_id', 'name')
  30. ->limit(10)
  31. ->get();
  32. if ($teachers->isEmpty()) {
  33. $this->teachers = [
  34. ['teacher_id' => 1, 'name' => '张老师'],
  35. ['teacher_id' => 2, 'name' => '李老师'],
  36. ['teacher_id' => 3, 'name' => '王老师'],
  37. ];
  38. } else {
  39. $this->teachers = $teachers->toArray();
  40. }
  41. }
  42. public function updatedSelectedTeacherId(): void
  43. {
  44. $this->students = [];
  45. $this->selectedStudentId = '';
  46. $this->selectedStudentName = '';
  47. $this->masteryData = [];
  48. if (!$this->selectedTeacherId) {
  49. return;
  50. }
  51. $students = \App\Models\Student::query()
  52. ->where('teacher_id', $this->selectedTeacherId)
  53. ->select('student_id', 'name')
  54. ->limit(20)
  55. ->get();
  56. if ($students->isEmpty()) {
  57. $this->students = [
  58. ['student_id' => 101, 'name' => '学生A'],
  59. ['student_id' => 102, 'name' => '学生B'],
  60. ['student_id' => 103, 'name' => '学生C'],
  61. ];
  62. } else {
  63. $this->students = $students->toArray();
  64. }
  65. }
  66. public function updatedSelectedStudentId(): void
  67. {
  68. $this->masteryData = [];
  69. $this->selectedStudentName = '';
  70. if (!$this->selectedStudentId) {
  71. $this->dispatch('mastery-updated', data: []);
  72. return;
  73. }
  74. // Find student name
  75. $student = collect($this->students)->firstWhere('student_id', $this->selectedStudentId);
  76. $this->selectedStudentName = $student['name'] ?? '';
  77. try {
  78. $service = app(\App\Services\LearningAnalyticsService::class);
  79. $response = $service->getStudentMasteryList($this->selectedStudentId);
  80. $data = $response['data'] ?? [];
  81. // Transform to map: { 'KP_CODE': { mastery_level: 0.8, total_attempts: 5, ... } }
  82. $masteryMap = [];
  83. foreach ($data as $item) {
  84. if (isset($item['kp_code'])) {
  85. $masteryMap[$item['kp_code']] = $item;
  86. }
  87. }
  88. $this->masteryData = $masteryMap;
  89. // Dispatch event to notify frontend to refresh graph
  90. $this->dispatch('mastery-updated', data: $masteryMap);
  91. } catch (\Exception $e) {
  92. \Illuminate\Support\Facades\Log::error('Failed to fetch mastery data', [
  93. 'student_id' => $this->selectedStudentId,
  94. 'error' => $e->getMessage()
  95. ]);
  96. $this->dispatch('mastery-updated', data: []);
  97. }
  98. }
  99. }