1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Judite\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\DB; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use App\Exceptions\UserHasAlreadyAnInviteInGroupException; |
8
|
|
|
|
9
|
|
|
class Invitation extends Model |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Get the student of this invitation. |
13
|
|
|
* |
14
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
15
|
|
|
*/ |
16
|
|
|
public function student() |
17
|
|
|
{ |
18
|
|
|
return $this->belongsTo(Student::class); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Get the group of this invitation. |
23
|
|
|
* |
24
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
25
|
|
|
*/ |
26
|
|
|
public function group() |
27
|
|
|
{ |
28
|
|
|
return $this->belongsTo(Group::class); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Scope a query to get the student amount of invitations in a given course. |
33
|
|
|
* |
34
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
35
|
|
|
* @param string $studentNumber |
36
|
|
|
* @param int $courseId |
37
|
|
|
* |
38
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
39
|
|
|
*/ |
40
|
|
|
public function scopeOfStudentInCourse($query, $studentNumber, $courseId) |
41
|
|
|
{ |
42
|
|
|
return $query->where([ |
43
|
|
|
['course_id', '=', $courseId], |
44
|
|
|
['student_number', '=', $studentNumber], |
45
|
|
|
]); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Saves new invitation. |
50
|
|
|
* |
51
|
|
|
* @param $studentNumber |
52
|
|
|
* @param $groupId |
53
|
|
|
* @param $courseId |
54
|
|
|
* |
55
|
|
|
* @throws \App\Exceptions\UserHasAlreadyAnInviteInGroupException |
56
|
|
|
*/ |
57
|
|
|
public function create($studentNumber, $groupId, $courseId) |
58
|
|
|
{ |
59
|
|
|
$invitation = DB::transaction(function () use ($studentNumber, $courseId) { |
60
|
|
|
return Invitation::where([ |
61
|
|
|
['student_number', $studentNumber], |
62
|
|
|
['course_id', $courseId], |
63
|
|
|
]); |
64
|
|
|
}); |
65
|
|
|
|
66
|
|
|
if ($invitation->exists()) { |
67
|
|
|
throw new UserHasAlreadyAnInviteInGroupException(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->save(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|