RoomController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Event;
6
use App\Models\Room;
7
use Carbon\Carbon;
8
9
class RoomController extends Controller
10
{
11
    public function __construct()
12
    {
13
        parent::__construct();
14
        $this->middleware(['permission:calendars.view']);
15
    }
16
17
    /**
18
     * Display a listing of the resource.
19
     */
20
    public function index()
21
    {
22
        // Do not fetch all events but only those closest to current date. TODO optimize this.
23
        $events = Event::with('course')
24
            ->where('start', '>', Carbon::now()->subDays(30))
25
            ->where('end', '<', Carbon::now()->addDays(90))
26
            ->orderBy('id', 'desc')
27
            ->get()
28
            ->map(fn ($event) => [
29
                'title' => $event->name,
30
                'resourceId' => $event->room_id,
31
                'start' => $event->start,
32
                'end' => $event->end,
33
                'groupId' => $event->course_id,
34
                'backgroundColor' => $event->color,
35
                'borderColor' => $event->color,
36
            ]);
37
38
        $rooms = Room::all()->toArray();
39
40
        $rooms = array_map(fn ($room) => [
41
            'id' => $room['id'],
42
            'title' => $room['name'],
43
        ], $rooms);
44
45
        array_push($rooms, ['id' => 'tbd',
46
            'title' => 'Unassigned', ]);
47
48
        $unassigned_events = Event::with('course')
49
            ->whereNull('room_id')
50
            ->get()
51
            ->map(fn ($event) => [
52
                'title' => $event->name,
53
                'resourceId' => 'tbd',
54
                'start' => $event->start,
55
                'end' => $event->end,
56
                'groupId' => $event->course_id,
57
                'backgroundColor' => $event->color,
58
                'borderColor' => $event->color,
59
            ]);
60
61
        return view('calendars.overview', [
62
            'events' => $events,
63
            'resources' => $rooms,
64
            'unassigned_events' => $unassigned_events,
65
        ]);
66
    }
67
68
    /**
69
     * Display the specified resource.
70
     */
71
    public function show(Room $room)
72
    {
73
        $events = $room->events->toArray();
74
        $events = array_map(fn ($event) => [
75
            'title' => $event['name'],
76
            'start' => $event['start'],
77
            'end' => $event['end'],
78
            'backgroundColor' => $event['color'],
79
            'borderColor' => $event['color'],
80
        ], $events);
81
82
        return view('calendars.simple', [
83
            'events' => $events,
84
            'resource' => $room,
85
        ]);
86
    }
87
}
88