-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDashboardAdminController.php
More file actions
484 lines (393 loc) · 16.6 KB
/
DashboardAdminController.php
File metadata and controls
484 lines (393 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<?php
namespace App\Http\Controllers;
use App\Helpers\GroupBalancedDivision;
use App\Helpers\GroupCourseDivision;
use App\Helpers\SlotAssignment;
use App\Models\Course;
use App\Models\CourseGroup;
use App\Models\Event;
use App\Models\Registration;
use App\Models\Slot;
use App\Models\User;
use App\Models\Group;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request as IlluminateRequest;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Storage;
use Inertia\Inertia;
use Inertia\Response;
use OpenTelemetry\API\Globals;
use Spatie\Permission\Models\Role;
class DashboardAdminController extends Controller
{
/**
* Display the dashboard admin page
*/
public function index(): Response
{
$tracer = Globals::tracerProvider()->getTracer('local-test');
$span = $tracer->spanBuilder('manual-span')->startSpan();
$courses = Course::all();
foreach ($courses as $course) {
$course->users = $course->users()->doesntHave('roles')->get();
}
$span->addEvent('rolled dice', ['courses' => $courses])->end();
return Inertia::render('Dashboard/Admin/Index', [
'courses' => $courses,
]);
}
/**
* Display the dashboard admin users page
*/
public function users(): Response
{
$roles = Role::where('name', '!=', 'super admin')->get();
$coures = Course::all();
return Inertia::render('Dashboard/Admin/Users', [
'roles' => $roles,
'courses' => $coures,
]);
}
/**
* Submit edit a user
*/
public function editUser(IlluminateRequest $request): RedirectResponse
{
$user = User::find($request->user);
if (!$user) {
Session::flash('error', 'Der angegebene User existiert nicht');
return Redirect::back();
}
// validate the request
$validated = Request::validate([
'firstname' => ['required', 'string', 'min:2', 'max:255'],
'lastname' => ['required', 'string', 'min:2', 'max:255'],
'email' => ['required', 'string', 'email', 'min:3', 'max:255', 'unique:users,email,' . $user->id],
'email_confirm' => ['required', 'string', 'email', 'min:3', 'max:255', 'same:email'],
'course_id' => ['required', 'integer', 'exists:courses,id'],
'role_id' => ['array'],
'is_disabled' => ['boolean'],
'remove_avatar' => ['boolean'],
'avatar' => ['nullable', 'string'],
]);
// check if all roles exists and not super admin if so add to roles array
$roles = [];
if (array_key_exists('role_id', $validated) && !$user->hasRole('super admin')) {
foreach ($validated['role_id'] as $role) {
if (!Role::find($role)) {
Session::flash('error', 'Die angegebene Rolle existiert nicht');
return Redirect::back();
}
$roleObject = Role::find($role);
if ($roleObject->name == 'super admin') {
Session::flash('error', 'Der User kann nicht zu Super Admin gemacht werden');
return Redirect::back();
}
$roles[] = $role;
}
}
// check if remove_avatar is set
if (array_key_exists('remove_avatar', $validated) && $validated['remove_avatar']) {
if ($user->avatar) {
// delete old avatar
Storage::disk('s3')->delete($user->avatar);
}
// set avatar to null
$validated['avatar'] = null;
}
// remove email_confirm, role_id and remove_avatar from array
unset($validated['email_confirm']);
unset($validated['role_id']);
unset($validated['remove_avatar']);
// update the user
$user->update($validated);
// sync roles
if (!$user->hasRole('super admin')) {
$user->syncRoles($roles);
}
Session::flash('success', 'Der Account <strong>' . $user->email . '</strong> wurde erfolgreich bearbeitet. Die Tabelle aktualisiert sich in wenigen Sekunden automatisch.');
return Redirect::back();
}
/**
* Submit delete a user
*/
public function deleteUser(IlluminateRequest $request): RedirectResponse
{
$user = User::find($request->user);
if (!$user) {
Session::flash('error', 'Der angegebene User existiert nicht');
return Redirect::back();
}
// check if user has super admin role
if ($user->hasRole('super admin')) {
Session::flash('error', 'Der User kann nicht gelöscht werden');
return Redirect::back();
}
// copy user to temp user variable
$userTemp = $user;
// delete the user
$user->delete();
// delete avatar
if ($userTemp->avatar) {
Storage::disk('s3')->delete($userTemp->avatar);
}
Session::flash('success', 'Der Account <strong>' . $userTemp->email . '</strong> wurde erfolgreich gelöscht. Die Tabelle aktualisiert sich in wenigen Sekunden automatisch.');
return Redirect::back();
}
/**
* Display the dashboard admin registrations page
*/
public function registrations(IlluminateRequest $request): Response
{
$event = Event::with('groups')->with('slots')->find($request->event);
if (!$event) {
return Inertia::render('Dashboard/404');
}
$event->registrations = $event->registrations()->with('user')->get();
$courses = Course::all();
return Inertia::render('Dashboard/Admin/Registrations', [
'event' => $event,
'courses' => $courses,
]);
}
/**
* Display the dashboard admin event page
*/
public function event(IlluminateRequest $request): Response
{
$event = Event::find($request->event);
if (!$event) {
return Inertia::render('Dashboard/404');
}
$event->slots = $event->slots()->with('registrations')->get();
$event->groups = $event->groups()->with('registrations')->get();
foreach ($event->groups as $group) {
$group->courses = $group->courses()->get();
}
$event->registrations = $event->registrations()->with('user')->get();
$courses = Course::all();
return Inertia::render('Dashboard/Admin/Event', [
'event' => $event,
'courses' => $courses,
]);
}
/**
* Display the dashboard admin event submit page
*/
public function eventSubmit(IlluminateRequest $request): Response
{
$event = Event::find($request->event);
if (!$event) {
return Inertia::render('Dashboard/404');
}
// check if any groups has a course
$hasCourse = false;
if ($event->groups->first()->courses()->exists()) $hasCourse = true;
return Inertia::render('Dashboard/Admin/Submit', [
'event' => $event,
'course_collections' => $this->getCourseCollections($event),
'hasCourse' => $hasCourse,
]);
}
/**
* Execute the dashboard admin event submit action
*/
public function eventExecuteSubmit(IlluminateRequest $request): RedirectResponse
{
$event = Event::find($request->event);
if (!$event) {
Session::flash('error', 'Das angegebene Event existiert nicht');
return Redirect::back();
}
// check event type
if ($event->type == 'group_phase') {
// check if any groups has a course
$hasCourse = false;
if ($event->groups->first()->courses()->exists()) $hasCourse = true;
// check which division method to use
if ($hasCourse) {
$courseModelCollections = [];
$courseCollections = $this->getCourseCollections($event);
foreach ($courseCollections as $courseCollection) {
$collection = Course::whereIn('abbreviation', $courseCollection)->get();
$courseModelCollections[] = $collection;
}
foreach ($courseModelCollections as $index => $collection) {
$maxGroups = $request->input('max_groups_' . $index);
$maxParticipants = $request->input('max_participants_' . $index);
$groupCourseDivision = new GroupCourseDivision($event, $collection, $event->consider_alcohol, (int) $maxGroups, (int) $maxParticipants);
$groupCourseDivision->assign();
}
Session::flash('success', 'Die Gruppen wurden erfolgreich nach Studiengängen aufgeteilt');
} else {
// get max_groups and max_participants
$maxGroups = $request->max_groups;
$maxParticipants = $request->max_participants;
$groupBalancedDivision = new GroupBalancedDivision($event, $event->consider_alcohol, (int) $maxGroups, (int) $maxParticipants);
$groupBalancedDivision->assign();
Session::flash('success', 'Die Gruppen wurden erfolgreich aufgeteilt');
}
return Redirect::route('dashboard.admin.event.index', ['event' => $event->id]);
} elseif ($event->type == 'slot_booking') {
// foreach event slots
foreach ($event->slots as $slot) {
$slotAssignment = new SlotAssignment($slot);
$slotAssignment->assign();
}
Session::flash('success', 'Die Slots wurden erfolgreich zugewiesen');
return Redirect::route('dashboard.admin.event.index', ['event' => $event->id]);
} else {
Session::flash('error', 'Das angegebene Event ist nicht vom Typ "group_phase" oder "slot_booking"');
return Redirect::back();
}
}
/**
* Display the register page
*/
public function register(Request $request): Response
{
// get courses ordered by name
$courses = Course::orderBy('name')->get();
// get all events with slots
$events = Event::with('slots', 'groups')->get();
return Inertia::render('Dashboard/Admin/Register', [
'courses' => $courses,
'events' => $events,
]);
}
/**
* Register a new user
*/
public function registerUser(): RedirectResponse
{
// check if user with email already exists
$user = User::where('email', Request::input('email'))->first();
if ($user) {
Session::flash('info', 'Der Account existiert bereits.');
return Redirect::back();
}
// validate the request
$validated = Request::validate([
'firstname' => ['required', 'string', 'min:2', 'max:255'],
'lastname' => ['required', 'string', 'min:2', 'max:255'],
'email' => ['required', 'string', 'email', 'min:3', 'max:255', 'unique:users'],
'email_confirm' => ['required', 'string', 'email', 'min:3', 'max:255', 'same:email'],
'course_id' => ['required', 'integer', 'exists:courses,id'],
'avatar' => ['nullable', 'string'],
]);
// remove email_confirm from array
unset($validated['email_confirm']);
// create the user
$user = User::create($validated);
Session::flash('success', 'Der Account <strong>' . $user->email . '</strong> wurde erfolgreich erstellt.');
return Redirect::back();
}
// TODO: Refactor this (╯°□°)╯︵ ┻━┻
/**
* Assign a new user
*/
public function assignUser(): RedirectResponse
{
// check if user with email not exists
$user = User::where('email', Request::input('email'))->first();
if (!$user) {
Session::flash('error', 'Der Account existiert nicht.');
return Redirect::back();
}
// get event
$event = Event::find(Request::input('event_id'));
if (!$event) {
Session::flash('error', 'Das Event existiert nicht.');
return Redirect::back();
}
// validate the request
$userRegistration = Request::validate([
'email' => ['required', 'string', 'email', 'min:3', 'max:255', 'exists:users,email'],
'event_id' => ['required', 'integer', 'exists:events,id'],
'slot_id' => ['integer', 'exists:slots,id'],
'group_id' => ['integer', 'exists:groups,id']
]);
// check for existing registration for this event and user
$existingRegistration = Registration::where('user_id', $user->id)->where('event_id', $event->id)->first();
if ($existingRegistration) {
Session::flash('error', 'Der Account ist bereits für dieses Event registriert.');
return Redirect::back();
}
// check if event consider alcohol
if ($event->consider_alcohol) {
$userRegistration['drinks_alcohol'] = Request::input('drinks_no_alcohol');
}
// set default queue position
$queuePosition = null;
// check if group is set
if (array_key_exists('group_id', $userRegistration)) {
// get group
$group = Group::find($userRegistration['group_id']);
// check if group exists
if (! $group) {
Session::flash('error', 'Die Gruppe existiert nicht.');
return Redirect::back();
}
}
// check if slot is set
if (array_key_exists('slot_id', $userRegistration)) {
// get slot
$slot = Slot::find($userRegistration['slot_id']);
// check if slot exists
if (!$slot) {
Session::flash('error', 'Das Slot existiert nicht.');
return Redirect::back();
}
// check if slot has maximum_participants
if ($slot->maximum_participants) {
$queuePosition = Registration::where('event_id', $event->id)->where('slot_id', $userRegistration['slot_id'])->max('queue_position');
if (!$queuePosition || $queuePosition == -1) {
$queuePosition = -1;
} else {
$queuePosition++;
}
$userRegistration['queue_position'] = $queuePosition;
}
} else {
$queuePosition = Registration::where('event_id', $event->id)->max('queue_position');
if ($queuePosition == -1) {
$queuePosition = -1;
} elseif ($queuePosition > 0) {
$queuePosition++;
}
}
// create registration
Registration::create([
'user_id' => $user->id,
'event_id' => $userRegistration['event_id'],
'slot_id' => (array_key_exists('slot_id', $userRegistration) ? $userRegistration['slot_id'] : null),
'group_id' => (array_key_exists('group_id', $userRegistration) ? $userRegistration['group_id'] : null),
'drinks_alcohol' => (array_key_exists('drinks_alcohol', $userRegistration) ? $userRegistration['drinks_alcohol'] : null),
'queue_position' => $queuePosition,
]);
Session::flash('success', 'Der Account <strong>' . $user->email . '</strong> wurde erfolgreich für das Event <strong>' . $event->name . '</strong>' . (array_key_exists('slot_id', $userRegistration) ? ' zu dem Slot <strong>' . $slot->name . '</strong>' : '') . (array_key_exists('group_id', $userRegistration) ? ' zu der Gruppe <strong>' . $group->name . '</strong>' : '') . ' zugewiesen.');
return Redirect::back();
}
private function getCourseCollections(Event $event): array
{
// save all course collections that occur in all groups of this event
$courseCollections = [];
// loop through all groups
foreach ($event->groups()->orderBy('id')->get() as $group) {
// check if group has a course
if ($group->courses()->exists()) {
$courseCollection = [];
foreach ($group->courses()->orderBy('id')->get() as $course) {
$courseCollection[] = $course->abbreviation;
}
// add course abbreviations to collection only if not already in collection
if (!in_array($courseCollection, $courseCollections)) {
$courseCollections[] = $courseCollection;
}
}
}
return $courseCollections;
}
}