Passed
Push — develop ( a20adc...5e6aa2 )
by Francisco
02:34
created

Enrollment::scopeOrderByStudent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
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 order enrollments by course.
29
     *
30
     * @param \Illuminate\Database\Eloquent\Builder $query
31
     *
32
     * @return \Illuminate\Database\Eloquent\Builder
33
     */
34
    public function scopeOrderByCourse($query)
35
    {
36
        return $query->select('enrollments.*')
37
            ->join('courses', 'enrollments.course_id', '=', 'courses.id')
38
            ->orderBy('courses.year', 'asc')
39
            ->orderBy('courses.semester', 'asc')
40
            ->orderBy('courses.name', 'asc');
41
    }
42
43
    /**
44
     * Scope a query to filter similar enrollments.
45
     *
46
     * @param \Illuminate\Database\Eloquent\Builder $query
47
     * @param \App\Judite\Models\Enrollment         $enrollment
48
     *
49
     * @return \Illuminate\Database\Eloquent\Builder
50
     */
51
    public function scopeSimilarEnrollments($query, Enrollment $enrollment)
52
    {
53
        return $query->where('enrollments.id', '!=', $enrollment->id)
54
            ->where('course_id', $enrollment->course->id)
55
            ->where('shift_id', '!=', $enrollment->shift->id);
56
    }
57
58
    /**
59
     * Scope a query to order enrollments by students.
60
     *
61
     * @param \Illuminate\Database\Eloquent\Builder $query
62
     *
63
     * @return \Illuminate\Database\Eloquent\Builder
64
     */
65
    public function scopeOrderByStudent($query)
66
    {
67
        return $query->select('enrollments.*')
68
            ->join('students', 'enrollments.student_id', '=', 'students.id')
69
            ->orderBy('students.student_number', 'asc');
70
    }
71
72
    /**
73
     * Exchange shifts with the given enrollment.
74
     *
75
     * @param \App\Judite\Models\Enrollment $enrollment
76
     *
77
     * @return $this
78
     */
79
    public function exchange(Enrollment $enrollment): Enrollment
80
    {
81
        $fromShiftId = $this->shift_id;
82
        $this->shift()->associate($enrollment->shift_id);
83
        $enrollment->shift()->associate($fromShiftId);
84
85
        $this->save();
86
        $enrollment->save();
87
88
        return $this;
89
    }
90
91
    /**
92
     * Get exchanges of this enrollment as source.
93
     *
94
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
95
     */
96
    public function exchangesAsSource()
97
    {
98
        return $this->hasMany(Exchange::class, 'from_enrollment_id');
99
    }
100
101
    /**
102
     * Get student of this enrollment.
103
     *
104
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
105
     */
106
    public function student()
107
    {
108
        return $this->belongsTo(Student::class);
109
    }
110
111
    /**
112
     * Get course of this enrollment.
113
     *
114
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
115
     */
116
    public function course()
117
    {
118
        return $this->belongsTo(Course::class);
119
    }
120
121
    /**
122
     * Get shift of this enrollment.
123
     *
124
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
125
     */
126
    public function shift()
127
    {
128
        return $this->belongsTo(Shift::class);
129
    }
130
131
    /**
132
     * Check whether this enrollment is available for exchange.
133
     *
134
     * @return bool
135
     */
136
    public function availableForExchange()
137
    {
138
        $isBeingExchanged = is_null($this->exchanges_as_source_count)
0 ignored issues
show
Bug introduced by
The property exchanges_as_source_count does not seem to exist on App\Judite\Models\Enrollment. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
139
            ? $this->exchangesAsSource()->exists()
140
            : $this->exchanges_as_source_count > 0;
141
142
        return ! $isBeingExchanged && ! is_null($this->shift_id);
143
    }
144
}
145