Passed
Pull Request — develop (#18)
by
unknown
07:13
created

Course::groups()   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 0
dl 0
loc 3
rs 10
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\CoursePresenter;
8
9
class Course extends Model
10
{
11
    use PresentableTrait;
12
13
    /**
14
     * The presenter for this entity.
15
     *
16
     * @var string
17
     */
18
    protected $presenter = CoursePresenter::class;
19
20
    protected $visible = ['id', 'code', 'name'];
21
22
    /**
23
     * Scope a query to order courses by year, semester and name.
24
     *
25
     * @param \Illuminate\Database\Eloquent\Builder $query
26
     *
27
     * @return \Illuminate\Database\Eloquent\Builder
28
     */
29
    public function scopeOrderedList($query)
30
    {
31
        return $query->orderBy('year', 'asc')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->orderBy('...>orderBy('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...
32
            ->orderBy('semester', 'asc')
33
            ->orderBy('name', 'asc');
34
    }
35
36
    /**
37
     * Get shifts of this course.
38
     *
39
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
40
     */
41
    public function shifts()
42
    {
43
        return $this->hasMany(Shift::class);
44
    }
45
46
    /**
47
     * Get enrollments on this course.
48
     *
49
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
50
     */
51
    public function enrollments()
52
    {
53
        return $this->hasMany(Enrollment::class);
54
    }
55
56
    /**
57
     * Get groups of this course.
58
     *
59
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
60
     */
61
    public function groups()
62
    {
63
        return $this->hasMany(Group::class);
64
    }
65
66
    /**
67
     * Add shift to this course.
68
     *
69
     * @param \App\Judite\Models\Shift $shift
70
     *
71
     * @return $this
72
     */
73
    public function addShift(Shift $shift): self
74
    {
75
        $this->shifts()->save($shift);
76
77
        return $this;
78
    }
79
80
    /**
81
     * Get shift of this course by tag.
82
     *
83
     * @param string $tag
84
     *
85
     * @return \App\Judite\Models\Shift|null
86
     */
87
    public function getShiftByTag($tag): ?Shift
88
    {
89
        return $this->shifts()->where('tag', $tag)->first();
90
    }
91
92
    /**
93
     * Add group to this course.
94
     *
95
     * @param \App\Judite\Models\Group $group
96
     *
97
     * @return $this
98
     */
99
    public function addGroup(Group $group): self
100
    {
101
        $this->groups()->save($group);
102
103
        return $this;
104
    }
105
}
106