Paper.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Repositories\Models\Exam;
  3. use App\Repositories\Enums\ModelStatusEnum;
  4. use App\Repositories\Models\Course\Category;
  5. use App\Repositories\Models\Course\Course;
  6. use App\Repositories\Models\Course\Video;
  7. use App\Repositories\Models\Model;
  8. use App\Repositories\Models\User\User;
  9. use Prettus\Repository\Contracts\Transformable;
  10. use Prettus\Repository\Traits\TransformableTrait;
  11. /**
  12. * Class Paper.
  13. *
  14. * @package namespace App\Repositories\Models\Exam;
  15. */
  16. class Paper extends Model implements Transformable
  17. {
  18. use TransformableTrait;
  19. protected $table = 'exam_papers';
  20. // protected $casts = [
  21. // 'topics' => 'array'
  22. // ];
  23. /**
  24. * The attributes that are mass assignable.
  25. *
  26. * @var array
  27. */
  28. protected $guarded = [];
  29. protected static function boot()
  30. {
  31. parent::boot(); // TODO: Change the autogenerated stub
  32. self::saving(function (Paper $paper) {
  33. if ($paper->isDirty('topics') && is_array($paper->topics)) {
  34. $paper->topic_nums = count($paper->topics);
  35. $paper->mark = Topic::query()->whereIn('id', $paper->topics)->sum('point');
  36. }
  37. });
  38. }
  39. public function category()
  40. {
  41. return $this->belongsTo(Category::class)->select(['id', 'name']);
  42. }
  43. public function course()
  44. {
  45. return $this->belongsTo(Course::class)->select(['title', 'id']);
  46. }
  47. public function video()
  48. {
  49. return $this->belongsTo(Video::class)->select(['title', 'id']);
  50. }
  51. public function setTopicsAttribute($val)
  52. {
  53. if (count($val) == 0) return '';
  54. $this->attributes['topics'] = '-' . arr2str($val, '-') . '-';
  55. }
  56. public function getTopicsAttribute($val)
  57. {
  58. return str2arr(trim($val, '-'), '-');
  59. }
  60. public function user()
  61. {
  62. return $this->belongsTo(User::class)->select(['id', 'name', 'turename', 'mobile']);
  63. }
  64. public function getTopicListsAttribute()
  65. {
  66. $topics = [];
  67. if (!isset($this->attributes['topics'])) {
  68. return $topics;
  69. }
  70. foreach (str2arr($this->attributes['topics'], '-') as $topic) {
  71. $topics[] = Topic::query()->where('id', $topic)->select(['id', 'title', 'type', 'level', 'point', 'body', 'result', 'analysis'])->first();
  72. }
  73. return $topics;
  74. }
  75. public function getExamStatusAttribute()
  76. {
  77. return PaperResult::query()->where('paper_id', $this->attributes['id'])->where('user_id', login_user_id())->where('status', ModelStatusEnum::OK)->exists();
  78. }
  79. }