StudentAnalytics.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Livewire;
  3. use App\Services\LearningAnalyticsService;
  4. use App\Models\User;
  5. use Illuminate\Support\Facades\DB;
  6. use Livewire\Component;
  7. class StudentAnalytics extends Component
  8. {
  9. public User $student;
  10. public string $studentId;
  11. public array $masteryData = [];
  12. public array $analysis = [];
  13. public bool $loading = false;
  14. public string $error = '';
  15. protected LearningAnalyticsService $laService;
  16. public function mount($studentId)
  17. {
  18. $this->studentId = $studentId;
  19. $this->laService = new LearningAnalyticsService();
  20. $this->loadStudentData();
  21. }
  22. public function loadStudentData()
  23. {
  24. $this->loading = true;
  25. $this->error = '';
  26. try {
  27. // 获取学生基本信息
  28. $this->student = User::where('user_id', $this->studentId)->firstOrFail();
  29. // 从 MySQL 获取练习历史
  30. $exercises = DB::connection('remote_mysql')
  31. ->table('student_exercises')
  32. ->where('student_id', $this->studentId)
  33. ->orderBy('created_at', 'desc')
  34. ->limit(50)
  35. ->get()
  36. ->toArray();
  37. // 从 LearningAnalytics 获取掌握度数据
  38. $this->masteryData = $this->laService->getStudentMastery($this->studentId) ?: [];
  39. // 从 LearningAnalytics 获取学习分析
  40. $this->analysis = $this->laService->getStudentAnalysis($this->studentId);
  41. // 合并数据
  42. $this->analysis['exercises'] = $exercises;
  43. $this->analysis['exercises_count'] = count($exercises);
  44. } catch (\Exception $e) {
  45. $this->error = '加载学生数据失败: ' . $e->getMessage();
  46. \Log::error('StudentAnalytics load error', [
  47. 'student_id' => $this->studentId,
  48. 'error' => $e->getMessage(),
  49. 'trace' => $e->getTraceAsString()
  50. ]);
  51. } finally {
  52. $this->loading = false;
  53. }
  54. }
  55. public function render()
  56. {
  57. return view('livewire.student-analytics', [
  58. 'masteryPoints' => $this->masteryData['data'] ?? [],
  59. 'totalKnowledgePoints' => count($this->masteryData['data'] ?? []),
  60. 'student' => $this->student,
  61. ]);
  62. }
  63. }