You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.3 KiB

1 month ago
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class AudienceRegistration extends Model
{
public const PURPOSES = [
1 month ago
'networking',
'collaboration',
'investment',
'study',
'media',
'other',
1 month ago
];
protected $fillable = [
'competition_id',
'audience_session_id',
'user_id',
'name',
'phone',
'company',
'job',
'purpose',
1 month ago
'answers_json',
1 month ago
'ticket_code',
'registered_at',
'checked_in_at',
'checked_in_by_admin_id',
'checked_in_by_competition_admin_id',
];
/**
* @var array<string, string>
*/
protected $casts = [
'registered_at' => 'datetime',
'checked_in_at' => 'datetime',
1 month ago
'answers_json' => 'array',
1 month ago
];
protected static function booted(): void
{
static::creating(function (self $row) {
if (! filled($row->ticket_code)) {
$row->ticket_code = self::newTicketCode();
}
});
}
public static function newTicketCode(): string
{
do {
$code = bin2hex(random_bytes(8));
} while (self::query()->where('ticket_code', $code)->exists());
return $code;
}
public function competition(): BelongsTo
{
return $this->belongsTo(Competition::class);
}
public function session(): BelongsTo
{
return $this->belongsTo(AudienceSession::class, 'audience_session_id');
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function checkedInBy(): BelongsTo
{
return $this->belongsTo(AdminUser::class, 'checked_in_by_admin_id');
}
public function checkedInByCompetitionAdmin(): BelongsTo
{
return $this->belongsTo(CompetitionAdmin::class, 'checked_in_by_competition_admin_id');
}
public function isCheckedIn(): bool
{
return $this->checked_in_at !== null;
}
1 month ago
public static function existsForUserCompetition(int $userId, int $competitionId): bool
{
return static::query()
->where('user_id', $userId)
->where('competition_id', $competitionId)
->exists();
}
1 month ago
}