Completed
Push — master ( 91f8d1...9dd64d )
by
unknown
01:55 queued 39s
created

ResourceLink::getParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Entity;
8
9
use ApiPlatform\Metadata\ApiResource;
10
use Chamilo\CoreBundle\Entity\Listener\ResourceLinkListener;
11
use Chamilo\CoreBundle\Repository\ResourceLinkRepository;
12
use Chamilo\CoreBundle\Traits\TimestampableTypedEntity;
13
use Chamilo\CourseBundle\Entity\CGroup;
14
use DateTimeInterface;
15
use Doctrine\Common\Collections\ArrayCollection;
16
use Doctrine\Common\Collections\Collection;
17
use Doctrine\ORM\Mapping as ORM;
18
use Gedmo\Mapping\Annotation as Gedmo;
19
use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
20
use LogicException;
21
use Stringable;
22
use Symfony\Component\Serializer\Annotation\Groups;
23
24
#[ApiResource]
25
#[ORM\Table(name: 'resource_link')]
26
#[ORM\Index(
27
    columns: ['c_id', 'session_id', 'usergroup_id', 'group_id', 'user_id', 'resource_type_group'],
28
    name: 'idx_resource_link_sortable_groups'
29
)]
30
#[ORM\Index(
31
    columns: ['parent_id'],
32
    name: 'idx_resource_link_parent'
33
)]
34
#[ORM\Entity(repositoryClass: ResourceLinkRepository::class)]
35
#[ORM\EntityListeners([ResourceLinkListener::class])]
36
#[Gedmo\SoftDeleteable(fieldName: 'deletedAt', timeAware: false, hardDelete: true)]
37
class ResourceLink implements Stringable
38
{
39
    use SoftDeleteableEntity;
40
    use TimestampableTypedEntity;
41
42
    public const VISIBILITY_DRAFT = 0;
43
    public const VISIBILITY_PENDING = 1;
44
    public const VISIBILITY_PUBLISHED = 2;
45
46
    #[ORM\Id]
47
    #[ORM\Column(type: 'integer')]
48
    #[ORM\GeneratedValue]
49
    protected ?int $id = null;
50
51
    #[ORM\ManyToOne(targetEntity: ResourceNode::class, inversedBy: 'resourceLinks')]
52
    #[ORM\JoinColumn(name: 'resource_node_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
53
    protected ResourceNode $resourceNode;
54
55
    /**
56
     * Parent link for the document hierarchy by context (course/session).
57
     */
58
    #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
59
    #[ORM\JoinColumn(name: 'parent_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
60
    private ?self $parent = null;
61
62
    /**
63
     * @var Collection<int, self>
64
     *
65
     * Children links in the document hierarchy by context.
66
     */
67
    #[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)]
68
    private Collection $children;
69
70
    #[Gedmo\SortableGroup]
71
    #[ORM\ManyToOne(targetEntity: Course::class)]
72
    #[ORM\JoinColumn(name: 'c_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
73
    protected ?Course $course = null;
74
75
    #[Gedmo\SortableGroup]
76
    #[ORM\ManyToOne(targetEntity: Session::class, cascade: ['persist'], inversedBy: 'resourceLinks')]
77
    #[ORM\JoinColumn(name: 'session_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
78
    #[Groups(['resource_link:read', 'resource_link:write', 'student_publication:read', 'student_publication_comment:read'])]
79
    protected ?Session $session = null;
80
81
    #[Gedmo\SortableGroup]
82
    #[ORM\ManyToOne(targetEntity: Usergroup::class, cascade: ['persist'])]
83
    #[ORM\JoinColumn(name: 'usergroup_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
84
    protected ?Usergroup $userGroup = null;
85
86
    #[Gedmo\SortableGroup]
87
    #[ORM\ManyToOne(targetEntity: CGroup::class, cascade: ['persist'])]
88
    #[ORM\JoinColumn(name: 'group_id', referencedColumnName: 'iid', nullable: true, onDelete: 'CASCADE')]
89
    protected ?CGroup $group = null;
90
91
    #[Gedmo\SortableGroup]
92
    #[ORM\ManyToOne(targetEntity: User::class, cascade: ['persist'])]
93
    #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
94
    protected ?User $user = null;
95
96
    /**
97
     * @var Collection<int, ResourceRight>
98
     */
99
    #[ORM\OneToMany(
100
        mappedBy: 'resourceLink',
101
        targetEntity: ResourceRight::class,
102
        cascade: ['persist', 'remove'],
103
        orphanRemoval: true
104
    )]
