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