teacherId = $teacherId; $this->laService = app(LearningAnalyticsService::class); $this->loadDashboardData(); } public function loadDashboardData() { $this->loading = true; $this->error = ''; try { // 获取老师名下的学生 $this->students = DB::connection('remote_mysql') ->table('students as s') ->leftJoin('users as u', 's.student_id', '=', 'u.user_id') ->where('s.teacher_id', $this->teacherId) ->select( 's.student_id', 's.name as student_name', 's.grade', 's.class_name', 'u.username', 'u.email' ) ->get() ->map(function($student) { // 获取每个学生的掌握度数据 $masteryData = $this->laService->getStudentMastery($student->student_id); $knowledgePoints = $masteryData['data'] ?? []; // 计算统计信息 $totalPoints = count($knowledgePoints); $masteredPoints = 0; $avgMastery = 0; $totalAttempts = 0; if (!empty($knowledgePoints)) { $masterySum = 0; foreach ($knowledgePoints as $kp) { $mastery = ($kp['mastery_level'] ?? 0) * 100; $masterySum += $mastery; $totalAttempts += $kp['total_attempts'] ?? 0; if ($mastery >= 80) { $masteredPoints++; } } $avgMastery = round($masterySum / $totalPoints, 1); } return [ 'student_id' => $student->student_id, 'name' => $student->student_name, 'grade' => $student->grade, 'class' => $student->class_name, 'email' => $student->email, 'total_knowledge_points' => $totalPoints, 'mastered_points' => $masteredPoints, 'avg_mastery' => $avgMastery, 'total_attempts' => $totalAttempts, 'mastery_data' => $knowledgePoints, ]; }) ->toArray(); // 计算总体统计 $this->calculateStats(); } catch (\Exception $e) { $this->error = '加载数据失败: ' . $e->getMessage(); \Log::error('TeacherDashboard load error', [ 'teacher_id' => $this->teacherId, 'error' => $e->getMessage() ]); } finally { $this->loading = false; } } private function calculateStats() { $totalStudents = count($this->students); $totalMasteredPoints = 0; $totalAvgMastery = 0; $totalAttempts = 0; foreach ($this->students as $student) { $totalMasteredPoints += $student['mastered_points']; $totalAvgMastery += $student['avg_mastery']; $totalAttempts += $student['total_attempts']; } $this->stats = [ 'total_students' => $totalStudents, 'total_mastered_points' => $totalMasteredPoints, 'avg_mastery' => $totalStudents > 0 ? round($totalAvgMastery / $totalStudents, 1) : 0, 'total_attempts' => $totalAttempts, ]; } public function render() { return view('livewire.teacher-dashboard', [ 'stats' => $this->stats, 'students' => $this->students, ]); } }