Completed
Push — master ( c58ed8...556108 )
by Julito
10:31
created

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