|
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
|
|
|
try { |
|
64
|
|
|
Invitation::create($studentNumber, $groupId, $courseId); |
|
65
|
|
|
flash('Invitation successfully sent.')->success(); |
|
66
|
|
|
} catch (UserHasAlreadyAnInviteInGroupException $e) { |
|
67
|
|
|
flash($e->getMessage())->error(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return redirect()->back(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Remove the specified resource from storage. |
|
75
|
|
|
* |
|
76
|
|
|
* @param int $id |
|
77
|
|
|
* |
|
78
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
79
|
|
|
*/ |
|
80
|
|
|
public function destroy($id) |
|
81
|
|
|
{ |
|
82
|
|
|
$courseId = 0; |
|
83
|
|
|
|
|
84
|
|
|
$remainingInvitations = DB::transaction(function () use ($id, &$courseId) { |
|
85
|
|
|
$invitation = Invitation::find($id) |
|
86
|
|
|
->whereStudentNumber(Auth::student()->student_number) |
|
87
|
|
|
->first(); |
|
88
|
|
|
|
|
89
|
|
|
$courseId = $invitation->course_id; |
|
90
|
|
|
$studentNumber = $invitation->student_number; |
|
91
|
|
|
|
|
92
|
|
|
$invitation->delete(); |
|
93
|
|
|
|
|
94
|
|
|
return Invitation::with('group.memberships.student.user') |
|
95
|
|
|
->where([ |
|
96
|
|
|
['student_number', $studentNumber], |
|
97
|
|
|
['course_id', $courseId], |
|
98
|
|
|
]) |
|
99
|
|
|
->count(); |
|
100
|
|
|
}); |
|
101
|
|
|
|
|
102
|
|
|
flash('Invitation destroyed.')->success(); |
|
103
|
|
|
|
|
104
|
|
|
if ($remainingInvitations > 0) { |
|
105
|
|
|
return redirect()->back(); |
|
106
|
|
|
} else { |
|
107
|
|
|
return redirect()->route('groups.show', compact('courseId')); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|