200 lines
4.7 KiB
PHP
200 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\UserFlags;
|
|
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class User extends Authenticatable implements MustVerifyEmail
|
|
{
|
|
use HasFactory, Notifiable;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'first_name',
|
|
'last_name',
|
|
'judging_preference',
|
|
'cell_phone',
|
|
'email',
|
|
'password',
|
|
'profile_image_url',
|
|
'school_id',
|
|
'is_tab',
|
|
'is_admin',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for serialization.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
];
|
|
|
|
/**
|
|
* Get the attributes that should be cast.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'email_verified_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
];
|
|
}
|
|
|
|
public function full_name(bool $last_name_first = false): string
|
|
{
|
|
if ($last_name_first) {
|
|
return $this->last_name.', '.$this->first_name;
|
|
}
|
|
|
|
return $this->first_name.' '.$this->last_name;
|
|
}
|
|
|
|
public function short_name(): string
|
|
{
|
|
// return the first letter of $this->first_name and the full $this->last_name
|
|
return $this->first_name[0].'. '.$this->last_name;
|
|
}
|
|
|
|
public function has_school(): bool
|
|
{
|
|
return $this->school_id !== null;
|
|
}
|
|
|
|
public function emailDomain(): string
|
|
{
|
|
$pos = strpos($this->email, '@');
|
|
|
|
return substr($this->email, $pos + 1);
|
|
}
|
|
|
|
public function school(): BelongsTo
|
|
{
|
|
return $this->belongsTo(School::class);
|
|
}
|
|
|
|
public function students(): HasManyThrough
|
|
{
|
|
return $this
|
|
->hasManyThrough(Student::class, School::class, 'id', 'school_id', 'school_id', 'id')
|
|
->orderBy('last_name')
|
|
->orderBy('first_name');
|
|
}
|
|
|
|
public function entries(): HasManyThrough
|
|
{
|
|
return $this->hasManyThrough(
|
|
Entry::class,
|
|
Student::class,
|
|
'school_id',
|
|
'student_id',
|
|
'school_id',
|
|
'id'
|
|
);
|
|
}
|
|
|
|
public function rooms(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Room::class, 'room_user');
|
|
}
|
|
|
|
public function judgingAssignments(): BelongsToMany
|
|
{
|
|
return $this->rooms();
|
|
}
|
|
|
|
public function bonusJudgingAssignments(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(BonusScoreDefinition::class, 'bonus_score_judge_assignment');
|
|
}
|
|
|
|
public function isJudge(): bool
|
|
{
|
|
return $this->judgingAssignments()->count() > 0 || $this->bonusJudgingAssignments()->count() > 0;
|
|
}
|
|
|
|
public function possibleSchools(): Collection
|
|
{
|
|
if ($this->school_id) {
|
|
$return[] = $this->school;
|
|
|
|
return collect($return);
|
|
}
|
|
|
|
return SchoolEmailDomain::with('school')->where('domain', '=', $this->emailDomain())->get();
|
|
}
|
|
|
|
public function canTab(): bool
|
|
{
|
|
if ($this->is_admin) {
|
|
return true;
|
|
}
|
|
|
|
if ($this->is_tab) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function scoreSheets(): HasMany
|
|
{
|
|
return $this->hasMany(ScoreSheet::class);
|
|
}
|
|
|
|
public function flags(): HasMany
|
|
{
|
|
return $this->hasMany(UserFlag::class);
|
|
}
|
|
|
|
public function hasFlag($flag): bool
|
|
{
|
|
$flags = [];
|
|
foreach ($this->flags as $checkFlag) {
|
|
$flags[] = $checkFlag->flag_name->value;
|
|
}
|
|
|
|
return in_array($flag, $flags);
|
|
|
|
}
|
|
|
|
public function addFlag($flag): void
|
|
{
|
|
if ($this->hasFlag($flag)) {
|
|
return;
|
|
}
|
|
$enum = match ($flag) {
|
|
'head_director' => UserFlags::HEAD_DIRECTOR,
|
|
};
|
|
$this->flags()->create(['flag_name' => $enum]);
|
|
$this->load('flags');
|
|
}
|
|
|
|
public function scoresForEntry($entry)
|
|
{
|
|
return $this->scoreSheets->where('entry_id', '=', $entry)->first()?->subscores;
|
|
}
|
|
|
|
public function timeForEntryScores($entry)
|
|
{
|
|
return $this->scoreSheets->where('entry_id', '=', $entry)->first()?->created_at;
|
|
}
|
|
}
|