-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEditDefaultPresentationForm.php
151 lines (124 loc) · 4.8 KB
/
EditDefaultPresentationForm.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
<?php
namespace App\Http\Livewire\DefaultPresentations;
use App\Http\Controllers\TimeslotController;
use App\Models\DefaultPresentation;
use App\Models\Presentation;
use App\Models\Timeslot;
use Carbon\Carbon;
use Illuminate\Contracts\Foundation\Application as ApplicationContract;
use Illuminate\Contracts\View\Factory;
use Illuminate\Foundation\Application;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
use Livewire\Component;
class EditDefaultPresentationForm extends Component
{
public DefaultPresentation $presentation;
public $starting;
public $ending;
public $confirmationTimeslotRegeneration;
/**
* After the component is instantiated set the starting and ending separately from the model
*/
public function mount()
{
$this->starting = Carbon::parse($this->presentation->timeslot->start);
$this->ending = Carbon::parse($this->presentation->timeslot->start)
->addMinutes($this->presentation->timeslot->duration)->format('H:i');
$this->starting = $this->starting->format('H:i');
}
protected $rules = [
'presentation.name' => 'required',
'presentation.description' => 'required',
'presentation.room_id' => 'required|numeric',
'starting' => 'required|date_format:H:i',
'ending' => 'required|date_format:H:i|after:starting'
];
/**
* Triggered by the save button, based on the default presentation type calls
* different checks
*/
public function save()
{
$this->validate();
if ($this->presentation->type == 'opening') {
$this->checkSaveOpening();
} else {
$this->checkSaveClosing();
}
}
/**
* Checks if the ending time of the opening presentation overlaps the generated timeslots and
* if it does, it opens another modal to confirm their regeneration, while if it doesn't it calls
* a method that saves the changes of the opening presentation
*/
public function checkSaveOpening()
{
$closestTimeslot = $this->presentation->timeslot->closestStartingTimeslot();
if (Carbon::parse($this->ending)->gt(Carbon::parse($closestTimeslot->start))) {
$this->confirmationTimeslotRegeneration = true;
} else {
$this->saveChanges();
return redirect()->to(route('moderator.schedule.index'));
}
}
/**
* Checks if the starting time of the closing presentation overlaps the generated timeslots and
* if it does, it opens another modal to confirm their regeneration, while if it doesn't it calls
* a method that saves the changes of the closing presentation
*/
public function checkSaveClosing()
{
$latestEndingTime = Timeslot::getTheLatestEndingUsed();
if (Carbon::parse($this->starting)
->lt(Carbon::parse($latestEndingTime))) {
$this->confirmationTimeslotRegeneration = true;
} else {
$this->saveChanges();
return redirect()->to(route('moderator.schedule.index'));
}
}
/**
* Regenerates all timeslots
*/
public function confirmedTimeslotRegeneration()
{
$this->saveChanges();
$timeslotPadding = Timeslot::paddingBetweenSlots();
foreach (Presentation::all() as $presentation) {
$presentation->timeslot_id = null;
$presentation->room_id = null;
$presentation->save();
}
Timeslot::where(function ($query) {
$query->where('id', '!=', DefaultPresentation::opening()->timeslot_id)
->where('id', '!=', DefaultPresentation::closing()->timeslot_id);
})->delete();
$startTimeOfNewTimeslots = Carbon::parse(DefaultPresentation::opening()->timeslot->start)
->addMinutes(DefaultPresentation::opening()->timeslot->duration)
->format("H:i");
$endingTimeOfNewTimeslots = DefaultPresentation::closing()->timeslot->start;
TimeslotController::generate($startTimeOfNewTimeslots, $endingTimeOfNewTimeslots, $timeslotPadding);
return redirect()->to(route('moderator.schedule.index'));
}
/**
* Saves the changes on the presentation
*/
public function saveChanges()
{
$this->validate();
$this->presentation->timeslot->start = $this->starting;
$this->presentation->timeslot->duration = Carbon::parse($this->ending)
->diffInMinutes(Carbon::parse($this->starting));
$this->presentation->timeslot->save();
$this->presentation->save();
}
/**
* Render the component
* @return Factory|Application|\Illuminate\Contracts\View\View|ApplicationContract
*/
public function render()
{
return view('moderator.schedule.default-presentations.edit-default-presentation-form');
}
}