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