From 969efd5e54c66ae3001c80b28d28157ebb1a4520 Mon Sep 17 00:00:00 2001 From: Matt Young Date: Thu, 6 Jun 2024 22:07:59 -0500 Subject: [PATCH] Draw working --- .../Controllers/Admin/AuditionController.php | 31 +++++++++ .../Controllers/Admin/EntryController.php | 3 + app/Models/Audition.php | 69 +++++++++++++++++++ ...dd_draw_number_column_to_entries_table.php | 32 +++++++++ .../admin/entries/prepare_draw.blade.php | 31 +++++++++ .../draw-page/audition-switches.blade.php | 30 ++++++++ .../components/form/toggle-checkbox.blade.php | 4 +- .../layout/navbar/menus/admin.blade.php | 1 + resources/views/entries/index.blade.php | 6 +- resources/views/test.blade.php | 23 ++++--- routes/web.php | 3 + 11 files changed, 220 insertions(+), 13 deletions(-) create mode 100644 database/migrations/2024_06_06_121457_add_draw_number_column_to_entries_table.php create mode 100644 resources/views/admin/entries/prepare_draw.blade.php create mode 100644 resources/views/components/draw-page/audition-switches.blade.php diff --git a/app/Http/Controllers/Admin/AuditionController.php b/app/Http/Controllers/Admin/AuditionController.php index d51cf8c..448edd2 100644 --- a/app/Http/Controllers/Admin/AuditionController.php +++ b/app/Http/Controllers/Admin/AuditionController.php @@ -9,10 +9,14 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Session; use function abort; +use function array_keys; +use function compact; +use function dd; use function redirect; use function request; use function response; use function route; +use function view; class AuditionController extends Controller { @@ -143,4 +147,31 @@ class AuditionController extends Controller $audition->delete(); return redirect('/admin/auditions'); } + + public function prepareDraw() + { + if(! Auth::user()->is_admin) abort(403); + $allAuditions = Audition::with('entries')->orderBy('score_order')->get(); + $nodraw_auditions = $allAuditions->filter(function($audition) { + return $audition->has_no_draw(); + }); + $drawn_auditions = $allAuditions->filter(function($audition) { + return $audition->has_complete_draw(); + }); + $partial_draw_auditions = $allAuditions->filter(function($audition) { + return $audition->has_partial_draw(); + }); + return view('admin.entries.prepare_draw',compact('nodraw_auditions','drawn_auditions','partial_draw_auditions')); + } + + public function runDraw(Request $request) + { + if(! Auth::user()->is_admin) abort(403); + $draw_auditions = Audition::with('entries')->find(array_keys($request->input('auditions'))); + foreach ($draw_auditions as $audition) { + $audition->runDraw(); + } + + return redirect('/admin/auditions/run_draw'); + } } diff --git a/app/Http/Controllers/Admin/EntryController.php b/app/Http/Controllers/Admin/EntryController.php index 0de49cf..06ea82c 100644 --- a/app/Http/Controllers/Admin/EntryController.php +++ b/app/Http/Controllers/Admin/EntryController.php @@ -9,6 +9,7 @@ use App\Models\School; use App\Models\Student; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; +use function compact; class EntryController extends Controller { @@ -108,4 +109,6 @@ class EntryController extends Controller return redirect('/admin/entries'); } + + } diff --git a/app/Models/Audition.php b/app/Models/Audition.php index 689be7a..b030f4b 100644 --- a/app/Models/Audition.php +++ b/app/Models/Audition.php @@ -45,9 +45,78 @@ class Audition extends Model return '$' . number_format($this->entry_fee / 100, 2); } + public function has_no_draw(): bool + { + // return true if all of my entries have a null draw_number + //return $this->entries->every(fn($entry) => is_null($entry->draw_number)); + // return $this->entries()->whereNotNull('draw_number')->doesntExist(); + if ($this->entries->count() == 0) { + return false; + } + if ($this->relationLoaded('entries')) { + return $this->entries->every(function ($entry) { + return is_null($entry->draw_number); + }); + } else { + return $this->entries()->whereNotNull('draw_number')->doesntExist(); + } + } + public function has_complete_draw(): bool + { + // return true if all of my entries have a draw_number + // return $this->entries->every(fn($entry) => !is_null($entry->draw_number)); + // return $this->entries()->whereNull('draw_number')->doesntExist(); + if ($this->entries->count() == 0) { + return false; + } + if ($this->relationLoaded('entries')) { + return $this->entries->every(function ($entry) { + return !is_null($entry->draw_number); + }); + } else { + return $this->entries()->whereNull('draw_number')->doesntExist(); + } + } + public function has_partial_draw(): bool + { + if ($this->entries->count() == 0) { + return false; + } + // return true I have at least one entry with a null draw number AND at least one entry with a non-null draw number + //return $this->entries->contains(fn($entry) => is_null($entry->draw_number)) && $this->entries->contains(fn($entry) => !is_null($entry->draw_number)); + //$hasNull = $this->entries()->whereNull('draw_number')->exists(); + //$hasNotNull = $this->entries()->whereNotNull('draw_number')->exists(); + //return $hasNull && $hasNotNull; + if ($this->relationLoaded('entries')) { + $hasNull = $this->entries->contains(function ($entry) { + return is_null($entry->draw_number); + }); + + $hasNotNull = $this->entries->contains(function ($entry) { + return !is_null($entry->draw_number); + }); + + return $hasNull && $hasNotNull; + } else { + $hasNull = $this->entries()->whereNull('draw_number')->exists(); + $hasNotNull = $this->entries()->whereNotNull('draw_number')->exists(); + return $hasNull && $hasNotNull; + } + } + + public function runDraw(): null + { + $entries = $this->entries->shuffle(); + + foreach ($entries as $index => $entry) { + $entry->update(['draw_number' => $index + 1]); + $entry->save(); + } + return null; + } } diff --git a/database/migrations/2024_06_06_121457_add_draw_number_column_to_entries_table.php b/database/migrations/2024_06_06_121457_add_draw_number_column_to_entries_table.php new file mode 100644 index 0000000..41b95c7 --- /dev/null +++ b/database/migrations/2024_06_06_121457_add_draw_number_column_to_entries_table.php @@ -0,0 +1,32 @@ +integer('draw_number')->nullable()->default(null); + $table->unique(['audition_id', 'draw_number']); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('entries', function (Blueprint $table) { + //drop unique constraint for audition_id and draw_number + $table->dropUnique(['audition_id', 'draw_number']); + // drop column draw_number + $table->dropColumn('draw_number'); + }); + } +}; diff --git a/resources/views/admin/entries/prepare_draw.blade.php b/resources/views/admin/entries/prepare_draw.blade.php new file mode 100644 index 0000000..854be7c --- /dev/null +++ b/resources/views/admin/entries/prepare_draw.blade.php @@ -0,0 +1,31 @@ + + Run Draw +
+ @csrf +
+ Run Draw for Selected Auditions +
+
+ + @if($nodraw_auditions->count() > 0) + + These auditions have no draw + + @endif + + @if($partial_draw_auditions->count() > 0) + + These auditions have a partial draw + + @endif + + @if($drawn_auditions->count() > 0) + + These auditions have already been drawn + + @endif +
+
+
+ +{{--TODO when an entry is made anywhere in the system, there needs to be a check if a draw needs to be run--}} diff --git a/resources/views/components/draw-page/audition-switches.blade.php b/resources/views/components/draw-page/audition-switches.blade.php new file mode 100644 index 0000000..67033cc --- /dev/null +++ b/resources/views/components/draw-page/audition-switches.blade.php @@ -0,0 +1,30 @@ +@props([ + 'auditions', + ]) +merge(['class'=>'grow mx-2 my-2']) }}> + + {{ $slot }} + +
+
+ + +
+
+ @foreach($auditions->chunk($auditions->count() /2) as $chunk) +
+ @foreach($chunk as $audition) +
+
+ +
+
+ +
+
+ @endforeach +
+ @endforeach +
+
+
diff --git a/resources/views/components/form/toggle-checkbox.blade.php b/resources/views/components/form/toggle-checkbox.blade.php index 531c054..3d6086a 100644 --- a/resources/views/components/form/toggle-checkbox.blade.php +++ b/resources/views/components/form/toggle-checkbox.blade.php @@ -1,7 +1,7 @@ @props(['name', 'checked' => false])
-
diff --git a/resources/views/entries/index.blade.php b/resources/views/entries/index.blade.php index 35bb0c8..ad5f4bf 100644 --- a/resources/views/entries/index.blade.php +++ b/resources/views/entries/index.blade.php @@ -9,7 +9,7 @@ Add Entry - + @@ -29,7 +29,7 @@ - Save + Save @@ -38,7 +38,7 @@ Entry Listing You have {{ $entries->count() }} entries -
+
diff --git a/resources/views/test.blade.php b/resources/views/test.blade.php index 765d45d..df88c43 100644 --- a/resources/views/test.blade.php +++ b/resources/views/test.blade.php @@ -3,15 +3,22 @@ Test Page @php - $scores =[ - ['subcoreID' => 13, 'score' => 93], - ['subcoreID' => 23, 'score' => 91], - ['subcoreID' => 42, 'score' => 81], - ['subcoreID' => 78, 'score' => 16], - ['subcoreID' => 74, 'score' => 23], - ]; - + $auditions = Audition::with('entries')->get(); @endphp +@foreach($auditions as $audition) + {{ $audition->name }} has {{ $audition->entries->count() }} entries.
+ @if($audition->has_complete_draw()) + Draw is complete. + @endif + @if($audition->has_partial_draw()) + Has a partial draw. + @endif + + @if($audition->has_no_draw()) + Has no draw. + @endif +

+@endforeach diff --git a/routes/web.php b/routes/web.php index 77509b3..97dfabf 100644 --- a/routes/web.php +++ b/routes/web.php @@ -68,6 +68,8 @@ Route::middleware(['auth','verified',CheckIfAdmin::class])->prefix('admin/')->gr Route::patch('/{audition}','update'); Route::post('/reorder','reorder'); Route::delete('/{audition}','destroy'); + Route::get('/run_draw','prepareDraw'); + Route::post('/run_draw','runDraw'); }); // Admin Entries Routes @@ -77,6 +79,7 @@ Route::middleware(['auth','verified',CheckIfAdmin::class])->prefix('admin/')->gr Route::post('/','store'); Route::get('/{entry}/edit','edit'); Route::patch('/{entry}','update'); + }); // Admin Student Routes