1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Judite\Models\Group; |
6
|
|
|
use App\Judite\Models\Invitation; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
use Illuminate\Support\Facades\Auth; |
9
|
|
|
use App\Http\Requests\Invitation\UpdateRequest; |
10
|
|
|
use App\Exceptions\UserHasAlreadyAnInviteInGroupException; |
11
|
|
|
|
12
|
|
|
class InvitationController extends Controller |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Create a new controller instance. |
16
|
|
|
*/ |
17
|
|
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
$this->middleware('auth'); |
20
|
|
|
$this->middleware('can.student'); |
21
|
|
|
$this->middleware('student.verified'); |
22
|
|
|
$this->middleware('can.group'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Display a listing of the resource. |
27
|
|
|
* |
28
|
|
|
* @return \Illuminate\View\View |
29
|
|
|
*/ |
30
|
|
|
public function index($courseId) |
31
|
|
|
{ |
32
|
|
|
$invitations = DB::transaction(function () use ($courseId) { |
33
|
|
|
return Invitation::with('group.memberships.student.user') |
34
|
|
|
->where([ |
35
|
|
|
['student_number', Auth::student()->student_number], |
36
|
|
|
['course_id', $courseId], |
37
|
|
|
]) |
38
|
|
|
->get(); |
39
|
|
|
}); |
40
|
|
|
|
41
|
|
|
return view('invitations.index', compact('invitations')); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Store a newly created resource in storage. |
46
|
|
|
* |
47
|
|
|
* @param \App\Http\Requests\Invitation\UpdateRequest $request |
48
|
|
|
* @param $groupId |
49
|
|
|
* @param $courseId |
50
|
|
|
* |
51
|
|
|
* @return \Illuminate\Http\RedirectResponse |
52
|
|
|
*/ |
53
|
|
|
public function store(UpdateRequest $request, $groupId, $courseId) |
54
|
|
|
{ |
55
|
|
|
$studentNumber = $request->input('student_number'); |
56
|
|
|
|
57
|
|
|
if ($studentNumber == Auth::student()->student_number) { |
58
|
|
|
flash('You can not invite yourself.')->error(); |
59
|
|
|
|
60
|
|
|
return redirect()->back(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$invitation = new Invitation(); |
64
|
|
|
$invitation->student_number = $studentNumber; |
65
|
|
|
$invitation->group_id = $groupId; |
66
|
|
|
$invitation->course_id = $courseId; |
67
|
|
|
|
68
|
|
|
try { |
69
|
|
|
$invitation->create($studentNumber, $groupId, $courseId); |
70
|
|
|
flash('Invitation successfully sent.')->success(); |
71
|
|
|
} catch (UserHasAlreadyAnInviteInGroupException $e) { |
72
|
|
|
flash($e->getMessage())->error(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return redirect()->back(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Remove the specified resource from storage. |
80
|
|
|
* |
81
|
|
|
* @param int $id |
82
|
|
|
* |
83
|
|
|
* @return \Illuminate\Http\RedirectResponse |
84
|
|
|
*/ |
85
|
|
|
public function destroy($id) |
86
|
|
|
{ |
87
|
|
|
$courseId = 0; |
88
|
|
|
|
89
|
|
|
$remainingInvitations = DB::transaction(function () use ($id, &$courseId) { |
90
|
|
|
$invitation = Invitation::whereId($id)->first(); |
91
|
|
|
|
92
|
|
|
$courseId = $invitation->course_id; |
93
|
|
|
$studentNumber = $invitation->student_number; |
94
|
|
|
|
95
|
|
|
$invitation->delete(); |
96
|
|
|
|
97
|
|
|
return Invitation::with('group.memberships.student.user') |
98
|
|
|
->where([ |
99
|
|
|
['student_number', $studentNumber], |
100
|
|
|
['course_id', $courseId], |
101
|
|
|
]) |
102
|
|
|
->count(); |
103
|
|
|
}); |
104
|
|
|
|
105
|
|
|
flash('Invitation destroyed.')->success(); |
106
|
|
|
|
107
|
|
|
if ($remainingInvitations > 0) { |
108
|
|
|
return redirect()->back(); |
109
|
|
|
} else { |
110
|
|
|
return redirect()->route('groups.show', compact('courseId')); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|