1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Models\Traits\CrudTrait; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Prologue\Alerts\Facades\Alert; |
9
|
|
|
use Spatie\Activitylog\Traits\LogsActivity; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @mixin IdeHelperEvent |
13
|
|
|
*/ |
14
|
|
|
class Event extends Model |
15
|
|
|
{ |
16
|
|
|
use CrudTrait; |
|
|
|
|
17
|
|
|
use LogsActivity; |
18
|
|
|
|
19
|
|
|
protected static function boot() |
20
|
|
|
{ |
21
|
|
|
parent::boot(); |
22
|
|
|
|
23
|
|
|
// before adding an event, we check that the teacher is available |
24
|
|
|
static::saving(function ($event) { |
25
|
|
|
$teacher = Teacher::find($event->teacher_id); |
26
|
|
|
// if the teacher is on leave on the day of the event |
27
|
|
|
if ($event->teacher_id !== null && $teacher) { |
28
|
|
|
if ($teacher->leaves->contains('date', Carbon::parse($event->start)->toDateString())) { |
|
|
|
|
29
|
|
|
// detach the teacher from the event |
30
|
|
|
$event->teacher_id = null; |
31
|
|
|
Alert::warning(__('The selected teacher is not available on this date'))->flash(); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
}); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public $timestamps = true; |
38
|
|
|
|
39
|
|
|
protected $guarded = ['id']; |
40
|
|
|
|
41
|
|
|
protected $appends = ['length']; |
42
|
|
|
|
43
|
|
|
protected static bool $logUnguarded = true; |
44
|
|
|
|
45
|
|
|
/* |
46
|
|
|
|-------------------------------------------------------------------------- |
47
|
|
|
| FUNCTIONS |
48
|
|
|
|-------------------------------------------------------------------------- |
49
|
|
|
*/ |
50
|
|
|
|
51
|
|
|
/* |
52
|
|
|
|-------------------------------------------------------------------------- |
53
|
|
|
| RELATIONS |
54
|
|
|
|-------------------------------------------------------------------------- |
55
|
|
|
*/ |
56
|
|
|
|
57
|
|
|
public function coursetime() |
58
|
|
|
{ |
59
|
|
|
return $this->belongsTo(CourseTime::class); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function course() |
63
|
|
|
{ |
64
|
|
|
return $this->belongsTo(Course::class)->withCount('enrollments'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function enrollments() |
68
|
|
|
{ |
69
|
|
|
return $this->course->enrollments(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function attendance() |
73
|
|
|
{ |
74
|
|
|
return $this->hasMany(Attendance::class); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function teacher() |
78
|
|
|
{ |
79
|
|
|
return $this->belongsTo(Teacher::class)->withTrashed(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function room() |
83
|
|
|
{ |
84
|
|
|
return $this->belongsTo(Room::class)->withTrashed(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getPeriodAttribute() |
88
|
|
|
{ |
89
|
|
|
return $this->course->period_id; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/* |
93
|
|
|
|-------------------------------------------------------------------------- |
94
|
|
|
| SCOPES |
95
|
|
|
|-------------------------------------------------------------------------- |
96
|
|
|
*/ |
97
|
|
|
|
98
|
|
|
public function scopeUnassigned($query) |
99
|
|
|
{ |
100
|
|
|
return $query->whereNull('teacher_id'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/* |
104
|
|
|
|-------------------------------------------------------------------------- |
105
|
|
|
| ACCESORS |
106
|
|
|
|-------------------------------------------------------------------------- |
107
|
|
|
*/ |
108
|
|
|
|
109
|
|
|
public function getLengthAttribute() |
110
|
|
|
{ |
111
|
|
|
return Carbon::parse($this->end)->diffInSeconds(Carbon::parse($this->start)) / 3600; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getVolumeAttribute() |
115
|
|
|
{ |
116
|
|
|
return Carbon::parse($this->start)->diffInMinutes(Carbon::parse($this->end)) / 60; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getAttendanceCountAttribute() |
120
|
|
|
{ |
121
|
|
|
return $this->attendance->count(); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getFormattedDateAttribute() |
125
|
|
|
{ |
126
|
|
|
return Carbon::parse($this->start)->toFormattedDateString(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function getStartTimeAttribute() |
130
|
|
|
{ |
131
|
|
|
return Carbon::parse($this->start)->toTimeString(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function getEndTimeAttribute() |
135
|
|
|
{ |
136
|
|
|
return Carbon::parse($this->end)->toTimeString(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function getEventLengthAttribute() |
140
|
|
|
{ |
141
|
|
|
return round(Carbon::parse($this->end)->diffInMinutes(Carbon::parse($this->start)) / 60, 2); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function getShortDateAttribute() |
145
|
|
|
{ |
146
|
|
|
return Carbon::parse($this->start)->day.'/'.Carbon::parse($this->start)->month; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function getColorAttribute() |
150
|
|
|
{ |
151
|
|
|
return $this?->course->color ?? ('#'.substr(md5($this->course_id ?? '0'), 0, 6)); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/* |
155
|
|
|
|-------------------------------------------------------------------------- |
156
|
|
|
| MUTATORS |
157
|
|
|
|-------------------------------------------------------------------------- |
158
|
|
|
*/ |
159
|
|
|
} |
160
|
|
|
|