Passed
Push — master ( 814048...d8d7f0 )
by Angel Fernando Quiroz
11:26
created

User::isSoftDeleted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Entity;
8
9
use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter;
10
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
11
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
12
use ApiPlatform\Metadata\ApiFilter;
13
use ApiPlatform\Metadata\ApiProperty;
14
use ApiPlatform\Metadata\ApiResource;
15
use ApiPlatform\Metadata\Delete;
16
use ApiPlatform\Metadata\Get;
17
use ApiPlatform\Metadata\GetCollection;
18
use ApiPlatform\Metadata\Link;
19
use ApiPlatform\Metadata\Post;
20
use ApiPlatform\Metadata\Put;
21
use ApiPlatform\OpenApi\Model\Operation;
22
use ApiPlatform\OpenApi\Model\Parameter;
23
use Chamilo\CoreBundle\Controller\Api\CreateUserOnAccessUrlAction;
24
use Chamilo\CoreBundle\Controller\Api\GetStatsAction;
25
use Chamilo\CoreBundle\Controller\Api\UserSkillsController;
26
use Chamilo\CoreBundle\Dto\CreateUserOnAccessUrlInput;
27
use Chamilo\CoreBundle\Entity\Listener\UserListener;
28
use Chamilo\CoreBundle\Filter\PartialSearchOrFilter;
29
use Chamilo\CoreBundle\Repository\Node\UserRepository;
30
use Chamilo\CoreBundle\Traits\UserCreatorTrait;
31
use Chamilo\CourseBundle\Entity\CGroupRelTutor;
32
use Chamilo\CourseBundle\Entity\CGroupRelUser;
33
use Chamilo\CourseBundle\Entity\CSurveyInvitation;
34
use DateTime;
35
use DateTimeInterface;
36
use Doctrine\Common\Collections\ArrayCollection;
37
use Doctrine\Common\Collections\Collection;
38
use Doctrine\Common\Collections\Criteria;
39
use Doctrine\Common\Collections\ReadableCollection;
40
use Doctrine\ORM\Mapping as ORM;
41
use Gedmo\Timestampable\Traits\TimestampableEntity;
42
use Stringable;
43
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
44
use Symfony\Component\Security\Core\User\EquatableInterface;
45
use Symfony\Component\Security\Core\User\LegacyPasswordAuthenticatedUserInterface;
46
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
47
use Symfony\Component\Security\Core\User\UserInterface;
48
use Symfony\Component\Serializer\Annotation\Groups;
49
use Symfony\Component\Uid\Uuid;
50
use Symfony\Component\Validator\Constraints as Assert;
51
use Symfony\Component\Validator\Mapping\ClassMetadata;
52
use UserManager;
53
54
#[ApiResource(
55
    types: ['http://schema.org/Person'],
56
    operations: [
57
        new Get(
58
            openapi: new Operation(
59
                summary: 'Get details of one specific user, including name, e-mail and role.'
60
            ),
61
            security: "is_granted('VIEW', object)",
62
        ),
63
        new Get(
64
            uriTemplate: '/users/{id}/courses/{courseId}/stats/{metric}',
65
            requirements: [
66
                'id' => '\d+',
67
                'courseId' => '\d+',
68
                'metric' => 'avg-lp-progress|certificates|gradebook-global',
69
            ],
70
            controller: GetStatsAction::class,
71
            openapi: new Operation(
72
                summary: 'User-course statistics, switch by {metric}',
73
                parameters: [
74
                    new Parameter(
75
                        name: 'courseId',
76
                        in: 'path',
77
                        description: 'Course ID',
78
                        required: true,
79
                        schema: ['type' => 'integer'],
80
                    ),
81
                    new Parameter(
82
                        name: 'metric',
83
                        in: 'path',
84
                        description: 'Metric selector',
85
                        required: true,
86
                        schema: [
87
                            'type' => 'string',
88
                            'enum' => ['avg-lp-progress', 'certificates', 'gradebook-global'],
89
                        ],
90
                    ),
91
                    new Parameter(
92
                        name: 'sessionId',
93
                        in: 'query',
94
                        description: 'Optional Session ID',
95
                        required: false,
96
                        schema: ['type' => 'integer'],
97
                    ),
98
                ],
99
            ),
100
            security: "is_granted('ROLE_USER')",
101
            output: false,
102
            read: false,
103
            name: 'stats_user_course_metric'
104
        ),
105
        new Put(security: "is_granted('EDIT', object)"),
106
        new Delete(security: "is_granted('DELETE', object)"),
107
        new GetCollection(security: "is_granted('ROLE_ADMIN')"),
108
        new Post(security: "is_granted('ROLE_ADMIN')"),
109
        new GetCollection(
110
            uriTemplate: '/users/{id}/skills',
111
            controller: UserSkillsController::class,
112
            normalizationContext: ['groups' => ['user_skills:read']],
113
            name: 'get_user_skills'
114
        ),
115
    ],
116
    normalizationContext: ['groups' => ['user:read']],
117
    denormalizationContext: ['groups' => ['user:write']],
118
    security: 'is_granted("ROLE_USER")'
119
)]
120
#[ApiResource(
121
    uriTemplate: '/access_urls/{id}/user',
122
    operations: [
123
        new Post(
124
            controller: CreateUserOnAccessUrlAction::class,
125
        ),
126
    ],
127
    uriVariables: [
128
        'id' => new Link(
129
            description: 'AccessUrl identifier',
130
        ),
131
    ],
132
    normalizationContext: ['groups' => ['user:read']],
133
    denormalizationContext: ['groups' => ['user:write']],
134
    input: CreateUserOnAccessUrlInput::class,
135
    output: User::class,
136
    security: "is_granted('ROLE_ADMIN') or is_granted('ROLE_SESSION_MANAGER')",
137
)]
138
#[ORM\Table(name: 'user')]
139
#[ORM\Index(columns: ['status'], name: 'status')]
140
#[UniqueEntity('username')]
141
#[ORM\Entity(repositoryClass: UserRepository::class)]
142
#[ORM\EntityListeners([UserListener::class])]
143
#[ApiFilter(
144
    filterClass: SearchFilter::class,
145
    properties: [
146
        'username' => 'partial',
147
        'firstname' => 'partial',
148
        'lastname' => 'partial',
149
    ]
150
)]
151
#[ApiFilter(PartialSearchOrFilter::class, properties: ['username', 'firstname', 'lastname'])]
152
#[ApiFilter(filterClass: BooleanFilter::class, properties: ['isActive'])]
153
#[ApiFilter(filterClass: OrderFilter::class, properties: ['username', 'firstname', 'lastname'])]
154
class User implements UserInterface, EquatableInterface, ResourceInterface, ResourceIllustrationInterface, PasswordAuthenticatedUserInterface, LegacyPasswordAuthenticatedUserInterface, ExtraFieldItemInterface, Stringable
155
{
156
    use TimestampableEntity;
157
    use UserCreatorTrait;
158
159
    public const USERNAME_MAX_LENGTH = 100;
160
    public const ROLE_DEFAULT = 'ROLE_USER';
161
    public const ANONYMOUS = 6;
162
163
    /**
164
     * Global status for the fallback user.
165
     * This special status is used for a system user that acts as a placeholder
166
     * or fallback for content ownership when regular users are deleted.
167
     * This ensures data integrity and prevents orphaned content within the system.
168
     */
169
    public const ROLE_FALLBACK = 99;
170
171
    // User active field constants
172
    public const ACTIVE = 1;
173
    public const INACTIVE = 0;
174
    public const INACTIVE_AUTOMATIC = -1;
175
    public const SOFT_DELETED = -2;
176
177
    #[Groups(['user_json:read'])]
178
    #[ORM\OneToOne(targetEntity: ResourceNode::class, cascade: ['persist'])]
179
    #[ORM\JoinColumn(name: 'resource_node_id', onDelete: 'CASCADE')]
180
    public ?ResourceNode $resourceNode = null;
181
182
    /**
183
     * Resource illustration URL - Property set by ResourceNormalizer.php.
184
     */
185
    #[ApiProperty(iris: ['http://schema.org/contentUrl'])]
186
    #[Groups([
187
        'user_export',
188
        'user:read',
189
        'resource_node:read',
190
        'document:read',
191
        'media_object_read',
192
        'course:read',
193
        'course_rel_user:read',
194
        'user_json:read',
195
        'message:read',
196
        'user_rel_user:read',
197
        'social_post:read',
198
        'user_subscriptions:sessions',
199
    ])]
200
    public ?string $illustrationUrl = null;
201
202
    #[Groups([
203
        'user:read',
204
        'course:read',
205
        'resource_node:read',
206
        'user_json:read',
207
        'message:read',
208
        'user_rel_user:read',
209
        'session:item:read',
210
    ])]
211
    #[ORM\Column(name: 'id', type: 'integer')]
212
    #[ORM\Id]
213
    #[ORM\GeneratedValue]
214
    protected ?int $id = null;
215
216
    #[Assert\NotBlank]
217
    #[Groups([
218
        'user_export',
219
        'user:read',
220
        'user:write',
221
        'course:read',
222
        'course_rel_user:read',
223
        'resource_node:read',
224
        'user_json:read',
225
        'message:read',
226
        'page:read',
227
        'user_rel_user:read',
228
        'social_post:read',
229
        'track_e_exercise:read',
230
        'user_subscriptions:sessions',
231
        'student_publication_rel_user:read',
232
    ])]
233
    #[ORM\Column(name: 'username', type: 'string', length: 100, unique: true)]
234
    protected string $username;
235
236
    #[ORM\Column(name: 'api_token', type: 'string', unique: true, nullable: true)]
237
    protected ?string $apiToken = null;
238
239
    #[ApiProperty(iris: ['http://schema.org/name'])]
240
    #[Groups(['user:read', 'user:write', 'resource_node:read', 'user_json:read', 'track_e_exercise:read', 'user_rel_user:read', 'user_subscriptions:sessions', 'student_publication_rel_user:read'])]
241
    #[ORM\Column(name: 'firstname', type: 'string', length: 64, nullable: true)]
242
    protected ?string $firstname = null;
243
244
    #[Groups(['user:read', 'user:write', 'resource_node:read', 'user_json:read', 'track_e_exercise:read', 'user_rel_user:read', 'user_subscriptions:sessions', 'student_publication_rel_user:read'])]
245
    #[ORM\Column(name: 'lastname', type: 'string', length: 64, nullable: true)]
246
    protected ?string $lastname = null;
247
248
    #[Groups(['user:read', 'user:write'])]
249
    #[ORM\Column(name: 'website', type: 'string', length: 255, nullable: true)]
250
    protected ?string $website;
251
252
    #[Groups(['user:read', 'user:write'])]
253
    #[ORM\Column(name: 'biography', type: 'text', nullable: true)]
