-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEdition.php
348 lines (320 loc) · 10.4 KB
/
Edition.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
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
<?php
namespace App\Models;
use Barryvdh\LaravelIdeHelper\Eloquent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Carbon;
/**
* \App\Models\Edition
*
* @property int $id
* @property string $name
* @property int $state
* @property Carbon|null $start_at
* @property Carbon|null $end_at
* @property boolean $is_final_programme_released
* @property boolean $is_participant_registration_opened
* @property boolean $is_company_registration_opened
* @property boolean $is_requesting_presentation_opened
* @property boolean $is_in_progress
* @property boolean $is_over
* @property string $displayed_state
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @method static Builder|Edition newModelQuery()
* @method static Builder|Edition newQuery()
* @method static Builder|Edition query()
* @method static Builder|Edition whereCreatedAt($value)
* @method static Builder|Edition whereId($value)
* @method static Builder|Edition whereName($value)
* @method static Builder|Edition whereState($value)
* @method static Builder|Edition whereStartAt($value)
* @method static Builder|Edition whereEndAt($value)
* @method static Builder|Edition whereUpdatedAt($value)
* @mixin Eloquent
*/
class Edition extends Model
{
use HasFactory;
protected $fillable = [
'name',
'state',
'start_at',
'end_at',
'lecture_duration',
'workshop_duration',
'keynote_name',
'keynote_description',
'keynote_photo_path',
];
protected $casts = [
'start_at' => 'datetime',
'end_at' => 'datetime',
];
const STATE_DESIGN = 10;
const STATE_ANNOUNCE = 20;
const STATE_ENROLLMENT = 30;
const STATE_EXECUTION = 40;
const STATE_ARCHIVE = 50;
/**
* Returns basic validation rules for Edition
* @return string[]
*/
public static function rules(): array
{
return [
'name' => 'required|unique:editions|max:255',
'start_at' => 'required|date|after:' . Carbon::now()->addMonth() . '|before:' . Carbon::now()->addYears(2),
'end_at' => 'required|date|after:start_at|before:' . Carbon::now()->addYears(2),
'lecture_duration' => 'required|numeric|min:1|max:120',
'workshop_duration' => 'required|numeric|min:1|max:180'
];
}
/**
* Establishes a relationship between Edition and
* EditionEvent models (events that are executed during given edition)
*
* @return HasMany
*/
public function editionEvents(): HasMany
{
return $this->hasMany(EditionEvent::class);
}
/**
* Check if the final programme is released
*
* @return Attribute
*/
protected function isFinalProgrammeReleased(): Attribute
{
return Attribute::make(
get: fn() => $this->state == Edition::STATE_ENROLLMENT
|| $this->state == Edition::STATE_EXECUTION
);
}
/**
* Check if the company registration is opened
* @return Attribute
*/
public function isCompanyRegistrationOpened(): Attribute
{
return Attribute::make(
get: fn() => ($this->state == Edition::STATE_ANNOUNCE
|| $this->state == Edition::STATE_ENROLLMENT)
&& (Carbon::today() >= $this->getEvent('Company registration')->start_at
&& $this->getEvent('Company registration')->end_at >= Carbon::today())
);
}
/**
* Check if the participant registration is opened
* @return Attribute
*/
public function isParticipantRegistrationOpened(): Attribute
{
return Attribute::make(
get: fn() => ($this->state == Edition::STATE_ANNOUNCE
|| $this->state == Edition::STATE_ENROLLMENT)
&& (Carbon::now() >= $this->getEvent('Participant registration')->start_at
&& $this->getEvent('Participant registration')->end_at >= Carbon::now())
);
}
/**
* Check if requesting presentations is opened
* @return Attribute
*/
public function isRequestingPresentationOpened(): Attribute
{
return Attribute::make(
get: fn() => $this->state == Edition::STATE_ANNOUNCE
&& (Carbon::now() >= $this->getEvent('Presentation request')->start_at
&& $this->getEvent('Presentation request')->end_at >= Carbon::now())
);
}
/**
* Determine whether the edition is in progress
* TODO: change the '||' to '&&' once the automation of the state change is implemented
*
* @return Attribute
*/
public function isInProgress(): Attribute
{
return Attribute::make(
get: fn() => $this->state == Edition::STATE_EXECUTION
|| (Carbon::now() >= $this->start_at && Carbon::now() <= $this->end_at)
);
}
/**
* Determine whether the edition is over
* NOTE: possibly redundant method in case state change is automated
*
* @return Attribute
*/
public function isOver(): Attribute
{
return Attribute::make(
get: fn() => $this->state == Edition::STATE_ARCHIVE
|| Carbon::now() >= $this->end_at
);
}
/**
* Gets an instance that represents the current edition, meaning the
* edition that is currently opened for registration, in state of enrollment or executed
*
* @return Builder|Model|object|null
*/
public static function current()
{
return self::query()
->whereNot('state', '=', self::STATE_DESIGN)
->whereNot('state', '=', self::STATE_ARCHIVE)
->first();
}
/**
* Determine the state to display on the page
*
* @return Attribute
*/
public function displayedState(): Attribute
{
return Attribute::make(
get: function () {
if ($this->state == Edition::STATE_DESIGN) {
return 'Design';
} elseif ($this->is_company_registration_opened) {
return 'Company registration opened';
} elseif ($this->is_participant_registration_opened) {
return 'Participant registration opened';
} elseif ($this->state == Edition::STATE_ANNOUNCE) {
return 'Announced';
} elseif ($this->is_final_programme_released) {
return 'Final programme released';
} elseif ($this->state == Edition::STATE_EXECUTION) {
return 'In progress';
} else {
return 'Archived';
}
}
);
}
/**
* Determine whether all the dates of the edition were added
*
* @return Attribute
*/
public function configured(): Attribute
{
return Attribute::make(
get: function () {
if (!$this->start_at || !$this->end_at) {
return false;
}
foreach ($this->editionEvents as $event) {
if (!$event->start_at || !$event->end_at) {
return false;
}
}
return true;
}
);
}
/**
* Determine whether the keynote speaker details were added
*
* @return Attribute
*/
public function keynoteConfigured(): Attribute
{
return Attribute::make(
get: fn() => ($this->keynote_photo_path && $this->keynote_name && $this->keynote_description)
);
}
/**
* Changes state of the edition to 'announce', which means that it is officially opened for registration
* of companies
*
* @return void
*/
public function activate()
{
// check if the dates are configured
if ($this->configured()) {
// archive current active edition, if exists
$currentEdition = self::current();
if ($currentEdition) {
$currentEdition->state = self::STATE_ARCHIVE;
$currentEdition->save();
}
// activate the edition
$this->state = self::STATE_ANNOUNCE;
$this->save();
}
}
/**
* Adds an event to the edition
*
* @param Event $event event to attach to edition
* @return void
*/
public function addEvent(Event $event): void
{
if (!$this->editionEvents->contains($event)) {
EditionEvent::create([
'edition_id' => $this->id,
'event_id' => $event->id,
]);
}
}
/**
* Removes an event from the edition
*
* @param Event $event event to remove from edition
* @return void
*/
public function removeEvent(Event $event): void
{
$editionEvent = $this->editionEvents
->where('edition_id', '=', $this->id)
->where('event_id', '=', $event->id)
->first();
if ($editionEvent) {
$editionEvent->delete();
}
}
/**
* Returns information about event for the particular edition
*
* @param string $name of the event to look for
* @return EditionEvent
*/
public function getEvent(string $name): EditionEvent
{
return $this->editionEvents
->where('event_id', Event::where('name', $name)->first()->id)
->first();
}
/**
* Function that makes sure that the keynote has a picture even if it is
* just the default avatar
* @return Attribute
*/
public function keynotePictureSource(): Attribute
{
return Attribute::make(
get: function () {
if ($this->keynote_photo_path) {
return url('storage/' . $this->keynote_photo_path);
} else {
$name = trim(collect(explode(' ', $this->keynote_name))->map(function ($segment) {
return mb_substr($segment, 0, 1);
})->join(' '));
return 'https://ui-avatars.com/api/?name=' .
urlencode($name) .
'&color=7F9CF5&background=EBF4FF&size=128';
}
}
);
}
}