StudentKnowledgeMastery.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. class StudentKnowledgeMastery extends Model
  7. {
  8. use HasFactory;
  9. protected $table = 'student_knowledge_mastery';
  10. protected $fillable = [
  11. 'student_id',
  12. 'kp_code',
  13. 'mastery_level',
  14. 'confidence_level',
  15. 'total_attempts',
  16. 'correct_attempts',
  17. 'incorrect_attempts',
  18. 'partial_attempts',
  19. 'avg_time_seconds',
  20. 'fastest_time',
  21. 'slowest_time',
  22. 'attempts_easy',
  23. 'attempts_medium',
  24. 'attempts_hard',
  25. 'correct_easy',
  26. 'correct_medium',
  27. 'correct_hard',
  28. 'first_attempt_at',
  29. 'last_attempt_at',
  30. 'last_mastery_update',
  31. 'mastery_trend',
  32. 'mastery_change',
  33. 'calculation_version',
  34. 'notes',
  35. ];
  36. protected $casts = [
  37. 'mastery_level' => 'decimal:4',
  38. 'confidence_level' => 'decimal:4',
  39. 'mastery_change' => 'decimal:4',
  40. 'avg_time_seconds' => 'decimal:2',
  41. 'first_attempt_at' => 'datetime',
  42. 'last_attempt_at' => 'datetime',
  43. 'last_mastery_update' => 'datetime',
  44. 'created_at' => 'datetime',
  45. 'updated_at' => 'datetime',
  46. ];
  47. /**
  48. * 关联学生
  49. */
  50. public function student(): BelongsTo
  51. {
  52. return $this->belongsTo(Student::class, 'student_id', 'student_id');
  53. }
  54. /**
  55. * 关联知识点
  56. */
  57. public function knowledgePoint(): BelongsTo
  58. {
  59. return $this->belongsTo(KnowledgePoint::class, 'kp_code', 'kp_code');
  60. }
  61. /**
  62. * 作用域:按学生筛选
  63. */
  64. public function scopeForStudent($query, string $studentId)
  65. {
  66. return $query->where('student_id', $studentId);
  67. }
  68. /**
  69. * 作用域:按知识点筛选
  70. */
  71. public function scopeForKnowledgePoint($query, string $kpCode)
  72. {
  73. return $query->where('kp_code', $kpCode);
  74. }
  75. /**
  76. * 作用域:薄弱点(掌握度低于阈值)
  77. */
  78. public function scopeWeaknesses($query, float $threshold = 0.7)
  79. {
  80. return $query->where('mastery_level', '<', $threshold);
  81. }
  82. /**
  83. * 作用域:按掌握度排序
  84. */
  85. public function scopeOrderByMastery($query, string $direction = 'asc')
  86. {
  87. return $query->orderBy('mastery_level', $direction);
  88. }
  89. /**
  90. * 获取薄弱点列表
  91. */
  92. public static function getWeaknesses(string $studentId, float $threshold = 0.7, int $limit = 20): array
  93. {
  94. return self::forStudent($studentId)
  95. ->weaknesses($threshold)
  96. ->orderByMastery('asc')
  97. ->limit($limit)
  98. ->get()
  99. ->toArray();
  100. }
  101. /**
  102. * 计算掌握度等级
  103. */
  104. public function getMasteryLevelAttribute($value): string
  105. {
  106. if ($value >= 0.85) {
  107. return '优秀';
  108. } elseif ($value >= 0.70) {
  109. return '良好';
  110. } elseif ($value >= 0.50) {
  111. return '及格';
  112. } else {
  113. return '薄弱';
  114. }
  115. }
  116. /**
  117. * 获取趋势标签
  118. */
  119. public function getTrendLabelAttribute(): string
  120. {
  121. return match ($this->mastery_trend) {
  122. 'improving' => '上升',
  123. 'declining' => '下降',
  124. 'stable' => '稳定',
  125. 'insufficient' => '数据不足',
  126. default => '未知',
  127. };
  128. }
  129. /**
  130. * 计算成功率
  131. */
  132. public function getSuccessRateAttribute(): float
  133. {
  134. if ($this->total_attempts <= 0) {
  135. return 0.0;
  136. }
  137. return round(($this->correct_attempts / $this->total_attempts) * 100, 2);
  138. }
  139. }