Rehap.app/Course.php

43 lines
1.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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; // Все упражнения завершены
}
}