Bristol SU Support Package
Activity.php
Go to the documentation of this file.
1 <?php
2 
4 
12 use Carbon\Carbon;
16 
20 class Activity extends Model
21 {
22  use HasRevisions;
23 
29  protected $fillable = [
30  'name',
31  'description',
32  'activity_for',
33  'for_logic',
34  'admin_logic',
35  'start_date',
36  'end_date',
37  'slug',
38  'type',
39  'enabled',
40  'user_id'
41  ];
42 
48  protected $casts = [
49  'start_date' => 'datetime',
50  'end_date' => 'datetime',
51  'enabled' => 'boolean'
52  ];
53 
62  public function __construct(array $attributes = [])
63  {
64  parent::__construct($attributes);
65  self::creating(function($model) {
66  if ($model->slug === null) {
67  $model->slug = Str::slug($model->name);
68  }
69  if($model->user_id === null && ($user = app(UserAuthentication::class)->getUser()) !== null) {
70  $model->user_id = $user->controlId();
71  }
72  });
73  }
74 
80  public function moduleInstances()
81  {
82  return $this->hasMany(ModuleInstance::class);
83  }
84 
91  public function scopeEnabled(Builder $query)
92  {
93  return $query->where('enabled', true);
94  }
95 
101  public function forLogic()
102  {
103  return $this->belongsTo(Logic::class, 'for_logic');
104  }
105 
111  public function adminLogic()
112  {
113  return $this->belongsTo(Logic::class, 'admin_logic');
114  }
115 
124  public function scopeActive(Builder $query) {
125  return $query
126  ->where(['start_date' => null, 'end_date'=>null])
127  ->orWhere([
128  ['start_date', '<=', Carbon::now()],
129  ['end_date', '>=', Carbon::now()]
130  ]);
131  }
132 
140  public function isCompletable(): bool {
141  return $this->type === 'completable' || $this->type === 'multi-completable';
142  }
143 
149  public function activityInstances()
150  {
151  return $this->hasMany(ActivityInstance::class);
152  }
153 
160  public function user(): \BristolSU\ControlDB\Contracts\Models\User
161  {
162  if($this->user_id === null) {
163  throw new \Exception(sprintf('Activity #%u is not owned by a user.', $this->id));
164  }
165  return app(User::class)->getById($this->user_id);
166  }
167 }
__construct(array $attributes=[])
Definition: Activity.php:61