Hackathonners /
swap
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Judite\Models; |
||
| 4 | |||
| 5 | use Illuminate\Database\Eloquent\Model; |
||
| 6 | use App\Exceptions\EnrollmentCannotBeDeleted; |
||
| 7 | use App\Exceptions\StudentIsNotEnrolledInCourseException; |
||
| 8 | use App\Exceptions\UserIsAlreadyEnrolledInCourseException; |
||
| 9 | |||
| 10 | class Student extends Model |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * The relations to eager load on every query. |
||
| 14 | * |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | protected $with = ['user']; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The attributes that are mass assignable. |
||
| 21 | * |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $fillable = ['student_number']; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Get user who owns this student. |
||
| 28 | * |
||
| 29 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 30 | */ |
||
| 31 | public function user() |
||
| 32 | { |
||
| 33 | return $this->belongsTo(User::class); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Get exchanges requested by this student. |
||
| 38 | * |
||
| 39 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 40 | */ |
||
| 41 | public function requestedExchanges() |
||
| 42 | { |
||
| 43 | $enrollmentsRelationship = $this->enrollments(); |
||
| 44 | $enrollmentsKeyName = $enrollmentsRelationship->getRelated()->getKeyName(); |
||
| 45 | $enrollmentsIdsQuery = $enrollmentsRelationship |
||
| 46 | ->select($enrollmentsKeyName) |
||
| 47 | ->getBaseQuery(); |
||
| 48 | |||
| 49 | return Exchange::whereFromEnrollmentIn($enrollmentsIdsQuery); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get exchanges proposed to this student. |
||
| 54 | * |
||
| 55 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 56 | */ |
||
| 57 | public function proposedExchanges() |
||
| 58 | { |
||
| 59 | $enrollmentsRelationship = $this->enrollments(); |
||
| 60 | $enrollmentsKeyName = $enrollmentsRelationship->getRelated()->getKeyName(); |
||
| 61 | $enrollmentsIdsQuery = $enrollmentsRelationship |
||
| 62 | ->select($enrollmentsKeyName) |
||
| 63 | ->getBaseQuery(); |
||
| 64 | |||
| 65 | return Exchange::whereToEnrollmentIn($enrollmentsIdsQuery); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Get enrollments of this student. |
||
| 70 | * |
||
| 71 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 72 | */ |
||
| 73 | public function enrollments() |
||
| 74 | { |
||
| 75 | return $this->hasMany(Enrollment::class); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Get enrollment of this student in a given course. |
||
| 80 | * |
||
| 81 | * @param \App\Judite\Models\Course $course |
||
| 82 | * |
||
| 83 | * @return \App\Judite\Models\Enrollment|null |
||
| 84 | */ |
||
| 85 | public function getEnrollmentInCourse(Course $course) |
||
| 86 | { |
||
| 87 | return $this->enrollments() |
||
| 88 | ->where('course_id', $course->id) |
||
| 89 | ->first(); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Enroll this student with a given course. |
||
| 94 | * |
||
| 95 | * @param \App\Judite\Models\Course $course |
||
| 96 | * |
||
| 97 | * @throws \App\Exceptions\UserIsAlreadyEnrolledInCourseException |
||
| 98 | * |
||
| 99 | * @return \App\Judite\Models\Enrollment |
||
| 100 | */ |
||
| 101 | public function enroll(Course $course): Enrollment |
||
| 102 | { |
||
| 103 | if ($this->isEnrolledInCourse($course)) { |
||
| 104 | throw new UserIsAlreadyEnrolledInCourseException($course); |
||
| 105 | } |
||
| 106 | |||
| 107 | $enrollment = $this->enrollments()->make(); |
||
| 108 | $enrollment->course()->associate($course); |
||
| 109 | $enrollment->save(); |
||
| 110 | |||
| 111 | return $enrollment; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Check if this student is enrolled in a course. |
||
| 116 | * |
||
| 117 | * @param \App\Judite\Models\Course $course |
||
| 118 | * |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | public function isEnrolledInCourse(Course $course): bool |
||
| 122 | { |
||
| 123 | return $this->enrollments()->where('course_id', $course->id)->exists(); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Remove enrollment in the given course. |
||
| 128 | * |
||
| 129 | * @param \App\Judite\Models\Course $course |
||
| 130 | * |
||
| 131 | * @throws \App\Exceptions\StudentIsNotEnrolledInCourseException|\App\Exceptions\EnrollmentCannotBeDeleted |
||
| 132 | * |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | public function unenroll(Course $course): bool |
||
| 136 | { |
||
| 137 | $enrollment = $this->getEnrollmentInCourse($course); |
||
| 138 | |||
| 139 | if (is_null($enrollment)) { |
||
| 140 | throw new StudentIsNotEnrolledInCourseException($course); |
||
| 141 | } |
||
| 142 | |||
| 143 | if (! $enrollment->isDeletable()) { |
||
| 144 | throw new EnrollmentCannotBeDeleted($enrollment); |
||
| 145 | } |
||
| 146 | |||
| 147 | return $enrollment->delete(); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Scope a query to only include users with the given student number. |
||
| 152 | * |
||
| 153 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 154 | * @param string $studentNumber |
||
| 155 | * |
||
| 156 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 157 | */ |
||
| 158 | public function scopeWhereNumber($query, $studentNumber) |
||
| 159 | { |
||
| 160 | return $query->where('student_number', $studentNumber); |
||
| 161 | } |
||
| 162 | } |
||
| 163 |