254
    protected ?string $biography;
255
256
    #[Groups(['user:read', 'user:write', 'user_json:read'])]
257
    #[ORM\Column(name: 'locale', type: 'string', length: 10)]
258
    protected string $locale;
259
260
    #[Groups(['user:write'])]
261
    protected ?string $plainPassword = null;
262
263
    #[ORM\Column(name: 'password', type: 'string', length: 255)]
264
    protected string $password = '';
265
266
    #[ORM\Column(name: 'username_canonical', type: 'string', length: 180)]
267
    protected string $usernameCanonical;
268
269
    #[Groups(['user:read', 'user:write', 'user_json:read'])]
270
    #[ORM\Column(name: 'timezone', type: 'string', length: 64)]
271
    protected string $timezone;
272
273
    #[ORM\Column(name: 'email_canonical', type: 'string', length: 100)]
274
    protected string $emailCanonical;
275
276
    #[Groups(['user:read', 'user:write', 'user_json:read'])]
277
    #[Assert\NotBlank]
278
    #[Assert\Email]
279
    #[ORM\Column(name: 'email', type: 'string', length: 100)]
280
    protected string $email;
281
282
    #[ORM\Column(name: 'locked', type: 'boolean')]
283
    protected bool $locked;
284
285
    #[Groups(['user:read', 'user:write'])]
286
    #[ORM\Column(name: 'expired', type: 'boolean')]
287
    protected bool $expired;
288
289
    #[ORM\Column(name: 'credentials_expired', type: 'boolean')]
290
    protected bool $credentialsExpired;
291
292
    #[ORM\Column(name: 'credentials_expire_at', type: 'datetime', nullable: true)]
293
    protected ?DateTime $credentialsExpireAt;
294
295
    #[ORM\Column(name: 'date_of_birth', type: 'datetime', nullable: true)]
296
    protected ?DateTime $dateOfBirth = null;
297
298
    #[Groups(['user:read', 'user:write'])]
299
    #[ORM\Column(name: 'expires_at', type: 'datetime', nullable: true)]
300
    protected ?DateTime $expiresAt;
301
302
    #[Groups(['user:read', 'user:write'])]
303
    #[ORM\Column(name: 'phone', type: 'string', length: 64, nullable: true)]
304
    protected ?string $phone = null;
305
306
    #[Groups(['user:read', 'user:write'])]
307
    #[ORM\Column(name: 'address', type: 'string', length: 250, nullable: true)]
308
    protected ?string $address = null;
309
310
    #[ORM\Column(type: 'string', length: 255)]
311
    protected string $salt;
312
313
    #[ORM\Column(name: 'gender', type: 'string', length: 1, nullable: true)]
314
    protected ?string $gender = null;
315
316
    #[Groups(['user:read'])]
317
    #[ORM\Column(name: 'last_login', type: 'datetime', nullable: true)]
318
    protected ?DateTime $lastLogin = null;
319
320
    /**
321
     * Random string sent to the user email address in order to verify it.
322
     */
323
    #[ORM\Column(name: 'confirmation_token', type: 'string', length: 255, nullable: true)]
324
    protected ?string $confirmationToken = null;
325
326
    #[ORM\Column(name: 'password_requested_at', type: 'datetime', nullable: true)]
327
    protected ?DateTime $passwordRequestedAt;
328
329
    /**
330
     * @var Collection<int, CourseRelUser>
331
     */
332
    #[ORM\OneToMany(mappedBy: 'user', targetEntity: CourseRelUser::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
333
    protected Collection $courses;
334
335
    /**
336
     * @var Collection<int, UsergroupRelUser>
337
     */
338
    #[ORM\OneToMany(mappedBy: 'user', targetEntity: UsergroupRelUser::class)]
339
    protected Collection $classes;
340
341
    /**
342
     * ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CDropboxPost", mappedBy="user").
343
     */
344
    protected Collection $dropBoxReceivedFiles;
345
346
    /**
347
     * ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CDropboxFile", mappedBy="userSent").
348
     */
349
    protected Collection $dropBoxSentFiles;
350
351
    /**
352
     * An array of roles. Example: ROLE_USER, ROLE_TEACHER, ROLE_ADMIN.
353
     */
354
    #[Groups(['user:read', 'user:write', 'user_json:read'])]
355
    #[ORM\Column(type: 'array')]
356
    protected array $roles = [];
357
358
    #[ORM\Column(name: 'profile_completed', type: 'boolean', nullable: true)]
359
    protected ?bool $profileCompleted = null;
360
361
    /**
362
     * ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\JuryMembers", mappedBy="user").
363
     */
364
    // protected $jurySubscriptions;
365
366
    /**
367
     * @var Collection<int, Group>
368
     */
369
    #[ORM\JoinTable(name: 'fos_user_user_group')]
370
    #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', onDelete: 'cascade')]
371
    #[ORM\InverseJoinColumn(name: 'group_id', referencedColumnName: 'id')]
372
    #[ORM\ManyToMany(targetEntity: Group::class, inversedBy: 'users')]
373
    protected Collection $groups;
374
375
    /**
376
     * ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\CurriculumItemRelUser", mappedBy="user").
377
     */
378
    protected Collection $curriculumItems;
379
380
    /**
381
     * @var Collection<int, AccessUrlRelUser>
382
     */
383
    #[ORM\OneToMany(
384
        mappedBy: 'user',
385
        targetEntity: AccessUrlRelUser::class,
386
        cascade: ['persist', 'remove'],
387
        orphanRemoval: true
388
    )]
389
    protected Collection $portals;
390
391
    /**
392
     * @var Collection<int, ResourceNode>
393
     */
394
    #[ORM\OneToMany(mappedBy: 'creator', targetEntity: ResourceNode::class, cascade: ['persist', 'remove'])]
395
    protected Collection $resourceNodes;
396
397
    /**
398
     * @var Collection<int, SessionRelCourseRelUser>
399
     */
400
    #[ORM\OneToMany(
401
        mappedBy: 'user',
402
        targetEntity: SessionRelCourseRelUser::class,
403
        cascade: ['persist'],
404
        orphanRemoval: true
405
    )]
406
    protected Collection $sessionRelCourseRelUsers;
407
408
    /**
409
     * @var Collection<int, SessionRelUser>
410
     */
411
    #[ORM\OneToMany(
412
        mappedBy: 'user',
413
        targetEntity: SessionRelUser::class,
414
        cascade: ['persist', 'remove'],
415
        orphanRemoval: true
416
    )]
417
    protected Collection $sessionsRelUser;
418
419
    /**
420
     * @var Collection<int, SkillRelUser>
421
     */
422
    #[ORM\OneToMany(
423
        mappedBy: 'user',
424
        targetEntity: SkillRelUser::class,
425
        cascade: ['persist', 'remove'],
426
        orphanRemoval: true
427
    )]
428
    protected Collection $achievedSkills;
429
430
    /**
431
     * @var Collection<int, SkillRelUserComment>
432
     */
433
    #[ORM\OneToMany(
434
        mappedBy: 'feedbackGiver',
435
        targetEntity: SkillRelUserComment::class,
436
        cascade: ['persist', 'remove'],
437
        orphanRemoval: true
438
    )]
439
    protected Collection $commentedUserSkills;
440
441
    /**
442
     * @var Collection<int, GradebookCategory>
443
     */
444
    #[ORM\OneToMany(mappedBy: 'user', targetEntity: GradebookCategory::class)]
445
    protected Collection $gradeBookCategories;
446
447
    /**
448
     * @var Collection<int, GradebookCertificate>
449
     */
450
    #[ORM\OneToMany(
451
        mappedBy: 'user',
452
        targetEntity: GradebookCertificate::class,
453
        cascade: ['persist', 'remove'],
454
        orphanRemoval: true
455
    )]
456
    protected Collection $gradeBookCertificates;
457
458
    /**
459
     * @var Collection<int, GradebookComment>
460
     */
461
    #[ORM\OneToMany(mappedBy: 'user', targetEntity: GradebookComment::class)]
462
    protected Collection $gradeBookComments;
463
464
    /**
465
     * @var Collection<int, GradebookResult>
466
     */
467
    #[ORM\OneToMany(
468
        mappedBy: 'user',
469
        targetEntity: GradebookResult::class,
470
        cascade: ['persist', 'remove'],
471
        orphanRemoval: true
472
    )]
473
    protected Collection $gradeBookResults;
474
475
    /**
476
     * @var Collection<int, GradebookResultLog>
477
     */
478
    #[ORM\OneToMany(
479
        mappedBy: 'user',
480
        targetEntity: GradebookResultLog::class,
481
        cascade: ['persist', 'remove'],
482
        orphanRemoval: true
483
    )]
484
    protected Collection $gradeBookResultLogs;
485
486
    /**
487
     * @var Collection<int, GradebookScoreLog>
488
     */
489
    #[ORM\OneToMany(
490
        mappedBy: 'user',
491
        targetEntity: GradebookScoreLog::class,
492
        cascade: ['persist', 'remove'],
493
        orphanRemoval: true
494
    )]
495
    protected Collection $gradeBookScoreLogs;
496
497
    /**
498
     * @var Collection<int, UserRelUser>
499
     */
500
    #[ORM\OneToMany(
501
        mappedBy: 'user',
502
        targetEntity: UserRelUser::class,
503
        cascade: ['persist', 'remove'],
504
        fetch: 'EXTRA_LAZY',
505
        orphanRemoval: true
506
    )]
507
    protected Collection $friends;
508
509
    /**
510
     * @var Collection<int, UserRelUser>
511
     */
512
    #[ORM\OneToMany(
513
        mappedBy: 'friend',
514
        targetEntity: UserRelUser::class,
515
        cascade: ['persist', 'remove'],
516
        fetch: 'EXTRA_LAZY',
517
        orphanRemoval: true
518
    )]
519
    protected Collection $friendsWithMe;
520
521
    /**
522
     * @var Collection<int, GradebookLinkevalLog>
523
     */
524
    #[ORM\OneToMany(
525
        mappedBy: 'user',
526
        targetEntity: GradebookLinkevalLog::class,
527
        cascade: ['persist', 'remove'],
528
        orphanRemoval: true
529
    )]
530
    protected Collection $gradeBookLinkEvalLogs;
531
532
    /**
533
     * @var Collection<int, SequenceValue>
534
     */
535
    #[ORM\OneToMany(
536
        mappedBy: 'user',
537
        targetEntity: SequenceValue::class,
538
        cascade: ['persist', 'remove'],
539
        orphanRemoval: true
540
    )]
541
    protected Collection $sequenceValues;
542
543
    /**
544
     * @var Collection<int, TrackEExerciseConfirmation>
545
     */
546
    #[ORM\OneToMany(
547
        mappedBy: 'user',