105
    protected Collection $resourceRights;
106
107
    #[Groups([
108
        'ctool:read',
109
        'c_tool_intro:read',
110
        'student_publication:read',
111
    ])]
112
    #[ORM\Column(name: 'visibility', type: 'integer', nullable: false)]
113
    protected int $visibility;
114
115
    #[Groups(['resource_node:read', 'resource_node:write', 'document:write', 'document:read'])]
116
    #[ORM\Column(name: 'start_visibility_at', type: 'datetime', nullable: true)]
117
    protected ?DateTimeInterface $startVisibilityAt = null;
118
119
    #[Groups(['resource_node:read', 'resource_node:write', 'document:write', 'document:read'])]
120
    #[ORM\Column(name: 'end_visibility_at', type: 'datetime', nullable: true)]
121
    protected ?DateTimeInterface $endVisibilityAt = null;
122
123
    #[Gedmo\SortablePosition]
124
    #[ORM\Column]
125
    private int $displayOrder;
126
127
    #[Gedmo\SortableGroup]
128
    #[ORM\Column]
129
    private ?int $resourceTypeGroup;
130
131
    public function __construct()
132
    {
133
        $this->resourceRights = new ArrayCollection();
134
        $this->visibility = self::VISIBILITY_DRAFT;
135
        $this->children = new ArrayCollection();
136
    }
137
138
    public function __toString(): string
139
    {
140
        return (string) $this->getId();
141
    }
142
143
    public function getId(): ?int
144
    {
145
        return $this->id;
146
    }
147
148
    public function getResourceNode(): ResourceNode
149
    {
150
        return $this->resourceNode;
151
    }
152
153
    public function setResourceNode(ResourceNode $resourceNode): self
154
    {
155
        $this->resourceNode = $resourceNode;
156
        $this->resourceTypeGroup = $resourceNode->getResourceType()->getId();
157
158
        return $this;
159
    }
160
161
    /**
162
     * Parent link for the document hierarchy by context.
163
     */
164
    public function getParent(): ?self
165
    {
166
        return $this->parent;
167
    }
168
169
    public function setParent(?self $parent): self
170
    {
171
        $this->parent = $parent;
172
173
        return $this;
174
    }
175
176
    /**
177
     * @return Collection<int, self>
178
     */
179
    public function getChildren(): Collection
180
    {
181
        return $this->children;
182
    }
183
184
    public function getCourse(): ?Course
185
    {
186
        return $this->course;
187
    }
188
189
    public function setCourse(?Course $course = null): self
190
    {
191
        $this->course = $course;
192
193
        return $this;
194
    }
195
196
    public function getSession(): ?Session
197
    {
198
        return $this->session;
199
    }
200
201
    public function setSession(?Session $session = null): self
202
    {
203
        $this->session = $session;
204
205
        return $this;
206
    }
207
208
    public function hasSession(): bool
209
    {
210
        return null !== $this->session;
211
    }
212
213
    public function hasCourse(): bool
214
    {
215
        return null !== $this->course;
216
    }
217
218
    public function hasGroup(): bool
219
    {
220
        return null !== $this->group;
221
    }
222
223
    public function getGroup(): ?CGroup
224
    {
225
        return $this->group;
226
    }
227
228
    public function setGroup(?CGroup $group = null): self
229
    {
230
        $this->group = $group;
231
232
        return $this;
233
    }
234
235
    public function getUserGroup(): ?Usergroup
236
    {
237
        return $this->userGroup;
238
    }
239
240
    public function setUserGroup(?Usergroup $group = null): self
