| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace App\Livewire;
- use App\Services\LearningAnalyticsService;
- use App\Models\User;
- use Illuminate\Support\Facades\DB;
- use Livewire\Component;
- class MasteryHeatmap extends Component
- {
- public string $studentId;
- public array $heatmapData = [];
- public bool $loading = false;
- public string $error = '';
- protected LearningAnalyticsService $laService;
- public function mount($studentId)
- {
- $this->studentId = $studentId;
- $this->laService = new LearningAnalyticsService();
- $this->loadHeatmapData();
- }
- public function loadHeatmapData()
- {
- $this->loading = true;
- $this->error = '';
- try {
- $masteryData = $this->laService->getStudentMastery($this->studentId) ?: [];
- $knowledgePoints = $masteryData['data'] ?? [];
- // 构建热力图数据
- $this->heatmapData = array_map(function($kp) {
- $mastery = ($kp['mastery_level'] ?? 0) * 100;
-
- // 根据掌握度分配颜色
- if ($mastery >= 80) {
- $color = '#10b981'; // green-500
- $bgClass = 'bg-green-500';
- } elseif ($mastery >= 60) {
- $color = '#3b82f6'; // blue-500
- $bgClass = 'bg-blue-500';
- } elseif ($mastery >= 40) {
- $color = '#f59e0b'; // yellow-500
- $bgClass = 'bg-yellow-500';
- } elseif ($mastery >= 20) {
- $color = '#f97316'; // orange-500
- $bgClass = 'bg-orange-500';
- } else {
- $color = '#ef4444'; // red-500
- $bgClass = 'bg-red-500';
- }
- return [
- 'code' => $kp['kp_code'],
- 'name' => $kp['kp_name'] ?? $kp['kp_code'],
- 'mastery' => round($mastery, 1),
- 'total_attempts' => $kp['total_attempts'] ?? 0,
- 'correct_attempts' => $kp['correct_attempts'] ?? 0,
- 'color' => $color,
- 'bgClass' => $bgClass,
- ];
- }, $knowledgePoints);
- } catch (\Exception $e) {
- $this->error = '加载热力图数据失败: ' . $e->getMessage();
- \Log::error('MasteryHeatmap load error', [
- 'student_id' => $this->studentId,
- 'error' => $e->getMessage()
- ]);
- } finally {
- $this->loading = false;
- }
- }
- public function render()
- {
- return view('livewire.mastery-heatmap');
- }
- }
|