548
        targetEntity: TrackEExerciseConfirmation::class,
549
        cascade: ['persist', 'remove'],
550
        orphanRemoval: true
551
    )]
552
    protected Collection $trackEExerciseConfirmations;
553
554
    /**
555
     * @var Collection<int, TrackEAttempt>
556
     */
557
    #[ORM\OneToMany(
558
        mappedBy: 'user',
559
        targetEntity: TrackEAccessComplete::class,
560
        cascade: [
561
            'persist',
562
            'remove',
563
        ],
564
        orphanRemoval: true
565
    )]
566
    protected Collection $trackEAccessCompleteList;
567
568
    /**
569
     * @var Collection<int, Templates>
570
     */
571
    #[ORM\OneToMany(
572
        mappedBy: 'user',
573
        targetEntity: Templates::class,
574
        cascade: ['persist', 'remove'],
575
        fetch: 'EXTRA_LAZY',
576
        orphanRemoval: true
577
    )]
578
    protected Collection $templates;
579
580
    /**
581
     * @var Collection<int, TrackEAttempt>
582
     */
583
    #[ORM\OneToMany(
584
        mappedBy: 'user',
585
        targetEntity: TrackEAttempt::class,
586
        cascade: ['persist', 'remove'],
587
        orphanRemoval: true
588
    )]
589
    protected Collection $trackEAttempts;
590
591
    /**
592
     * @var Collection<int, TrackECourseAccess>
593
     */
594
    #[ORM\OneToMany(
595
        mappedBy: 'user',
596
        targetEntity: TrackECourseAccess::class,
597
        cascade: ['persist', 'remove'],
598
        orphanRemoval: true
599
    )]
600
    protected Collection $trackECourseAccess;
601
602
    /**
603
     * @var Collection<int, UserCourseCategory>
604
     */
605
    #[ORM\OneToMany(
606
        mappedBy: 'user',
607
        targetEntity: UserCourseCategory::class,
608
        cascade: ['persist', 'remove'],
609
        orphanRemoval: true
610
    )]
611
    protected Collection $userCourseCategories;
612
613
    /**
614
     * @var Collection<int, UserRelCourseVote>
615
     */
616
    #[ORM\OneToMany(
617
        mappedBy: 'user',
618
        targetEntity: UserRelCourseVote::class,
619
        cascade: ['persist', 'remove'],
620
        orphanRemoval: true
621
    )]
622
    protected Collection $userRelCourseVotes;
623
624
    /**
625
     * @var Collection<int, UserRelTag>
626
     */
627
    #[ORM\OneToMany(
628
        mappedBy: 'user',
629
        targetEntity: UserRelTag::class,
630
        cascade: ['persist', 'remove'],
631
        orphanRemoval: true
632
    )]
633
    protected Collection $userRelTags;
634
635
    /**
636
     * @var Collection<int, CGroupRelUser>
637
     */
638
    #[ORM\OneToMany(
639
        mappedBy: 'user',
640
        targetEntity: CGroupRelUser::class,
641
        cascade: ['persist', 'remove'],
642
        orphanRemoval: true
643
    )]
644
    protected Collection $courseGroupsAsMember;
645
646
    /**
647
     * @var Collection<int, CGroupRelTutor>
648
     */
649
    #[ORM\OneToMany(mappedBy: 'user', targetEntity: CGroupRelTutor::class, orphanRemoval: true)]
650
    protected Collection $courseGroupsAsTutor;
651
652
    #[ORM\Column(name: 'status', type: 'integer')]
653
    protected int $status;
654
655
    #[ORM\Column(name: 'official_code', type: 'string', length: 40, nullable: true)]
656
    protected ?string $officialCode = null;
657
658
    #[ORM\Column(name: 'picture_uri', type: 'string', length: 250, nullable: true)]
659
    protected ?string $pictureUri = null;
660
661
    #[ORM\Column(name: 'creator_id', type: 'integer', unique: false, nullable: true)]
662
    protected ?int $creatorId = null;
663
664
    #[ORM\Column(name: 'competences', type: 'text', unique: false, nullable: true)]
665
    protected ?string $competences = null;
666
667
    #[ORM\Column(name: 'diplomas', type: 'text', unique: false, nullable: true)]
668
    protected ?string $diplomas = null;
669
670
    #[ORM\Column(name: 'openarea', type: 'text', unique: false, nullable: true)]
671
    protected ?string $openarea = null;
672
673
    #[ORM\Column(name: 'teach', type: 'text', unique: false, nullable: true)]
674
    protected ?string $teach = null;
675
676
    #[ORM\Column(name: 'productions', type: 'string', length: 250, unique: false, nullable: true)]
677
    protected ?string $productions = null;
678
679
    #[ORM\Column(name: 'expiration_date', type: 'datetime', unique: false, nullable: true)]
680
    protected ?DateTime $expirationDate = null;
681
682
    #[Groups(['user:read', 'user_json:read'])]
683
    #[ORM\Column(name: 'active', type: 'integer')]
684
    protected int $active;
685
686
    #[ORM\Column(name: 'openid', type: 'string', length: 255, unique: false, nullable: true)]
687
    protected ?string $openid = null;
688
689
    #[ORM\Column(name: 'theme', type: 'string', length: 255, unique: false, nullable: true)]
690
    protected ?string $theme = null;
691
692
    #[ORM\Column(name: 'hr_dept_id', type: 'smallint', unique: false, nullable: true)]
693
    protected ?int $hrDeptId = null;
694
695
    #[Groups(['user:write'])]
696
    protected ?AccessUrl $currentUrl = null;
697
698
    /**
699
     * @var Collection<int, MessageTag>
700
     */
701
    #[ORM\OneToMany(
702
        mappedBy: 'user',
703
        targetEntity: MessageTag::class,
704
        cascade: ['persist', 'remove'],
705
        orphanRemoval: true
706
    )]
707
    protected Collection $messageTags;
708
709
    /**
710
     * @var Collection<int, Message>
711
     */
712
    #[ORM\OneToMany(
713
        mappedBy: 'sender',
714
        targetEntity: Message::class,
715
        cascade: ['persist']
716
    )]
717
    protected Collection $sentMessages;
718
719
    /**
720
     * @var Collection<int, MessageRelUser>
721
     */
722
    #[ORM\OneToMany(mappedBy: 'receiver', targetEntity: MessageRelUser::class, cascade: ['persist', 'remove'])]
723
    protected Collection $receivedMessages;
724
725
    /**
726
     * @var Collection<int, CSurveyInvitation>
727
     */
728
    #[ORM\OneToMany(mappedBy: 'user', targetEntity: CSurveyInvitation::class, cascade: ['persist', 'remove'])]
729
    protected Collection $surveyInvitations;
730
731
    /**
732
     * @var Collection<int, TrackELogin>
733
     */
734
    #[ORM\OneToMany(mappedBy: 'user', targetEntity: TrackELogin::class, cascade: ['persist', 'remove'])]
735
    protected Collection $logins;
736
737
    #[ORM\OneToOne(mappedBy: 'user', targetEntity: Admin::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
738
    protected ?Admin $admin = null;
739
740
    #[ORM\Column(type: 'uuid', unique: true)]
741
    protected Uuid $uuid;
742
743
    // Property used only during installation.
744
    protected bool $skipResourceNode = false;
745
746
    #[Groups([
747
        'user:read',
748
        'user_json:read',
749
        'social_post:read',
750
        'course:read',
751
        'course_rel_user:read',
752
        'message:read',
753
        'user_subscriptions:sessions',
754
        'student_publication_rel_user:read',
755
        'student_publication:read',
756
        'student_publication_comment:read',
757
    ])]
758
    protected string $fullName;
759
760
    #[ORM\OneToMany(mappedBy: 'sender', targetEntity: SocialPost::class, orphanRemoval: true)]
761
    private Collection $sentSocialPosts;
762
763
    #[ORM\OneToMany(mappedBy: 'userReceiver', targetEntity: SocialPost::class)]
764
    private Collection $receivedSocialPosts;
765
766
    #[ORM\OneToMany(mappedBy: 'user', targetEntity: SocialPostFeedback::class, orphanRemoval: true)]
767
    private Collection $socialPostsFeedbacks;
768
769
    #[ORM\Column(name: 'mfa_enabled', type: 'boolean', options: ['default' => false])]
770
    protected bool $mfaEnabled = false;
771
772
    #[ORM\Column(name: 'mfa_service', type: 'string', length: 255, nullable: true)]
773
    protected ?string $mfaService = null;
774
775
    #[ORM\Column(name: 'mfa_secret', type: 'string', length: 255, nullable: true)]
776
    protected ?string $mfaSecret = null;
777
778
    #[ORM\Column(name: 'mfa_backup_codes', type: 'text', nullable: true)]
779
    protected ?string $mfaBackupCodes = null;
780
781
    #[ORM\Column(name: 'mfa_last_used', type: 'datetime', nullable: true)]
782
    protected ?DateTimeInterface $mfaLastUsed = null;
783
784
    /**
785
     * @var Collection<int, UserAuthSource>
786
     */
787
    #[ORM\OneToMany(mappedBy: 'user', targetEntity: UserAuthSource::class, cascade: ['persist'], orphanRemoval: true)]
788
    private Collection $authSources;
789
790
    #[Groups(['user:read', 'user:write'])]
791
    #[ORM\Column(name: 'password_updated_at', type: 'datetime', nullable: true)]
792
    protected ?DateTimeInterface $passwordUpdatedAt = null;
793
794
    public function __construct()