241
    {
242
        $this->userGroup = $group;
243
244
        return $this;
245
    }
246
247
    public function getUser(): ?User
248
    {
249
        return $this->user;
250
    }
251
252
    public function setUser(?User $user = null): self
253
    {
254
        $this->user = $user;
255
256
        return $this;
257
    }
258
259
    public function hasUser(): bool
260
    {
261
        return null !== $this->user;
262
    }
263
264
    public function addResourceRight(ResourceRight $right): self
265
    {
266
        if (!$this->resourceRights->contains($right)) {
267
            $right->setResourceLink($this);
268
            $this->resourceRights->add($right);
269
        }
270
271
        return $this;
272
    }
273
274
    /**
275
     * @return Collection<int, ResourceRight>
276
     */
277
    public function getResourceRights(): Collection
278
    {
279
        return $this->resourceRights;
280
    }
281
282
    public function setResourceRights(Collection $rights): self
283
    {
284
        $this->resourceRights = $rights;
285
286
        return $this;
287
    }
288
289
    public function getVisibility(): int
290
    {
291
        return $this->visibility;
292
    }
293
294
    public function setVisibility(int $visibility): self
295
    {
296
        if (!\in_array($visibility, self::getVisibilityList(), true)) {
297
            $message = \sprintf(
298
                'The visibility is not valid. Valid options: %s',
299
                print_r(self::getVisibilityList(), true)
300
            );
301
302
            throw new LogicException($message);
303
        }
304
        $this->visibility = $visibility;
305
306
        return $this;
307
    }
308
309
    public static function getVisibilityList(): array
310
    {
311
        return [
312
            'Draft' => self::VISIBILITY_DRAFT,
313
            'Pending' => self::VISIBILITY_PENDING,
314
            'Published' => self::VISIBILITY_PUBLISHED,
315
        ];
316
    }
317
318
    public function isPublished(): bool
319
    {
320
        return self::VISIBILITY_PUBLISHED === $this->getVisibility();
321
    }
322
323
    public function isPending(): bool
324
    {
325
        return self::VISIBILITY_PENDING === $this->getVisibility();
326
    }
327
328
    public function isDraft(): bool
329
    {
330
        return self::VISIBILITY_DRAFT === $this->getVisibility();
331
    }
332
333
    public function getVisibilityName(): string
334
    {
335
        return array_flip(static::getVisibilityList())[$this->getVisibility()];
336
    }
337
338
    public function getStartVisibilityAt(): ?DateTimeInterface
339
    {
340
        return $this->startVisibilityAt;
341
    }
342
343
    public function setStartVisibilityAt(?DateTimeInterface $startVisibilityAt): self
344
    {
345
        $this->startVisibilityAt = $startVisibilityAt;
346
347
        return $this;
348
    }
349
350
    public function getEndVisibilityAt(): ?DateTimeInterface
351
    {
352
        return $this->endVisibilityAt;
353
    }
354
355
    public function setEndVisibilityAt(?DateTimeInterface $endVisibilityAt): self
356
    {
357
        $this->endVisibilityAt = $endVisibilityAt;
358
359
        return $this;
360
    }
361
362
    public function getDisplayOrder(): int
363
    {
364
        return $this->displayOrder;
365
    }
366
367
    public function setDisplayOrder(int $displayOrder): static
368
    {
369
        $this->displayOrder = $displayOrder;
370
371
        return $this;
372
    }
373
374
    public function getResourceTypeGroup(): ?int
375
    {
376
        return $this->resourceTypeGroup;
377
    }
378
379
    public function setResourceTypeGroup(int $resourceTypeGroup): static
380
    {
381
        $this->resourceTypeGroup = $resourceTypeGroup;
382
383
        return $this;
384
    }
385
386
    public function moveUpPosition(): static
387
    {
388
        $this->displayOrder--;
389
390
        return $this;
391
    }
392
393
    public function moveDownPosition(): static
394
    {
395
        $this->displayOrder++;
396
397
        return $this;
398
    }
399
}
400