| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace App\Livewire;
- use App\Services\LearningAnalyticsService;
- use Illuminate\Support\Facades\DB;
- use Livewire\Component;
- class TeacherDashboard extends Component
- {
- public string $teacherId;
- public array $students = [];
- public array $stats = [];
- public bool $loading = false;
- public string $error = '';
- protected LearningAnalyticsService $laService;
- public function mount($teacherId)
- {
- $this->teacherId = $teacherId;
- $this->laService = new LearningAnalyticsService();
- $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,
- ]);
- }
- }
|