Enrollment::course()   A
last analyzed

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
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Judite\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Laracasts\Presenter\PresentableTrait;
7
use App\Judite\Presenters\EnrollmentPresenter;
8
9
class Enrollment extends Model
10
{
11
    use PresentableTrait;
12
13
    /**
14
     * The relations to eager load on every query.
15
     *
16
     * @var array
17
     */
18
    protected $with = ['student', 'shift', 'course'];
19
20
    /**
21
     * The presenter for this entity.
22
     *
23
     * @var string
24
     */
25
    protected $presenter = EnrollmentPresenter::class;
26
27
    /**
28
     * Scope a query to filter enrollments by owner.
29
     *
30
     * @param \Illuminate\Database\Eloquent\Builder $query
31
     * @param \App\Judite\Models\Student            $student
32
     *
33
     * @return \Illuminate\Database\Eloquent\Builder
34
     */
35
    public function scopeOwnedBy($query, Student $student)
36
    {
37
        return $query->whereStudentId($student->id);
38
    }
39
40
    /**
41
     * Scope a query to order enrollments by course.
42
     *
43
     * @param \Illuminate\Database\Eloquent\Builder $query
44
     *
45
     * @return \Illuminate\Database\Eloquent\Builder
46
     */
47
    public function scopeOrderByCourse($query)
48
    {
49
        return $query->select('enrollments.*')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->select('e...('courses.name', 'asc') also could return the type Illuminate\Database\Query\Builder which is incompatible with the documented return type Illuminate\Database\Eloquent\Builder.
Loading history...
50
            ->join('courses', 'enrollments.course_id', '=', 'courses.id')
51
            ->orderBy('courses.year', 'asc')
52
            ->orderBy('courses.semester', 'asc')
53
            ->orderBy('courses.name', 'asc');
54
    }
55
56
    /**
57
     * Scope a query to filter similar enrollments.
58
     *
59
     * @param \Illuminate\Database\Eloquent\Builder $query
60
     * @param \App\Judite\Models\Enrollment         $enrollment
61
     *
62
     * @return \Illuminate\Database\Eloquent\Builder
63
     */
64
    public function scopeSimilarEnrollments($query, self $enrollment)
65
    {
66
        return $query->where('enrollments.id', '!=', $enrollment->id)
67
            ->where('course_id', $enrollment->course->id)
68
            ->where('shift_id', '!=', $enrollment->shift->id);
69
    }
70
71
    /**
72
     * Scope a query to order enrollments by students.
73
     *
74
     * @param \Illuminate\Database\Eloquent\Builder $query
75
     *
76
     * @return \Illuminate\Database\Eloquent\Builder
77
     */
78
    public function scopeOrderByStudent($query)
79
    {
80
        return $query->select('enrollments.*')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->select('e...student_number', 'asc') also could return the type Illuminate\Database\Query\Builder which is incompatible with the documented return type Illuminate\Database\Eloquent\Builder.
Loading history...
81
            ->join('students', 'enrollments.student_id', '=', 'students.id')
82
            ->orderBy('students.student_number', 'asc');
83
    }
84
85
    /**
86
     * Exchange shifts with the given enrollment.
87
     *
88
     * @param \App\Judite\Models\Enrollment $enrollment
89
     *
90
     * @return $this
91
     */
92
    public function exchange(self $enrollment): self
93
    {
94
        $fromShiftId = $this->shift_id;
95
        $this->shift()->associate($enrollment->shift_id);
96
        $enrollment->shift()->associate($fromShiftId);
97
98
        $this->save();
99
        $enrollment->save();
100
101
        return $this;
102
    }
103
104
    /**
105
     * Get exchanges of this enrollment as source.
106
     *
107
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
108
     */
109
    public function exchangesAsSource()
110
    {
111
        return $this->hasMany(Exchange::class, 'from_enrollment_id');
112
    }
113
114
    /**
115
     * Get student of this enrollment.
116
     *
117
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
118
     */
119
    public function student()
120
    {
121
        return $this->belongsTo(Student::class);
122
    }
123
124
    /**
125
     * Get course of this enrollment.
126
     *
127
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
128
     */
129
    public function course()
130
    {
131
        return $this->belongsTo(Course::class);
132
    }
133
134
    /**
135
     * Get shift of this enrollment.
136
     *
137
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
138
     */
139
    public function shift()
140
    {
141
        return $this->belongsTo(Shift::class);
142
    }
143
144
    /**
145
     * Check whether this enrollment is available for exchange.
146
     *
147
     * @return bool
148
     */
149
    public function availableForExchange()
150
    {
151
        $isBeingExchanged = is_null($this->exchanges_as_source_count)
0 ignored issues
show
Bug introduced by
The property exchanges_as_source_count does not exist on App\Judite\Models\Enrollment. Did you mean changes?
Loading history...
152
            ? $this->exchangesAsSource()->exists()
153
            : $this->exchanges_as_source_count > 0;
154
155
        return ! $isBeingExchanged && ! is_null($this->shift_id);
156
    }
157
158
    /**
159
     * Check if a enrollment can be deleted.
160
     *
161
     * @return bool
162
     */
163
    public function isDeletable()
164
    {
165
        return is_null($this->shift);
166
    }
167
}
168