StudentKnowledgeMastery.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. public static function allAtLeast(int $studentId, array $kpCodes, float $threshold): bool
  102. {
  103. if (empty($kpCodes)) {
  104. return false;
  105. }
  106. $levels = self::query()
  107. ->where('student_id', $studentId)
  108. ->whereIn('kp_code', $kpCodes)
  109. ->pluck('mastery_level', 'kp_code')
  110. ->toArray();
  111. foreach ($kpCodes as $kpCode) {
  112. $level = isset($levels[$kpCode]) ? (float) $levels[$kpCode] : 0.0;
  113. if ($level < $threshold) {
  114. return false;
  115. }
  116. }
  117. return true;
  118. }
  119. /**
  120. * 计算掌握度等级
  121. */
  122. public function getMasteryLevelAttribute($value): string
  123. {
  124. if ($value >= 0.85) {
  125. return '优秀';
  126. } elseif ($value >= 0.70) {
  127. return '良好';
  128. } elseif ($value >= 0.50) {
  129. return '及格';
  130. } else {
  131. return '薄弱';
  132. }
  133. }
  134. /**
  135. * 获取趋势标签
  136. */
  137. public function getTrendLabelAttribute(): string
  138. {
  139. return match ($this->mastery_trend) {
  140. 'improving' => '上升',
  141. 'declining' => '下降',
  142. 'stable' => '稳定',
  143. 'insufficient' => '数据不足',
  144. default => '未知',
  145. };
  146. }
  147. /**
  148. * 计算成功率
  149. */
  150. public function getSuccessRateAttribute(): float
  151. {
  152. if ($this->total_attempts <= 0) {
  153. return 0.0;
  154. }
  155. return round(($this->correct_attempts / $this->total_attempts) * 100, 2);
  156. }
  157. }