Passed
Pull Request — master (#5143)
by Angel Fernando Quiroz
06:58
created

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