94 خطوط
2.7 KiB
PHP
94 خطوط
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Courses\Domain;
|
|
|
|
use App\Models\User;
|
|
use App\Modules\Assessments\Domain\Assessment;
|
|
use App\Modules\Courses\Domain\Enums\CourseVersionStatus;
|
|
use DomainException;
|
|
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class CourseVersion extends Model
|
|
{
|
|
use HasUlids;
|
|
|
|
protected $fillable = [
|
|
'organization_id', 'course_id', 'source_version_id', 'created_by', 'published_by', 'version_number', 'status',
|
|
'title', 'description', 'settings', 'completion_rules', 'taxonomy_snapshot', 'published_at',
|
|
'review_submitted_at', 'scheduled_publish_at', 'scheduled_unpublish_at', 'unpublished_at',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'status' => CourseVersionStatus::class,
|
|
'settings' => 'array',
|
|
'completion_rules' => 'array',
|
|
'taxonomy_snapshot' => 'array',
|
|
'published_at' => 'immutable_datetime',
|
|
'review_submitted_at' => 'immutable_datetime',
|
|
'scheduled_publish_at' => 'immutable_datetime',
|
|
'scheduled_unpublish_at' => 'immutable_datetime',
|
|
'unpublished_at' => 'immutable_datetime',
|
|
];
|
|
}
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::updating(function (self $version) {
|
|
if ($version->getOriginal('status') === CourseVersionStatus::Published->value) {
|
|
throw new DomainException('Published course versions are immutable.');
|
|
}
|
|
});
|
|
|
|
static::deleting(function (self $version) {
|
|
if ($version->status === CourseVersionStatus::Published) {
|
|
throw new DomainException('Published course versions cannot be deleted.');
|
|
}
|
|
});
|
|
}
|
|
|
|
public function course(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Course::class);
|
|
}
|
|
|
|
public function modules(): HasMany
|
|
{
|
|
return $this->hasMany(CourseModule::class);
|
|
}
|
|
|
|
public function lessons(): HasMany
|
|
{
|
|
return $this->hasMany(Lesson::class);
|
|
}
|
|
|
|
public function blocks(): HasMany
|
|
{
|
|
return $this->hasMany(Block::class);
|
|
}
|
|
|
|
public function assessments(): HasMany
|
|
{
|
|
return $this->hasMany(Assessment::class);
|
|
}
|
|
|
|
public function sourceVersion(): BelongsTo
|
|
{
|
|
return $this->belongsTo(self::class, 'source_version_id');
|
|
}
|
|
|
|
public function creator(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'created_by');
|
|
}
|
|
|
|
public function publisher(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'published_by');
|
|
}
|
|
}
|