795
    {
796
        $this->skipResourceNode = false;
797
        $this->uuid = Uuid::v4();
798
        $this->apiToken = null;
799
        $this->biography = '';
800
        $this->website = '';
801
        $this->locale = 'en';
802
        $this->timezone = 'Europe/Paris';
803
        $this->status = CourseRelUser::STUDENT;
804
        $this->salt = sha1(uniqid('', true));
805
        $this->active = 1;
806
        $this->locked = false;
807
        $this->expired = false;
808
        $this->courses = new ArrayCollection();
809
        $this->classes = new ArrayCollection();
810
        $this->curriculumItems = new ArrayCollection();
811
        $this->portals = new ArrayCollection();
812
        $this->dropBoxSentFiles = new ArrayCollection();
813
        $this->dropBoxReceivedFiles = new ArrayCollection();
814
        $this->groups = new ArrayCollection();
815
        $this->gradeBookCertificates = new ArrayCollection();
816
        $this->courseGroupsAsMember = new ArrayCollection();
817
        $this->courseGroupsAsTutor = new ArrayCollection();
818
        $this->resourceNodes = new ArrayCollection();
819
        $this->sessionRelCourseRelUsers = new ArrayCollection();
820
        $this->achievedSkills = new ArrayCollection();
821
        $this->commentedUserSkills = new ArrayCollection();
822
        $this->gradeBookCategories = new ArrayCollection();
823
        $this->gradeBookComments = new ArrayCollection();
824
        $this->gradeBookResults = new ArrayCollection();
825
        $this->gradeBookResultLogs = new ArrayCollection();
826
        $this->gradeBookScoreLogs = new ArrayCollection();
827
        $this->friends = new ArrayCollection();
828
        $this->friendsWithMe = new ArrayCollection();
829
        $this->gradeBookLinkEvalLogs = new ArrayCollection();
830
        $this->messageTags = new ArrayCollection();
831
        $this->sequenceValues = new ArrayCollection();
832
        $this->trackEExerciseConfirmations = new ArrayCollection();
833
        $this->trackEAccessCompleteList = new ArrayCollection();
834
        $this->templates = new ArrayCollection();
835
        $this->trackEAttempts = new ArrayCollection();
836
        $this->trackECourseAccess = new ArrayCollection();
837
        $this->userCourseCategories = new ArrayCollection();
838
        $this->userRelCourseVotes = new ArrayCollection();
839
        $this->userRelTags = new ArrayCollection();
840
        $this->sessionsRelUser = new ArrayCollection();
841
        $this->sentMessages = new ArrayCollection();
842
        $this->receivedMessages = new ArrayCollection();
843
        $this->surveyInvitations = new ArrayCollection();
844
        $this->logins = new ArrayCollection();
845
        $this->createdAt = new DateTime();
846
        $this->updatedAt = new DateTime();
847
        $this->roles = [];
848
        $this->credentialsExpired = false;
849
        $this->credentialsExpireAt = new DateTime();
850
        $this->dateOfBirth = null;
851
        $this->expiresAt = new DateTime();
852
        $this->passwordRequestedAt = new DateTime();
853
        $this->sentSocialPosts = new ArrayCollection();
854
        $this->receivedSocialPosts = new ArrayCollection();
855
        $this->socialPostsFeedbacks = new ArrayCollection();
856
        $this->authSources = new ArrayCollection();
857
    }
858
859
    public function __toString(): string
860
    {
861
        return $this->username;
862
    }
863
864
    public static function getPasswordConstraints(): array
865
    {
866
        return [
867
            new Assert\Length(['min' => 5]),
868
            new Assert\Regex(['pattern' => '/^[a-z\-_0-9]+$/i', 'htmlPattern' => '/^[a-z\-_0-9]+$/i']),
869
            new Assert\Regex(['pattern' => '/[0-9]{2}/', 'htmlPattern' => '/[0-9]{2}/']),
870
        ];
871
    }
872
873
    public static function loadValidatorMetadata(ClassMetadata $metadata): void {}
874
875
    public function getUuid(): Uuid
876
    {
877
        return $this->uuid;
878
    }
879
880
    public function setUuid(Uuid $uuid): self
881
    {
882
        $this->uuid = $uuid;
883
884
        return $this;
885
    }
886
887
    public function getResourceNode(): ?ResourceNode
888
    {
889
        return $this->resourceNode;
890
    }
891
892
    public function setResourceNode(ResourceNode $resourceNode): self
893
    {
894
        $this->resourceNode = $resourceNode;
895
896
        return $this;
897
    }
898
899
    public function hasResourceNode(): bool
900
    {
901
        return $this->resourceNode instanceof ResourceNode;
902
    }
903
904
    public function getResourceNodes(): Collection
905
    {
906
        return $this->resourceNodes;
907
    }
908
909
    public function addResourceNode(ResourceNode $resourceNode): static
910
    {
911
        if (!$this->resourceNodes->contains($resourceNode)) {
912
            $this->resourceNodes->add($resourceNode);
913
            $resourceNode->setCreator($this);
914
        }
915
916
        return $this;
917
    }
918
919
    public function getDropBoxSentFiles(): Collection
920
    {
921
        return $this->dropBoxSentFiles;
922
    }
923
924
    public function setDropBoxSentFiles(Collection $value): self
925
    {
926
        $this->dropBoxSentFiles = $value;
927
928
        return $this;
929
    }
930
931
    /**
932
     * @return Collection<int, CourseRelUser>
933
     */
934
    public function getCourses(): Collection
935
    {
936
        return $this->courses;
937
    }
938
939
    /**
940
     * @param Collection<int, CourseRelUser> $courses
941
     */
942
    public function setCourses(Collection $courses): self
943
    {
944
        $this->courses = $courses;
945
946
        return $this;
947
    }
948
949
    public function setPortal(AccessUrlRelUser $portal): self
950
    {
951
        $this->portals->add($portal);
952
953
        return $this;
954
    }
955
956
    /**
957
     * Get a bool on whether the user is active or not. Active can be "-1" which means pre-deleted, and is returned as false (not active).
958
     *
959
     * @return bool True if active = 1, false in any other case (0 = inactive, -1 = predeleted)
960
     */
961
    public function getIsActive(): bool
962
    {
963
        return self::ACTIVE === $this->active;
964
    }
965
966
    public function isSoftDeleted(): bool
967
    {
968
        return self::SOFT_DELETED === $this->active;
969
    }
970
971
    public function isEnabled(): bool
972
    {
973
        return $this->isActive();
974
    }
975
976
    /**
977
     * Returns the list of classes for the user.
978
     */
979
    public function getFullNameWithClasses(): string
980
    {
981
        $classSubscription = $this->getClasses();
982
        $classList = [];
983
984
        /** @var UsergroupRelUser $subscription */
985
        foreach ($classSubscription as $subscription) {
986
            $class = $subscription->getUsergroup();
987
            $classList[] = $class->getTitle();
988
        }
989
        $classString = empty($classList) ? null : ' ['.implode(', ', $classList).']';
990
991
        return UserManager::formatUserFullName($this).$classString;
992
    }
993
994
    public function getClasses(): Collection
995
    {
996
        return $this->classes;
997
    }
998
999
    /**
1000
     * @param Collection<int, UsergroupRelUser> $classes
1001
     */
1002
    public function setClasses(Collection $classes): self
1003
    {
1004
        $this->classes = $classes;
1005
1006
        return $this;
1007
    }
1008
1009
    public function getEmail(): string
1010
    {
1011
        return $this->email;
1012
    }
1013
1014
    public function setEmail(string $email): self
1015
    {
1016
        $this->email = $email;
1017
1018
        return $this;
1019
    }
1020
1021
    public function getOfficialCode(): ?string
1022
    {
1023
        return $this->officialCode;
1024
    }
1025
1026
    public function setOfficialCode(?string $officialCode): self
1027
    {
1028
        $this->officialCode = $officialCode;
1029
1030
        return $this;
1031
    }
1032
1033
    public function getPhone(): ?string
1034
    {
1035
        return $this->phone;
1036
    }
1037
1038
    public function setPhone(?string $phone): self
1039
    {
1040
        $this->phone = $phone;
1041
1042
        return $this;
1043
    }
1044
1045
    public function getAddress(): ?string
1046
    {
1047
        return $this->address;
1048
    }
1049
1050
    public function setAddress(?string $address): self
1051
    {
1052
        $this->address = $address;
1053
1054
        return $this;
1055
    }
1056
1057
    public function getCreatorId(): ?int
1058
    {
1059
        return $this->creatorId;
1060
    }
1061
1062
    public function setCreatorId(int $creatorId): self
1063
    {
1064
        $this->creatorId = $creatorId;
1065
1066
        return $this;
1067
    }
1068
1069
    public function getCompetences(): ?string
1070
    {
1071
        return $this->competences;
1072
    }
1073
1074
    public function setCompetences(?string $competences): self
1075
    {
1076
        $this->competences = $competences;
1077
1078
        return $this;
1079
    }
1080
1081
    public function getDiplomas(): ?string
1082
    {
1083
        return $this->diplomas;
1084
    }
1085
1086
    public function setDiplomas(?string $diplomas): self
1087
    {
1088
        $this->diplomas = $diplomas;
1089
1090
        return $this;
1091
    }
1092
1093
    public function getOpenarea(): ?string
1094
    {
1095
        return $this->openarea;
1096
    }
1097
1098
    public function setOpenarea(?string $openarea): self
1099
    {
1100
        $this->openarea = $openarea;
1101
1102
        return $this;
1103
    }
1104
1105
    public function getTeach(): ?string
1106
    {
1107
        return $this->teach;
1108
    }
1109
1110
    public function setTeach(?string $teach): self
1111
    {
1112
        $this->teach = $teach;
1113
1114
        return $this;
1115
    }
1116
1117
    public function getProductions(): ?string
1118
    {
1119
        return $this->productions;
1120
    }
1121
1122
    public function setProductions(?string $productions): self
1123
    {
1124
        $this->productions = $productions;
1125
1126
        return $this;
1127
    }
1128
1129
    public function getExpirationDate(): ?DateTime
1130
    {
1131
        return $this->expirationDate;
1132
    }
1133
1134
    public function setExpirationDate(?DateTime $expirationDate): self
1135
    {
1136
        $this->expirationDate = $expirationDate;
1137
1138
        return $this;
1139
    }
1140
1141
    public function getActive(): int
1142
    {
1143
        return $this->active;
1144
    }
1145
1146
    public function isActive(): bool
1147
    {
1148
        return $this->getIsActive();
1149
    }
1150
1151
    public function setActive(int $active): self
1152
    {
1153
        $this->active = $active;
1154
1155
        return $this;
1156
    }
1157
1158
    public function getOpenid(): ?string
1159
    {
1160
        return $this->openid;
1161
    }
1162
1163
    public function setOpenid(string $openid): self
1164
    {
1165
        $this->openid = $openid;
1166
1167
        return $this;
1168
    }
1169
1170
    public function getTheme(): ?string
1171
    {
1172
        return $this->theme;
1173
    }
1174
1175
    public function setTheme(string $theme): self
1176
    {
1177
        $this->theme = $theme;
1178
1179
        return $this;
1180
    }
1181
1182
    public function getHrDeptId(): ?int
1183
    {
1184
        return $this->hrDeptId;
1185
    }
1186
1187
    public function setHrDeptId(int $hrDeptId): self
1188
    {
1189
        $this->hrDeptId = $hrDeptId;
1190
1191
        return $this;
1192
    }
1193
1194
    public function isOnline(): bool
1195
    {
1196
        return false;
1197
    }
1198
1199
    public function getIdentifier(): int
1200
    {
1201
        return $this->getId();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getId() could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
1202
    }
1203
1204
    public function getId(): ?int
1205
    {
1206
        return $this->id;
1207
    }
1208
1209
    public function getIri(): ?string
1210
    {
1211
        if (null === $this->id) {
1212
            return null;
1213
        }
1214
1215
        return '/api/users/'.$this->getId();
1216
    }
