TeacherDashboard.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Livewire;
  3. use App\Services\LearningAnalyticsService;
  4. use Illuminate\Support\Facades\DB;
  5. use Livewire\Component;
  6. class TeacherDashboard extends Component
  7. {
  8. public string $teacherId;
  9. public array $students = [];
  10. public array $stats = [];
  11. public bool $loading = false;
  12. public string $error = '';
  13. protected LearningAnalyticsService $laService;
  14. public function mount($teacherId)
  15. {
  16. $this->teacherId = $teacherId;
  17. $this->laService = new LearningAnalyticsService();
  18. $this->loadDashboardData();
  19. }
  20. public function loadDashboardData()
  21. {
  22. $this->loading = true;
  23. $this->error = '';
  24. try {
  25. // 获取老师名下的学生
  26. $this->students = DB::connection('remote_mysql')
  27. ->table('students as s')
  28. ->leftJoin('users as u', 's.student_id', '=', 'u.user_id')
  29. ->where('s.teacher_id', $this->teacherId)
  30. ->select(
  31. 's.student_id',
  32. 's.name as student_name',
  33. 's.grade',
  34. 's.class_name',
  35. 'u.username',
  36. 'u.email'
  37. )
  38. ->get()
  39. ->map(function($student) {
  40. // 获取每个学生的掌握度数据
  41. $masteryData = $this->laService->getStudentMastery($student->student_id);
  42. $knowledgePoints = $masteryData['data'] ?? [];
  43. // 计算统计信息
  44. $totalPoints = count($knowledgePoints);
  45. $masteredPoints = 0;
  46. $avgMastery = 0;
  47. $totalAttempts = 0;
  48. if (!empty($knowledgePoints)) {
  49. $masterySum = 0;
  50. foreach ($knowledgePoints as $kp) {
  51. $mastery = ($kp['mastery_level'] ?? 0) * 100;
  52. $masterySum += $mastery;
  53. $totalAttempts += $kp['total_attempts'] ?? 0;
  54. if ($mastery >= 80) {
  55. $masteredPoints++;
  56. }
  57. }
  58. $avgMastery = round($masterySum / $totalPoints, 1);
  59. }
  60. return [
  61. 'student_id' => $student->student_id,
  62. 'name' => $student->student_name,
  63. 'grade' => $student->grade,
  64. 'class' => $student->class_name,
  65. 'email' => $student->email,
  66. 'total_knowledge_points' => $totalPoints,
  67. 'mastered_points' => $masteredPoints,
  68. 'avg_mastery' => $avgMastery,
  69. 'total_attempts' => $totalAttempts,
  70. 'mastery_data' => $knowledgePoints,
  71. ];
  72. })
  73. ->toArray();
  74. // 计算总体统计
  75. $this->calculateStats();
  76. } catch (\Exception $e) {
  77. $this->error = '加载数据失败: ' . $e->getMessage();
  78. \Log::error('TeacherDashboard load error', [
  79. 'teacher_id' => $this->teacherId,
  80. 'error' => $e->getMessage()
  81. ]);
  82. } finally {
  83. $this->loading = false;
  84. }
  85. }
  86. private function calculateStats()
  87. {
  88. $totalStudents = count($this->students);
  89. $totalMasteredPoints = 0;
  90. $totalAvgMastery = 0;
  91. $totalAttempts = 0;
  92. foreach ($this->students as $student) {
  93. $totalMasteredPoints += $student['mastered_points'];
  94. $totalAvgMastery += $student['avg_mastery'];
  95. $totalAttempts += $student['total_attempts'];
  96. }
  97. $this->stats = [
  98. 'total_students' => $totalStudents,
  99. 'total_mastered_points' => $totalMasteredPoints,
  100. 'avg_mastery' => $totalStudents > 0 ? round($totalAvgMastery / $totalStudents, 1) : 0,
  101. 'total_attempts' => $totalAttempts,
  102. ];
  103. }
  104. public function render()
  105. {
  106. return view('livewire.teacher-dashboard', [
  107. 'stats' => $this->stats,
  108. 'students' => $this->students,
  109. ]);
  110. }
  111. }