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
|
|
|
/** |
21
|
|
|
* Scope a query to order courses by year, semester and name. |
22
|
|
|
* |
23
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
24
|
|
|
* |
25
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
26
|
|
|
*/ |
27
|
|
|
public function scopeOrderedList($query) |
28
|
|
|
{ |
29
|
|
|
return $query->orderBy('year', 'asc') |
|
|
|
|
30
|
|
|
->orderBy('semester', 'asc') |
31
|
|
|
->orderBy('name', 'asc'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Scope a query to order courses with groups by year, semester and name. |
36
|
|
|
* |
37
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
38
|
|
|
* |
39
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
40
|
|
|
*/ |
41
|
|
|
public function scopeGroupOrderedList($query) |
42
|
|
|
{ |
43
|
|
|
return $query->where('group_min', '>', '0') |
|
|
|
|
44
|
|
|
->orderBy('year', 'asc') |
45
|
|
|
->orderBy('semester', 'asc') |
46
|
|
|
->orderBy('name', 'asc'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get shifts of this course. |
51
|
|
|
* |
52
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
53
|
|
|
*/ |
54
|
|
|
public function shifts() |
55
|
|
|
{ |
56
|
|
|
return $this->hasMany(Shift::class); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get enrollments on this course. |
61
|
|
|
* |
62
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
63
|
|
|
*/ |
64
|
|
|
public function enrollments() |
65
|
|
|
{ |
66
|
|
|
return $this->hasMany(Enrollment::class); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Add shift to this course. |
71
|
|
|
* |
72
|
|
|
* @param \App\Judite\Models\Shift $shift |
73
|
|
|
* |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function addShift(Shift $shift): self |
77
|
|
|
{ |
78
|
|
|
$this->shifts()->save($shift); |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get shift of this course by tag. |
85
|
|
|
* |
86
|
|
|
* @param string $tag |
87
|
|
|
* |
88
|
|
|
* @return \App\Judite\Models\Shift|null |
89
|
|
|
*/ |
90
|
|
|
public function getShiftByTag($tag): ?Shift |
91
|
|
|
{ |
92
|
|
|
return $this->shifts()->where('tag', $tag)->first(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|