1217
1218
    public function getSlug(): string
1219
    {
1220
        return $this->getUsername();
1221
    }
1222
1223
    public function getUsername(): string
1224
    {
1225
        return $this->username;
1226
    }
1227
1228
    public function setUsername(string $username): self
1229
    {
1230
        $this->username = $username;
1231
1232
        return $this;
1233
    }
1234
1235
    public function setSlug(string $slug): self
1236
    {
1237
        return $this->setUsername($slug);
1238
    }
1239
1240
    public function getLastLogin(): ?DateTime
1241
    {
1242
        return $this->lastLogin;
1243
    }
1244
1245
    public function setLastLogin(?DateTime $lastLogin = null): self
1246
    {
1247
        $this->lastLogin = $lastLogin;
1248
1249
        return $this;
1250
    }
1251
1252
    public function getConfirmationToken(): ?string
1253
    {
1254
        return $this->confirmationToken;
1255
    }
1256
1257
    public function setConfirmationToken(string $confirmationToken): self
1258
    {
1259
        $this->confirmationToken = $confirmationToken;
1260
1261
        return $this;
1262
    }
1263
1264
    public function isPasswordRequestNonExpired(int $ttl): bool
1265
    {
1266
        return $this->getPasswordRequestedAt() instanceof DateTime && $this->getPasswordRequestedAt()->getTimestamp(
1267
        ) + $ttl > time();
1268
    }
1269
1270
    public function getPasswordRequestedAt(): ?DateTime
1271
    {
1272
        return $this->passwordRequestedAt;
1273
    }
1274
1275
    public function setPasswordRequestedAt(?DateTime $date = null): self
1276
    {
1277
        $this->passwordRequestedAt = $date;
1278
1279
        return $this;
1280
    }
1281
1282
    public function getPlainPassword(): ?string
1283
    {
1284
        return $this->plainPassword;
1285
    }
1286
1287
    public function setPlainPassword(?string $password): self
1288
    {
1289
        $this->plainPassword = $password;
1290
        // forces the object to look "dirty" to Doctrine. Avoids
1291
        // Doctrine *not* saving this entity, if only plainPassword changes
1292
        $this->password = '';
1293
1294
        return $this;
1295
    }
1296
1297
    /**
1298
     * Returns the expiration date.
1299
     */
1300
    public function getExpiresAt(): ?DateTime
1301
    {
1302
        return $this->expiresAt;
1303
    }
1304
1305
    public function setExpiresAt(DateTime $date): self
1306
    {
1307
        $this->expiresAt = $date;
1308
1309
        return $this;
1310
    }
1311
1312
    /**
1313
     * Returns the credentials expiration date.
1314
     */
1315
    public function getCredentialsExpireAt(): ?DateTime
1316
    {
1317
        return $this->credentialsExpireAt;
1318
    }
1319
1320
    /**
1321
     * Sets the credentials expiration date.
1322
     */
1323
    public function setCredentialsExpireAt(?DateTime $date = null): self
1324
    {
1325
        $this->credentialsExpireAt = $date;
1326
1327
        return $this;
1328
    }
1329
1330
    public function getFullName(): string
1331
    {
1332
        if (empty($this->fullName)) {
1333
            return \sprintf('%s %s', $this->getFirstname(), $this->getLastname());
1334
        }
1335
1336
        return $this->fullName;
1337
    }
1338
1339
    public function setFullName(string $fullName): self
1340
    {
1341
        $this->fullName = $fullName;
1342
1343
        return $this;
1344
    }
1345
1346
    public function getFirstname(): ?string
1347
    {
1348
        return $this->firstname;
1349
    }
1350
1351
    public function setFirstname(?string $firstname): self
1352
    {
1353
        $this->firstname = $firstname;
1354
1355
        return $this;
1356
    }
1357
1358
    public function getLastname(): ?string
1359
    {
1360
        return $this->lastname;
1361
    }
1362
1363
    public function setLastname(?string $lastname): self
1364
    {
1365
        $this->lastname = $lastname;
1366
1367
        return $this;
1368
    }
1369
1370
    public function hasGroup(string $name): bool
1371
    {
1372
        return \in_array($name, $this->getGroupNames(), true);
1373
    }
1374
1375
    public function getGroupNames(): array
1376
    {
1377
        $names = [];
1378
        foreach ($this->getGroups() as $group) {
1379
            $names[] = $group->getTitle();
1380
        }
1381
1382
        return $names;
1383
    }
1384
1385
    public function getGroups(): Collection
1386
    {
1387
        return $this->groups;
1388
    }
1389
1390
    /**
1391
     * Sets the user groups.
1392
     */
1393
    public function setGroups(Collection $groups): self
1394
    {
1395
        foreach ($groups as $group) {
1396
            $this->addGroup($group);
1397
        }
1398
1399
        return $this;
1400
    }
1401
1402
    public function addGroup(Group $group): self
1403
    {
1404
        if (!$this->getGroups()->contains($group)) {
1405
            $this->getGroups()->add($group);
1406
        }
1407
1408
        return $this;
1409
    }
1410
1411
    public function removeGroup(Group $group): self
1412
    {
1413
        if ($this->getGroups()->contains($group)) {
1414
            $this->getGroups()->removeElement($group);
1415
        }
1416
1417
        return $this;
1418
    }
1419
1420
    public function isAccountNonExpired(): bool
1421
    {
1422
        return true;
1423
    }
1424
1425
    public function isAccountNonLocked(): bool
1426
    {
1427
        return true;
1428
    }
1429
1430
    public function isCredentialsNonExpired(): bool
1431
    {
1432
        return true;
1433
    }
1434
1435
    public function getCredentialsExpired(): bool
1436
    {
1437
        return $this->credentialsExpired;
1438
    }
1439
1440
    public function setCredentialsExpired(bool $boolean): self
1441
    {
1442
        $this->credentialsExpired = $boolean;
1443
1444
        return $this;
1445
    }
1446
1447
    public function getExpired(): bool
1448
    {
1449
        return $this->expired;
1450
    }
1451
1452
    /**
1453
     * Sets this user to expired.
1454
     */
1455
    public function setExpired(bool $boolean): self
1456
    {
1457
        $this->expired = $boolean;
1458
1459
        return $this;
1460
    }
1461
1462
    public function getLocked(): bool
1463
    {
1464
        return $this->locked;
1465
    }
1466
1467
    public function setLocked(bool $boolean): self
1468
    {
1469
        $this->locked = $boolean;
1470
1471
        return $this;
1472
    }
1473
1474
    /**
1475
     * Check if the user has the skill.
1476
     *
1477
     * @param Skill $skill The skill
1478
     */
1479
    public function hasSkill(Skill $skill): bool
1480
    {
1481
        $achievedSkills = $this->getAchievedSkills();
1482
        foreach ($achievedSkills as $userSkill) {
1483
            if ($userSkill->getSkill()->getId() !== $skill->getId()) {
1484
                continue;
1485
            }
1486
1487
            return true;
1488
        }
1489
1490
        return false;
1491
    }
1492
1493
    /**
1494
     * @return Collection<int, SkillRelUser>
1495
     */
1496
    public function getAchievedSkills(): Collection
1497
    {
1498
        return $this->achievedSkills;
1499
    }
1500
1501
    /**
1502
     * @param Collection<int, SkillRelUser> $value
1503
     */
1504
    public function setAchievedSkills(Collection $value): self
1505
    {
1506
        $this->achievedSkills = $value;
1507
1508
        return $this;
1509
    }
1510
1511
    public function isProfileCompleted(): ?bool
1512
    {
1513
        return $this->profileCompleted;
1514
    }
1515
1516
    public function setProfileCompleted(?bool $profileCompleted): self
1517
    {
1518
        $this->profileCompleted = $profileCompleted;
1519
1520
        return $this;
1521
    }
1522
1523
    public function getCurrentUrl(): ?AccessUrl
1524
    {
1525
        return $this->currentUrl;
1526
    }
1527
1528
    public function setCurrentUrl(AccessUrl $url): self
1529
    {
1530
        $accessUrlRelUser = (new AccessUrlRelUser())->setUrl($url)->setUser($this);
1531
        $this->getPortals()->add($accessUrlRelUser);
1532
1533
        return $this;
1534
    }
1535
1536
    /**
1537
     * @return Collection<int, AccessUrlRelUser>
1538
     */
1539
    public function getPortals(): Collection
1540
    {
1541
        return $this->portals;
1542
    }
1543
1544
    /**
1545
     * @param Collection<int, AccessUrlRelUser> $value
1546
     */
1547
    public function setPortals(Collection $value): void
1548
    {
1549
        $this->portals = $value;
1550
    }
1551
1552
    /**
1553
     * @return array<int, Session>
1554
     */
1555
    public function getSessionsAsGeneralCoach(): array
1556
    {
1557
        return $this->getSessions(Session::GENERAL_COACH);
1558
    }
1559
1560
    /**
1561
     * Retrieves this user's related sessions.
1562
     */
1563
    public function getSessions(int $relationType): array
1564
    {
1565
        $sessions = [];
1566
        foreach ($this->getSessionsRelUser() as $sessionRelUser) {
1567
            if ($sessionRelUser->getRelationType() === $relationType) {
1568
                $sessions[] = $sessionRelUser->getSession();
1569
            }
1570
        }
1571
1572
        return $sessions;
1573
    }
1574
1575
    /**
1576
     * @return Collection<int, SessionRelUser>
1577
     */
1578
    public function getSessionsRelUser(): Collection
1579
    {
1580
        return $this->sessionsRelUser;
1581
    }
1582
1583
    public function getSessionsAsAdmin(): array
1584
    {
1585
        return $this->getSessions(Session::SESSION_ADMIN);
1586
    }
1587
1588
    public function getCommentedUserSkills(): Collection
1589
    {
1590
        return $this->commentedUserSkills;
1591
    }
1592
1593
    /**
1594
     * @param Collection<int, SkillRelUserComment> $commentedUserSkills
1595
     */
1596
    public function setCommentedUserSkills(Collection $commentedUserSkills): self
1597
    {
1598
        $this->commentedUserSkills = $commentedUserSkills;
1599
1600
        return $this;
1601
    }
1602
1603
    public function isEqualTo(UserInterface $user): bool
1604
    {
1605
        if ($this->password !== $user->getPassword()) {
1606
            return false;
1607
        }
1608
        if ($this->salt !== $user->getSalt()) {
1609
            return false;
1610
        }
1611
        if ($this->username !== $user->getUserIdentifier()) {
1612
            return false;
1613
        }
1614
1615
        return true;
1616
    }
1617
1618
    public function getPassword(): ?string
1619
    {
1620
        return $this->password;
1621
    }
1622
1623
    public function setPassword(string $password): self
