auditionadmin/app/Models/Audition.php

195 lines
5.7 KiB
PHP

<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use function now;
class Audition extends Model
{
use HasFactory;
protected $guarded = [];
protected $rankedEntries = null;
protected static $completeAuditions = null;
protected $fully_scored; // Set by TabulationService
protected $scored_entries_count; //Set by TabulationService
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;
// TODO move all draw functions to a DrawService
}
/**
* @return BelongsToMany|\App\Models\User[]
*/
public function judges(): array|BelongsToMany
{
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
);
}
/**
* Ensures judges_count property is always available
*/
public function getJudgesCountAttribute()
{
if (! isset($this->attributes['judges_count'])) {
$this->attributes['judges_count'] = $this->judges()->count();
}
return $this->attributes['judges_count'];
}
public function flags(): HasMany
{
return $this->hasMany(AuditionFlag::class);
}
public function hasFlag($flag): bool
{
// return true if any flag in $this->flags has a flag_name of declined without making another db query if flags are loaded
return $this->flags->contains('flag_name', $flag);
}
public function addFlag($flag): void
{
if ($this->hasFlag($flag)) {
return;
}
$this->flags()->create(['flag_name' => $flag]);
}
public function removeFlag($flag): void
{
// remove related auditionFlag where flag_name = $flag
$this->flags()->where('flag_name', $flag)->delete();
}
public function scopeOpen(Builder $query): void
{
$query->where('entry_deadline', '>=', Carbon::now());
}
}