|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Judite\Models\Group; |
|
6
|
|
|
use App\Judite\Models\Course; |
|
7
|
|
|
use App\Judite\Models\Student; |
|
8
|
|
|
use App\Judite\Models\Invitation; |
|
9
|
|
|
use Illuminate\Support\Facades\DB; |
|
10
|
|
|
use Illuminate\Support\Facades\Auth; |
|
11
|
|
|
use App\Exceptions\UserHasAlreadyGroupInCourseException; |
|
12
|
|
|
|
|
13
|
|
|
class GroupController extends Controller |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Create a new controller instance. |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->middleware('auth'); |
|
21
|
|
|
$this->middleware('can.student'); |
|
22
|
|
|
$this->middleware('student.verified'); |
|
23
|
|
|
$this->middleware('can.group'); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Display a listing of the resource. |
|
28
|
|
|
* |
|
29
|
|
|
* @return \Illuminate\View\View |
|
30
|
|
|
*/ |
|
31
|
|
|
public function index() |
|
32
|
|
|
{ |
|
33
|
|
|
$student = Auth::student(); |
|
34
|
|
|
|
|
35
|
|
|
$enrollments = $student |
|
36
|
|
|
->enrollments() |
|
37
|
|
|
->orderByCourse() |
|
38
|
|
|
->get(); |
|
39
|
|
|
|
|
40
|
|
|
foreach ($enrollments as $enrollmentKey => $enrollment) { |
|
41
|
|
|
$course = DB::transaction(function () use ($enrollment) { |
|
42
|
|
|
return Course::whereId($enrollment->course_id)->first(); |
|
43
|
|
|
}); |
|
44
|
|
|
|
|
45
|
|
|
if ($course->group_max <= 0) { |
|
46
|
|
|
unset($enrollments[$enrollmentKey]); |
|
47
|
|
|
continue; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$membership = $student->findMembershipByCourse($course->id); |
|
51
|
|
|
|
|
52
|
|
|
if (is_null($membership)) { |
|
53
|
|
|
$enrollment->group_status = 0; |
|
54
|
|
|
} else { |
|
55
|
|
|
$group = DB::transaction(function () use ($membership) { |
|
56
|
|
|
return Group::with('memberships') |
|
57
|
|
|
->whereId($membership->group_id) |
|
58
|
|
|
->first(); |
|
59
|
|
|
}); |
|
60
|
|
|
|
|
61
|
|
|
$enrollment->group_status = $group->memberships->count(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$enrollment->name = $course->name; |
|
65
|
|
|
$enrollment->group_min = $course->group_min; |
|
66
|
|
|
$enrollment->group_max = $course->group_max; |
|
67
|
|
|
|
|
68
|
|
|
$enrollment->number_invitations = Invitation::ofStudentInCourse( |
|
69
|
|
|
$student->student_number, |
|
70
|
|
|
$enrollment->course_id |
|
71
|
|
|
)->count(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return view('groups.index', compact('enrollments')); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Store a newly created resource in storage. |
|
79
|
|
|
* |
|
80
|
|
|
* @param $courseId |
|
81
|
|
|
* |
|
82
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
83
|
|
|
*/ |
|
84
|
|
|
public function store($courseId) |
|
85
|
|
|
{ |
|
86
|
|
|
$group = DB::transaction(function () use ($courseId) { |
|
87
|
|
|
$course = Course::find($courseId); |
|
88
|
|
|
|
|
89
|
|
|
if ($course == null) { |
|
90
|
|
|
return $course; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$group = new Group(); |
|
94
|
|
|
$group->course_id = $courseId; |
|
95
|
|
|
$group->save(); |
|
96
|
|
|
|
|
97
|
|
|
return $group; |
|
98
|
|
|
}); |
|
99
|
|
|
|
|
100
|
|
|
if ($group == null) { |
|
101
|
|
|
return redirect()->back(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
try { |
|
105
|
|
|
Auth::student()->join($group); |
|
106
|
|
|
|
|
107
|
|
|
flash('You have successfully joined a group.')->success(); |
|
108
|
|
|
} catch (UserHasAlreadyGroupInCourseException $e) { |
|
109
|
|
|
flash('You already have a group.')->error(); |
|
110
|
|
|
$group->delete(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return redirect()->route('groups.show', compact('courseId')); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Display the specified resource. |
|
118
|
|
|
* |
|
119
|
|
|
* @param int $courseId |
|
120
|
|
|
* |
|
121
|
|
|
* @return \Illuminate\View\View |
|
122
|
|
|
*/ |
|
123
|
|
|
public function show($courseId) |
|
124
|
|
|
{ |
|
125
|
|
|
$student = Auth::student(); |
|
126
|
|
|
|
|
127
|
|
|
$course = DB::transaction(function () use ($courseId, $student) { |
|
128
|
|
|
$course = Course::whereId($courseId)->first(); |
|
129
|
|
|
|
|
130
|
|
|
$course->number_invitations = Invitation::ofStudentInCourse( |
|
|
|
|
|
|
131
|
|
|
$student->student_number, |
|
132
|
|
|
$courseId |
|
133
|
|
|
)->count(); |
|
134
|
|
|
|
|
135
|
|
|
return $course; |
|
136
|
|
|
}); |
|
137
|
|
|
|
|
138
|
|
|
$membership = DB::transaction(function () use ($courseId, $student) { |
|
139
|
|
|
return $student->memberships() |
|
140
|
|
|
->with('group.memberships.student.user') |
|
141
|
|
|
->whereCourseId($courseId) |
|
142
|
|
|
->first(); |
|
143
|
|
|
}); |
|
144
|
|
|
|
|
145
|
|
|
return view('groups.show', compact('course', 'membership')); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Update the specified resource in storage. |
|
150
|
|
|
* |
|
151
|
|
|
* @param int $inviteId |
|
152
|
|
|
* |
|
153
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
154
|
|
|
*/ |
|
155
|
|
|
public function update($inviteId) |
|
156
|
|
|
{ |
|
157
|
|
|
$invitation = DB::transaction(function () use ($inviteId) { |
|
158
|
|
|
return Invitation::with(['group.memberships', 'group.course']) |
|
159
|
|
|
->whereId($inviteId) |
|
160
|
|
|
->whereStudentNumber(Auth::student()->student_number) |
|
161
|
|
|
->first(); |
|
162
|
|
|
}); |
|
163
|
|
|
|
|
164
|
|
|
if ($invitation == null) { |
|
165
|
|
|
flash('Invalid invitation.')->error(); |
|
166
|
|
|
|
|
167
|
|
|
return redirect()->back(); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
$numberOfGroupMembers = $invitation->group->memberships->count(); |
|
171
|
|
|
$groupMaxSize = $invitation->group->course->group_max; |
|
172
|
|
|
|
|
173
|
|
|
if ($numberOfGroupMembers >= $groupMaxSize) { |
|
174
|
|
|
flash('Course group limit exceeded')->error(); |
|
175
|
|
|
|
|
176
|
|
|
return redirect()->back(); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
try { |
|
180
|
|
|
Auth::student()->join($invitation->group); |
|
181
|
|
|
|
|
182
|
|
|
flash('You have successfully joined the group.') |
|
183
|
|
|
->success(); |
|
184
|
|
|
|
|
185
|
|
|
return redirect()->route('invitations.destroy', compact('inviteId')); |
|
186
|
|
|
} catch (UserHasAlreadyGroupInCourseException $e) { |
|
187
|
|
|
flash('You already have a group.') |
|
188
|
|
|
->error(); |
|
189
|
|
|
|
|
190
|
|
|
$courseId = $invitation->course_id; |
|
191
|
|
|
|
|
192
|
|
|
return redirect()->route('groups.show', compact('courseId')); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Remove the specified resource from storage. |
|
198
|
|
|
* |
|
199
|
|
|
* @param int $courseId |
|
200
|
|
|
* |
|
201
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
202
|
|
|
*/ |
|
203
|
|
|
public function destroy($courseId) |
|
204
|
|
|
{ |
|
205
|
|
|
$group = Auth::student()->memberships()->get() |
|
206
|
|
|
->where('course_id', '=', $courseId)->first() |
|
207
|
|
|
->group()->first(); |
|
208
|
|
|
|
|
209
|
|
|
Auth::student()->leave($group); |
|
210
|
|
|
|
|
211
|
|
|
if (! $group->memberships()->count()) { |
|
212
|
|
|
$invitations = Invitation::whereGroupId($group->id)->get(); |
|
213
|
|
|
|
|
214
|
|
|
foreach ($invitations as $invitation) { |
|
215
|
|
|
$invitation->delete(); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
$group->delete(); |
|
219
|
|
|
flash('Group deleted.')->success(); |
|
220
|
|
|
} else { |
|
221
|
|
|
flash('You have successfully left the group.')->success(); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
return redirect()->route('groups.show', compact('courseId')); |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.