94 lines
2.2 KiB
PHP
94 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Enums\Permission;
|
|
use App\Models\Enums\ReportStatus;
|
|
use App\Models\Enums\StudyLevelStatus;
|
|
use App\Models\Traits\HashableId;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
|
|
class Study extends BaseModel
|
|
{
|
|
use HashableId;
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'is_locked' => 'boolean',
|
|
'is_archived' => 'boolean',
|
|
'study_status' => StudyLevelStatus::class,
|
|
'report_status' => ReportStatus::class,
|
|
'received_at' => 'datetime',
|
|
'reported_at' => 'datetime',
|
|
'assigned_at' => 'datetime',
|
|
'study_date' => 'datetime',
|
|
];
|
|
}
|
|
|
|
public function details(): HasOne
|
|
{
|
|
return $this->hasOne(StudyDetails::class);
|
|
}
|
|
|
|
public function attachments(): HasMany
|
|
{
|
|
return $this->hasMany(StudyAttachment::class);
|
|
}
|
|
|
|
public function reports(): HasMany
|
|
{
|
|
return $this->hasMany(StudyReport::class);
|
|
}
|
|
|
|
public function shares(): HasMany
|
|
{
|
|
return $this->hasMany(SharedStudy::class);
|
|
}
|
|
|
|
public function scopeActive($query)
|
|
{
|
|
return $query->where('is_archived', false);
|
|
}
|
|
|
|
public function scopeArchived($query)
|
|
{
|
|
return $query->where('is_archived', true);
|
|
}
|
|
|
|
public function scopeUnlocked($query)
|
|
{
|
|
return $query->where('is_locked', false);
|
|
}
|
|
|
|
public function getHistoryLink(): string
|
|
{
|
|
$user = auth()->user();
|
|
if ($user->may(Permission::StudyHistoryEdit)) {
|
|
return route('staff.history.edit', $this->hash);
|
|
}
|
|
|
|
if ($user->may(Permission::StudyHistoryView)) {
|
|
return route('staff.history.view', $this->hash);
|
|
}
|
|
|
|
return '#';
|
|
}
|
|
|
|
public function getMetadataLink(): string
|
|
{
|
|
$user = auth()->user();
|
|
|
|
if ($user->may(Permission::StudyMetadataEdit)) {
|
|
return route('staff.meta.edit', $this->hash);
|
|
}
|
|
|
|
if ($user->may(Permission::StudyMetadataView)) {
|
|
return route('staff.meta.view', $this->hash);
|
|
}
|
|
|
|
return '#';
|
|
}
|
|
}
|