StudentKnowledgeMastery.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. *
  123. * @param int $studentId 学生ID
  124. * @param array $kpCodes 知识点编码列表
  125. * @param float $threshold 达标阈值(默认0.9)
  126. * @return bool 是否全部达标
  127. */
  128. public static function allAtLeastSkipNoQuestions(int $studentId, array $kpCodes, float $threshold = 0.9): bool
  129. {
  130. if (empty($kpCodes)) {
  131. return true; // 没有知识点,视为达标
  132. }
  133. // 获取掌握度
  134. $levels = self::query()
  135. ->where('student_id', $studentId)
  136. ->whereIn('kp_code', $kpCodes)
  137. ->pluck('mastery_level', 'kp_code')
  138. ->toArray();
  139. // 获取有题目的知识点
  140. $kpCodesWithQuestions = \App\Models\Question::query()
  141. ->whereIn('kp_code', $kpCodes)
  142. ->distinct()
  143. ->pluck('kp_code')
  144. ->toArray();
  145. $hasAnyKpWithQuestions = false;
  146. foreach ($kpCodes as $kpCode) {
  147. // 跳过没有题目的知识点
  148. if (!in_array($kpCode, $kpCodesWithQuestions)) {
  149. continue;
  150. }
  151. $hasAnyKpWithQuestions = true;
  152. $level = isset($levels[$kpCode]) ? (float) $levels[$kpCode] : 0.0;
  153. if ($level < $threshold) {
  154. return false;
  155. }
  156. }
  157. // 如果没有任何有题的知识点,视为达标
  158. return $hasAnyKpWithQuestions ? true : true;
  159. }
  160. /**
  161. * 获取第一个未达标的知识点(跳过没有题目的知识点)
  162. *
  163. * @param int $studentId 学生ID
  164. * @param array $kpCodes 知识点编码列表(按顺序)
  165. * @param float $threshold 达标阈值(默认0.9)
  166. * @return string|null 第一个未达标的知识点编码,如果全部达标返回null
  167. */
  168. public static function getFirstUnmasteredKpCode(int $studentId, array $kpCodes, float $threshold = 0.9): ?string
  169. {
  170. if (empty($kpCodes)) {
  171. return null;
  172. }
  173. // 获取掌握度
  174. $levels = self::query()
  175. ->where('student_id', $studentId)
  176. ->whereIn('kp_code', $kpCodes)
  177. ->pluck('mastery_level', 'kp_code')
  178. ->toArray();
  179. // 获取有题目的知识点
  180. $kpCodesWithQuestions = \App\Models\Question::query()
  181. ->whereIn('kp_code', $kpCodes)
  182. ->distinct()
  183. ->pluck('kp_code')
  184. ->toArray();
  185. foreach ($kpCodes as $kpCode) {
  186. // 跳过没有题目的知识点
  187. if (!in_array($kpCode, $kpCodesWithQuestions)) {
  188. continue;
  189. }
  190. $level = isset($levels[$kpCode]) ? (float) $levels[$kpCode] : 0.0;
  191. if ($level < $threshold) {
  192. return $kpCode;
  193. }
  194. }
  195. return null; // 全部达标
  196. }
  197. /**
  198. * 计算掌握度等级
  199. */
  200. public function getMasteryLevelAttribute($value): string
  201. {
  202. if ($value >= 0.85) {
  203. return '优秀';
  204. } elseif ($value >= 0.70) {
  205. return '良好';
  206. } elseif ($value >= 0.50) {
  207. return '及格';
  208. } else {
  209. return '薄弱';
  210. }
  211. }
  212. /**
  213. * 获取趋势标签
  214. */
  215. public function getTrendLabelAttribute(): string
  216. {
  217. return match ($this->mastery_trend) {
  218. 'improving' => '上升',
  219. 'declining' => '下降',
  220. 'stable' => '稳定',
  221. 'insufficient' => '数据不足',
  222. default => '未知',
  223. };
  224. }
  225. /**
  226. * 计算成功率
  227. */
  228. public function getSuccessRateAttribute(): float
  229. {
  230. if ($this->total_attempts <= 0) {
  231. return 0.0;
  232. }
  233. return round(($this->correct_attempts / $this->total_attempts) * 100, 2);
  234. }
  235. }