MistakeRecord.php 10 KB

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