77 lines
2.3 KiB
PHP
77 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Observers;
|
|
|
|
use App\Actions\Tabulation\DoublerSync;
|
|
use App\Models\Audition;
|
|
use App\Models\Doubler;
|
|
use App\Models\Entry;
|
|
|
|
class EntryObserver
|
|
{
|
|
/**
|
|
* Handle the Entry "created" event.
|
|
*/
|
|
public function created(Entry $entry): void
|
|
{
|
|
// Count how many entries the student has for the event
|
|
$count = $entry->student->entriesForEvent($entry->audition->event_id)->count();
|
|
|
|
// If less than two entries, they're not a doubler
|
|
if ($count > 1) {
|
|
// Update doublers for the event
|
|
$syncer = app(DoublerSync::class);
|
|
$syncer($entry->audition->event_id);
|
|
}
|
|
|
|
// Log Entry Creation
|
|
$message = 'Created Entry #'.$entry->id;
|
|
$message .= '<br>Audition: '.$entry->audition->name;
|
|
$message .= '<br>Student: '.$entry->student->full_name();
|
|
$message .= '<br>Grade: '.$entry->student->grade;
|
|
$message .= '<br>School: '.$entry->student->school->name;
|
|
|
|
$affected = [
|
|
'students' => [$entry->student_id],
|
|
'schools' => [$entry->student->school_id],
|
|
'auditions' => [$entry->audition_id],
|
|
];
|
|
auditionLog($message, $affected);
|
|
|
|
}
|
|
|
|
/**
|
|
* Handle the Entry "updated" event.
|
|
*/
|
|
public function updated(Entry $entry): void
|
|
{
|
|
$syncer = app(DoublerSync::class);
|
|
// Update doubler table when an entry is updated
|
|
$syncer();
|
|
}
|
|
|
|
/**
|
|
* Handle the Entry "deleted" event.
|
|
*/
|
|
public function deleted(Entry $entry): void
|
|
{
|
|
$syncer = app(DoublerSync::class);
|
|
Doubler::where('student_id', $entry->student_id)->delete();
|
|
$audition = Audition::where('id', $entry->audition_id)->first();
|
|
$syncer($audition->event_id);
|
|
|
|
$message = 'Deleted Entry #'.$entry->id;
|
|
$message .= '<br>Audition: '.$entry->audition->name;
|
|
$message .= '<br>Student: '.$entry->student->full_name();
|
|
$message .= '<br>Grade: '.$entry->student->grade;
|
|
$message .= '<br>School: '.$entry->student->school->name;
|
|
|
|
$affected = [
|
|
'students' => [$entry->student_id],
|
|
'schools' => [$entry->student->school_id],
|
|
'auditions' => [$entry->audition_id],
|
|
];
|
|
auditionLog($message, $affected);
|
|
}
|
|
}
|