Passed
Push — develop ( dfa246...179c36 )
by Francisco
02:27
created

Enrollment   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
dl 0
loc 120
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A scopeOrderByStudent() 0 5 1
A exchange() 0 10 1
A course() 0 3 1
A exchangesAsSource() 0 3 1
A scopeSimilarEnrollments() 0 5 1
A student() 0 3 1
A shift() 0 3 1
A scopeOrderByCourse() 0 7 1
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)
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