MistakeRecord.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. $this->increment('review_count');
  133. $this->update([
  134. 'review_status' => self::REVIEW_STATUS_REVIEWED,
  135. 'reviewed_at' => now(),
  136. ]);
  137. // 根据复习次数计算下次复习时间
  138. $this->calculateNextReviewDate();
  139. return $this;
  140. }
  141. /**
  142. * 标记为已掌握
  143. */
  144. public function markAsMastered(): self
  145. {
  146. $this->update([
  147. 'review_status' => self::REVIEW_STATUS_MASTERED,
  148. 'mastery_level' => 1.0,
  149. ]);
  150. return $this;
  151. }
  152. /**
  153. * 切换收藏状态
  154. */
  155. public function toggleFavorite(): self
  156. {
  157. $this->update([
  158. 'is_favorite' => !$this->is_favorite,
  159. ]);
  160. return $this;
  161. }
  162. /**
  163. * 加入重练清单
  164. */
  165. public function addToRetryList(): self
  166. {
  167. $this->update([
  168. 'in_retry_list' => true,
  169. 'force_review' => true,
  170. ]);
  171. return $this;
  172. }
  173. /**
  174. * 从重练清单移除
  175. */
  176. public function removeFromRetryList(): self
  177. {
  178. $this->update([
  179. 'in_retry_list' => false,
  180. ]);
  181. return $this;
  182. }
  183. /**
  184. * 计算下次复习时间(艾宾浩斯遗忘曲线)
  185. */
  186. public function calculateNextReviewDate(): self
  187. {
  188. $intervals = [1, 2, 4, 7, 15, 30, 60]; // 复习间隔天数
  189. $reviewCount = $this->review_count;
  190. // 如果复习次数超过间隔数组长度,使用最大间隔
  191. $days = $intervals[min($reviewCount - 1, count($intervals) - 1)] ?? 60;
  192. $this->update([
  193. 'next_review_at' => now()->addDays($days),
  194. ]);
  195. return $this;
  196. }
  197. /**
  198. * 作用域:按学生筛选
  199. */
  200. public function scopeForStudent($query, int|string $studentId)
  201. {
  202. return $query->where('student_id', $studentId);
  203. }
  204. /**
  205. * 作用域:按复习状态筛选
  206. */
  207. public function scopeByReviewStatus($query, string $status)
  208. {
  209. return $query->where('review_status', $status);
  210. }
  211. /**
  212. * 作用域:待复习
  213. */
  214. public function scopePending($query)
  215. {
  216. return $query->where('review_status', self::REVIEW_STATUS_PENDING);
  217. }
  218. /**
  219. * 作用域:已收藏
  220. */
  221. public function scopeFavorites($query)
  222. {
  223. return $query->where('is_favorite', true);
  224. }
  225. /**
  226. * 作用域:在重练清单
  227. */
  228. public function scopeInRetryList($query)
  229. {
  230. return $query->where('in_retry_list', true);
  231. }
  232. /**
  233. * 作用域:本周新增
  234. */
  235. public function scopeThisWeek($query)
  236. {
  237. return $query->whereBetween('created_at', [
  238. now()->startOfWeek(),
  239. now()->endOfWeek(),
  240. ]);
  241. }
  242. /**
  243. * 作用域:按时间范围筛选
  244. */
  245. public function scopeInDateRange($query, Carbon $startDate, Carbon $endDate)
  246. {
  247. return $query->whereBetween('created_at', [$startDate, $endDate]);
  248. }
  249. /**
  250. * 作用域:按错误类型筛选
  251. */
  252. public function scopeByErrorType($query, string $errorType)
  253. {
  254. return $query->where('error_type', $errorType);
  255. }
  256. /**
  257. * 作用域:按知识点筛选
  258. */
  259. public function scopeByKnowledgePoint($query, string|array $kpIds)
  260. {
  261. if (is_array($kpIds)) {
  262. return $query->where(function ($q) use ($kpIds) {
  263. foreach ($kpIds as $kpId) {
  264. $q->orWhereJsonContains('kp_ids', $kpId);
  265. }
  266. });
  267. }
  268. return $query->whereJsonContains('kp_ids', $kpIds);
  269. }
  270. /**
  271. * 获取统计摘要
  272. */
  273. public static function getSummary(int|string $studentId): array
  274. {
  275. $query = self::forStudent($studentId);
  276. return [
  277. 'total' => (clone $query)->count(),
  278. 'pending' => (clone $query)->pending()->count(),
  279. 'reviewed' => (clone $query)->byReviewStatus(self::REVIEW_STATUS_REVIEWED)->count(),
  280. 'mastered' => (clone $query)->byReviewStatus(self::REVIEW_STATUS_MASTERED)->count(),
  281. 'favorites' => (clone $query)->favorites()->count(),
  282. 'in_retry_list' => (clone $query)->inRetryList()->count(),
  283. 'this_week' => (clone $query)->thisWeek()->count(),
  284. 'mastery_rate' => self::calculateMasteryRate($studentId),
  285. ];
  286. }
  287. /**
  288. * 计算掌握率
  289. */
  290. public static function calculateMasteryRate(int|string $studentId): float
  291. {
  292. $total = self::forStudent($studentId)->count();
  293. if ($total === 0) {
  294. return 0.0;
  295. }
  296. $mastered = self::forStudent($studentId)
  297. ->byReviewStatus(self::REVIEW_STATUS_MASTERED)
  298. ->count();
  299. return round(($mastered / $total) * 100, 2);
  300. }
  301. /**
  302. * 获取错误模式分析
  303. */
  304. public static function getMistakePatterns(int|string $studentId): array
  305. {
  306. $query = self::forStudent($studentId);
  307. // 错误类型分布
  308. $errorTypes = (clone $query)
  309. ->selectRaw('error_type, COUNT(*) as count')
  310. ->groupBy('error_type')
  311. ->pluck('count', 'error_type')
  312. ->toArray();
  313. // 知识点分布
  314. $knowledgePoints = self::forStudent($studentId)
  315. ->selectRaw('knowledge_point, COUNT(*) as count')
  316. ->groupBy('knowledge_point')
  317. ->orderByDesc('count')
  318. ->limit(10)
  319. ->pluck('count', 'knowledge_point')
  320. ->toArray();
  321. // 来源分布
  322. $sources = (clone $query)
  323. ->selectRaw('source, COUNT(*) as count')
  324. ->groupBy('source')
  325. ->pluck('count', 'source')
  326. ->toArray();
  327. // 难度分布
  328. $difficultyStats = (clone $query)
  329. ->selectRaw('AVG(difficulty) as avg_difficulty, COUNT(*) as total')
  330. ->first();
  331. return [
  332. 'error_types' => $errorTypes,
  333. 'knowledge_points' => $knowledgePoints,
  334. 'sources' => $sources,
  335. 'difficulty_stats' => [
  336. 'average' => round($difficultyStats->avg_difficulty ?? 0, 2),
  337. 'total' => $difficultyStats->total ?? 0,
  338. ],
  339. 'weak_kps' => array_keys(array_slice($knowledgePoints, 0, 5, true)),
  340. 'top_error_types' => array_keys(array_slice($errorTypes, 0, 3, true)),
  341. ];
  342. }
  343. }