Passed
Push — master ( 1c618d...c1c6b0 )
by Yannick
10:36 queued 02:52
created

Usergroup::setUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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\Metadata\ApiResource;
10
use ApiPlatform\Metadata\Delete;
11
use ApiPlatform\Metadata\Get;
12
use ApiPlatform\Metadata\GetCollection;
13
use ApiPlatform\Metadata\Post;
14
use ApiPlatform\Metadata\Put;
15
use Chamilo\CoreBundle\Repository\Node\UsergroupRepository;
16
use Chamilo\CoreBundle\State\GroupMembersStateProvider;
17
use Chamilo\CoreBundle\State\UsergroupPostStateProcessor;
18
use Chamilo\CoreBundle\State\UsergroupStateProvider;
19
use Doctrine\Common\Collections\ArrayCollection;
20
use Doctrine\Common\Collections\Collection;
21
use Doctrine\ORM\Mapping as ORM;
22
use Gedmo\Timestampable\Traits\TimestampableEntity;
23
use Stringable;
24
use Symfony\Component\Serializer\Annotation\Groups;
25
use Symfony\Component\Validator\Constraints as Assert;
26
27
/**
28
 * Classes and social groups.
29
 */
30
#[ApiResource(
31
    operations: [
32
        new Get(
33
            uriTemplate: '/usergroup/{id}',
34
            normalizationContext: ['groups' => ['usergroup:read']],
35
            security: "is_granted('ROLE_USER')",
36
            name: 'get_usergroup'
37
        ),
38
        new Put(security: "is_granted('EDIT', object)"),
39
        new Delete(security: "is_granted('DELETE', object)"),
40
        new GetCollection(
41
            uriTemplate: '/usergroup/list/my',
42
            normalizationContext: ['groups' => ['usergroup:read']],
43
            security: "is_granted('ROLE_USER')",
44
            name: 'get_my_usergroups',
45
            provider: UsergroupStateProvider::class
46
        ),
47
        new GetCollection(
48
            uriTemplate: '/usergroup/list/newest',
49
            normalizationContext: ['groups' => ['usergroup:read']],
50
            security: "is_granted('ROLE_USER')",
51
            name: 'get_newest_usergroups',
52
            provider: UsergroupStateProvider::class
53
        ),
54
        new GetCollection(
55
            uriTemplate: '/usergroup/list/popular',
56
            normalizationContext: ['groups' => ['usergroup:read']],
57
            security: "is_granted('ROLE_USER')",
58
            name: 'get_popular_usergroups',
59
            provider: UsergroupStateProvider::class
60
        ),
61
        new GetCollection(
62
            uriTemplate: '/usergroups/search',
63
            normalizationContext: ['groups' => ['usergroup:read']],
64
            security: "is_granted('ROLE_USER')",
65
            name: 'search_usergroups',
66
            provider: UsergroupStateProvider::class
67
        ),
68
        new GetCollection(
69
            uriTemplate: '/usergroups/{id}/members',
70
            normalizationContext: ['groups' => ['usergroup:read']],
71
            security: "is_granted('ROLE_USER')",
72
            name: 'get_group_members',
73
            provider: GroupMembersStateProvider::class
74
        ),
75
        new Post(
76
            securityPostDenormalize: "is_granted('CREATE', object)",
77
            processor: UsergroupPostStateProcessor::class
78
        ),
79
    ],
80
    normalizationContext: [
81
        'groups' => ['usergroup:read'],
82
    ],
83
    denormalizationContext: [
84
        'groups' => ['usergroup:write'],
85
    ],
86
    security: "is_granted('ROLE_USER')",
