Passed
Push — master ( a86a3f...6d8511 )
by Julito
09:12
created

ResourceLink::getSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
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
namespace Chamilo\CoreBundle\Entity;
6
7
use ApiPlatform\Core\Annotation\ApiResource;
8
use Chamilo\CourseBundle\Entity\CGroup;
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
 * @ORM\Entity
16
 * @ORM\Table(name="resource_link")
17
 */
18
class ResourceLink
19
{
20
    public const VISIBILITY_DRAFT = 0;
21
    public const VISIBILITY_PENDING = 1;
22
    public const VISIBILITY_PUBLISHED = 2;
23
    public const VISIBILITY_DELETED = 3;
24
25
    /**
26
     * @ORM\Id
27
     * @ORM\Column(type="integer")
28
     * @ORM\GeneratedValue
29
     */
30
    protected $id;
31
32
    /**
33
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\ResourceNode", inversedBy="resourceLinks")
34
     * @ORM\JoinColumn(name="resource_node_id", referencedColumnName="id", onDelete="SET NULL")
35
     */
36
    protected $resourceNode;
37
38
    /**
39
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course", inversedBy="resourceLinks")
40
     * @ORM\JoinColumn(name="c_id", referencedColumnName="id", nullable=true)
41
     */
42
    protected $course;
43
44
    /**
45
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Session", inversedBy="resourceLinks")
46
     * @ORM\JoinColumn(name="session_id", referencedColumnName="id", nullable=true)
47
     */
48
    protected $session;
49
50
    /**
51
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\User")
52
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true)
53
     */
54
    protected $user;
55
56
    /**
57
     * @ORM\ManyToOne(targetEntity="Chamilo\CourseBundle\Entity\CGroup")
58
     * @ORM\JoinColumn(name="group_id", referencedColumnName="iid", nullable=true, onDelete="CASCADE")
59
     */
60
    protected $group;
61
62
    /**
63
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Usergroup")
64
     * @ORM\JoinColumn(name="usergroup_id", referencedColumnName="id", nullable=true)
65
     */
66
    protected $userGroup;
67
68
    /**
69
     * @ORM\OneToMany(
70
     *     targetEntity="Chamilo\CoreBundle\Entity\ResourceRight",
71
     *     mappedBy="resourceLink", cascade={"persist", "remove"}, orphanRemoval=true
72
     * )
73
     */
74
    protected $resourceRight;
75
76
    /**
77
     * @ORM\Column(name="visibility", type="integer", nullable=false)
78
     */
79
    protected $visibility;
80
81
    /**
82
     * @Groups({"resource_node:read", "resource_node:write", "document:write", "document:read"})
83
     *
84
     * @ORM\Column(name="start_visibility_at", type="datetime", nullable=true)
85
     */
86
    protected $startVisibilityAt;
87
88
    /**
89
     * @Groups({"resource_node:read", "resource_node:write", "document:write", "document:read"})
90
     *
91
     * @ORM\Column(name="end_visibility_at", type="datetime", nullable=true)
92
     */
93
    protected $endVisibilityAt;
94
95
    /**
96
     * Constructor.
97
     */
98
    public function __construct()
99
    {
100
        $this->resourceRight = new ArrayCollection();
101
        $this->visibility = self::VISIBILITY_DRAFT;
102
    }
103
104
    public function __toString(): string
105
    {
106
        return (string) $this->getId();
107
    }
108
109
    public function getStartVisibilityAt()
110
    {
111
        return $this->startVisibilityAt;
112
    }
113
114
    public function setStartVisibilityAt($startVisibilityAt): self
115
    {
116
        $this->startVisibilityAt = $startVisibilityAt;
117
118
        return $this;
119
    }
120
121
    public function getEndVisibilityAt()
122
    {
123
        return $this->endVisibilityAt;
124
    }
125
126
    public function setEndVisibilityAt($endVisibilityAt): self
127
    {
128
        $this->endVisibilityAt = $endVisibilityAt;
129
130
        return $this;
131
    }
132
133
    /**
134
     * @param ArrayCollection $rights
135
     */
136
    public function setResourceRight($rights): self
137
    {
138
        $this->resourceRight = $rights;
139
140
        /*foreach ($rights as $right) {
141
            $this->addResourceRight($right);
142
        }*/
143
144
        return $this;
145
    }
146
147
    public function addResourceRight(ResourceRight $right): self
148
    {
149
        $right->setResourceLink($this);
150
        $this->resourceRight[] = $right;
151
152
        return $this;
153
    }
154
155
    /**
156
     * @return ArrayCollection|ResourceRight[]
157
     */
158
    public function getResourceRight()
159
    {
160
        return $this->resourceRight;
161
    }
162
163
    /**
164
     * @return int
165
     */
166
    public function getId()
167
    {
168
        return $this->id;
169
    }
170
171
    public function setUser(User $user = null): self
172
    {
173
        $this->user = $user;
174
175
        return $this;
176
    }
177
178
    public function getCourse(): ?Course
179
    {
180
        return $this->course;
181
    }
182
183
    public function setCourse(Course $course = null): self
184
    {
185
        $this->course = $course;
186
187
        return $this;
188
    }
189
190
    public function getSession(): ?Session
191
    {
192
        return $this->session;
193
    }
194
195
    public function setSession(Session $session = null): self
196
    {
197
        $this->session = $session;
198
199
        return $this;
200
    }
201
202
    public function hasCourse(): bool
203
    {
204
        return null !== $this->course;
205
    }
206
207
    public function hasGroup(): bool
208
    {
209
        return null !== $this->group;
210
    }
211
212
    public function getGroup(): ?CGroup
213
    {
214
        return $this->group;
215
    }
216
217
    public function setGroup(CGroup $group = null): self
218
    {
219
        $this->group = $group;
220
221
        return $this;
222
    }
223
224
    public function getUserGroup(): ?Usergroup
225
    {
226
        return $this->userGroup;
227
    }
228
229
    public function setUserGroup(Usergroup $group = null): self
230
    {
231
        $this->userGroup = $group;
232
233
        return $this;
234
    }
235
236
    /**
237
     * Get user.
238
     */
239
    public function getUser(): ?User
240
    {
241
        return $this->user;
242
    }
243
244
    public function hasUser(): bool
245
    {
246
        return null !== $this->user;
247
    }
248
249
    public function setResourceNode(ResourceNode $resourceNode): self
250
    {
251
        $this->resourceNode = $resourceNode;
252
253
        return $this;
254
    }
255
256
    public function getResourceNode(): ResourceNode
257
    {
258
        return $this->resourceNode;
259
    }
260
261
    public function getVisibility(): int
262
    {
263
        return $this->visibility;
264
    }
265
266
    public function setVisibility(int $visibility): self
267
    {
268
        if (!in_array($visibility, self::getVisibilityList(), true)) {
269
            throw new \LogicException('The visibility is not valid');
270
        }
271
272
        $this->visibility = $visibility;
273
274
        return $this;
275
    }
276
277
    public function isPublished(): bool
278
    {
279
        return self::VISIBILITY_PUBLISHED === $this->getVisibility();
280
    }
281
282
    public function isPending(): bool
283
    {
284
        return self::VISIBILITY_PENDING === $this->getVisibility();
285
    }
286
287
    public function isDraft(): bool
288
    {
289
        return self::VISIBILITY_DRAFT === $this->getVisibility();
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 getVisibilityName(): string
303
    {
304
        return array_flip($this->getVisibilityList())[$this->getVisibility()];
305
    }
306
}
307