43 lines
1.5 KiB
PHP
43 lines
1.5 KiB
PHP
<?php
|
||
namespace App\Models;
|
||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
use Illuminate\Database\Eloquent\Model;
|
||
|
||
class Course extends Model
|
||
{
|
||
use HasFactory;
|
||
|
||
protected $fillable = ['title', 'description'];
|
||
|
||
public function exercises()
|
||
{
|
||
return $this->belongsToMany(Exercise::class, 'course_exercise');
|
||
}
|
||
public function patients()
|
||
{
|
||
return $this->belongsToMany(User::class, 'course_patient'); // Связь с пациентами через промежуточную таблицу
|
||
}
|
||
public function users()
|
||
{
|
||
return $this->belongsToMany(User::class)->withTimestamps();
|
||
}
|
||
public function isCompleted($userId)
|
||
{
|
||
// Получаем все упражнения, связанные с курсом
|
||
$exercises = $this->exercises;
|
||
|
||
// Проверяем, завершены ли все упражнения для данного пользователя
|
||
foreach ($exercises as $exercise) {
|
||
// Проверка завершенности упражнения для пользователя через связь с таблицей pivot
|
||
$completed = $exercise->users()->wherePivot('user_id', $userId)->wherePivot('completed', true)->exists();
|
||
|
||
if (!$completed) {
|
||
return false; // Если хотя бы одно упражнение не завершено
|
||
}
|
||
}
|
||
|
||
return true; // Все упражнения завершены
|
||
}
|
||
}
|