auditionadmin/app/Models/Audition.php

171 lines
5.3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use phpDocumentor\Reflection\Types\Boolean;
use PhpParser\Node\Scalar\String_;
use function now;
class Audition extends Model
{
use HasFactory;
protected $guarded = [];
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;
}
// public function judges()
// {
// // Very inefficient, need a better way
// return User::join('room_user', 'users.id', '=', 'room_user.user_id')
// ->join('rooms', 'room_user.room_id', '=', 'rooms.id')
// ->join('auditions', 'rooms.id', '=', 'auditions.room_id')
// ->where('auditions.id', $this->id)
// ->select('users.*') // avoid getting other tables' columns
// ->get();
// }
/**
* @return Collection
*/
public function judges()
{
return $this->room->judges;
}
public function scoredEntries()
{
return $this->entries->filter(function($entry) {
return $entry->scoreSheets->count() >= $this->judges()->count();
});
}
public function 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);
return $collection;
}
}