MistakeRecord.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. use Illuminate\Database\Eloquent\Relations\HasMany;
  7. use Illuminate\Support\Carbon;
  8. class MistakeRecord extends Model
  9. {
  10. use HasFactory;
  11. protected $table = 'mistake_records';
  12. protected $fillable = [
  13. 'student_id',
  14. 'question_id',
  15. 'source',
  16. 'question_text',
  17. 'student_answer',
  18. 'correct_answer',
  19. 'knowledge_point',
  20. 'explanation',
  21. 'is_corrected',
  22. 'review_status',
  23. 'review_count',
  24. 'force_review',
  25. 'is_favorite',
  26. 'in_retry_list',
  27. 'reviewed_at',
  28. 'next_review_at',
  29. 'error_type',
  30. 'kp_ids',
  31. 'skill_ids',
  32. 'difficulty',
  33. 'importance',
  34. 'mastery_level',
  35. 'remark',
  36. ];
  37. protected $casts = [
  38. 'is_corrected' => 'boolean',
  39. 'force_review' => 'boolean',
  40. 'is_favorite' => 'boolean',
  41. 'in_retry_list' => 'boolean',
  42. 'reviewed_at' => 'datetime',
  43. 'next_review_at' => 'datetime',
  44. 'kp_ids' => 'array',
  45. 'skill_ids' => 'array',
  46. 'difficulty' => 'decimal:2',
  47. 'mastery_level' => 'decimal:2',
  48. ];
  49. // 复习状态常量
  50. const REVIEW_STATUS_PENDING = 'pending';
  51. const REVIEW_STATUS_REVIEWED = 'reviewed';
  52. const REVIEW_STATUS_MASTERED = 'mastered';
  53. const REVIEW_STATUS_IGNORED = 'ignored';
  54. // 错误类型常量
  55. const ERROR_TYPE_CONCEPT = 'concept';
  56. const ERROR_TYPE_CALCULATION = 'calculation';
  57. const ERROR_TYPE_CARELESS = 'careless';
  58. const ERROR_TYPE_LOGIC = 'logic';
  59. const ERROR_TYPE_OTHER = 'other';
  60. // 来源常量
  61. const SOURCE_EXAM = 'exam';
  62. const SOURCE_PRACTICE = 'practice';
  63. const SOURCE_HOMEWORK = 'homework';
  64. const SOURCE_TEST = 'test';
  65. /**
  66. * 关联学生
  67. */
  68. public function student(): BelongsTo
  69. {
  70. return $this->belongsTo(Student::class, 'student_id', 'student_id');
  71. }
  72. /**
  73. * 获取复习状态标签
  74. */
  75. public function getReviewStatusLabelAttribute(): string
  76. {
  77. return match ($this->review_status) {
  78. self::REVIEW_STATUS_PENDING => '待复习',
  79. self::REVIEW_STATUS_REVIEWED => '已复习',
  80. self::REVIEW_STATUS_MASTERED => '已掌握',
  81. self::REVIEW_STATUS_IGNORED => '已忽略',
  82. default => '未知',
  83. };
  84. }
  85. /**
  86. * 获取错误类型标签
  87. */
  88. public function getErrorTypeLabelAttribute(): string
  89. {
  90. return match ($this->error_type) {
  91. self::ERROR_TYPE_CONCEPT => '概念错误',
  92. self::ERROR_TYPE_CALCULATION => '计算错误',
  93. self::ERROR_TYPE_CARELESS => '粗心错误',
  94. self::ERROR_TYPE_LOGIC => '逻辑错误',
  95. self::ERROR_TYPE_OTHER => '其他',
  96. default => '未知',
  97. };
  98. }
  99. /**
  100. * 获取来源标签
  101. */
  102. public function getSourceLabelAttribute(): string
  103. {
  104. return match ($this->source) {
  105. self::SOURCE_EXAM => '考试',
  106. self::SOURCE_PRACTICE => '练习',
  107. self::SOURCE_HOMEWORK => '作业',
  108. self::SOURCE_TEST => '测试',
  109. default => '未知',
  110. };
  111. }
  112. /**
  113. * 获取难度等级
  114. */
  115. public function getDifficultyLevelAttribute(): string
  116. {
  117. if (!$this->difficulty) {
  118. return '未知';
  119. }
  120. return match (true) {
  121. $this->difficulty < 0.3 => '简单',
  122. $this->difficulty < 0.7 => '中等',
  123. default => '困难',
  124. };
  125. }
  126. /**
  127. * 标记为已复习
  128. */
  129. public function markAsReviewed(): self
  130. {
  131. $this->increment('review_count');
  132. $this->update([
  133. 'review_status' => self::REVIEW_STATUS_REVIEWED,
  134. 'reviewed_at' => now(),
  135. ]);
  136. // 根据复习次数计算下次复习时间
  137. $this->calculateNextReviewDate();
  138. return $this;
  139. }
  140. /**
  141. * 标记为已掌握
  142. */
  143. public function markAsMastered(): self
  144. {
  145. $this->update([
  146. 'review_status' => self::REVIEW_STATUS_MASTERED,
  147. 'mastery_level' => 1.0,
  148. ]);
  149. return $this;
  150. }
  151. /**
  152. * 切换收藏状态
  153. */
  154. public function toggleFavorite(): self
  155. {
  156. $this->update([
  157. 'is_favorite' => !$this->is_favorite,
  158. ]);
  159. return $this;
  160. }
  161. /**
  162. * 加入重练清单
  163. */
  164. public function addToRetryList(): self
  165. {
  166. $this->update([
  167. 'in_retry_list' => true,
  168. 'force_review' => true,
  169. ]);
  170. return $this;
  171. }
  172. /**
  173. * 从重练清单移除
  174. */
  175. public function removeFromRetryList(): self
  176. {
  177. $this->update([
  178. 'in_retry_list' => false,
  179. ]);
  180. return $this;
  181. }
  182. /**
  183. * 计算下次复习时间(艾宾浩斯遗忘曲线)
  184. */
  185. public function calculateNextReviewDate(): self
  186. {
  187. $intervals = [1, 2, 4, 7, 15, 30, 60]; // 复习间隔天数
  188. $reviewCount = $this->review_count;
  189. // 如果复习次数超过间隔数组长度,使用最大间隔
  190. $days = $intervals[min($reviewCount - 1, count($intervals) - 1)] ?? 60;
  191. $this->update([
  192. 'next_review_at' => now()->addDays($days),
  193. ]);
  194. return $this;
  195. }
  196. /**
  197. * 作用域:按学生筛选
  198. */
  199. public function scopeForStudent($query, int|string $studentId)
  200. {
  201. return $query->where('student_id', $studentId);
  202. }
  203. /**
  204. * 作用域:按复习状态筛选
  205. */
  206. public function scopeByReviewStatus($query, string $status)
  207. {
  208. return $query->where('review_status', $status);
  209. }
  210. /**
  211. * 作用域:待复习
  212. */
  213. public function scopePending($query)
  214. {
  215. return $query->where('review_status', self::REVIEW_STATUS_PENDING);
  216. }
  217. /**
  218. * 作用域:已收藏
  219. */
  220. public function scopeFavorites($query)
  221. {
  222. return $query->where('is_favorite', true);
  223. }
  224. /**
  225. * 作用域:在重练清单
  226. */
  227. public function scopeInRetryList($query)
  228. {
  229. return $query->where('in_retry_list', true);
  230. }
  231. /**
  232. * 作用域:本周新增
  233. */
  234. public function scopeThisWeek($query)
  235. {
  236. return $query->whereBetween('created_at', [
  237. now()->startOfWeek(),
  238. now()->endOfWeek(),
  239. ]);
  240. }
  241. /**
  242. * 作用域:按时间范围筛选
  243. */
  244. public function scopeInDateRange($query, Carbon $startDate, Carbon $endDate)
  245. {
  246. return $query->whereBetween('created_at', [$startDate, $endDate]);
  247. }
  248. /**
  249. * 作用域:按错误类型筛选
  250. */
  251. public function scopeByErrorType($query, string $errorType)
  252. {
  253. return $query->where('error_type', $errorType);
  254. }
  255. /**
  256. * 作用域:按知识点筛选
  257. */
  258. public function scopeByKnowledgePoint($query, string|array $kpIds)
  259. {
  260. if (is_array($kpIds)) {
  261. return $query->where(function ($q) use ($kpIds) {
  262. foreach ($kpIds as $kpId) {
  263. $q->orWhereJsonContains('kp_ids', $kpId);
  264. }
  265. });
  266. }
  267. return $query->whereJsonContains('kp_ids', $kpIds);
  268. }
  269. /**
  270. * 获取统计摘要
  271. */
  272. public static function getSummary(int|string $studentId): array
  273. {
  274. $query = self::forStudent($studentId);
  275. return [
  276. 'total' => (clone $query)->count(),
  277. 'pending' => (clone $query)->pending()->count(),
  278. 'reviewed' => (clone $query)->byReviewStatus(self::REVIEW_STATUS_REVIEWED)->count(),
  279. 'mastered' => (clone $query)->byReviewStatus(self::REVIEW_STATUS_MASTERED)->count(),
  280. 'favorites' => (clone $query)->favorites()->count(),
  281. 'in_retry_list' => (clone $query)->inRetryList()->count(),
  282. 'this_week' => (clone $query)->thisWeek()->count(),
  283. 'mastery_rate' => self::calculateMasteryRate($studentId),
  284. ];
  285. }
  286. /**
  287. * 计算掌握率
  288. */
  289. public static function calculateMasteryRate(int|string $studentId): float
  290. {
  291. $total = self::forStudent($studentId)->count();
  292. if ($total === 0) {
  293. return 0.0;
  294. }
  295. $mastered = self::forStudent($studentId)
  296. ->byReviewStatus(self::REVIEW_STATUS_MASTERED)
  297. ->count();
  298. return round(($mastered / $total) * 100, 2);
  299. }
  300. /**
  301. * 获取错误模式分析
  302. */
  303. public static function getMistakePatterns(int|string $studentId): array
  304. {
  305. $query = self::forStudent($studentId);
  306. // 错误类型分布
  307. $errorTypes = (clone $query)
  308. ->selectRaw('error_type, COUNT(*) as count')
  309. ->groupBy('error_type')
  310. ->pluck('count', 'error_type')
  311. ->toArray();
  312. // 知识点分布
  313. $knowledgePoints = self::forStudent($studentId)
  314. ->selectRaw('knowledge_point, COUNT(*) as count')
  315. ->groupBy('knowledge_point')
  316. ->orderByDesc('count')
  317. ->limit(10)
  318. ->pluck('count', 'knowledge_point')
  319. ->toArray();
  320. // 来源分布
  321. $sources = (clone $query)
  322. ->selectRaw('source, COUNT(*) as count')
  323. ->groupBy('source')
  324. ->pluck('count', 'source')
  325. ->toArray();
  326. // 难度分布
  327. $difficultyStats = (clone $query)
  328. ->selectRaw('AVG(difficulty) as avg_difficulty, COUNT(*) as total')
  329. ->first();
  330. return [
  331. 'error_types' => $errorTypes,
  332. 'knowledge_points' => $knowledgePoints,
  333. 'sources' => $sources,
  334. 'difficulty_stats' => [
  335. 'average' => round($difficultyStats->avg_difficulty ?? 0, 2),
  336. 'total' => $difficultyStats->total ?? 0,
  337. ],
  338. 'weak_kps' => array_keys(array_slice($knowledgePoints, 0, 5, true)),
  339. 'top_error_types' => array_keys(array_slice($errorTypes, 0, 3, true)),
  340. ];
  341. }
  342. }