Passed
Pull Request — master (#5143)
by Angel Fernando Quiroz
07:00
created

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