Skip to content

Commit 6d4fcca

Browse files
committed
make it a trait
1 parent 3c1839d commit 6d4fcca

File tree

5 files changed

+220
-186
lines changed

5 files changed

+220
-186
lines changed

app/Http/Controllers/Auth/ApplicationController.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
namespace App\Http\Controllers\Auth;
44

55
use App\Exports\ApplicantsExport;
6+
use App\Http\Controllers\Controller;
67
use App\Models\ApplicationForm;
78
use App\Models\Faculty;
8-
use App\Models\PeriodicEvents\PeriodicEventController;
9+
use App\Models\PeriodicEvents\HasPeriodicEvent;
910
use App\Models\User;
1011
use App\Models\Workshop;
1112
use App\Models\RoleUser;
@@ -20,10 +21,15 @@
2021
use Illuminate\Support\Facades\DB;
2122
use Maatwebsite\Excel\Facades\Excel;
2223

23-
class ApplicationController extends PeriodicEventController
24+
class ApplicationController extends Controller
2425
{
25-
protected const connectedToSemester = true;
26-
protected const hasShowUntil = true;
26+
use HasPeriodicEvent;
27+
28+
function __construct()
29+
{
30+
$this->hasShowUntil = true;
31+
$this->connectedToSemester = true;
32+
}
2733

2834
private const EDUCATIONAL_ROUTE = 'educational';
2935
private const QUESTIONS_ROUTE = 'questions';
@@ -32,6 +38,15 @@ class ApplicationController extends PeriodicEventController
3238
private const ADD_PROFILE_PIC_ROUTE = 'files.profile';
3339
private const SUBMIT_ROUTE = 'submit';
3440

41+
/**
42+
* @see PeriodicEventController::authorizeChangePeriodicEvent()
43+
* @throws AuthorizationException
44+
*/
45+
public function authorizeChangePeriodicEvent(): void
46+
{
47+
$this->authorize('finalize', ApplicationForm::class);
48+
}
49+
3550
/**
3651
* Return the view based on the request's page parameter.
3752
* @param Request $request
@@ -301,7 +316,7 @@ public function submitApplication(User $user)
301316
if ($user->application->missingData() == []) {
302317
$user->application->update(['status' => ApplicationForm::STATUS_SUBMITTED]);
303318
$user->internetAccess->setWifiCredentials($user->educationalInformation->neptun);
304-
$user->internetAccess()->update(['has_internet_until' => $this::getDeadline()?->addMonth()]);
319+
$user->internetAccess()->update(['has_internet_until' => $this->getDeadline()?->addMonth()]);
305320
return back()->with('message', 'Sikeresen véglegesítette a jelentkezését!');
306321
} else {
307322
return back()->with('error', 'Hiányzó adatok!');

app/Http/Controllers/Secretariat/SemesterEvaluationController.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
namespace App\Http\Controllers\Secretariat;
44

5+
use App\Http\Controllers\Controller;
56
use App\Mail\EvaluationFormClosed;
67
use App\Mail\StatusDeactivated;
78
use App\Models\Faculty;
89
use App\Models\GeneralAssemblies\GeneralAssembly;
9-
use App\Models\PeriodicEvents\PeriodicEventController;
10+
use App\Models\PeriodicEvents\HasPeriodicEvent;
1011
use App\Models\Role;
1112
use App\Models\RoleUser;
1213
use App\Models\Semester;
@@ -21,16 +22,20 @@
2122
use Illuminate\Support\Facades\Validator;
2223
use Illuminate\Validation\Rule;
2324

24-
class SemesterEvaluationController extends PeriodicEventController
25+
class SemesterEvaluationController extends Controller
2526
{
26-
/**
27-
* Handle PeriodicEvent start.
28-
*/
29-
public static function handleStart(): void
27+
use HasPeriodicEvent;
28+
29+
30+
public function authorizeChangePeriodicEvent(): void
3031
{
31-
self::sendEvaluationAvailableMail();
32+
// TODO
3233
}
3334

35+
public function handlePeriodicEventStart(): void
36+
{
37+
self::sendEvaluationAvailableMail();
38+
}
3439

3540
/**
3641
* Show the evaluation form.
@@ -58,11 +63,12 @@ public function show()
5863

5964
/**
6065
* Update form information.
66+
* @throws \Exception
6167
*/
6268
public function store(Request $request)
6369
{
6470
$this->authorize('is-collegist');
65-
if (!SemesterEvaluation::isActive()) {
71+
if (!self::isActive()) {
6672
return redirect('home')->with('error', 'Lejárt a határidő a kérdőív kitöltésére. Keresd fel a titkárságot.');
6773
}
6874

@@ -95,9 +101,13 @@ public function store(Request $request)
95101
$validator->validate();
96102

97103
$user = user();
98-
$evaluation = $user->semesterEvaluations()->where('semester_id', Semester::current()->id)->first();
104+
$semester = self::semester();
105+
if(!$semester) {
106+
throw new \Exception('No semester found for the event');
107+
}
108+
$evaluation = $user->semesterEvaluations()->where('semester_id', $semester->id)->first();
99109
if (!$evaluation) {
100-
$evaluation = SemesterEvaluation::create(['semester_id' => Semester::current()->id, 'user_id' => $user->id]);
110+
$evaluation = SemesterEvaluation::create(['semester_id' => $semester->id, 'user_id' => $user->id]);
101111
}
102112
switch ($request->section) {
103113
case 'alfonso':
@@ -159,7 +169,7 @@ public function store(Request $request)
159169
/**
160170
* Send out the request to fill out the form.
161171
*/
162-
public static function sendEvaluationAvailableMail()
172+
public static function sendEvaluationAvailableMail(): void
163173
{
164174
Mail::to(config('contacts.mail_membra'))->queue(new \App\Mail\EvaluationFormAvailable());
165175
if (User::secretary()) {
@@ -173,7 +183,7 @@ public static function sendEvaluationAvailableMail()
173183
/**
174184
* Send out a reminder.
175185
*/
176-
public static function sendEvaluationReminder()
186+
public static function sendEvaluationReminder(): void
177187
{
178188
//TODO
179189
$userCount = self::usersHaventFilledOutTheForm()->count();
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
3+
namespace App\Models\PeriodicEvents;
4+
5+
use App\Models\Semester;
6+
use Carbon\Carbon;
7+
use Illuminate\Auth\Access\AuthorizationException;
8+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
9+
use Illuminate\Http\RedirectResponse;
10+
use Illuminate\Http\Request;
11+
use Illuminate\Validation\Rule;
12+
13+
trait HasPeriodicEvent
14+
{
15+
protected bool $connectedToSemester = false;
16+
protected bool $hasStartDate = false;
17+
protected bool $hasShowUntil = false;
18+
19+
public final function periodicEvent(): ?PeriodicEvent {
20+
return PeriodicEvent::where('event_model', self::class)
21+
->where(function($query) {
22+
$query
23+
->orWhere('extended_end_date', '>=', now())
24+
->orWhere(function($query) {
25+
$query
26+
->whereNull('extended_end_date')
27+
->where('end_date', '>=', now());
28+
})
29+
->orWhere('show_until', '>=', now());
30+
})
31+
->orderBy('start_date', 'desc')
32+
->first();
33+
}
34+
35+
/**
36+
* Handle authorization to change the PeriodicEvent.
37+
*/
38+
public abstract function authorizeChangePeriodicEvent(): void;
39+
40+
/**
41+
* Handle periodic event start event.
42+
*/
43+
public function handlePeriodicEventStart(): void
44+
{
45+
// Do nothing by default
46+
}
47+
48+
/**
49+
* Handle periodic event end event.
50+
*/
51+
public function handlePeriodicEventEnd(): void
52+
{
53+
// Do nothing by default
54+
}
55+
56+
/**
57+
* Check if the PeriodicEvent is currently active or not.
58+
* start date <= now <= (extended) end date
59+
* @return bool
60+
*/
61+
public final function isActive(): bool
62+
{
63+
return $this->periodicEvent()?->isActive() ?? false;
64+
}
65+
66+
/**
67+
* @return bool if the end date has been extended or not
68+
*/
69+
public final function isExtended(): bool
70+
{
71+
return $this->periodicEvent()?->isExtended() ?? false;
72+
}
73+
74+
/**
75+
* @return Carbon|null the start date of the current PeriodicEvent
76+
*/
77+
public final function getStartDate(): ?Carbon
78+
{
79+
if(!$this->periodicEvent()) return null;
80+
return Carbon::parse($this->periodicEvent()->start_date);
81+
}
82+
83+
/**
84+
* @return Carbon|null the end date of the current PeriodicEvent
85+
*/
86+
public final function getEndDate(): ?Carbon
87+
{
88+
if(!$this->periodicEvent()) return null;
89+
return Carbon::parse($this->periodicEvent()->real_end_date);
90+
}
91+
92+
/**
93+
* @return Carbon|null the end date of the current PeriodicEvent
94+
*/
95+
public final function getDeadline(): ?Carbon
96+
{
97+
return $this->getEndDate();
98+
}
99+
100+
/**
101+
* @return Semester|BelongsTo|null the semester connected to the current PeriodicEvent
102+
*/
103+
public final function semester(): Semester|BelongsTo|null
104+
{
105+
return $this->periodicEvent()?->semester()->first();
106+
}
107+
108+
/**
109+
* Create or update the current PeriodicEvent connected to the model.
110+
* @throws AuthorizationException
111+
*/
112+
public function storeOrUpdatePeriodicEvent(Request $request): RedirectResponse
113+
{
114+
$this->authorizeChangePeriodicEvent();
115+
$request->validate([
116+
'semester_id' => ['exists:semesters,id', Rule::requiredIf(fn () => $this->connectedToSemester)],
117+
'start_date' => ['date', Rule::requiredIf(fn () => $this->hasStartDate)],
118+
'end_date' => 'required|date|after:now|after:start_date',
119+
'extended_end_date' => 'nullable|date|after:end_date',
120+
'show_until' => ['date', Rule::requiredIf(fn () => $this->hasShowUntil)],
121+
]);
122+
$event = $this->periodicEvent();
123+
if($event) {
124+
$event->update([
125+
'start_date' => $request->get('start_date') ?? $event->start_date,
126+
'end_date' => $request->get('end_date'),
127+
'extended_end_date' => $request->get('extended_end_date') ?? $event->extended_end_date,
128+
'show_until' => $request->get('show_until') ?? $event->show_until,
129+
]);
130+
//TODO reset _handled fields
131+
} else {
132+
PeriodicEvent::create([
133+
'event_model' => self::class,
134+
'semester_id' => $request->get('semester_id'),
135+
'start_date' => $request->get('start_date') ?? now(),
136+
'end_date' => $request->get('end_date'),
137+
'extended_end_date' => $request->get('extended_end_date'),
138+
'show_until' => $request->get('show_until'),
139+
]);
140+
}
141+
142+
return redirect()->back()->with('message', __('general.successful_modification'));
143+
}
144+
145+
146+
}
147+

app/Models/PeriodicEvents/PeriodicEvent.php

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use App\Models\Semester;
66
use Carbon\Carbon;
7-
use Illuminate\Database\Eloquent\Casts\Attribute;
87
use Illuminate\Database\Eloquent\Model;
98
use Illuminate\Database\Eloquent\Relations\BelongsTo;
109

@@ -27,36 +26,51 @@ class PeriodicEvent extends Model
2726
'extended_end_date' => 'datetime'
2827
];
2928

30-
public function realEndDate(): Attribute
29+
public final function semester(): BelongsTo
3130
{
32-
return Attribute::make(
33-
get: function (): Carbon {
34-
return Carbon::parse($this->extended_end_date ?? $this->end_date);
35-
}
36-
);
31+
return $this->belongsTo(Semester::class);
3732
}
3833

39-
public function semester(): BelongsTo
34+
/**
35+
* @return Carbon|null the start date of the current PeriodicEvent
36+
*/
37+
public final function startDate(): ?Carbon
4038
{
41-
return $this->belongsTo(Semester::class);
39+
return Carbon::parse($this->start_date);
40+
}
41+
42+
/**
43+
* @return Carbon|null the end date of the current PeriodicEvent
44+
*/
45+
public final function endDate(): ?Carbon
46+
{
47+
return Carbon::parse($this->extended_end_date ?? $this->end_date);
48+
}
49+
50+
/**
51+
* @return Carbon|null the end date of the current PeriodicEvent
52+
*/
53+
public final function deadline(): ?Carbon
54+
{
55+
return $this->endDate();
4256
}
4357

4458
/**
4559
* Check if the PeriodicEvent is currently active or not.
4660
* start date <= now <= (extended) end date
4761
* @return bool
4862
*/
49-
public function isActive(): bool
63+
public final function isActive(): bool
5064
{
51-
if(Carbon::parse($this->start_date)->isFuture()) return false;
52-
if(Carbon::parse($this->real_end_date)->isPast()) return false;
65+
if($this->startDate()->isFuture()) return false;
66+
if($this->endDate()->isPast()) return false;
5367
return true;
5468
}
5569

5670
/**
5771
* @return bool if the end date has been extended or not
5872
*/
59-
public function isExtended(): bool
73+
public final function isExtended(): bool
6074
{
6175
return $this->extended_end_date != null;
6276
}
@@ -67,17 +81,16 @@ public function isExtended(): bool
6781
*/
6882
public static function listen(): void
6983
{
70-
//TODO fire events
7184
foreach (PeriodicEvent::all() as $event) {
72-
if (Carbon::parse($event->start_date)->isPast() && !$event->start_handled) {
73-
app($event->event_model)::handleStart();
85+
if ($event->startDate()->isPast() && !$event->start_handled) {
86+
app($event->event_model)->handlePeriodicEventStart();
7487
$event->start_handled = now();
7588
$event->save(['timestamps' => false]);
7689
}
7790
//TODO reminders
7891

79-
if (Carbon::parse($event->real_end_date)->isPast() && !$event->end_handled) {
80-
app($event->event_model)::handleEnd();
92+
if ($event->endDate()->isPast() && !$event->end_handled) {
93+
app($event->event_model)->handlePeriodicEventEnd();
8194
$event->end_handled = now();
8295
$event->save(['timestamps' => false]);
8396
}

0 commit comments

Comments
 (0)