-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCell.php
178 lines (156 loc) · 5.42 KB
/
Cell.php
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
<?php
namespace App\Livewire\Schedule;
use App\Actions\Schedule\PresentationAllocationHelper;
use App\Actions\Schedule\PresentationConflictChecker;
use App\Models\Presentation;
use App\Models\Room;
use App\Models\Timeslot;
use Carbon\Carbon;
use Illuminate\View\View;
use Livewire\Attributes\On;
use Livewire\Component;
use Masmerise\Toaster\Toaster;
class Cell extends Component
{
public $id;
public $timeslot;
public $room;
public $presentations;
public $height;
/**
* Initializes the component
* @param $timeslot
* @param $room
* @return void
*/
public function mount($timeslot, $room)
{
$this->timeslot = $timeslot;
$this->room = $room;
$this->id = "r-{$this->room->id}-t-{$this->timeslot->id}";
$this->height = $this->calculateHeightInREM();
$this->presentations = $this->room->presentations()
->where('start', '>=', Carbon::parse($this->timeslot->start))
->where('start', '<', Carbon::parse($this->timeslot->start)
->copy()
->addMinutes($this->timeslot->duration))
->orderBy('start', 'asc')
->get();
}
/**
* Handles the moving of a presentation by doing checks to see if the move is allowed
* and then sends an event to the parent component
* @param $id
* @param $newRoom
* @param $newTimeslot
* @return void
*/
public function movePresentation($id, $newRoom, $newTimeslot)
{
$presentation = Presentation::find($id);
$room = Room::find($newRoom);
$timeslot = Timeslot::find($newTimeslot);
if (is_null($presentation) || is_null($room) || is_null($timeslot)) {
Toaster::error('An issue has occurred. Try again');
return;
}
$passedChecks = false;
$presentationChecker = new PresentationConflictChecker();
if ($presentationChecker->isClearOfConflicts($presentation, $room, $timeslot->start)) {
$this->dispatchMoveEventToGrid($presentation->id, $room->id, $timeslot->id, $timeslot->start);
$passedChecks = true;
} else {
$passedChecks = $this->tryUsingPresentationAllocator($presentation, $timeslot, $room);
}
if (!$passedChecks) {
Toaster::error('A scheduling conflict has occurred, the presentation cannot be moved to the
desired position');
}
}
/**
* If the presentation is not directly cleared from the checks, an allocator can help
* to schedule the presentation
* @param $presentation
* @param $timeslot
* @param $room
* @return bool
*/
public function tryUsingPresentationAllocator($presentation, $timeslot, $room)
{
$allocationHelper = new PresentationAllocationHelper();
$canAllocatorHelp = $allocationHelper->canHelp($presentation, $timeslot, $room);
if ($canAllocatorHelp == 1) {
$possibleStartingTime = $allocationHelper
->tryToSchedulePresentationInTimeslot($presentation, $timeslot, $room);
$this->dispatchMoveEventToGrid(
$presentation->id,
$room->id,
$timeslot->id,
$possibleStartingTime->format('H:i')
);
} elseif ($canAllocatorHelp == 2) {
$possibleStartingTime = $allocationHelper
->tryToScheduleInPreviousTimeslot($presentation, $timeslot, $room);
$newTimeslot = $allocationHelper->findTimeslotByStartingTime($possibleStartingTime);
$this->dispatchMoveEventToGrid(
$presentation->id,
$room->id,
$newTimeslot->id,
$possibleStartingTime->format('H:i')
);
}
return $canAllocatorHelp != 0;
}
/**
* Dispatches a moving event to the parent
* @param $id
* @param $newRoom
* @param $newTimeslot
* @param $newTime
* @return void
*/
public function dispatchMoveEventToGrid($id, $newRoom, $newTimeslot, $newTime)
{
$this->dispatch('move-presentation', data: [
'presentation_id' => $id,
'new_room_id' => $newRoom,
'new_timeslot_id' => $newTimeslot,
'new_time' => $newTime
]);
}
/**
* Listens for the parent to dispatch an event to update the cell
* and sends update event to the child components in this cell
* @return void
*/
#[On("update-cell-{id}")]
public function refresh()
{
$timeslotStart = Carbon::parse($this->timeslot->start);
$this->room = $this->room->fresh();
$this->presentations = $this->room->presentations()
->where('start', '>=', $timeslotStart)
->where('start', '<', $timeslotStart->copy()->addMinutes($this->timeslot->duration))
->orderBy('start', 'asc')
->get();
foreach ($this->presentations as $presentation) {
$this->dispatch('update-presentation-' . $presentation->id);
}
}
/**
* Calculates the height of the element in REM based on it's timeslot duration
* @return float
*/
protected function calculateHeightInREM()
{
return $this->timeslot->duration * (14 / 30) * 0.25;
}
/**
* Renders the component
* @return View
*/
public function render() : View
{
return view('livewire.schedule.cell');
}
}