1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace App\Repositories\Models\Exam;
- use App\Repositories\Enums\ModelStatusEnum;
- use App\Repositories\Models\Course\Category;
- use App\Repositories\Models\Course\Course;
- use App\Repositories\Models\Course\Video;
- use App\Repositories\Models\Model;
- use App\Repositories\Models\User\User;
- use Prettus\Repository\Contracts\Transformable;
- use Prettus\Repository\Traits\TransformableTrait;
- /**
- * Class Paper.
- *
- * @package namespace App\Repositories\Models\Exam;
- */
- class Paper extends Model implements Transformable
- {
- use TransformableTrait;
- protected $table = 'exam_papers';
- // protected $casts = [
- // 'topics' => 'array'
- // ];
- /**
- * The attributes that are mass assignable.
- *
- * @var array
- */
- protected $guarded = [];
- protected static function boot()
- {
- parent::boot(); // TODO: Change the autogenerated stub
- self::saving(function (Paper $paper) {
- if ($paper->isDirty('topics') && is_array($paper->topics)) {
- $paper->topic_nums = count($paper->topics);
- $paper->mark = Topic::query()->whereIn('id', $paper->topics)->sum('point');
- }
- });
- }
- public function category()
- {
- return $this->belongsTo(Category::class)->select(['id', 'name']);
- }
- public function course()
- {
- return $this->belongsTo(Course::class)->select(['title', 'id']);
- }
- public function video()
- {
- return $this->belongsTo(Video::class)->select(['title', 'id']);
- }
- public function setTopicsAttribute($val)
- {
- if (count($val) == 0) return '';
- $this->attributes['topics'] = '-' . arr2str($val, '-') . '-';
- }
- public function getTopicsAttribute($val)
- {
- return str2arr(trim($val, '-'), '-');
- }
- public function user()
- {
- return $this->belongsTo(User::class)->select(['id', 'name', 'turename', 'mobile']);
- }
- public function getTopicListsAttribute()
- {
- $topics = [];
- if (!isset($this->attributes['topics'])) {
- return $topics;
- }
- foreach (str2arr($this->attributes['topics'], '-') as $topic) {
- $topics[] = Topic::query()->where('id', $topic)->select(['id', 'title', 'type', 'level', 'point', 'body', 'result', 'analysis'])->first();
- }
- return $topics;
- }
- public function getExamStatusAttribute()
- {
- return PaperResult::query()->where('paper_id', $this->attributes['id'])->where('user_id', login_user_id())->where('status', ModelStatusEnum::OK)->exists();
- }
- }
|