1624
    {
1625
        $this->password = $password;
1626
1627
        return $this;
1628
    }
1629
1630
    public function getSalt(): ?string
1631
    {
1632
        return $this->salt;
1633
    }
1634
1635
    public function setSalt(string $salt): self
1636
    {
1637
        $this->salt = $salt;
1638
1639
        return $this;
1640
    }
1641
1642
    public function getUserIdentifier(): string
1643
    {
1644
        return $this->username;
1645
    }
1646
1647
    /**
1648
     * @return Collection<int, Message>
1649
     */
1650
    public function getSentMessages(): Collection
1651
    {
1652
        return $this->sentMessages;
1653
    }
1654
1655
    public function getReceivedMessages(): Collection
1656
    {
1657
        return $this->receivedMessages;
1658
    }
1659
1660
    public function getCourseGroupsAsMember(): Collection
1661
    {
1662
        return $this->courseGroupsAsMember;
1663
    }
1664
1665
    public function getCourseGroupsAsTutor(): Collection
1666
    {
1667
        return $this->courseGroupsAsTutor;
1668
    }
1669
1670
    public function getCourseGroupsAsMemberFromCourse(Course $course): Collection
1671
    {
1672
        $criteria = Criteria::create();
1673
        $criteria->where(Criteria::expr()->eq('cId', $course));
1674
1675
        return $this->courseGroupsAsMember->matching($criteria);
1676
    }
1677
1678
    public function eraseCredentials(): void
1679
    {
1680
        $this->plainPassword = null;
1681
    }
1682
1683
    /**
1684
     * Returns whether a user can be admin of all multi-URL portals in the case of a multi-URL install.
1685
     */
1686
    public function isSuperAdmin(): bool
1687
    {
1688
        // Treat "global admin" as super-admin
1689
        return $this->hasRole('ROLE_GLOBAL_ADMIN');
1690
    }
1691
1692
    public function hasRole(string $role): bool
1693
    {
1694
        return \in_array(strtoupper($role), $this->getRoles(), true);
1695
    }
1696
1697
    /**
1698
     * Returns the user roles.
1699
     */
1700
    public function getRoles(): array
1701
    {
1702
        $roles = $this->roles;
1703
        foreach ($this->getGroups() as $group) {
1704
            $roles = array_merge($roles, $group->getRoles());
1705
        }
1706
        // we need to make sure to have at least one role
1707
        $roles[] = 'ROLE_USER';
1708
1709
        return array_unique($roles);
1710
    }
1711
1712
    public function setRoles(array $roles): self
1713
    {
1714
        $this->roles = [];
1715
        foreach ($roles as $role) {
1716
            $this->addRole($role);
1717
        }
1718
1719
        return $this;
1720
    }
1721
1722
    public function setRoleFromStatus(int $status): void
1723
    {
1724
        $role = self::getRoleFromStatus($status);
1725
        $this->addRole($role);
1726
    }
1727
1728
    public static function getRoleFromStatus(int $status): string
1729
    {
1730
        return match ($status) {
1731
            COURSEMANAGER => 'ROLE_TEACHER',
1732
            STUDENT => 'ROLE_STUDENT',
1733
            DRH => 'ROLE_HR',
1734
            SESSIONADMIN => 'ROLE_SESSION_MANAGER',
1735
            STUDENT_BOSS => 'ROLE_STUDENT_BOSS',
1736
            INVITEE => 'ROLE_INVITEE',
1737
            default => 'ROLE_USER',
1738
        };
1739
    }
1740
1741
    public function addRole(string $role): self
1742
    {
1743
        $role = strtoupper($role);
1744
        if ($role === static::ROLE_DEFAULT || empty($role)) {
1745
            return $this;
1746
        }
1747
        if (!\in_array($role, $this->roles, true)) {
1748
            $this->roles[] = $role;
1749
        }
1750
1751
        return $this;
1752
    }
1753
1754
    public function removeRole(string $role): self
1755
    {
1756
        if (false !== ($key = array_search(strtoupper($role), $this->roles, true))) {
1757
            unset($this->roles[$key]);
1758
            $this->roles = array_values($this->roles);
1759
        }
1760
1761
        return $this;
1762
    }
1763
1764
    public function getUsernameCanonical(): string
1765
    {
1766
        return $this->usernameCanonical;
1767
    }
1768
1769
    public function setUsernameCanonical(string $usernameCanonical): self
1770
    {
1771
        $this->usernameCanonical = $usernameCanonical;
1772
1773
        return $this;
1774
    }
1775
1776
    public function getEmailCanonical(): string
1777
    {
1778
        return $this->emailCanonical;
1779
    }
1780
1781
    public function setEmailCanonical(string $emailCanonical): self
1782
    {
1783
        $this->emailCanonical = $emailCanonical;
1784
1785
        return $this;
1786
    }
1787
1788
    public function getTimezone(): string
1789
    {
1790
        return $this->timezone;
1791
    }
1792
1793
    public function setTimezone(string $timezone): self
1794
    {
1795
        $this->timezone = $timezone;
1796
1797
        return $this;
1798
    }
1799
1800
    public function getLocale(): string
1801
    {
1802
        return $this->locale;
1803
    }
1804
1805
    public function setLocale(string $locale): self
1806
    {
1807
        $this->locale = $locale;
1808
1809
        return $this;
1810
    }
1811
1812
    public function getApiToken(): ?string
1813
    {
1814
        return $this->apiToken;
1815
    }
1816
1817
    public function setApiToken(string $apiToken): self
1818
    {
1819
        $this->apiToken = $apiToken;
1820
1821
        return $this;
1822
    }
1823
1824
    public function getWebsite(): ?string
1825
    {
1826
        return $this->website;
1827
    }
1828
1829
    public function setWebsite(string $website): self
1830
    {
1831
        $this->website = $website;
1832
1833
        return $this;
1834
    }
1835
1836
    public function getBiography(): ?string
1837
    {
1838
        return $this->biography;
1839
    }
1840
1841
    public function setBiography(string $biography): self
1842
    {
1843
        $this->biography = $biography;
1844
1845
        return $this;
1846
    }
1847
1848
    public function getDateOfBirth(): ?DateTime
1849
    {
1850
        return $this->dateOfBirth;
1851
    }
1852
1853
    public function setDateOfBirth(?DateTime $dateOfBirth = null): self
1854
    {
1855
        $this->dateOfBirth = $dateOfBirth;
1856
1857
        return $this;
1858
    }
1859
1860
    public function getProfileUrl(): string
1861
    {
1862
        return '/main/social/profile.php?u='.$this->id;
1863
    }
1864
1865
    public function getIconStatus(): string
1866
    {
1867
        $hasCertificates = $this->getGradeBookCertificates()->count() > 0;
1868
        $urlImg = '/img/';
1869
        if ($this->isStudent()) {
1870
            $iconStatus = $urlImg.'icons/svg/identifier_student.svg';
1871
            if ($hasCertificates) {
1872
                $iconStatus = $urlImg.'icons/svg/identifier_graduated.svg';
1873
            }
1874
1875
            return $iconStatus;
1876
        }
1877
        if ($this->isTeacher()) {
1878
            $iconStatus = $urlImg.'icons/svg/identifier_teacher.svg';
1879
            if ($this->isAdmin()) {
1880
                $iconStatus = $urlImg.'icons/svg/identifier_admin.svg';
1881
            }
1882
1883
            return $iconStatus;
1884
        }
1885
        if ($this->isStudentBoss()) {
1886
            return $urlImg.'icons/svg/identifier_teacher.svg';
1887
        }
1888
1889
        return '';
1890
    }
1891
1892
    public function getGradeBookCertificates(): Collection
1893
    {
1894
        return $this->gradeBookCertificates;
1895
    }
1896
1897
    /**
1898
     * @param Collection<int, GradebookCertificate> $gradeBookCertificates
1899
     */
1900
    public function setGradeBookCertificates(Collection $gradeBookCertificates): self
1901
    {
1902
        $this->gradeBookCertificates = $gradeBookCertificates;
1903
1904
        return $this;
1905
    }
1906
1907
    public function isStudent(): bool
1908
    {
1909
        return $this->hasRole('ROLE_STUDENT');
1910
    }
1911
1912
    public function isTeacher(): bool
1913
    {
1914
        return $this->hasRole('ROLE_TEACHER');
1915
    }
1916
1917
    public function isAdmin(): bool
1918
    {
1919
        return $this->hasRole('ROLE_ADMIN');
1920
    }
1921
1922
    public function isStudentBoss(): bool
1923
    {
1924
        return $this->hasRole('ROLE_STUDENT_BOSS');
1925
    }
1926
1927
    public function isSessionAdmin(): bool
1928
    {
1929
        return $this->hasRole('ROLE_SESSION_MANAGER');
1930
    }
1931
1932
    public function isInvitee(): bool
1933
    {
1934
        return $this->hasRole('ROLE_INVITEE');
1935
    }
1936
1937
    public function isHRM(): bool
1938
    {
1939
        return $this->hasRole('ROLE_HR');
1940
    }
1941
1942
    public function getStatus(): int
1943
    {
1944
        return $this->status;
1945
    }
1946
1947
    public function setStatus(int $status): self
1948
    {
1949
        $this->status = $status;
1950
1951
        return $this;
1952
    }
1953
1954
    public function getPictureUri(): ?string
1955
    {
1956
        return $this->pictureUri;
1957
    }
1958
1959
    /**
1960
     * @return Collection<int, GradebookCategory>
1961
     */
1962
    public function getGradeBookCategories(): Collection
1963
    {
1964
        return $this->gradeBookCategories;
1965
    }
1966
1967
    /**
1968
     * @return Collection<int, GradebookComment>
1969
     */
1970
    public function getGradeBookComments(): Collection
1971
    {
1972
        return $this->gradeBookComments;
1973
    }
1974
1975
    /**
1976
     * @return Collection<int, GradebookResult>
1977
     */
1978
    public function getGradeBookResults(): Collection
1979
    {
1980
        return $this->gradeBookResults;
1981
    }
1982
1983
    /**
1984
     * @return Collection<int, GradebookResultLog>
1985
     */
1986
    public function getGradeBookResultLogs(): Collection
1987
    {
1988
        return $this->gradeBookResultLogs;
1989
    }
1990
1991
    /**
1992
     * @return Collection<int, GradebookScoreLog>
1993
     */
1994
    public function getGradeBookScoreLogs(): Collection
1995
    {
1996
        return $this->gradeBookScoreLogs;
1997
    }
1998
1999
    /**
2000
     * @return Collection<int, GradebookLinkevalLog>
2001
     */
2002
    public function getGradeBookLinkEvalLogs(): Collection
2003
    {
2004
        return $this->gradeBookLinkEvalLogs;
2005
    }
