File: D:/HostingSpaces/SBogers10/liempde.ehbo.today/app/KommaApp/Courses/Models/Course.php
<?php
namespace App\KommaApp\Courses\Models;
use App\KommaApp\Competences\Models\Competence;
use App\KommaApp\DisplayNameTrait;
use App\KommaApp\Documents\Kms\DocumentableInterface;
use App\KommaApp\Documents\Models\Document;
use App\KommaApp\Images\Models\Image;
use App\KommaApp\Kms\Core\AbstractTranslatableModel;
use App\KommaApp\Users\Models\User;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
/**
* App\KommaApp\Courses\Models\Course
*
* @property int $id
* @property int $active
* @property string $date
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Competences\Models\Competence[] $competences
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Documents\Models\Document[] $documents
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Images\Models\Image[] $images
* @property-read \App\KommaApp\Courses\Models\CourseTranslation $translation
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Courses\Models\CourseTranslation[] $translations
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Courses\Models\Course whereActive($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Courses\Models\Course whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Courses\Models\Course whereDate($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Courses\Models\Course whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Courses\Models\Course whereUpdatedAt($value)
* @mixin \Eloquent
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Users\Models\User[] $users
*/
class Course extends AbstractTranslatableModel implements DocumentableInterface {
protected $kmsClass = Course::class;
public const FilterAll = 'all';
public const FilterRecommended = 'recommended';
public const FilterSubscribed = 'subscribed';
public const FilterCompleted = 'completed';
public const UserStatusSubscribed = 1;
public const UserStatusPresent = 2;
/*
* Transient properties on Eloquent models
* These are not saved to database.
*/
public $thumbnail = false;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'Courses';
protected $fillable = ['name', 'date'];
/**
* Get the images from the current Course
*
* @return \Illuminate\Database\Eloquent\Relations\hasMany
*/
public function images()
{
/**
* On the Image model is an MorphTo relation
* By using a hasMany relation:
* where the imageable_type is filled in with the KmsClass
* And the imageable_id is set as the foreign_id,
* we can collect the images of the given model directly.
*
*/
return $this->hasMany(Image::class, 'imageable_id')
->where('imageable_type', '=', $this->kmsClass);
}
public function __get($key)
{
if($key == 'title' && $this->exists) {
return $this->translations->first()->name .'<br/><sub>'.\Carbon\Carbon::parse($this->date)->format('d-m-Y').'</sub>';
}
return parent::__get($key); // TODO: Change the autogenerated stub
}
/**
* Gets the translation models for this model
*
* @return HasMany that resolves to AbstractTranslationModel instances
*/
public function translations(): HasMany
{
return $this->hasMany(CourseTranslation::class);
}
public function documents(): MorphMany
{
return $this->MorphMany(Document::class, 'documentable');
}
public function competences(): BelongsToMany
{
return $this->BelongsToMany(Competence::class);
}
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class);
}
public function hasSubscriptionMaximum()
{
return $this->attributes['max_subscriptions'] !== null && $this->attributes['max_subscriptions'] !== 0;
}
/**
* Checks if, by default, 80% of the courses max amount of subscriptions is reached.
* Returns true if so. false if not. You can modify the percentage by modifying the
* $thresholdPercentage variable.
*
* @param int $thresholdPercentage
* @return bool
*/
public function subscriptionPercentageReached($thresholdPercentage = 80): bool
{
return ($this->hasSubscriptionMaximum()) ? $this->attributes['max_subscriptions'] > $this->attributes['max_subscriptions'] * $thresholdPercentage / 10 : false;
}
/**
* Returns true if no users may subscribe anymore for the course, since all "seats" are occupied
*
* @return bool
*/
public function maxSubscriptionsReached(): bool
{
return $this->hasSubscriptionMaximum() ? $this->users()->count() >= $this->attributes['max_subscriptions']: false;
}
/**
* Gets the text that displays the maximum amount of subscriptions and current amount of subscriptions.
* Example for a situation in which 4 of the max 10 users subscribed: 4 / 10
*/
public function getSubscriptionCounterText(): string
{
return $this->users()->count().' / '.(!empty($this->attributes['max_subscriptions']) ? $this->attributes['max_subscriptions'] : __('kms/courses.unlimited'));
}
}