87
)]
88
#[ORM\Table(name: 'usergroup')]
89
#[ORM\Entity(repositoryClass: UsergroupRepository::class)]
90
class Usergroup extends AbstractResource implements ResourceInterface, ResourceIllustrationInterface, ResourceWithAccessUrlInterface, Stringable
91
{
92
    use TimestampableEntity;
93
    public const SOCIAL_CLASS = 1;
94
    public const NORMAL_CLASS = 0;
95
    // Definition of constants for user permissions
96
    public const GROUP_USER_PERMISSION_ADMIN = 1; // The admin of a group
97
    public const GROUP_USER_PERMISSION_READER = 2; // A normal user
98
    public const GROUP_USER_PERMISSION_PENDING_INVITATION = 3; // When an admin/moderator invites a user
99
    public const GROUP_USER_PERMISSION_PENDING_INVITATION_SENT_BY_USER = 4; // A user requests to join a group
100
    public const GROUP_USER_PERMISSION_MODERATOR = 5; // A moderator of the group
101
    public const GROUP_USER_PERMISSION_ANONYMOUS = 6; // An anonymous user, not part of the group
102
    public const GROUP_USER_PERMISSION_HRM = 7; // A human resource manager
103
104
    public const GROUP_PERMISSION_OPEN = 1;
105
    public const GROUP_PERMISSION_CLOSED = 2;
106
107
    #[Groups(['usergroup:read'])]
108
    #[ORM\Column(name: 'id', type: 'integer', nullable: false)]
109
    #[ORM\Id]
110
    #[ORM\GeneratedValue]
111
    protected ?int $id = null;
112
    #[Assert\NotBlank]
113
    #[Groups(['usergroup:read', 'usergroup:write'])]
114
    #[ORM\Column(name: 'title', type: 'string', length: 255)]
115
    protected string $title;
116
    #[Groups(['usergroup:read', 'usergroup:write'])]
117
    #[ORM\Column(name: 'description', type: 'text', nullable: true)]
118
    protected ?string $description = null;
119
    #[Assert\NotBlank]
120
    #[Groups(['usergroup:read', 'usergroup:write'])]
121
    #[ORM\Column(name: 'group_type', type: 'integer', nullable: false)]
122
    protected int $groupType;
123
    #[ORM\Column(name: 'picture', type: 'string', length: 255, nullable: true)]
124
    protected ?string $picture = null;
125
    #[Groups(['usergroup:read', 'usergroup:write'])]
126
    #[ORM\Column(name: 'url', type: 'string', length: 255, nullable: true)]
127
    protected ?string $url = null;
128
    #[Assert\NotBlank]
129
    #[Groups(['usergroup:read', 'usergroup:write'])]
130
    #[ORM\Column(name: 'visibility', type: 'string', length: 255, nullable: false)]
131
    protected string $visibility;
132
    #[ORM\Column(name: 'author_id', type: 'integer', nullable: true)]
133
    protected ?int $authorId = null;
134
    #[Assert\NotBlank]
135
    #[Groups(['usergroup:read', 'usergroup:write'])]
136
    #[ORM\Column(name: 'allow_members_leave_group', type: 'integer')]
137
    protected int $allowMembersToLeaveGroup;
138
139
    /**
140
     * @var Collection<int, UsergroupRelUser>
141
     */
142
    #[ORM\OneToMany(mappedBy: 'usergroup', targetEntity: UsergroupRelUser::class, cascade: ['persist'])]
143
    protected Collection $users;
144
145
    /**
146
     * @var Collection<int, UsergroupRelCourse>
147
     */
148
    #[ORM\OneToMany(mappedBy: 'usergroup', targetEntity: UsergroupRelCourse::class, cascade: ['persist'])]
149
    protected Collection $courses;
150
151
    /**
152
     * @var Collection<int, UsergroupRelSession>
153
     */
154
    #[ORM\OneToMany(mappedBy: 'usergroup', targetEntity: UsergroupRelSession::class, cascade: ['persist'])]
155
    protected Collection $sessions;
156
157
    /**
158
     * @var Collection<int, UsergroupRelQuestion>
159
     */
160
    #[ORM\OneToMany(mappedBy: 'usergroup', targetEntity: UsergroupRelQuestion::class, cascade: ['persist'])]
161
    protected Collection $questions;
162
163
    /**
164
     * @var Collection<int, AccessUrlRelUserGroup>
165
     */
166
    #[ORM\OneToMany(mappedBy: 'userGroup', targetEntity: AccessUrlRelUserGroup::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
167
    protected Collection $urls;
168
169
    #[Groups(['usergroup:read'])]
170
    private ?int $memberCount = null;
171
172
    #[Groups(['usergroup:read', 'usergroup:write'])]
173
    private ?string $pictureUrl = '';
174
175
    public function __construct()
176
    {
177
        $this->groupType = self::NORMAL_CLASS;
178
        $this->visibility = GROUP_PERMISSION_OPEN;
179
        $this->allowMembersToLeaveGroup = 0;
180
        $this->users = new ArrayCollection();
181
        $this->urls = new ArrayCollection();
182
        $this->courses = new ArrayCollection();
183
        $this->sessions = new ArrayCollection();
184
        $this->questions = new ArrayCollection();
185
    }
186
    public function __toString(): string
187
    {
188
        return $this->getTitle();
189
    }
190
191
    /**
192
     * @return Collection<int, UsergroupRelUser>
193
     */
194
    public function getUsers(): Collection
195
    {
196
        return $this->users;
197
    }
198
199
    /**
200
     * @return Collection<int, AccessUrlRelUserGroup>
201
     */
202
    public function getUrls(): Collection
203
    {
204
        return $this->urls;
205
    }
206
    public function addAccessUrl(?AccessUrl $url): self
207
    {
208
        $urlRelUsergroup = new AccessUrlRelUserGroup();
209
        $urlRelUsergroup->setUserGroup($this);
210
        $urlRelUsergroup->setUrl($url);
211
        $this->addUrlRelUsergroup($urlRelUsergroup);
212
213
        return $this;
214
    }
215
    public function addUrlRelUsergroup(AccessUrlRelUserGroup $urlRelUsergroup): self
216
    {
217
        $urlRelUsergroup->setUserGroup($this);
218
        $this->urls[] = $urlRelUsergroup;
219
220
        return $this;
221
    }
222
    public function setUsers(Collection $users): void
223
    {
224
        $this->users = new ArrayCollection();
225
        foreach ($users as $user) {
226
            $this->addUsers($user);
227
        }
228
    }
229
    public function addUsers(UsergroupRelUser $user): self
230
    {
231
        $user->setUsergroup($this);
232
        $this->users[] = $user;
233
234
        return $this;
235
    }
236
    public function removeUsers(UsergroupRelUser $user): void
237
    {
238
        foreach ($this->users as $key => $value) {
239
            if ($value->getId() === $user->getId()) {
240
                unset($this->users[$key]);
241
            }
242
        }
243
    }
244
245
    /**
246
     * Get id.
247
     */
248
    public function getId(): ?int
249
    {
250
        return $this->id;
251
    }
252
    public function setTitle(string $title): self
253
    {
254
        $this->title = $title;
255
256
        return $this;
257
    }
258
    public function getTitle(): string
259
    {
260
        return $this->title;
261
    }
262
    public function setDescription(string $description): self
263
    {
264
        $this->description = $description;
265
266
        return $this;
267
    }
268
    public function getDescription(): ?string
269
    {
270
        return $this->description;
271
    }
272
    public function getGroupType(): int
273
    {
274
        return $this->groupType;
275
    }
276
    public function setGroupType(int $groupType): self
277
    {
278
        $this->groupType = $groupType;
279
280
        return $this;
281
    }
282
    public function getVisibility(): string
283
    {
284
        return $this->visibility;
285
    }
286
    public function setVisibility(string $visibility): self
287
    {
288
        $this->visibility = $visibility;
289
290
        return $this;
291
    }
292
    public function getUrl(): ?string
293
    {
294
        return $this->url;
295
    }
296
    public function setUrl(?string $url): self
297
    {
298
        $this->url = $url;
299
300
        return $this;
301
    }
302
    public function getAuthorId(): ?int
303
    {
304
        return $this->authorId;
305
    }
306
    public function setAuthorId(?int $authorId): self
307
    {
308
        $this->authorId = $authorId;
309
310
        return $this;
311
    }
312
    public function getAllowMembersToLeaveGroup(): int
313
    {
314
        return $this->allowMembersToLeaveGroup;
315
    }
316
    public function setAllowMembersToLeaveGroup(int $allowMembersToLeaveGroup): self
317
    {
318
        $this->allowMembersToLeaveGroup = $allowMembersToLeaveGroup;
319
320
        return $this;
321
    }
322
323
    /**
324
     * @return Collection<int, UsergroupRelCourse>
325
     */
326
    public function getCourses(): Collection
327
    {
328
        return $this->courses;
329
    }
330
    public function setCourses(Collection $courses): self
331
    {
332
        $this->courses = $courses;
333
334
        return $this;
335
    }
336
337
    /**
338
     * @return Collection<int, UsergroupRelSession>
339
     */
340
    public function getSessions(): Collection
341
    {
342
        return $this->sessions;
343
    }
344
    public function setSessions(Collection $sessions): self
345
    {
346
        $this->sessions = $sessions;
347
348
        return $this;
349
    }
350
351
    /**
352
     * @return Collection<int, UsergroupRelQuestion>
353
     */
354
    public function getQuestions(): Collection
355
    {
356
        return $this->questions;
357
    }
358
    public function setQuestions(Collection $questions): self
359
    {
360
        $this->questions = $questions;
361
362
        return $this;
363
    }
364
    public function getPicture(): ?string
365
    {
366
        return $this->picture;
367
    }
368
369
    public function setPicture(?string $picture): self
370
    {
371
        $this->picture = $picture;
372
373
        return $this;
374
    }
375
376
    public function getPictureUrl(): ?string
377
    {
378
        return $this->pictureUrl;
379
    }
380
381
    public function setPictureUrl(?string $pictureUrl): self
382
    {
383
        $this->pictureUrl = $pictureUrl;
384
385
        return $this;
386
    }
387
388
    public function getMemberCount(): ?int
389
    {
390
        return $this->memberCount;
391
    }
392
393
    public function setMemberCount(int $memberCount): self
394
    {
395
        $this->memberCount = $memberCount;
396
397
        return $this;
398
    }
399
400
    public function getDefaultIllustration(int $size): string
401
    {
402
        $size = empty($size) ? 32 : $size;
403
404
        return sprintf('/img/icons/%s/group_na.png', $size);
405
    }
406
    public function getResourceIdentifier(): int
407
    {
408
        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...
409
    }
410
    public function getResourceName(): string
411
    {
412
        return $this->getTitle();
413
    }
414
    public function setResourceName(string $name): self
415
    {
416
        return $this->setTitle($name);
417
    }
418
}
419