Passed
Push — master ( 26caeb...0e28f7 )
by Thomas
11:18
created

Event::scopeUnassigned()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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;
0 ignored issues
show
introduced by
The trait Backpack\CRUD\app\Models\Traits\CrudTrait requires some properties which are not provided by App\Models\Event: $fakeColumns, $identifiableAttribute, $Type
Loading history...
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())) {
0 ignored issues
show
Bug introduced by
The property leaves does not seem to exist on Illuminate\Database\Eloquent\Collection.
Loading history...
Bug introduced by
The method contains() does not exist on Illuminate\Support\Collection. It seems like you code against a sub-type of Illuminate\Support\Collection such as Illuminate\Database\Eloquent\Collection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
                if ($teacher->leaves->/** @scrutinizer ignore-call */ contains('date', Carbon::parse($event->start)->toDateString())) {
Loading history...
Bug introduced by
The method contains() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
                if ($teacher->leaves->/** @scrutinizer ignore-call */ contains('date', Carbon::parse($event->start)->toDateString())) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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();
0 ignored issues
show
Bug introduced by
It seems like __('The selected teacher...vailable on this date') can also be of type array and array; however, parameter $text of Prologue\Alerts\Facades\Alert::warning() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
                    Alert::warning(/** @scrutinizer ignore-type */ __('The selected teacher is not available on this date'))->flash();
Loading history...
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();
0 ignored issues
show
Bug introduced by
The method count() does not exist on Illuminate\Database\Eloquent\Collection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
        return $this->attendance->/** @scrutinizer ignore-call */ count();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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