2006
2007
    /**
2008
     * @return Collection<int, UserRelCourseVote>
2009
     */
2010
    public function getUserRelCourseVotes(): Collection
2011
    {
2012
        return $this->userRelCourseVotes;
2013
    }
2014
2015
    /**
2016
     * @return Collection<int, UserRelTag>
2017
     */
2018
    public function getUserRelTags(): Collection
2019
    {
2020
        return $this->userRelTags;
2021
    }
2022
2023
    public function getCurriculumItems(): Collection
2024
    {
2025
        return $this->curriculumItems;
2026
    }
2027
2028
    /**
2029
     * @return Collection<int, UserRelUser>
2030
     */
2031
    public function getFriends(): Collection
2032
    {
2033
        return $this->friends;
2034
    }
2035
2036
    /**
2037
     * @return Collection<int, UserRelUser>
2038
     */
2039
    public function getFriendsWithMe(): Collection
2040
    {
2041
        return $this->friendsWithMe;
2042
    }
2043
2044
    public function addFriend(self $friend): self
2045
    {
2046
        return $this->addUserRelUser($friend, UserRelUser::USER_RELATION_TYPE_FRIEND);
2047
    }
2048
2049
    public function addUserRelUser(self $friend, int $relationType): self
2050
    {
2051
        $userRelUser = (new UserRelUser())->setUser($this)->setFriend($friend)->setRelationType($relationType);
2052
        $this->friends->add($userRelUser);
2053
2054
        return $this;
2055
    }
2056
2057
    /**
2058
     * @return Collection<int, Templates>
2059
     */
2060
    public function getTemplates(): Collection
2061
    {
2062
        return $this->templates;
2063
    }
2064
2065
    public function getDropBoxReceivedFiles(): Collection
2066
    {
2067
        return $this->dropBoxReceivedFiles;
2068
    }
2069
2070
    /**
2071
     * @return Collection<int, SequenceValue>
2072
     */
2073
    public function getSequenceValues(): Collection
2074
    {
2075
        return $this->sequenceValues;
2076
    }
2077
2078
    /**
2079
     * @return Collection<int, TrackEExerciseConfirmation>
2080
     */
2081
    public function getTrackEExerciseConfirmations(): Collection
2082
    {
2083
        return $this->trackEExerciseConfirmations;
2084
    }
2085
2086
    /**
2087
     * @return Collection<int, TrackEAttempt>
2088
     */
2089
    public function getTrackEAccessCompleteList(): Collection
2090
    {
2091
        return $this->trackEAccessCompleteList;
2092
    }
2093
2094
    /**
2095
     * @return Collection<int, TrackEAttempt>
2096
     */
2097
    public function getTrackEAttempts(): Collection
2098
    {
2099
        return $this->trackEAttempts;
2100
    }
2101
2102
    /**
2103
     * @return Collection<int, TrackECourseAccess>
2104
     */
2105
    public function getTrackECourseAccess(): Collection
2106
    {
2107
        return $this->trackECourseAccess;
2108
    }
2109
2110
    /**
2111
     * @return Collection<int, UserCourseCategory>
2112
     */
2113
    public function getUserCourseCategories(): Collection
2114
    {
2115
        return $this->userCourseCategories;
2116
    }
2117
2118
    public function getCourseGroupsAsTutorFromCourse(Course $course): Collection
2119
    {
2120
        $criteria = Criteria::create();
2121
        $criteria->where(Criteria::expr()->eq('cId', $course->getId()));
2122
2123
        return $this->courseGroupsAsTutor->matching($criteria);
2124
    }
2125
2126
    /**
2127
     * Retrieves this user's related student sessions.
2128
     *
2129
     * @return Session[]
2130
     */
2131
    public function getSessionsAsStudent(): array
2132
    {
2133
        return $this->getSessions(Session::STUDENT);
2134
    }
2135
2136
    public function addSessionRelUser(SessionRelUser $sessionSubscription): static
2137
    {
2138
        $this->sessionsRelUser->add($sessionSubscription);
2139
2140
        return $this;
2141
    }
2142
2143
    public function isSkipResourceNode(): bool
2144
    {
2145
        return $this->skipResourceNode;
2146
    }
2147
2148
    public function setSkipResourceNode(bool $skipResourceNode): self
2149
    {
2150
        $this->skipResourceNode = $skipResourceNode;
2151
2152
        return $this;
2153
    }
2154
2155
    /**
2156
     * Retrieves this user's related DRH sessions.
2157
     *
2158
     * @return Session[]
2159
     */
2160
    public function getDRHSessions(): array
2161
    {
2162
        return $this->getSessions(Session::DRH);
2163
    }
2164
2165
    /**
2166
     * Get this user's related accessible sessions of a type, student by default.
2167
     *
2168
     * @return Session[]
2169
     */
2170
    public function getCurrentlyAccessibleSessions(int $relationType = Session::STUDENT): array
2171
    {
2172
        $sessions = [];
2173
        foreach ($this->getSessions($relationType) as $session) {
2174
            if ($session->isCurrentlyAccessible()) {
2175
                $sessions[] = $session;
2176
            }
2177
        }
2178
2179
        return $sessions;
2180
    }
2181
2182
    public function getResourceIdentifier(): int
2183
    {
2184
        return $this->id;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->id could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
2185
    }
2186
2187
    public function getResourceName(): string
2188
    {
2189
        return $this->getUsername();
2190
    }
2191
2192
    public function setResourceName(string $name): void
2193
    {
2194
        $this->setUsername($name);
2195
    }
2196
2197
    public function setParent(AbstractResource $parent): void {}
2198
2199
    public function getDefaultIllustration(int $size): string
2200
    {
2201
        $size = empty($size) ? 32 : $size;
2202
2203
        return \sprintf('/img/icons/%s/unknown.png', $size);
2204
    }
2205
2206
    public function getAdmin(): ?Admin
2207
    {
2208
        return $this->admin;
2209
    }
2210
2211
    public function setAdmin(?Admin $admin): self
2212
    {
2213
        $this->admin = $admin;
2214
2215
        return $this;
2216
    }
2217
2218
    public function addUserAsAdmin(): self
2219
    {
2220
        if (null === $this->admin) {
2221
            $admin = new Admin();
2222
            $admin->setUser($this);
2223
            $this->setAdmin($admin);
2224
            $this->addRole('ROLE_ADMIN');
2225
        }
2226
2227
        return $this;
2228
    }
2229
2230
    public function removeUserAsAdmin(): self
2231
    {
2232
        $this->admin->setUser(null);
0 ignored issues
show
Bug introduced by
The method setUser() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2232
        $this->admin->/** @scrutinizer ignore-call */ 
2233
                      setUser(null);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2233
        $this->admin = null;
2234
        $this->removeRole('ROLE_ADMIN');
2235
2236
        return $this;
2237
    }
2238
2239
    public function getSessionsByStatusInCourseSubscription(int $status): ReadableCollection
2240
    {
2241
        $criteria = Criteria::create()->where(Criteria::expr()->eq('status', $status));
2242
2243
        /** @var ArrayCollection $subscriptions */
2244
        $subscriptions = $this->getSessionRelCourseRelUsers();
2245
2246
        return $subscriptions->matching($criteria)->map(
2247
            fn (SessionRelCourseRelUser $sessionRelCourseRelUser) => $sessionRelCourseRelUser->getSession()
2248
        );
2249
    }
2250
2251
    /**
2252
     * @return Collection<int, SessionRelCourseRelUser>
2253
     */
2254
    public function getSessionRelCourseRelUsers(): Collection
2255
    {
2256
        return $this->sessionRelCourseRelUsers;
2257
    }
2258
2259
    /**
2260
     * @param Collection<int, SessionRelCourseRelUser> $sessionRelCourseRelUsers
2261
     */
2262
    public function setSessionRelCourseRelUsers(Collection $sessionRelCourseRelUsers): self
2263
    {
2264
        $this->sessionRelCourseRelUsers = $sessionRelCourseRelUsers;
2265
2266
        return $this;
2267
    }
2268
2269
    public function getGender(): ?string
2270
    {
2271
        return $this->gender;
2272
    }
2273
2274
    public function setGender(?string $gender): self
2275
    {
2276
        $this->gender = $gender;
2277
2278
        return $this;
2279
    }
2280
2281
    /**
2282
     * @return Collection<int, CSurveyInvitation>
2283
     */
2284
    public function getSurveyInvitations(): Collection
2285
    {
2286
        return $this->surveyInvitations;
2287
    }
2288
2289
    public function setSurveyInvitations(Collection $surveyInvitations): self
2290
    {
2291
        $this->surveyInvitations = $surveyInvitations;
2292
2293
        return $this;
2294
    }
2295
2296
    public function getLogin(): string
2297
    {
2298
        return $this->username;
2299
    }
2300
2301
    public function setLogin(string $login): self
2302
    {
2303
        $this->username = $login;
2304
2305
        return $this;
2306
    }
2307
2308
    /**
2309
     * @return Collection<int, TrackELogin>
2310
     */
2311
    public function getLogins(): Collection
2312
    {
2313
        return $this->logins;
2314
    }
2315
2316
    public function setLogins(Collection $logins): self
2317
    {
2318
        $this->logins = $logins;
2319
2320
        return $this;
2321
    }
2322
2323
    /**
2324
     * @return Collection<int, MessageTag>
2325
     */
2326
    public function getMessageTags(): Collection
2327
    {
2328
        return $this->messageTags;
2329
    }
2330
2331
    /**
2332
     * @param Collection<int, MessageTag> $messageTags
2333
     */
2334
    public function setMessageTags(Collection $messageTags): self
2335
    {
2336
        $this->messageTags = $messageTags;
2337
2338
        return $this;
2339
    }
2340
2341
    /**
2342
     * @param null|UserCourseCategory $userCourseCategory the user_course_category
2343
     *
2344
     * @todo move in a repo
2345
     * Find the largest sort value in a given UserCourseCategory
2346
     * This method is used when we are moving a course to a different category
2347
     * and also when a user subscribes to courses (the new course is added at the end of the main category).
2348
     *
2349
     * Used to be implemented in global function \api_max_sort_value.
2350
     * Reimplemented using the ORM cache.
2351
     */
2352
    public function getMaxSortValue(?UserCourseCategory $userCourseCategory = null): int
2353
    {
2354
        $categoryCourses = $this->courses->matching(
2355
            Criteria::create()->where(Criteria::expr()->neq('relationType', COURSE_RELATION_TYPE_RRHH))->andWhere(
2356
                Criteria::expr()->eq('userCourseCat', $userCourseCategory)
2357
            )
2358
        );
2359
2360
        return $categoryCourses->isEmpty() ? 0 : max(
2361
            $categoryCourses->map(fn ($courseRelUser) => $courseRelUser->getSort())->toArray()
2362
        );
2363
    }
