StudentKnowledgeMastery.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. 'direct_mastery_level', // 直接学习掌握度(答题计算),判断达标时优先使用
  15. 'confidence_level',
  16. 'total_attempts',
  17. 'correct_attempts',
  18. 'incorrect_attempts',
  19. 'partial_attempts',
  20. 'avg_time_seconds',
  21. 'fastest_time',
  22. 'slowest_time',
  23. 'attempts_easy',
  24. 'attempts_medium',
  25. 'attempts_hard',
  26. 'correct_easy',
  27. 'correct_medium',
  28. 'correct_hard',
  29. 'first_attempt_at',
  30. 'last_attempt_at',
  31. 'last_mastery_update',
  32. 'mastery_trend',
  33. 'mastery_change',
  34. 'calculation_version',
  35. 'notes',
  36. ];
  37. protected $casts = [
  38. 'mastery_level' => 'decimal:4',
  39. 'direct_mastery_level' => 'decimal:4',
  40. 'confidence_level' => 'decimal:4',
  41. 'mastery_change' => 'decimal:4',
  42. 'avg_time_seconds' => 'decimal:2',
  43. 'first_attempt_at' => 'datetime',
  44. 'last_attempt_at' => 'datetime',
  45. 'last_mastery_update' => 'datetime',
  46. 'created_at' => 'datetime',
  47. 'updated_at' => 'datetime',
  48. ];
  49. /**
  50. * 关联学生
  51. */
  52. public function student(): BelongsTo
  53. {
  54. return $this->belongsTo(Student::class, 'student_id', 'student_id');
  55. }
  56. /**
  57. * 关联知识点
  58. */
  59. public function knowledgePoint(): BelongsTo
  60. {
  61. return $this->belongsTo(KnowledgePoint::class, 'kp_code', 'kp_code');
  62. }
  63. /**
  64. * 作用域:按学生筛选
  65. */
  66. public function scopeForStudent($query, string $studentId)
  67. {
  68. return $query->where('student_id', $studentId);
  69. }
  70. /**
  71. * 作用域:按知识点筛选
  72. */
  73. public function scopeForKnowledgePoint($query, string $kpCode)
  74. {
  75. return $query->where('kp_code', $kpCode);
  76. }
  77. /**
  78. * 作用域:薄弱点(掌握度低于阈值)
  79. */
  80. public function scopeWeaknesses($query, float $threshold = 0.7)
  81. {
  82. return $query->where('mastery_level', '<', $threshold);
  83. }
  84. /**
  85. * 作用域:按掌握度排序
  86. */
  87. public function scopeOrderByMastery($query, string $direction = 'asc')
  88. {
  89. return $query->orderBy('mastery_level', $direction);
  90. }
  91. /**
  92. * 获取薄弱点列表
  93. */
  94. public static function getWeaknesses(string $studentId, float $threshold = 0.7, int $limit = 20): array
  95. {
  96. return self::forStudent($studentId)
  97. ->weaknesses($threshold)
  98. ->orderByMastery('asc')
  99. ->limit($limit)
  100. ->get()
  101. ->toArray();
  102. }
  103. public static function allAtLeast(int $studentId, array $kpCodes, float $threshold): bool
  104. {
  105. if (empty($kpCodes)) {
  106. return false;
  107. }
  108. // 使用 DB::table 避免 Eloquent accessor 把数字转成文字标签
  109. // 【新增】同时获取 direct_mastery_level,判断时优先使用
  110. $records = \Illuminate\Support\Facades\DB::table('student_knowledge_mastery')
  111. ->where('student_id', $studentId)
  112. ->whereIn('kp_code', $kpCodes)
  113. ->get(['kp_code', 'mastery_level', 'direct_mastery_level'])
  114. ->keyBy('kp_code');
  115. foreach ($kpCodes as $kpCode) {
  116. $record = $records->get($kpCode);
  117. // 优先使用 direct_mastery_level(直接学习掌握度)
  118. if ($record) {
  119. $level = $record->direct_mastery_level !== null
  120. ? (float) $record->direct_mastery_level
  121. : (float) $record->mastery_level;
  122. } else {
  123. $level = 0.0;
  124. }
  125. if ($level < $threshold) {
  126. return false;
  127. }
  128. }
  129. return true;
  130. }
  131. /**
  132. * 判断所有知识点是否达标(跳过没有题目的知识点)
  133. * 用于章节摸底后的知识点学习流程
  134. *
  135. * @param int $studentId 学生ID
  136. * @param array $kpCodes 知识点编码列表
  137. * @param float $threshold 达标阈值(默认0.9)
  138. * @return bool 是否全部达标
  139. */
  140. public static function allAtLeastSkipNoQuestions(int $studentId, array $kpCodes, float $threshold = 0.9): bool
  141. {
  142. if (empty($kpCodes)) {
  143. return true; // 没有知识点,视为达标
  144. }
  145. // 获取掌握度(使用 DB::table 避免 Eloquent accessor 把数字转成文字标签)
  146. // 【新增】同时获取 direct_mastery_level,判断时优先使用
  147. $records = \Illuminate\Support\Facades\DB::table('student_knowledge_mastery')
  148. ->where('student_id', $studentId)
  149. ->whereIn('kp_code', $kpCodes)
  150. ->get(['kp_code', 'mastery_level', 'direct_mastery_level'])
  151. ->keyBy('kp_code');
  152. // 获取有题目的知识点
  153. $kpCodesWithQuestions = \App\Models\Question::query()
  154. ->whereIn('kp_code', $kpCodes)
  155. ->distinct()
  156. ->pluck('kp_code')
  157. ->toArray();
  158. $hasAnyKpWithQuestions = false;
  159. foreach ($kpCodes as $kpCode) {
  160. // 跳过没有题目的知识点
  161. if (!in_array($kpCode, $kpCodesWithQuestions)) {
  162. continue;
  163. }
  164. $hasAnyKpWithQuestions = true;
  165. // 优先使用 direct_mastery_level(直接学习掌握度)
  166. $record = $records->get($kpCode);
  167. if ($record) {
  168. $level = $record->direct_mastery_level !== null
  169. ? (float) $record->direct_mastery_level
  170. : (float) $record->mastery_level;
  171. } else {
  172. $level = 0.0;
  173. }
  174. if ($level < $threshold) {
  175. return false;
  176. }
  177. }
  178. // 如果没有任何有题的知识点,视为达标
  179. return $hasAnyKpWithQuestions ? true : true;
  180. }
  181. /**
  182. * 获取第一个未达标的知识点(跳过没有题目的知识点)
  183. *
  184. * @param int $studentId 学生ID
  185. * @param array $kpCodes 知识点编码列表(按顺序)
  186. * @param float $threshold 达标阈值(默认0.9)
  187. * @return string|null 第一个未达标的知识点编码,如果全部达标返回null
  188. */
  189. public static function getFirstUnmasteredKpCode(int $studentId, array $kpCodes, float $threshold = 0.9): ?string
  190. {
  191. if (empty($kpCodes)) {
  192. return null;
  193. }
  194. // 获取掌握度(使用 DB::table 避免 Eloquent accessor 把数字转成文字标签)
  195. // 【新增】同时获取 direct_mastery_level,判断时优先使用
  196. $records = \Illuminate\Support\Facades\DB::table('student_knowledge_mastery')
  197. ->where('student_id', $studentId)
  198. ->whereIn('kp_code', $kpCodes)
  199. ->get(['kp_code', 'mastery_level', 'direct_mastery_level'])
  200. ->keyBy('kp_code');
  201. // 获取有题目的知识点
  202. $kpCodesWithQuestions = \App\Models\Question::query()
  203. ->whereIn('kp_code', $kpCodes)
  204. ->distinct()
  205. ->pluck('kp_code')
  206. ->toArray();
  207. foreach ($kpCodes as $kpCode) {
  208. // 跳过没有题目的知识点
  209. if (!in_array($kpCode, $kpCodesWithQuestions)) {
  210. continue;
  211. }
  212. // 优先使用 direct_mastery_level(直接学习掌握度)
  213. $record = $records->get($kpCode);
  214. if ($record) {
  215. $level = $record->direct_mastery_level !== null
  216. ? (float) $record->direct_mastery_level
  217. : (float) $record->mastery_level;
  218. } else {
  219. $level = 0.0;
  220. }
  221. if ($level < $threshold) {
  222. return $kpCode;
  223. }
  224. }
  225. return null; // 全部达标
  226. }
  227. /**
  228. * 计算掌握度等级
  229. */
  230. public function getMasteryLevelAttribute($value): string
  231. {
  232. if ($value >= 0.85) {
  233. return '优秀';
  234. } elseif ($value >= 0.70) {
  235. return '良好';
  236. } elseif ($value >= 0.50) {
  237. return '及格';
  238. } else {
  239. return '薄弱';
  240. }
  241. }
  242. /**
  243. * 获取趋势标签
  244. */
  245. public function getTrendLabelAttribute(): string
  246. {
  247. return match ($this->mastery_trend) {
  248. 'improving' => '上升',
  249. 'declining' => '下降',
  250. 'stable' => '稳定',
  251. 'insufficient' => '数据不足',
  252. default => '未知',
  253. };
  254. }
  255. /**
  256. * 计算成功率
  257. */
  258. public function getSuccessRateAttribute(): float
  259. {
  260. if ($this->total_attempts <= 0) {
  261. return 0.0;
  262. }
  263. return round(($this->correct_attempts / $this->total_attempts) * 100, 2);
  264. }
  265. }