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 @@
+