2364
2365
    public function hasFriendWithRelationType(self $friend, int $relationType): bool
2366
    {
2367
        $friends = $this->getFriendsByRelationType($relationType);
2368
2369
        return $friends->exists(fn (int $index, UserRelUser $userRelUser) => $userRelUser->getFriend() === $friend);
2370
    }
2371
2372
    public function isFriendWithMeByRelationType(self $friend, int $relationType): bool
2373
    {
2374
        return $this
2375
            ->getFriendsWithMeByRelationType($relationType)
2376
            ->exists(fn (int $index, UserRelUser $userRelUser) => $userRelUser->getUser() === $friend)
2377
        ;
2378
    }
2379
2380
    /**
2381
     * @param int $relationType Example: UserRelUser::USER_RELATION_TYPE_BOSS
2382
     *
2383
     * @return Collection<int, UserRelUser>
2384
     */
2385
    public function getFriendsByRelationType(int $relationType): Collection
2386
    {
2387
        $criteria = Criteria::create();
2388
        $criteria->where(Criteria::expr()->eq('relationType', $relationType));
2389
2390
        return $this->friends->matching($criteria);
2391
    }
2392
2393
    public function getFriendsWithMeByRelationType(int $relationType): Collection
2394
    {
2395
        $criteria = Criteria::create();
2396
        $criteria->where(Criteria::expr()->eq('relationType', $relationType));
2397
2398
        return $this->friendsWithMe->matching($criteria);
2399
    }
2400
2401
    public function getFriendsOfFriends(): array
2402
    {
2403
        $friendsOfFriends = [];
2404
        foreach ($this->getFriends() as $friendRelation) {
2405
            foreach ($friendRelation->getFriend()->getFriends() as $friendOfFriendRelation) {
2406
                $friendsOfFriends[] = $friendOfFriendRelation->getFriend();
2407
            }
2408
        }
2409
2410
        return $friendsOfFriends;
2411
    }
2412
2413
    /**
2414
     * @return Collection<int, SocialPost>
2415
     */
2416
    public function getSentSocialPosts(): Collection
2417
    {
2418
        return $this->sentSocialPosts;
2419
    }
2420
2421
    public function addSentSocialPost(SocialPost $sentSocialPost): self
2422
    {
2423
        if (!$this->sentSocialPosts->contains($sentSocialPost)) {
2424
            $this->sentSocialPosts[] = $sentSocialPost;
2425
            $sentSocialPost->setSender($this);
2426
        }
2427
2428
        return $this;
2429
    }
2430
2431
    /**
2432
     * @return Collection<int, SocialPost>
2433
     */
2434
    public function getReceivedSocialPosts(): Collection
2435
    {
2436
        return $this->receivedSocialPosts;
2437
    }
2438
2439
    public function addReceivedSocialPost(SocialPost $receivedSocialPost): self
2440
    {
2441
        if (!$this->receivedSocialPosts->contains($receivedSocialPost)) {
2442
            $this->receivedSocialPosts[] = $receivedSocialPost;
2443
            $receivedSocialPost->setUserReceiver($this);
2444
        }
2445
2446
        return $this;
2447
    }
2448
2449
    public function getSocialPostFeedbackBySocialPost(SocialPost $post): ?SocialPostFeedback
2450
    {
2451
        $filtered = $this->getSocialPostsFeedbacks()->filter(
2452
            fn (SocialPostFeedback $postFeedback) => $postFeedback->getSocialPost() === $post
2453
        );
2454
        if ($filtered->count() > 0) {
2455
            return $filtered->first();
2456
        }
2457
2458
        return null;
2459
    }
2460
2461
    /**
2462
     * @return Collection<int, SocialPostFeedback>
2463
     */
2464
    public function getSocialPostsFeedbacks(): Collection
2465
    {
2466
        return $this->socialPostsFeedbacks;
2467
    }
2468
2469
    public function addSocialPostFeedback(SocialPostFeedback $socialPostFeedback): self
2470
    {
2471
        if (!$this->socialPostsFeedbacks->contains($socialPostFeedback)) {
2472
            $this->socialPostsFeedbacks[] = $socialPostFeedback;
2473
            $socialPostFeedback->setUser($this);
2474
        }
2475
2476
        return $this;
2477
    }
2478
2479
    public function getSubscriptionToSession(Session $session): ?SessionRelUser
2480
    {
2481
        $criteria = Criteria::create();
2482
        $criteria->where(
2483
            Criteria::expr()->eq('session', $session)
2484
        );
2485
2486
        $match = $this->sessionsRelUser->matching($criteria);
2487
2488
        if ($match->count() > 0) {
2489
            return $match->first();
2490
        }
2491
2492
        return null;
2493
    }
2494
2495
    public function getFirstAccessToSession(Session $session): ?TrackECourseAccess
2496
    {
2497
        $criteria = Criteria::create()
2498
            ->where(
2499
                Criteria::expr()->eq('sessionId', $session->getId())
2500
            )
2501
        ;
2502
2503
        $match = $this->trackECourseAccess->matching($criteria);
2504
2505
        return $match->count() > 0 ? $match->first() : null;
2506
    }
2507
2508
    public function isCourseTutor(?Course $course = null, ?Session $session = null): bool
2509
    {
2510
        return $session?->hasCoachInCourseList($this) || $course?->getSubscriptionByUser($this)?->isTutor();
2511
    }
2512
2513
    /**
2514
     * @return Collection<int, UserAuthSource>
2515
     */
2516
    public function getAuthSources(): Collection
2517
    {
2518
        return $this->authSources;
2519
    }
2520
2521
    /**
2522
     * @return Collection<int, UserAuthSource>
2523
     */
2524
    public function getAuthSourcesByUrl(AccessUrl $url): Collection
2525
    {
2526
        $criteria = Criteria::create();
2527
        $criteria->where(
2528
            Criteria::expr()->eq('url', $url)
2529
        );
2530
2531
        return $this->authSources->matching($criteria);
2532
    }
2533
2534
    /**
2535
     * @return array<int, string>
2536
     */
2537
    public function getAuthSourcesAuthentications(?AccessUrl $url = null): array
2538
    {
2539
        $authSources = $this->getAuthSourcesByUrl($url);
2540
2541
        if ($authSources->count() <= 0) {
2542
            return [];
2543
        }
2544
2545
        return $authSources->map(fn (UserAuthSource $authSource) => $authSource->getAuthentication())->toArray();
2546
    }
2547
2548
    public function addAuthSource(UserAuthSource $authSource): static
2549
    {
2550
        if (!$this->authSources->contains($authSource)) {
2551
            $this->authSources->add($authSource);
2552
            $authSource->setUser($this);
2553
        }
2554
2555
        return $this;
2556
    }
2557
2558
    public function addAuthSourceByAuthentication(string $authentication, AccessUrl $url): static
2559
    {
2560
        $authSource = $this->getAuthSourceByAuthentication($authentication, $url);
2561
2562
        if (!$authSource) {
2563
            $authSource = (new UserAuthSource())
2564
                ->setAuthentication($authentication)
2565
                ->setUrl($url)
2566
            ;
2567
2568
            $this->addAuthSource($authSource);
2569
        }
2570
2571
        return $this;
2572
    }
2573
2574
    public function hasAuthSourceByAuthentication(string $authentication): bool
2575
    {
2576
        return $this->authSources->exists(
2577
            fn ($key, $authSource) => $authSource instanceof UserAuthSource
2578
                && $authSource->getAuthentication() === $authentication
2579
        );
2580
    }
2581
2582
    public function getAuthSourceByAuthentication(string $authentication, AccessUrl $accessUrl): ?UserAuthSource
2583
    {
2584
        return $this->authSources->findFirst(
2585
            fn (int $index, UserAuthSource $authSource) => $authSource->getAuthentication() === $authentication
2586
                && $authSource->getUrl()->getId() === $accessUrl->getId()
2587
        );
2588
    }
2589
2590
    public function removeAuthSources(): static
2591
    {
2592
        foreach ($this->authSources as $authSource) {
2593
            $authSource->setUser(null);
2594
        }
2595
2596
        $this->authSources = new ArrayCollection();
2597
2598
        return $this;
2599
    }
2600
2601
    public function removeAuthSource(UserAuthSource $authSource): static
2602
    {
2603
        if ($this->authSources->removeElement($authSource)) {
2604
            // set the owning side to null (unless already changed)
2605
            if ($authSource->getUser() === $this) {
2606
                $authSource->setUser(null);
2607
            }
2608
        }
2609
2610
        return $this;
2611
    }
2612
2613
    public function getMfaEnabled(): bool
2614
    {
2615
        return $this->mfaEnabled;
2616
    }
2617
2618
    public function setMfaEnabled(bool $mfaEnabled): self
2619
    {
2620
        $this->mfaEnabled = $mfaEnabled;
2621
2622
        return $this;
2623
    }
2624
2625
    public function getMfaService(): ?string
2626
    {
2627
        return $this->mfaService;
2628
    }
2629
2630
    public function setMfaService(?string $mfaService): self
2631
    {
2632
        $this->mfaService = $mfaService;
2633
2634
        return $this;
2635
    }
2636
2637
    public function getMfaSecret(): ?string
2638
    {
2639
        return $this->mfaSecret;
2640
    }
2641
2642
    public function setMfaSecret(?string $mfaSecret): self
2643
    {
2644
        $this->mfaSecret = $mfaSecret;
2645
2646
        return $this;
2647
    }
2648
2649
    public function getMfaBackupCodes(): ?string
2650
    {
2651
        return $this->mfaBackupCodes;
2652
    }
2653
2654
    public function setMfaBackupCodes(?string $mfaBackupCodes): self
2655
    {
2656
        $this->mfaBackupCodes = $mfaBackupCodes;
2657
2658
        return $this;
2659
    }
2660
2661
    public function getMfaLastUsed(): ?DateTimeInterface
2662
    {
2663
        return $this->mfaLastUsed;
2664
    }
2665
2666
    public function setMfaLastUsed(?DateTimeInterface $mfaLastUsed): self
2667
    {
2668
        $this->mfaLastUsed = $mfaLastUsed;
2669
2670
        return $this;
2671
    }
2672
2673
    public function getPasswordUpdatedAt(): ?DateTimeInterface
2674
    {
2675
        return $this->passwordUpdatedAt;
2676
    }
2677
2678
    /**
2679
     * @return $this
2680
     */
2681
    public function setPasswordUpdatedAt(?DateTimeInterface $date): self
2682
    {
2683
        $this->passwordUpdatedAt = $date;
2684
2685
        return $this;
2686
    }
2687
2688
    public function getFullNameWithUsername(): string
2689
    {
2690
        return $this->getFullName().' ('.$this->getUsername().')';
2691
    }
2692
}
2693