125 lines
4.4 KiB
PHP
125 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Observers;
|
|
|
|
use App\Actions\Tabulation\DoublerSync;
|
|
use App\Models\Audition;
|
|
use App\Models\Doubler;
|
|
use App\Models\Entry;
|
|
use Illuminate\Support\Facades\Request;
|
|
|
|
use function auditionSetting;
|
|
|
|
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;
|
|
if ($entry->draw_number) {
|
|
$message .= '<br>Draw Number: '.$entry->draw_number;
|
|
}
|
|
|
|
$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
|
|
{
|
|
if (Request::route()->getName() !== 'admin.draw.store') { // Don't update doubler table during draw
|
|
$syncer = app(DoublerSync::class);
|
|
// Update doubler table when an entry is updated
|
|
$syncer();
|
|
}
|
|
|
|
// Log entry changes
|
|
$message = 'Updated Entry #'.$entry->id;
|
|
$affected['entries'] = [$entry->id];
|
|
$affected['auditions'] = [$entry->audition_id];
|
|
$affected['students'] = [$entry->student_id];
|
|
$shouldLog = false;
|
|
if ($entry->wasChanged('audition_id')) {
|
|
$originalAuditionName = Audition::find($entry->getOriginal('audition_id'))->name;
|
|
$message .= '<br>Audition: '.$originalAuditionName.' -> '.$entry->audition->name;
|
|
$affected['auditions'][] = $entry->getOriginal('audition_id');
|
|
$shouldLog = true;
|
|
}
|
|
if ($entry->wasChanged('student_id')) {
|
|
$originalStudentName = $entry->getOriginal('student')->full_name();
|
|
$message .= '<br>Student: '.$originalStudentName.' -> '.$entry->student->full_name();
|
|
$affected['students'][] = $entry->getOriginal('student_id');
|
|
$shouldLog = true;
|
|
}
|
|
if ($entry->wasChanged('for_advancement' && auditionSetting('advanceTo'))) {
|
|
$message .= '<br>Entered for '.auditionSetting('advanceTo');
|
|
$shouldLog = true;
|
|
}
|
|
if ($entry->wasChanged('for_seating')) {
|
|
$message .= '<br>Entered for seating';
|
|
$shouldLog = true;
|
|
}
|
|
if ($shouldLog) {
|
|
auditionLog($message, $affected);
|
|
}
|
|
|
|
if ($entry->wasChanged('draw_number')) {
|
|
$message = 'Assigned Entry #'.$entry->id.' draw number '.$entry->draw_number;
|
|
$message .= '<br>Audition: '.$entry->audition->name;
|
|
$message .= '<br>Student: '.$entry->student->full_name();
|
|
$affected['students'] = [$entry->student_id];
|
|
$affected['auditions'] = [$entry->audition_id];
|
|
$affected['entries'] = [$entry->id];
|
|
auditionLog($message, $affected);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
}
|