function($query) { $query->withCount('scoreSheets'); },'room.judges'])->get(); self::$completeAuditions = $auditions->filter(function ($audition) { return $audition->scoringIsComplete(); }); return self::$completeAuditions; } public static function deadlineNotPast() { return Audition::where('entry_deadline', '>=', now())->get(); } public function event(): BelongsTo { return $this->belongsTo(Event::class); } public function entries(): HasMany { return $this->hasMany(Entry::class); } public function room(): BelongsTo { return $this->belongsTo(Room::class); } public function scoringGuide(): BelongsTo { return $this->belongsTo(ScoringGuide::class); } public function dislpay_fee(): String { 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; } /** * @return BelongsToMany|\App\Models\User[] */ public function judges() { return $this->belongsToMany( User::class, // The related model 'room_user', // The intermediate table 'room_id', // The foreign key on the intermediate table 'user_id', // The related key on the intermediate table 'room_id', // The local key 'id' // The local ke ); } public function scoredEntries() { return $this->entries->filter(function($entry) { return $entry->score_sheets_count >= $this->judges()->count(); }); } public function rankedEntries() { if (! $this->rankedEntries) { $entries = $this->entries()->with(['audition.scoringGuide.subscores', 'scoreSheets.judge'])->get(); $entries = $entries->all(); usort($entries, function ($a, $b) { $aScores = $a->finalScoresArray(); $bScores = $b->finalScoresArray(); $length = min(count($aScores), count($bScores)); for ($i = 0; $i < $length; $i++) { if ($aScores[$i] !== $bScores[$i]) { return $bScores[$i] - $aScores[$i]; } } return 0; }); $collection = new \Illuminate\Database\Eloquent\Collection($entries); $this->rankedEntries = $collection; } return $this->rankedEntries; } public function scoringIsComplete() { if (self::$completeAuditions) { // check and see if this audition is in the list of complete auditions return self::$completeAuditions->contains('id', $this->id); } return $this->scoredEntries()->count() == $this->entries->count(); } }