| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- namespace App\Livewire;
- use App\Services\LearningAnalyticsService;
- use App\Models\User;
- use Illuminate\Support\Facades\DB;
- use Livewire\Component;
- class StudentAnalytics extends Component
- {
- public User $student;
- public string $studentId;
- public array $masteryData = [];
- public array $analysis = [];
- public bool $loading = false;
- public string $error = '';
- protected LearningAnalyticsService $laService;
- public function mount($studentId)
- {
- $this->studentId = $studentId;
- $this->laService = new LearningAnalyticsService();
- $this->loadStudentData();
- }
- public function loadStudentData()
- {
- $this->loading = true;
- $this->error = '';
- try {
- // 获取学生基本信息
- $this->student = User::where('user_id', $this->studentId)->firstOrFail();
- // 从 MySQL 获取练习历史
- $exercises = DB::connection('remote_mysql')
- ->table('student_exercises')
- ->where('student_id', $this->studentId)
- ->orderBy('created_at', 'desc')
- ->limit(50)
- ->get()
- ->toArray();
- // 从 LearningAnalytics 获取掌握度数据
- $this->masteryData = $this->laService->getStudentMastery($this->studentId) ?: [];
- // 从 LearningAnalytics 获取学习分析
- $this->analysis = $this->laService->getStudentAnalysis($this->studentId);
- // 合并数据
- $this->analysis['exercises'] = $exercises;
- $this->analysis['exercises_count'] = count($exercises);
- } catch (\Exception $e) {
- $this->error = '加载学生数据失败: ' . $e->getMessage();
- \Log::error('StudentAnalytics load error', [
- 'student_id' => $this->studentId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- } finally {
- $this->loading = false;
- }
- }
- public function render()
- {
- return view('livewire.student-analytics', [
- 'masteryPoints' => $this->masteryData['data'] ?? [],
- 'totalKnowledgePoints' => count($this->masteryData['data'] ?? []),
- 'student' => $this->student,
- ]);
- }
- }
|