| Total Complexity | 170 |
| Total Lines | 1377 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| Bugs | 2 | Features | 0 |
Complex classes like Session often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Session, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | #[ApiResource( |
||
| 36 | operations: [ |
||
| 37 | new Get( |
||
| 38 | normalizationContext: [ |
||
| 39 | 'groups' => ['session:read', 'session:item:read'], |
||
| 40 | ], |
||
| 41 | security: "is_granted('ROLE_ADMIN') or is_granted('VIEW', object)" |
||
| 42 | ), |
||
| 43 | new Put(security: "is_granted('ROLE_ADMIN')"), |
||
| 44 | new GetCollection(security: "is_granted('ROLE_ADMIN')"), |
||
| 45 | new GetCollection( |
||
| 46 | uriTemplate: '/users/{id}/session_subscriptions/past.{_format}', |
||
| 47 | uriVariables: [ |
||
| 48 | 'id' => new Link( |
||
| 49 | fromClass: User::class, |
||
| 50 | ), |
||
| 51 | ], |
||
| 52 | paginationEnabled: false, |
||
| 53 | normalizationContext: [ |
||
| 54 | 'groups' => [ |
||
| 55 | 'user_subscriptions:sessions', |
||
| 56 | ], |
||
| 57 | ], |
||
| 58 | security: "is_granted('ROLE_USER')", |
||
| 59 | name: 'user_session_subscriptions_past', |
||
| 60 | provider: UserSessionSubscriptionsStateProvider::class, |
||
| 61 | ), |
||
| 62 | new GetCollection( |
||
| 63 | uriTemplate: '/users/{id}/session_subscriptions/current.{_format}', |
||
| 64 | uriVariables: [ |
||
| 65 | 'id' => new Link( |
||
| 66 | fromClass: User::class, |
||
| 67 | ), |
||
| 68 | ], |
||
| 69 | paginationEnabled: false, |
||
| 70 | normalizationContext: [ |
||
| 71 | 'groups' => [ |
||
| 72 | 'user_subscriptions:sessions', |
||
| 73 | ], |
||
| 74 | ], |
||
| 75 | security: "is_granted('ROLE_USER')", |
||
| 76 | name: 'user_session_subscriptions_current', |
||
| 77 | provider: UserSessionSubscriptionsStateProvider::class, |
||
| 78 | ), |
||
| 79 | new GetCollection( |
||
| 80 | uriTemplate: '/users/{id}/session_subscriptions/upcoming.{_format}', |
||
| 81 | uriVariables: [ |
||
| 82 | 'id' => new Link( |
||
| 83 | fromClass: User::class, |
||
| 84 | ), |
||
| 85 | ], |
||
| 86 | paginationEnabled: false, |
||
| 87 | normalizationContext: [ |
||
| 88 | 'groups' => [ |
||
| 89 | 'user_subscriptions:sessions', |
||
| 90 | ], |
||
| 91 | ], |
||
| 92 | security: "is_granted('ROLE_USER')", |
||
| 93 | name: 'user_session_subscriptions_upcoming', |
||
| 94 | provider: UserSessionSubscriptionsStateProvider::class, |
||
| 95 | ), |
||
| 96 | new Post(security: "is_granted('ROLE_ADMIN')"), |
||
| 97 | new Delete(security: "is_granted('DELETE', object)"), |
||
| 98 | ], |
||
| 99 | normalizationContext: ['groups' => ['session:read']], |
||
| 100 | denormalizationContext: ['groups' => ['session:write']], |
||
| 101 | security: "is_granted('ROLE_ADMIN')" |
||
| 102 | )] |
||
| 103 | #[ORM\Table(name: 'session')] |
||
| 104 | #[ORM\UniqueConstraint(name: 'title', columns: ['title'])] |
||
| 105 | #[ORM\EntityListeners([SessionListener::class])] |
||
| 106 | #[ORM\Entity(repositoryClass: SessionRepository::class)] |
||
| 107 | #[UniqueEntity('title')] |
||
| 108 | #[ApiFilter(filterClass: SearchFilter::class, properties: ['title' => 'partial'])] |
||
| 109 | #[ApiFilter(filterClass: PropertyFilter::class)] |
||
| 110 | #[ApiFilter(filterClass: OrderFilter::class, properties: ['id', 'title'])] |
||
| 111 | class Session implements ResourceWithAccessUrlInterface, Stringable |
||
| 112 | { |
||
| 113 | public const READ_ONLY = 1; |
||
| 114 | public const VISIBLE = 2; |
||
| 115 | public const INVISIBLE = 3; |
||
| 116 | public const AVAILABLE = 4; |
||
| 117 | public const LIST_ONLY = 5; |
||
| 118 | |||
| 119 | public const STUDENT = 0; |
||
| 120 | public const DRH = 1; |
||
| 121 | public const COURSE_COACH = 2; |
||
| 122 | public const GENERAL_COACH = 3; |
||
| 123 | public const SESSION_ADMIN = 4; |
||
| 124 | |||
| 125 | #[Groups([ |
||
| 126 | 'session:read', |
||
| 127 | 'session_rel_user:read', |
||
| 128 | 'session_rel_course_rel_user:read', |
||
| 129 | 'course:read', |
||
| 130 | 'track_e_exercise:read', |
||
| 131 | 'user_subscriptions:sessions', |
||
| 132 | ])] |
||
| 133 | #[ORM\Column(name: 'id', type: 'integer')] |
||
| 134 | #[ORM\Id] |
||
| 135 | #[ORM\GeneratedValue] |
||
| 136 | protected ?int $id = null; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var Collection<int, SessionRelCourse> |
||
| 140 | */ |
||
| 141 | #[Groups([ |
||
| 142 | 'session:read', |
||
| 143 | 'session_rel_user:read', |
||
| 144 | 'session_rel_course_rel_user:read', |
||
| 145 | 'user_subscriptions:sessions', |
||
| 146 | ])] |
||
| 147 | #[ORM\OrderBy(['position' => 'ASC'])] |
||
| 148 | #[ORM\OneToMany( |
||
| 149 | mappedBy: 'session', |
||
| 150 | targetEntity: SessionRelCourse::class, |
||
| 151 | cascade: ['persist'], |
||
| 152 | orphanRemoval: true |
||
| 153 | )] |
||
| 154 | protected Collection $courses; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @var Collection<int, SessionRelUser> |
||
| 158 | */ |
||
| 159 | #[Groups([ |
||
| 160 | 'session:read', |
||
| 161 | ])] |
||
| 162 | #[ORM\OneToMany( |
||
| 163 | mappedBy: 'session', |
||
| 164 | targetEntity: SessionRelUser::class, |
||
| 165 | cascade: ['persist', 'remove'], |
||
| 166 | orphanRemoval: true |
||
| 167 | )] |
||
| 168 | protected Collection $users; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @var Collection<int, SessionRelCourseRelUser> |
||
| 172 | */ |
||
| 173 | #[Groups([ |
||
| 174 | 'session:read', |
||
| 175 | 'session_rel_course_rel_user:read', |
||
| 176 | ])] |
||
| 177 | #[ORM\OneToMany( |
||
| 178 | mappedBy: 'session', |
||
| 179 | targetEntity: SessionRelCourseRelUser::class, |
||
| 180 | cascade: ['persist'], |
||
| 181 | orphanRemoval: true |
||
| 182 | )] |
||
| 183 | protected Collection $sessionRelCourseRelUsers; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @var Collection<int, SkillRelCourse> |
||
| 187 | */ |
||
| 188 | #[ORM\OneToMany( |
||
| 189 | mappedBy: 'session', |
||
| 190 | targetEntity: SkillRelCourse::class, |
||
| 191 | cascade: ['persist', 'remove'] |
||
| 192 | )] |
||
| 193 | protected Collection $skills; |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @var Collection<int, SkillRelUser> |
||
| 197 | */ |
||
| 198 | #[ORM\OneToMany(mappedBy: 'session', targetEntity: SkillRelUser::class, cascade: ['persist'])] |
||
| 199 | protected Collection $issuedSkills; |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @var Collection<int, EntityAccessUrlInterface> |
||
| 203 | */ |
||
| 204 | #[ORM\OneToMany( |
||
| 205 | mappedBy: 'session', |
||
| 206 | targetEntity: AccessUrlRelSession::class, |
||
| 207 | cascade: ['persist'], |
||
| 208 | orphanRemoval: true |
||
| 209 | )] |
||
| 210 | protected Collection $urls; |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @var Collection<int, ResourceLink> |
||
| 214 | */ |
||
| 215 | #[ORM\OneToMany(mappedBy: 'session', targetEntity: ResourceLink::class, cascade: ['remove'], orphanRemoval: true)] |
||
| 216 | protected Collection $resourceLinks; |
||
| 217 | |||
| 218 | protected AccessUrl $currentUrl; |
||
| 219 | |||
| 220 | protected ?Course $currentCourse = null; |
||
| 221 | |||
| 222 | #[Assert\NotBlank] |
||
| 223 | #[Groups([ |
||
| 224 | 'session:read', |
||
| 225 | 'session:write', |
||
| 226 | 'session_rel_course_rel_user:read', |
||
| 227 | 'document:read', |
||
| 228 | 'session_rel_user:read', |
||
| 229 | 'course:read', |
||
| 230 | 'track_e_exercise:read', |
||
| 231 | 'calendar_event:read', |
||
| 232 | 'user_subscriptions:sessions', |
||
| 233 | ])] |
||
| 234 | #[ORM\Column(name: 'title', type: 'string', length: 150)] |
||
| 235 | protected string $title; |
||
| 236 | |||
| 237 | #[Groups([ |
||
| 238 | 'session:read', |
||
| 239 | 'session:write', |
||
| 240 | ])] |
||
| 241 | #[ORM\Column(name: 'description', type: 'text', unique: false, nullable: true)] |
||
| 242 | protected ?string $description; |
||
| 243 | |||
| 244 | #[Groups([ |
||
| 245 | 'session:read', |
||
| 246 | 'session:write', |
||
| 247 | ])] |
||
| 248 | #[ORM\Column(name: 'show_description', type: 'boolean', nullable: true)] |
||
| 249 | protected ?bool $showDescription; |
||
| 250 | |||
| 251 | #[Groups(['session:read', 'session:write', 'user_subscriptions:sessions'])] |
||
| 252 | #[ORM\Column(name: 'duration', type: 'integer', nullable: true)] |
||
| 253 | protected ?int $duration = null; |
||
| 254 | |||
| 255 | #[Groups(['session:read'])] |
||
| 256 | #[ORM\Column(name: 'nbr_courses', type: 'integer', unique: false, nullable: false)] |
||
| 257 | protected int $nbrCourses; |
||
| 258 | |||
| 259 | #[Groups(['session:read'])] |
||
| 260 | #[ORM\Column(name: 'nbr_users', type: 'integer', unique: false, nullable: false)] |
||
| 261 | protected int $nbrUsers; |
||
| 262 | |||
| 263 | #[Groups(['session:read'])] |
||
| 264 | #[ORM\Column(name: 'nbr_classes', type: 'integer', unique: false, nullable: false)] |
||
| 265 | protected int $nbrClasses; |
||
| 266 | |||
| 267 | #[Groups([ |
||
| 268 | 'session:read', |
||
| 269 | 'session:write', |
||
| 270 | ])] |
||
| 271 | #[ORM\Column(name: 'visibility', type: 'integer')] |
||
| 272 | protected int $visibility; |
||
| 273 | |||
| 274 | #[ORM\ManyToOne(targetEntity: Promotion::class, cascade: ['persist'], inversedBy: 'sessions')] |
||
| 275 | #[ORM\JoinColumn(name: 'promotion_id', referencedColumnName: 'id', onDelete: 'CASCADE')] |
||
| 276 | protected ?Promotion $promotion = null; |
||
| 277 | |||
| 278 | #[Groups([ |
||
| 279 | 'session:read', |
||
| 280 | 'session:write', |
||
| 281 | 'session_rel_user:read', |
||
| 282 | 'session_rel_course_rel_user:read', |
||
| 283 | 'user_subscriptions:sessions', |
||
| 284 | ])] |
||
| 285 | #[ORM\Column(name: 'display_start_date', type: 'datetime', unique: false, nullable: true)] |
||
| 286 | protected ?DateTime $displayStartDate; |
||
| 287 | |||
| 288 | #[Groups([ |
||
| 289 | 'session:read', |
||
| 290 | 'session:write', |
||
| 291 | 'session_rel_user:read', |
||
| 292 | 'session_rel_course_rel_user:read', |
||
| 293 | 'user_subscriptions:sessions', |
||
| 294 | ])] |
||
| 295 | #[ORM\Column(name: 'display_end_date', type: 'datetime', unique: false, nullable: true)] |
||
| 296 | protected ?DateTime $displayEndDate; |
||
| 297 | |||
| 298 | #[Groups([ |
||
| 299 | 'session:read', |
||
| 300 | 'session:write', |
||
| 301 | 'session_rel_user:read', |
||
| 302 | 'session_rel_course_rel_user:read', |
||
| 303 | ])] |
||
| 304 | #[ORM\Column(name: 'access_start_date', type: 'datetime', unique: false, nullable: true)] |
||
| 305 | protected ?DateTime $accessStartDate; |
||
| 306 | |||
| 307 | #[Groups([ |
||
| 308 | 'session:read', |
||
| 309 | 'session:write', |
||
| 310 | 'session_rel_user:read', |
||
| 311 | 'session_rel_course_rel_user:read', |
||
| 312 | ])] |
||
| 313 | #[ORM\Column(name: 'access_end_date', type: 'datetime', unique: false, nullable: true)] |
||
| 314 | protected ?DateTime $accessEndDate; |
||
| 315 | |||
| 316 | #[Groups([ |
||
| 317 | 'session:read', |
||
| 318 | 'session:write', |
||
| 319 | 'session_rel_user:read', |
||
| 320 | 'session_rel_course_rel_user:read', |
||
| 321 | ])] |
||
| 322 | #[ORM\Column(name: 'coach_access_start_date', type: 'datetime', unique: false, nullable: true)] |
||
| 323 | protected ?DateTime $coachAccessStartDate; |
||
| 324 | |||
| 325 | #[Groups([ |
||
| 326 | 'session:read', |
||
| 327 | 'session:write', |
||
| 328 | 'session_rel_user:read', |
||
| 329 | 'session_rel_course_rel_user:read', |
||
| 330 | ])] |
||
| 331 | #[ORM\Column(name: 'coach_access_end_date', type: 'datetime', unique: false, nullable: true)] |
||
| 332 | protected ?DateTime $coachAccessEndDate; |
||
| 333 | |||
| 334 | #[ORM\Column(name: 'position', type: 'integer', nullable: false, options: ['default' => 0])] |
||
| 335 | protected int $position; |
||
| 336 | |||
| 337 | #[Groups(['session:read'])] |
||
| 338 | #[ORM\Column(name: 'status', type: 'integer', nullable: false)] |
||
| 339 | protected int $status; |
||
| 340 | |||
| 341 | #[Groups(['session:read', 'session:write', 'session_rel_user:read', 'user_subscriptions:sessions'])] |
||
| 342 | #[ORM\ManyToOne(targetEntity: SessionCategory::class, inversedBy: 'sessions')] |
||
| 343 | #[ORM\JoinColumn(name: 'session_category_id', referencedColumnName: 'id')] |
||
| 344 | protected ?SessionCategory $category = null; |
||
| 345 | |||
| 346 | #[ORM\Column( |
||
| 347 | name: 'send_subscription_notification', |
||
| 348 | type: 'boolean', |
||
| 349 | nullable: false, |
||
| 350 | options: ['default' => false] |
||
| 351 | )] |
||
| 352 | protected bool $sendSubscriptionNotification; |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Image illustrating the session (was extra field 'image' in 1.11). |
||
| 356 | */ |
||
| 357 | #[Groups(['user_subscriptions:sessions'])] |
||
| 358 | #[ORM\ManyToOne(targetEntity: Asset::class, cascade: ['remove'])] |
||
| 359 | #[ORM\JoinColumn(name: 'image_id', referencedColumnName: 'id', onDelete: 'SET NULL')] |
||
| 360 | protected ?Asset $image = null; |
||
| 361 | |||
| 362 | #[Groups(['user_subscriptions:sessions', 'session:read', 'session:item:read'])] |
||
| 363 | private int $accessVisibility = 0; |
||
| 364 | |||
| 365 | public function __construct() |
||
| 366 | { |
||
| 367 | $this->skills = new ArrayCollection(); |
||
| 368 | $this->issuedSkills = new ArrayCollection(); |
||
| 369 | $this->resourceLinks = new ArrayCollection(); |
||
| 370 | $this->courses = new ArrayCollection(); |
||
| 371 | $this->users = new ArrayCollection(); |
||
| 372 | $this->sessionRelCourseRelUsers = new ArrayCollection(); |
||
| 373 | $this->urls = new ArrayCollection(); |
||
| 374 | $this->duration = 0; |
||
| 375 | $this->description = ''; |
||
| 376 | $this->nbrClasses = 0; |
||
| 377 | $this->nbrUsers = 0; |
||
| 378 | $this->nbrCourses = 0; |
||
| 379 | $this->sendSubscriptionNotification = false; |
||
| 380 | $now = new DateTime(); |
||
| 381 | $this->displayStartDate = $now; |
||
| 382 | $this->displayEndDate = $now; |
||
| 383 | $this->accessStartDate = $now; |
||
| 384 | $this->accessEndDate = $now; |
||
| 385 | $this->coachAccessStartDate = $now; |
||
| 386 | $this->coachAccessEndDate = $now; |
||
| 387 | $this->visibility = 1; |
||
| 388 | $this->showDescription = false; |
||
| 389 | $this->category = null; |
||
| 390 | $this->status = 0; |
||
| 391 | $this->position = 0; |
||
| 392 | } |
||
| 393 | |||
| 394 | public function __toString(): string |
||
| 395 | { |
||
| 396 | return $this->getTitle(); |
||
| 397 | } |
||
| 398 | |||
| 399 | public static function getRelationTypeList(): array |
||
| 400 | { |
||
| 401 | return [self::STUDENT, self::DRH, self::COURSE_COACH, self::GENERAL_COACH, self::SESSION_ADMIN]; |
||
| 402 | } |
||
| 403 | |||
| 404 | public static function getStatusList(): array |
||
| 405 | { |
||
| 406 | return [ |
||
| 407 | self::READ_ONLY => 'status_read_only', |
||
| 408 | self::VISIBLE => 'status_visible', |
||
| 409 | self::INVISIBLE => 'status_invisible', |
||
| 410 | self::AVAILABLE => 'status_available', |
||
| 411 | ]; |
||
| 412 | } |
||
| 413 | |||
| 414 | public function getDuration(): ?int |
||
| 415 | { |
||
| 416 | return $this->duration; |
||
| 417 | } |
||
| 418 | |||
| 419 | public function setDuration(int $duration): self |
||
| 420 | { |
||
| 421 | $this->duration = $duration; |
||
| 422 | |||
| 423 | return $this; |
||
| 424 | } |
||
| 425 | |||
| 426 | public function getShowDescription(): bool |
||
| 427 | { |
||
| 428 | return $this->showDescription; |
||
|
|
|||
| 429 | } |
||
| 430 | |||
| 431 | public function setShowDescription(bool $showDescription): self |
||
| 432 | { |
||
| 433 | $this->showDescription = $showDescription; |
||
| 434 | |||
| 435 | return $this; |
||
| 436 | } |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @return Collection<int, SessionRelUser> |
||
| 440 | */ |
||
| 441 | public function getUsers(): Collection |
||
| 442 | { |
||
| 443 | return $this->users; |
||
| 444 | } |
||
| 445 | |||
| 446 | public function setUsers(Collection $users): self |
||
| 447 | { |
||
| 448 | $this->users = new ArrayCollection(); |
||
| 449 | foreach ($users as $user) { |
||
| 450 | $this->addUserSubscription($user); |
||
| 451 | } |
||
| 452 | |||
| 453 | return $this; |
||
| 454 | } |
||
| 455 | |||
| 456 | public function addUserSubscription(SessionRelUser $subscription): void |
||
| 457 | { |
||
| 458 | $subscription->setSession($this); |
||
| 459 | if (!$this->hasUser($subscription)) { |
||
| 460 | $this->users->add($subscription); |
||
| 461 | $this->nbrUsers++; |
||
| 462 | } |
||
| 463 | } |
||
| 464 | |||
| 465 | public function hasUser(SessionRelUser $subscription): bool |
||
| 466 | { |
||
| 467 | if (0 !== $this->getUsers()->count()) { |
||
| 468 | $criteria = Criteria::create() |
||
| 469 | ->where( |
||
| 470 | Criteria::expr()->eq('user', $subscription->getUser()) |
||
| 471 | ) |
||
| 472 | ->andWhere( |
||
| 473 | Criteria::expr()->eq('session', $subscription->getSession()) |
||
| 474 | ) |
||
| 475 | ->andWhere( |
||
| 476 | Criteria::expr()->eq('relationType', $subscription->getRelationType()) |
||
| 477 | ) |
||
| 478 | ; |
||
| 479 | $relation = $this->getUsers()->matching($criteria); |
||
| 480 | |||
| 481 | return $relation->count() > 0; |
||
| 482 | } |
||
| 483 | |||
| 484 | return false; |
||
| 485 | } |
||
| 486 | |||
| 487 | public function hasCourse(Course $course): bool |
||
| 488 | { |
||
| 489 | if (0 !== $this->getCourses()->count()) { |
||
| 490 | $criteria = Criteria::create()->where(Criteria::expr()->eq('course', $course)); |
||
| 491 | $relation = $this->getCourses()->matching($criteria); |
||
| 492 | |||
| 493 | return $relation->count() > 0; |
||
| 494 | } |
||
| 495 | |||
| 496 | return false; |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @return Collection<int, SessionRelCourse> |
||
| 501 | */ |
||
| 502 | public function getCourses(): Collection |
||
| 503 | { |
||
| 504 | return $this->courses; |
||
| 505 | } |
||
| 506 | |||
| 507 | public function setCourses(ArrayCollection $courses): void |
||
| 508 | { |
||
| 509 | $this->courses = new ArrayCollection(); |
||
| 510 | foreach ($courses as $course) { |
||
| 511 | $this->addCourses($course); |
||
| 512 | } |
||
| 513 | } |
||
| 514 | |||
| 515 | public function addCourses(SessionRelCourse $course): void |
||
| 516 | { |
||
| 517 | $course->setSession($this); |
||
| 518 | $this->courses->add($course); |
||
| 519 | } |
||
| 520 | |||
| 521 | public function removeCourses(SessionRelCourse $course): void |
||
| 522 | { |
||
| 523 | foreach ($this->courses as $key => $value) { |
||
| 524 | if ($value->getId() === $course->getId()) { |
||
| 525 | unset($this->courses[$key]); |
||
| 526 | } |
||
| 527 | } |
||
| 528 | } |
||
| 529 | |||
| 530 | public function getId(): ?int |
||
| 531 | { |
||
| 532 | return $this->id; |
||
| 533 | } |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Remove course subscription for a user. |
||
| 537 | * If user status in session is student, then decrease number of course users. |
||
| 538 | */ |
||
| 539 | public function removeUserCourseSubscription(User $user, Course $course): void |
||
| 540 | { |
||
| 541 | foreach ($this->sessionRelCourseRelUsers as $i => $sessionRelUser) { |
||
| 542 | if ($sessionRelUser->getCourse()->getId() === $course->getId() |
||
| 543 | && $sessionRelUser->getUser()->getId() === $user->getId() |
||
| 544 | ) { |
||
| 545 | if (self::STUDENT === $this->sessionRelCourseRelUsers[$i]->getStatus()) { |
||
| 546 | $sessionCourse = $this->getCourseSubscription($course); |
||
| 547 | $sessionCourse->setNbrUsers($sessionCourse->getNbrUsers() - 1); |
||
| 548 | } |
||
| 549 | |||
| 550 | unset($this->sessionRelCourseRelUsers[$i]); |
||
| 551 | } |
||
| 552 | } |
||
| 553 | } |
||
| 554 | |||
| 555 | public function getStatus(): int |
||
| 556 | { |
||
| 557 | return $this->status; |
||
| 558 | } |
||
| 559 | |||
| 560 | public function setStatus(int $status): self |
||
| 561 | { |
||
| 562 | $this->status = $status; |
||
| 563 | |||
| 564 | return $this; |
||
| 565 | } |
||
| 566 | |||
| 567 | public function setTitle(string $title): self |
||
| 568 | { |
||
| 569 | $this->title = $title; |
||
| 570 | |||
| 571 | return $this; |
||
| 572 | } |
||
| 573 | |||
| 574 | public function getCourseSubscription(Course $course): ?SessionRelCourse |
||
| 575 | { |
||
| 576 | $criteria = Criteria::create()->where(Criteria::expr()->eq('course', $course)); |
||
| 577 | |||
| 578 | return $this->courses->matching($criteria)->current(); |
||
| 579 | } |
||
| 580 | |||
| 581 | public function getTitle(): string |
||
| 582 | { |
||
| 583 | return $this->title; |
||
| 584 | } |
||
| 585 | |||
| 586 | public function getNbrUsers(): int |
||
| 587 | { |
||
| 588 | return $this->nbrUsers; |
||
| 589 | } |
||
| 590 | |||
| 591 | public function setNbrUsers(int $nbrUsers): self |
||
| 592 | { |
||
| 593 | $this->nbrUsers = $nbrUsers; |
||
| 594 | |||
| 595 | return $this; |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * @return Collection<int, SessionRelCourseRelUser> |
||
| 600 | */ |
||
| 601 | public function getAllUsersFromCourse(int $status): Collection |
||
| 602 | { |
||
| 603 | $criteria = Criteria::create()->where(Criteria::expr()->eq('status', $status)); |
||
| 604 | |||
| 605 | return $this->getSessionRelCourseRelUsers()->matching($criteria); |
||
| 606 | } |
||
| 607 | |||
| 608 | public function getSessionRelCourseRelUsers(): Collection |
||
| 609 | { |
||
| 610 | return $this->sessionRelCourseRelUsers; |
||
| 611 | } |
||
| 612 | |||
| 613 | public function setSessionRelCourseRelUsers(Collection $sessionRelCourseRelUsers): self |
||
| 614 | { |
||
| 615 | $this->sessionRelCourseRelUsers = new ArrayCollection(); |
||
| 616 | foreach ($sessionRelCourseRelUsers as $item) { |
||
| 617 | $this->addSessionRelCourseRelUser($item); |
||
| 618 | } |
||
| 619 | |||
| 620 | return $this; |
||
| 621 | } |
||
| 622 | |||
| 623 | public function addSessionRelCourseRelUser(SessionRelCourseRelUser $sessionRelCourseRelUser): void |
||
| 624 | { |
||
| 625 | $sessionRelCourseRelUser->setSession($this); |
||
| 626 | if (!$this->hasUserCourseSubscription($sessionRelCourseRelUser)) { |
||
| 627 | $this->sessionRelCourseRelUsers->add($sessionRelCourseRelUser); |
||
| 628 | } |
||
| 629 | } |
||
| 630 | |||
| 631 | public function hasUserCourseSubscription(SessionRelCourseRelUser $subscription): bool |
||
| 632 | { |
||
| 633 | if (0 !== $this->getSessionRelCourseRelUsers()->count()) { |
||
| 634 | $criteria = Criteria::create() |
||
| 635 | ->where( |
||
| 636 | Criteria::expr()->eq('user', $subscription->getUser()) |
||
| 637 | ) |
||
| 638 | ->andWhere( |
||
| 639 | Criteria::expr()->eq('course', $subscription->getCourse()) |
||
| 640 | ) |
||
| 641 | ->andWhere( |
||
| 642 | Criteria::expr()->eq('session', $subscription->getSession()) |
||
| 643 | ) |
||
| 644 | ; |
||
| 645 | $relation = $this->getSessionRelCourseRelUsers()->matching($criteria); |
||
| 646 | |||
| 647 | return $relation->count() > 0; |
||
| 648 | } |
||
| 649 | |||
| 650 | return false; |
||
| 651 | } |
||
| 652 | |||
| 653 | /** |
||
| 654 | * @return Collection<int, SessionRelCourseRelUser> |
||
| 655 | */ |
||
| 656 | public function getSessionRelCourseByUser(User $user, ?int $status = null): Collection |
||
| 657 | { |
||
| 658 | $criteria = Criteria::create()->where(Criteria::expr()->eq('user', $user)); |
||
| 659 | if (null !== $status) { |
||
| 660 | $criteria->andWhere(Criteria::expr()->eq('status', $status)); |
||
| 661 | } |
||
| 662 | |||
| 663 | return $this->sessionRelCourseRelUsers->matching($criteria); |
||
| 664 | } |
||
| 665 | |||
| 666 | public function getDescription(): ?string |
||
| 667 | { |
||
| 668 | return $this->description; |
||
| 669 | } |
||
| 670 | |||
| 671 | public function setDescription(string $description): self |
||
| 672 | { |
||
| 673 | $this->description = $description; |
||
| 674 | |||
| 675 | return $this; |
||
| 676 | } |
||
| 677 | |||
| 678 | public function getNbrCourses(): int |
||
| 679 | { |
||
| 680 | return $this->nbrCourses; |
||
| 681 | } |
||
| 682 | |||
| 683 | public function setNbrCourses(int $nbrCourses): self |
||
| 684 | { |
||
| 685 | $this->nbrCourses = $nbrCourses; |
||
| 686 | |||
| 687 | return $this; |
||
| 688 | } |
||
| 689 | |||
| 690 | public function getNbrClasses(): int |
||
| 691 | { |
||
| 692 | return $this->nbrClasses; |
||
| 693 | } |
||
| 694 | |||
| 695 | public function setNbrClasses(int $nbrClasses): self |
||
| 696 | { |
||
| 697 | $this->nbrClasses = $nbrClasses; |
||
| 698 | |||
| 699 | return $this; |
||
| 700 | } |
||
| 701 | |||
| 702 | public function getVisibility(): int |
||
| 705 | } |
||
| 706 | |||
| 707 | public function setVisibility(int $visibility): self |
||
| 708 | { |
||
| 709 | $this->visibility = $visibility; |
||
| 710 | |||
| 711 | return $this; |
||
| 712 | } |
||
| 713 | |||
| 714 | public function getPromotion(): ?Promotion |
||
| 715 | { |
||
| 716 | return $this->promotion; |
||
| 717 | } |
||
| 718 | |||
| 719 | public function setPromotion(?Promotion $promotion): self |
||
| 720 | { |
||
| 721 | $this->promotion = $promotion; |
||
| 722 | |||
| 723 | return $this; |
||
| 724 | } |
||
| 725 | |||
| 726 | public function getDisplayStartDate(): ?DateTime |
||
| 727 | { |
||
| 728 | return $this->displayStartDate; |
||
| 729 | } |
||
| 730 | |||
| 731 | public function setDisplayStartDate(?DateTime $displayStartDate): self |
||
| 732 | { |
||
| 733 | $this->displayStartDate = $displayStartDate; |
||
| 734 | |||
| 735 | return $this; |
||
| 736 | } |
||
| 737 | |||
| 738 | public function getDisplayEndDate(): ?DateTime |
||
| 739 | { |
||
| 740 | return $this->displayEndDate; |
||
| 741 | } |
||
| 742 | |||
| 743 | public function setDisplayEndDate(?DateTime $displayEndDate): self |
||
| 744 | { |
||
| 745 | $this->displayEndDate = $displayEndDate; |
||
| 746 | |||
| 747 | return $this; |
||
| 748 | } |
||
| 749 | |||
| 750 | public function getGeneralCoaches(): ReadableCollection |
||
| 751 | { |
||
| 752 | return $this->getGeneralCoachesSubscriptions() |
||
| 753 | ->map(fn (SessionRelUser $subscription) => $subscription->getUser()) |
||
| 754 | ; |
||
| 755 | } |
||
| 756 | |||
| 757 | #[Groups(['user_subscriptions:sessions'])] |
||
| 758 | public function getGeneralCoachesSubscriptions(): Collection |
||
| 759 | { |
||
| 760 | $criteria = Criteria::create()->where(Criteria::expr()->eq('relationType', self::GENERAL_COACH)); |
||
| 761 | |||
| 762 | return $this->users->matching($criteria); |
||
| 763 | } |
||
| 764 | |||
| 765 | public function hasUserAsGeneralCoach(User $user): bool |
||
| 766 | { |
||
| 767 | $criteria = Criteria::create() |
||
| 768 | ->where( |
||
| 769 | Criteria::expr()->eq('relationType', self::GENERAL_COACH) |
||
| 770 | ) |
||
| 771 | ->andWhere( |
||
| 772 | Criteria::expr()->eq('user', $user) |
||
| 773 | ) |
||
| 774 | ; |
||
| 775 | |||
| 776 | return $this->users->matching($criteria)->count() > 0; |
||
| 777 | } |
||
| 778 | |||
| 779 | public function addGeneralCoach(User $coach): self |
||
| 780 | { |
||
| 781 | return $this->addUserInSession(self::GENERAL_COACH, $coach); |
||
| 782 | } |
||
| 783 | |||
| 784 | public function addUserInSession(int $relationType, User $user): self |
||
| 785 | { |
||
| 786 | $sessionRelUser = (new SessionRelUser())->setUser($user)->setRelationType($relationType); |
||
| 787 | $this->addUserSubscription($sessionRelUser); |
||
| 788 | |||
| 789 | return $this; |
||
| 790 | } |
||
| 791 | |||
| 792 | public function removeGeneralCoach(User $user): self |
||
| 793 | { |
||
| 794 | $this->removeUserInSession(self::GENERAL_COACH, $user); |
||
| 795 | |||
| 796 | return $this; |
||
| 797 | } |
||
| 798 | |||
| 799 | public function removeUserInSession(int $relationType, User $user): self |
||
| 800 | { |
||
| 801 | $criteria = Criteria::create() |
||
| 802 | ->where( |
||
| 803 | Criteria::expr()->eq('relationType', $relationType) |
||
| 804 | ) |
||
| 805 | ->andWhere( |
||
| 806 | Criteria::expr()->eq('user', $user) |
||
| 807 | ) |
||
| 808 | ; |
||
| 809 | $subscriptions = $this->users->matching($criteria); |
||
| 810 | |||
| 811 | foreach ($subscriptions as $subscription) { |
||
| 812 | $this->removeUserSubscription($subscription); |
||
| 813 | } |
||
| 814 | |||
| 815 | return $this; |
||
| 816 | } |
||
| 817 | |||
| 818 | public function removeUserSubscription(SessionRelUser $subscription): self |
||
| 819 | { |
||
| 820 | if ($this->hasUser($subscription)) { |
||
| 821 | $subscription->setSession(null); |
||
| 822 | $this->users->removeElement($subscription); |
||
| 823 | $this->nbrUsers--; |
||
| 824 | } |
||
| 825 | |||
| 826 | return $this; |
||
| 827 | } |
||
| 828 | |||
| 829 | public function getCategory(): ?SessionCategory |
||
| 830 | { |
||
| 831 | return $this->category; |
||
| 832 | } |
||
| 833 | |||
| 834 | public function setCategory(?SessionCategory $category): self |
||
| 835 | { |
||
| 836 | $this->category = $category; |
||
| 837 | |||
| 838 | return $this; |
||
| 839 | } |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Check if session is visible. |
||
| 843 | */ |
||
| 844 | public function isActive(): bool |
||
| 845 | { |
||
| 846 | $now = new DateTime('now'); |
||
| 847 | |||
| 848 | return $now > $this->getAccessStartDate(); |
||
| 849 | } |
||
| 850 | |||
| 851 | public function getAccessStartDate(): ?DateTime |
||
| 852 | { |
||
| 853 | return $this->accessStartDate; |
||
| 854 | } |
||
| 855 | |||
| 856 | public function setAccessStartDate(?DateTime $accessStartDate): self |
||
| 857 | { |
||
| 858 | $this->accessStartDate = $accessStartDate; |
||
| 859 | |||
| 860 | return $this; |
||
| 861 | } |
||
| 862 | |||
| 863 | public function isActiveForStudent(): bool |
||
| 864 | { |
||
| 865 | $start = $this->getAccessStartDate(); |
||
| 866 | $end = $this->getAccessEndDate(); |
||
| 867 | |||
| 868 | return $this->compareDates($start, $end); |
||
| 869 | } |
||
| 870 | |||
| 871 | public function getAccessEndDate(): ?DateTime |
||
| 874 | } |
||
| 875 | |||
| 876 | public function setAccessEndDate(?DateTime $accessEndDate): self |
||
| 877 | { |
||
| 878 | $this->accessEndDate = $accessEndDate; |
||
| 879 | |||
| 880 | return $this; |
||
| 881 | } |
||
| 882 | |||
| 883 | public function isActiveForCoach(): bool |
||
| 884 | { |
||
| 885 | $start = $this->getCoachAccessStartDate(); |
||
| 886 | $end = $this->getCoachAccessEndDate(); |
||
| 887 | |||
| 888 | return $this->compareDates($start, $end); |
||
| 889 | } |
||
| 890 | |||
| 891 | public function getCoachAccessStartDate(): ?DateTime |
||
| 892 | { |
||
| 893 | return $this->coachAccessStartDate; |
||
| 894 | } |
||
| 895 | |||
| 896 | public function setCoachAccessStartDate(?DateTime $coachAccessStartDate): self |
||
| 897 | { |
||
| 898 | $this->coachAccessStartDate = $coachAccessStartDate; |
||
| 899 | |||
| 900 | return $this; |
||
| 901 | } |
||
| 902 | |||
| 903 | public function getCoachAccessEndDate(): ?DateTime |
||
| 904 | { |
||
| 905 | return $this->coachAccessEndDate; |
||
| 906 | } |
||
| 907 | |||
| 908 | public function setCoachAccessEndDate(?DateTime $coachAccessEndDate): self |
||
| 913 | } |
||
| 914 | |||
| 915 | /** |
||
| 916 | * Compare the current date with start and end access dates. |
||
| 917 | * Either missing date is interpreted as no limit. |
||
| 918 | * |
||
| 919 | * @return bool whether now is between the session access start and end dates |
||
| 920 | */ |
||
| 921 | public function isCurrentlyAccessible(): bool |
||
| 922 | { |
||
| 923 | $now = new DateTime(); |
||
| 924 | |||
| 925 | return (!$this->accessStartDate || $now >= $this->accessStartDate) && (!$this->accessEndDate || $now <= $this->accessEndDate); |
||
| 926 | } |
||
| 927 | |||
| 928 | public function addCourse(Course $course): self |
||
| 929 | { |
||
| 930 | $sessionRelCourse = (new SessionRelCourse())->setCourse($course); |
||
| 931 | $this->addCourses($sessionRelCourse); |
||
| 932 | |||
| 933 | return $this; |
||
| 934 | } |
||
| 935 | |||
| 936 | /** |
||
| 937 | * Removes a course from this session. |
||
| 938 | * |
||
| 939 | * @param Course $course the course to remove from this session |
||
| 940 | * |
||
| 941 | * @return bool whether the course was actually found in this session and removed from it |
||
| 942 | */ |
||
| 943 | public function removeCourse(Course $course): bool |
||
| 954 | } |
||
| 955 | |||
| 956 | /** |
||
| 957 | * Add a user course subscription. |
||
| 958 | * If user status in session is student, then increase number of course users. |
||
| 959 | * Status example: Session::STUDENT. |
||
| 960 | */ |
||
| 961 | public function addUserInCourse(int $status, User $user, Course $course): SessionRelCourseRelUser |
||
| 962 | { |
||
| 963 | $userRelCourseRelSession = (new SessionRelCourseRelUser()) |
||
| 964 | ->setCourse($course) |
||
| 965 | ->setUser($user) |
||
| 966 | ->setSession($this) |
||
| 967 | ->setStatus($status) |
||
| 968 | ; |
||
| 969 | |||
| 970 | $this->addSessionRelCourseRelUser($userRelCourseRelSession); |
||
| 971 | |||
| 972 | if (self::STUDENT === $status) { |
||
| 973 | $sessionCourse = $this->getCourseSubscription($course); |
||
| 974 | $sessionCourse->setNbrUsers($sessionCourse->getNbrUsers() + 1); |
||
| 975 | } |
||
| 976 | |||
| 977 | return $userRelCourseRelSession; |
||
| 978 | } |
||
| 979 | |||
| 980 | /** |
||
| 981 | * currentCourse is set in CidReqListener. |
||
| 982 | */ |
||
| 983 | public function getCurrentCourse(): ?Course |
||
| 984 | { |
||
| 985 | return $this->currentCourse; |
||
| 986 | } |
||
| 987 | |||
| 988 | /** |
||
| 989 | * currentCourse is set in CidReqListener. |
||
| 990 | */ |
||
| 991 | public function setCurrentCourse(Course $course): self |
||
| 992 | { |
||
| 993 | // If the session is registered in the course session list. |
||
| 994 | $exists = $this->getCourses() |
||
| 995 | ->exists( |
||
| 996 | fn ($key, $element) => $course->getId() === $element->getCourse()->getId() |
||
| 997 | ) |
||
| 998 | ; |
||
| 999 | |||
| 1000 | if ($exists) { |
||
| 1001 | $this->currentCourse = $course; |
||
| 1002 | } |
||
| 1003 | |||
| 1004 | return $this; |
||
| 1005 | } |
||
| 1006 | |||
| 1007 | public function getSendSubscriptionNotification(): bool |
||
| 1010 | } |
||
| 1011 | |||
| 1012 | public function setSendSubscriptionNotification(bool $sendNotification): self |
||
| 1013 | { |
||
| 1014 | $this->sendSubscriptionNotification = $sendNotification; |
||
| 1015 | |||
| 1016 | return $this; |
||
| 1017 | } |
||
| 1018 | |||
| 1019 | /** |
||
| 1020 | * Get user from course by status. |
||
| 1021 | */ |
||
| 1022 | public function getSessionRelCourseRelUsersByStatus(Course $course, int $status): Collection |
||
| 1023 | { |
||
| 1024 | $criteria = Criteria::create() |
||
| 1025 | ->where( |
||
| 1026 | Criteria::expr()->eq('course', $course) |
||
| 1027 | ) |
||
| 1028 | ->andWhere( |
||
| 1029 | Criteria::expr()->eq('status', $status) |
||
| 1030 | ) |
||
| 1031 | ; |
||
| 1032 | |||
| 1033 | return $this->sessionRelCourseRelUsers->matching($criteria); |
||
| 1034 | } |
||
| 1035 | |||
| 1036 | public function getSessionRelCourseRelUserInCourse(Course $course): Collection |
||
| 1037 | { |
||
| 1038 | $criteria = Criteria::create() |
||
| 1039 | ->where( |
||
| 1040 | Criteria::expr()->eq('course', $course) |
||
| 1041 | ) |
||
| 1042 | ; |
||
| 1043 | |||
| 1044 | return $this->sessionRelCourseRelUsers->matching($criteria); |
||
| 1045 | } |
||
| 1046 | |||
| 1047 | public function getIssuedSkills(): Collection |
||
| 1048 | { |
||
| 1049 | return $this->issuedSkills; |
||
| 1050 | } |
||
| 1051 | |||
| 1052 | public function getCurrentUrl(): AccessUrl |
||
| 1053 | { |
||
| 1054 | return $this->currentUrl; |
||
| 1055 | } |
||
| 1056 | |||
| 1057 | public function setCurrentUrl(AccessUrl $url): self |
||
| 1058 | { |
||
| 1059 | $urlList = $this->getUrls(); |
||
| 1060 | foreach ($urlList as $item) { |
||
| 1061 | if ($item->getUrl()->getId() === $url->getId()) { |
||
| 1062 | $this->currentUrl = $url; |
||
| 1063 | |||
| 1064 | break; |
||
| 1065 | } |
||
| 1066 | } |
||
| 1067 | |||
| 1068 | return $this; |
||
| 1069 | } |
||
| 1070 | |||
| 1071 | /** |
||
| 1072 | * @return Collection<int, EntityAccessUrlInterface> |
||
| 1073 | */ |
||
| 1074 | public function getUrls(): Collection |
||
| 1075 | { |
||
| 1076 | return $this->urls; |
||
| 1077 | } |
||
| 1078 | |||
| 1079 | public function setUrls(Collection $urls): self |
||
| 1080 | { |
||
| 1081 | $this->urls = new ArrayCollection(); |
||
| 1082 | foreach ($urls as $url) { |
||
| 1083 | $this->addUrls($url); |
||
| 1084 | } |
||
| 1085 | |||
| 1086 | return $this; |
||
| 1087 | } |
||
| 1088 | |||
| 1089 | public function addUrls(AccessUrlRelSession $url): self |
||
| 1090 | { |
||
| 1091 | $url->setSession($this); |
||
| 1092 | $this->urls->add($url); |
||
| 1093 | |||
| 1094 | return $this; |
||
| 1095 | } |
||
| 1096 | |||
| 1097 | public function addAccessUrl(?AccessUrl $url): self |
||
| 1098 | { |
||
| 1099 | $accessUrlRelSession = new AccessUrlRelSession(); |
||
| 1100 | $accessUrlRelSession->setUrl($url); |
||
| 1101 | $accessUrlRelSession->setSession($this); |
||
| 1102 | $this->addUrls($accessUrlRelSession); |
||
| 1103 | |||
| 1104 | return $this; |
||
| 1105 | } |
||
| 1106 | |||
| 1107 | public function getPosition(): int |
||
| 1108 | { |
||
| 1109 | return $this->position; |
||
| 1110 | } |
||
| 1111 | |||
| 1112 | public function setPosition(int $position): self |
||
| 1113 | { |
||
| 1114 | $this->position = $position; |
||
| 1115 | |||
| 1116 | return $this; |
||
| 1117 | } |
||
| 1118 | |||
| 1119 | public function getSessionAdmins(): ReadableCollection |
||
| 1120 | { |
||
| 1121 | return $this->getGeneralAdminsSubscriptions() |
||
| 1122 | ->map(fn (SessionRelUser $subscription) => $subscription->getUser()) |
||
| 1123 | ; |
||
| 1124 | } |
||
| 1125 | |||
| 1126 | public function getGeneralAdminsSubscriptions(): Collection |
||
| 1127 | { |
||
| 1128 | $criteria = Criteria::create()->where(Criteria::expr()->eq('relationType', self::SESSION_ADMIN)); |
||
| 1129 | |||
| 1130 | return $this->users->matching($criteria); |
||
| 1131 | } |
||
| 1132 | |||
| 1133 | public function hasUserAsSessionAdmin(User $user): bool |
||
| 1134 | { |
||
| 1135 | $criteria = Criteria::create() |
||
| 1136 | ->where( |
||
| 1137 | Criteria::expr()->eq('relationType', self::SESSION_ADMIN) |
||
| 1138 | ) |
||
| 1139 | ->andWhere( |
||
| 1140 | Criteria::expr()->eq('user', $user) |
||
| 1141 | ) |
||
| 1142 | ; |
||
| 1143 | |||
| 1144 | return $this->users->matching($criteria)->count() > 0; |
||
| 1145 | } |
||
| 1146 | |||
| 1147 | public function addSessionAdmin(User $sessionAdmin): self |
||
| 1148 | { |
||
| 1149 | return $this->addUserInSession(self::SESSION_ADMIN, $sessionAdmin); |
||
| 1150 | } |
||
| 1151 | |||
| 1152 | public function getSkills(): Collection |
||
| 1153 | { |
||
| 1154 | return $this->skills; |
||
| 1155 | } |
||
| 1156 | |||
| 1157 | public function getResourceLinks(): Collection |
||
| 1158 | { |
||
| 1159 | return $this->resourceLinks; |
||
| 1160 | } |
||
| 1161 | |||
| 1162 | public function getImage(): ?Asset |
||
| 1163 | { |
||
| 1164 | return $this->image; |
||
| 1165 | } |
||
| 1166 | |||
| 1167 | public function setImage(?Asset $asset): self |
||
| 1168 | { |
||
| 1169 | $this->image = $asset; |
||
| 1170 | |||
| 1171 | return $this; |
||
| 1172 | } |
||
| 1173 | |||
| 1174 | public function hasImage(): bool |
||
| 1175 | { |
||
| 1176 | return null !== $this->image; |
||
| 1177 | } |
||
| 1178 | |||
| 1179 | /** |
||
| 1180 | * Check if $user is course coach in any course. |
||
| 1181 | */ |
||
| 1182 | public function hasCoachInCourseList(User $user): bool |
||
| 1183 | { |
||
| 1184 | foreach ($this->courses as $sessionCourse) { |
||
| 1185 | if ($this->hasCourseCoachInCourse($user, $sessionCourse->getCourse())) { |
||
| 1186 | return true; |
||
| 1187 | } |
||
| 1188 | } |
||
| 1189 | |||
| 1190 | return false; |
||
| 1191 | } |
||
| 1192 | |||
| 1193 | public function hasCourseCoachInCourse(User $user, ?Course $course = null): bool |
||
| 1194 | { |
||
| 1195 | if (null === $course) { |
||
| 1196 | return false; |
||
| 1197 | } |
||
| 1198 | |||
| 1199 | return $this->hasUserInCourse($user, $course, self::COURSE_COACH); |
||
| 1200 | } |
||
| 1201 | |||
| 1202 | /** |
||
| 1203 | * @param int $status if not set it will check if the user is registered |
||
| 1204 | * with any status |
||
| 1205 | */ |
||
| 1206 | public function hasUserInCourse(User $user, Course $course, ?int $status = null): bool |
||
| 1207 | { |
||
| 1208 | $relation = $this->getUserInCourse($user, $course, $status); |
||
| 1209 | |||
| 1210 | return $relation->count() > 0; |
||
| 1211 | } |
||
| 1212 | |||
| 1213 | public function getUserInCourse(User $user, Course $course, ?int $status = null): Collection |
||
| 1214 | { |
||
| 1215 | $criteria = Criteria::create() |
||
| 1216 | ->where( |
||
| 1217 | Criteria::expr()->eq('course', $course) |
||
| 1218 | ) |
||
| 1219 | ->andWhere( |
||
| 1220 | Criteria::expr()->eq('user', $user) |
||
| 1221 | ) |
||
| 1222 | ; |
||
| 1223 | |||
| 1224 | if (null !== $status) { |
||
| 1225 | $criteria->andWhere(Criteria::expr()->eq('status', $status)); |
||
| 1226 | } |
||
| 1227 | |||
| 1228 | return $this->getSessionRelCourseRelUsers()->matching($criteria); |
||
| 1229 | } |
||
| 1230 | |||
| 1231 | /** |
||
| 1232 | * Check if $user is student in any course. |
||
| 1233 | */ |
||
| 1234 | public function hasStudentInCourseList(User $user): bool |
||
| 1235 | { |
||
| 1236 | foreach ($this->courses as $sessionCourse) { |
||
| 1237 | if ($this->hasStudentInCourse($user, $sessionCourse->getCourse())) { |
||
| 1238 | return true; |
||
| 1239 | } |
||
| 1240 | } |
||
| 1241 | |||
| 1242 | return false; |
||
| 1243 | } |
||
| 1244 | |||
| 1245 | public function hasStudentInCourse(User $user, Course $course): bool |
||
| 1246 | { |
||
| 1247 | return $this->hasUserInCourse($user, $course, self::STUDENT); |
||
| 1248 | } |
||
| 1249 | |||
| 1250 | protected function compareDates(?DateTime $start, ?DateTime $end = null): bool |
||
| 1251 | { |
||
| 1252 | $now = new DateTime('now'); |
||
| 1253 | |||
| 1254 | if (!empty($start) && !empty($end)) { |
||
| 1255 | return $now >= $start && $now <= $end; |
||
| 1256 | } |
||
| 1257 | |||
| 1258 | if (!empty($start)) { |
||
| 1259 | return $now >= $start; |
||
| 1260 | } |
||
| 1261 | |||
| 1262 | return !empty($end) && $now <= $end; |
||
| 1263 | } |
||
| 1264 | |||
| 1265 | /** |
||
| 1266 | * @return Collection<int, SessionRelCourseRelUser> |
||
| 1267 | */ |
||
| 1268 | #[Groups(['user_subscriptions:sessions'])] |
||
| 1272 | } |
||
| 1273 | |||
| 1274 | public function isAvailableByDurationForUser(User $user): bool |
||
| 1275 | { |
||
| 1276 | $duration = $this->duration * 24 * 60 * 60; |
||
| 1277 | |||
| 1278 | if (0 === $user->getTrackECourseAccess()->count()) { |
||
| 1279 | return true; |
||
| 1280 | } |
||
| 1281 | |||
| 1282 | $trackECourseAccess = $user->getFirstAccessToSession($this); |
||
| 1283 | |||
| 1284 | $firstAccess = $trackECourseAccess |
||
| 1285 | ? $trackECourseAccess->getLoginCourseDate()->getTimestamp() |
||
| 1286 | : 0; |
||
| 1287 | |||
| 1288 | $userDurationData = $user->getSubscriptionToSession($this); |
||
| 1289 | |||
| 1290 | $userDuration = $userDurationData |
||
| 1291 | ? ($userDurationData->getDuration() * 24 * 60 * 60) |
||
| 1292 | : 0; |
||
| 1293 | |||
| 1294 | $totalDuration = $firstAccess + $duration + $userDuration; |
||
| 1295 | $currentTime = time(); |
||
| 1296 | |||
| 1297 | return $totalDuration > $currentTime; |
||
| 1298 | } |
||
| 1299 | |||
| 1300 | public function getDaysLeftByUser(User $user): int |
||
| 1301 | { |
||
| 1302 | $userSessionSubscription = $user->getSubscriptionToSession($this); |
||
| 1303 | |||
| 1304 | $duration = $this->duration; |
||
| 1305 | |||
| 1306 | if ($userSessionSubscription) { |
||
| 1307 | $duration += $userSessionSubscription->getDuration(); |
||
| 1308 | } |
||
| 1309 | |||
| 1310 | $courseAccess = $user->getFirstAccessToSession($this); |
||
| 1311 | |||
| 1312 | if (!$courseAccess) { |
||
| 1313 | return $duration; |
||
| 1314 | } |
||
| 1315 | |||
| 1316 | $endDateInSeconds = $courseAccess->getLoginCourseDate()->getTimestamp() + $duration * 24 * 60 * 60; |
||
| 1317 | $currentTime = time(); |
||
| 1318 | |||
| 1319 | return (int) round(($endDateInSeconds - $currentTime) / 60 / 60 / 24); |
||
| 1320 | } |
||
| 1321 | |||
| 1322 | private function getAccessVisibilityByDuration(User $user): int |
||
| 1323 | { |
||
| 1324 | // Session duration per student. |
||
| 1325 | if ($this->getDuration() > 0) { |
||
| 1326 | $duration = $this->getDuration() * 24 * 60 * 60; |
||
| 1327 | $courseAccess = $user->getFirstAccessToSession($this); |
||
| 1328 | |||
| 1329 | // If there is a session duration but there is no previous |
||
| 1330 | // access by the user, then the session is still available |
||
| 1331 | if (!$courseAccess) { |
||
| 1332 | return self::AVAILABLE; |
||
| 1333 | } |
||
| 1334 | |||
| 1335 | $currentTime = time(); |
||
| 1336 | $firstAccess = $courseAccess->getLoginCourseDate()->getTimestamp(); |
||
| 1337 | $userSessionSubscription = $user->getSubscriptionToSession($this); |
||
| 1338 | $userDuration = $userSessionSubscription |
||
| 1339 | ? $userSessionSubscription->getDuration() * 24 * 60 * 60 |
||
| 1340 | : 0; |
||
| 1341 | |||
| 1342 | $totalDuration = $firstAccess + $duration + $userDuration; |
||
| 1343 | |||
| 1344 | return $totalDuration > $currentTime ? self::AVAILABLE : self::READ_ONLY; |
||
| 1345 | } |
||
| 1346 | |||
| 1347 | return self::AVAILABLE; |
||
| 1348 | } |
||
| 1349 | |||
| 1350 | /** |
||
| 1351 | * Checks whether the user is a course or session coach. |
||
| 1352 | */ |
||
| 1353 | public function hasCoach(User $user): bool |
||
| 1354 | { |
||
| 1355 | return $this->hasUserAsGeneralCoach($user) || $this->hasCoachInCourseList($user); |
||
| 1356 | } |
||
| 1357 | |||
| 1358 | private function getAcessVisibilityByDates(User $user): int |
||
| 1359 | { |
||
| 1360 | $now = new DateTime(); |
||
| 1361 | |||
| 1362 | $userIsCoach = $this->hasCoach($user); |
||
| 1363 | |||
| 1364 | $sessionEndDate = $userIsCoach && $this->coachAccessEndDate |
||
| 1365 | ? $this->coachAccessEndDate |
||
| 1366 | : $this->accessEndDate; |
||
| 1367 | |||
| 1368 | if (!$userIsCoach && $this->accessStartDate && $now < $this->accessStartDate) { |
||
| 1369 | return self::LIST_ONLY; |
||
| 1370 | } |
||
| 1371 | |||
| 1372 | if ($sessionEndDate) { |
||
| 1373 | return $now <= $sessionEndDate ? self::AVAILABLE : $this->visibility; |
||
| 1374 | } |
||
| 1375 | |||
| 1376 | return self::AVAILABLE; |
||
| 1377 | } |
||
| 1378 | |||
| 1379 | public function setAccessVisibilityByUser(User $user): int |
||
| 1380 | { |
||
| 1381 | if ($user->isAdmin() || $user->isSuperAdmin()) { |
||
| 1382 | $this->accessVisibility = self::AVAILABLE; |
||
| 1383 | } elseif (!$this->getAccessStartDate() && !$this->getAccessEndDate()) { |
||
| 1384 | // I don't care the session visibility. |
||
| 1385 | $this->accessVisibility = $this->getAccessVisibilityByDuration($user); |
||
| 1386 | } else { |
||
| 1387 | $this->accessVisibility = $this->getAcessVisibilityByDates($user); |
||
| 1388 | } |
||
| 1389 | |||
| 1390 | return $this->accessVisibility; |
||
| 1391 | } |
||
| 1392 | |||
| 1393 | public function getAccessVisibility(): int |
||
| 1394 | { |
||
| 1395 | if (0 === $this->accessVisibility) { |
||
| 1396 | throw new LogicException('Access visibility by user is not set'); |
||
| 1397 | } |
||
| 1398 | |||
| 1399 | return $this->accessVisibility; |
||
| 1400 | } |
||
| 1401 | |||
| 1402 | public function getClosedOrHidenCourses(): Collection |
||
| 1412 | )); |
||
| 1413 | } |
||
| 1414 | } |
||
| 1415 |