MasteryHeatmap.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Livewire;
  3. use App\Services\LearningAnalyticsService;
  4. use App\Models\User;
  5. use Illuminate\Support\Facades\DB;
  6. use Livewire\Component;
  7. class MasteryHeatmap extends Component
  8. {
  9. public string $studentId;
  10. public array $heatmapData = [];
  11. public bool $loading = false;
  12. public string $error = '';
  13. protected LearningAnalyticsService $laService;
  14. public function mount($studentId)
  15. {
  16. $this->studentId = $studentId;
  17. $this->laService = new LearningAnalyticsService();
  18. $this->loadHeatmapData();
  19. }
  20. public function loadHeatmapData()
  21. {
  22. $this->loading = true;
  23. $this->error = '';
  24. try {
  25. $masteryData = $this->laService->getStudentMastery($this->studentId) ?: [];
  26. $knowledgePoints = $masteryData['data'] ?? [];
  27. // 构建热力图数据
  28. $this->heatmapData = array_map(function($kp) {
  29. $mastery = ($kp['mastery_level'] ?? 0) * 100;
  30. // 根据掌握度分配颜色
  31. if ($mastery >= 80) {
  32. $color = '#10b981'; // green-500
  33. $bgClass = 'bg-green-500';
  34. } elseif ($mastery >= 60) {
  35. $color = '#3b82f6'; // blue-500
  36. $bgClass = 'bg-blue-500';
  37. } elseif ($mastery >= 40) {
  38. $color = '#f59e0b'; // yellow-500
  39. $bgClass = 'bg-yellow-500';
  40. } elseif ($mastery >= 20) {
  41. $color = '#f97316'; // orange-500
  42. $bgClass = 'bg-orange-500';
  43. } else {
  44. $color = '#ef4444'; // red-500
  45. $bgClass = 'bg-red-500';
  46. }
  47. return [
  48. 'code' => $kp['kp_code'],
  49. 'name' => $kp['kp_name'] ?? $kp['kp_code'],
  50. 'mastery' => round($mastery, 1),
  51. 'total_attempts' => $kp['total_attempts'] ?? 0,
  52. 'correct_attempts' => $kp['correct_attempts'] ?? 0,
  53. 'color' => $color,
  54. 'bgClass' => $bgClass,
  55. ];
  56. }, $knowledgePoints);
  57. } catch (\Exception $e) {
  58. $this->error = '加载热力图数据失败: ' . $e->getMessage();
  59. \Log::error('MasteryHeatmap load error', [
  60. 'student_id' => $this->studentId,
  61. 'error' => $e->getMessage()
  62. ]);
  63. } finally {
  64. $this->loading = false;
  65. }
  66. }
  67. public function render()
  68. {
  69. return view('livewire.mastery-heatmap');
  70. }
  71. }