StudentExercise.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Models;
  3. use App\Services\MathFormulaProcessor;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. class StudentExercise extends Model
  7. {
  8. use HasFactory;
  9. protected $fillable = [
  10. 'student_id',
  11. 'question_id',
  12. 'knowledge_point_id',
  13. 'question_content',
  14. 'student_answer',
  15. 'correct_answer',
  16. 'is_correct',
  17. 'submission_status',
  18. 'batch_id',
  19. 'kp_code',
  20. 'selected_skills',
  21. 'skill_scores',
  22. 'time_spent_seconds',
  23. 'difficulty_level',
  24. 'created_at',
  25. 'updated_at',
  26. ];
  27. protected $casts = [
  28. 'is_correct' => 'boolean',
  29. 'knowledge_point_id' => 'integer',
  30. 'selected_skills' => 'array',
  31. 'skill_scores' => 'array',
  32. 'time_spent_seconds' => 'integer',
  33. 'difficulty_level' => 'float',
  34. 'created_at' => 'datetime',
  35. 'updated_at' => 'datetime',
  36. ];
  37. /**
  38. * 获取处理后的题目内容(包含数学公式处理)
  39. */
  40. public function getProcessedQuestionContentAttribute(): string
  41. {
  42. return MathFormulaProcessor::processFormulas($this->question_content ?? '');
  43. }
  44. /**
  45. * 获取处理后的学生答案(包含数学公式处理)
  46. */
  47. public function getProcessedStudentAnswerAttribute(): string
  48. {
  49. return MathFormulaProcessor::processFormulas($this->student_answer ?? '');
  50. }
  51. /**
  52. * 获取处理后的正确答案(包含数学公式处理)
  53. */
  54. public function getProcessedCorrectAnswerAttribute(): string
  55. {
  56. return MathFormulaProcessor::processFormulas($this->correct_answer ?? '');
  57. }
  58. /**
  59. * 获取处理后的数据数组(用于API返回)
  60. */
  61. public function toProcessedArray(): array
  62. {
  63. $data = $this->toArray();
  64. // 处理数学公式字段
  65. $mathFields = ['question_content', 'student_answer', 'correct_answer'];
  66. foreach ($mathFields as $field) {
  67. if (isset($data[$field]) && is_string($data[$field])) {
  68. $data[$field] = MathFormulaProcessor::processFormulas($data[$field]);
  69. }
  70. }
  71. return $data;
  72. }
  73. }