StudentDashboard.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Services\LearningAnalyticsService;
  4. use BackedEnum;
  5. use Filament\Pages\Page;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Log;
  8. use UnitEnum;
  9. use Livewire\Attributes\Layout;
  10. use Livewire\Attributes\Title;
  11. class StudentDashboard extends Page
  12. {
  13. use \Filament\Pages\Concerns\InteractsWithFormActions;
  14. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-chart-bar';
  15. protected static string|UnitEnum|null $navigationGroup = '学习分析';
  16. protected static ?string $navigationLabel = '学生仪表板';
  17. protected static ?int $navigationSort = 1;
  18. protected ?string $heading = '学生仪表板';
  19. protected string $view = 'filament.pages.student-dashboard';
  20. public string $studentId = '';
  21. public array $dashboardData = [];
  22. public bool $isLoading = false;
  23. public string $errorMessage = '';
  24. public function mount(Request $request): void
  25. {
  26. // 从请求中获取学生ID或使用默认值
  27. $this->studentId = $request->input('student_id', 'student_001');
  28. }
  29. public function loadDashboardData(): void
  30. {
  31. $this->isLoading = true;
  32. $this->errorMessage = '';
  33. try {
  34. $service = new LearningAnalyticsService();
  35. // 检查服务健康状态
  36. if (!$service->checkHealth()) {
  37. $this->errorMessage = '学习分析系统当前不可用,请稍后重试';
  38. $this->isLoading = false;
  39. return;
  40. }
  41. // 获取各项数据
  42. $masteryOverview = $service->getStudentMasteryOverview($this->studentId);
  43. $skillProficiency = $service->getStudentSkillProficiency($this->studentId);
  44. $skillSummary = $service->getStudentSkillSummary($this->studentId);
  45. $predictions = $service->getStudentPredictions($this->studentId, 5);
  46. $learningPaths = $service->getStudentLearningPaths($this->studentId, 3);
  47. $predictionAnalytics = $service->getPredictionAnalytics($this->studentId);
  48. $pathAnalytics = $service->getLearningPathAnalytics($this->studentId);
  49. $quickPrediction = $service->quickScorePrediction($this->studentId);
  50. $recommendations = $service->recommendLearningPaths($this->studentId, 3);
  51. // 组合数据
  52. $this->dashboardData = [
  53. 'mastery' => [
  54. 'overview' => $masteryOverview,
  55. 'list' => $service->getStudentMasteryList($this->studentId),
  56. ],
  57. 'skill' => [
  58. 'proficiency' => $skillProficiency,
  59. 'summary' => $skillSummary,
  60. ],
  61. 'prediction' => [
  62. 'list' => $predictions,
  63. 'analytics' => $predictionAnalytics,
  64. 'quick' => $quickPrediction,
  65. ],
  66. 'learning_path' => [
  67. 'list' => $learningPaths,
  68. 'analytics' => $pathAnalytics,
  69. 'recommendations' => $recommendations,
  70. ],
  71. ];
  72. } catch (\Exception $e) {
  73. $this->errorMessage = '加载数据时发生错误:' . $e->getMessage();
  74. Log::error('学生仪表板数据加载失败', [
  75. 'student_id' => $this->studentId,
  76. 'error' => $e->getMessage()
  77. ]);
  78. } finally {
  79. $this->isLoading = false;
  80. }
  81. }
  82. public function updatedStudentId(): void
  83. {
  84. // 学生ID更新后自动刷新数据
  85. $this->loadDashboardData();
  86. }
  87. public function recalculateMastery(string $kpCode): void
  88. {
  89. try {
  90. $service = new LearningAnalyticsService();
  91. $result = $service->recalculateMastery($this->studentId, $kpCode);
  92. if ($result) {
  93. $this->dispatch('notify', message: '掌握度重新计算完成', type: 'success');
  94. $this->loadDashboardData(); // 刷新数据
  95. } else {
  96. $this->dispatch('notify', message: '掌握度重新计算失败', type: 'danger');
  97. }
  98. } catch (\Exception $e) {
  99. Log::error('重新计算掌握度失败', [
  100. 'student_id' => $this->studentId,
  101. 'kp_code' => $kpCode,
  102. 'error' => $e->getMessage()
  103. ]);
  104. $this->dispatch('notify', message: '操作失败:' . $e->getMessage(), type: 'danger');
  105. }
  106. }
  107. public function batchUpdateSkills(): void
  108. {
  109. try {
  110. $service = new LearningAnalyticsService();
  111. $result = $service->batchUpdateSkillProficiency($this->studentId);
  112. if ($result) {
  113. $this->dispatch('notify', message: '技能熟练度更新完成', type: 'success');
  114. $this->loadDashboardData(); // 刷新数据
  115. } else {
  116. $this->dispatch('notify', message: '技能熟练度更新失败', type: 'danger');
  117. }
  118. } catch (\Exception $e) {
  119. Log::error('批量更新技能熟练度失败', [
  120. 'student_id' => $this->studentId,
  121. 'error' => $e->getMessage()
  122. ]);
  123. $this->dispatch('notify', message: '操作失败:' . $e->getMessage(), type: 'danger');
  124. }
  125. }
  126. public function generateQuickPrediction(): void
  127. {
  128. try {
  129. $service = new LearningAnalyticsService();
  130. $result = $service->quickScorePrediction($this->studentId);
  131. if ($result) {
  132. $this->dispatch('notify', message: '快速预测生成完成', type: 'success');
  133. $this->loadDashboardData(); // 刷新数据
  134. } else {
  135. $this->dispatch('notify', message: '快速预测生成失败', type: 'danger');
  136. }
  137. } catch (\Exception $e) {
  138. Log::error('生成快速预测失败', [
  139. 'student_id' => $this->studentId,
  140. 'error' => $e->getMessage()
  141. ]);
  142. $this->dispatch('notify', message: '操作失败:' . $e->getMessage(), type: 'danger');
  143. }
  144. }
  145. }