1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* For licensing terms, see /license.txt */ |
||
6 | |||
7 | namespace Chamilo\CourseBundle\Entity; |
||
8 | |||
9 | use ApiPlatform\Doctrine\Orm\Filter\OrderFilter; |
||
10 | use ApiPlatform\Doctrine\Orm\Filter\SearchFilter; |
||
11 | use ApiPlatform\Metadata\ApiFilter; |
||
12 | use ApiPlatform\Metadata\ApiProperty; |
||
13 | use ApiPlatform\Metadata\ApiResource; |
||
14 | use ApiPlatform\Metadata\Delete; |
||
15 | use ApiPlatform\Metadata\Get; |
||
16 | use ApiPlatform\Metadata\GetCollection; |
||
17 | use ApiPlatform\Metadata\Post; |
||
18 | use ApiPlatform\Metadata\Put; |
||
19 | use ApiPlatform\Serializer\Filter\PropertyFilter; |
||
20 | use Chamilo\CoreBundle\Controller\Api\CreateDocumentFileAction; |
||
21 | use Chamilo\CoreBundle\Controller\Api\DownloadSelectedDocumentsAction; |
||
22 | use Chamilo\CoreBundle\Controller\Api\ReplaceDocumentFileAction; |
||
23 | use Chamilo\CoreBundle\Controller\Api\UpdateDocumentFileAction; |
||
24 | use Chamilo\CoreBundle\Controller\Api\UpdateVisibilityDocument; |
||
25 | use Chamilo\CoreBundle\Entity\AbstractResource; |
||
26 | use Chamilo\CoreBundle\Entity\GradebookCategory; |
||
27 | use Chamilo\CoreBundle\Entity\Listener\ResourceListener; |
||
28 | use Chamilo\CoreBundle\Entity\ResourceInterface; |
||
29 | use Chamilo\CoreBundle\Entity\ResourceNode; |
||
30 | use Chamilo\CoreBundle\Entity\ResourceShowCourseResourcesInSessionInterface; |
||
31 | use Chamilo\CoreBundle\Filter\CidFilter; |
||
32 | use Chamilo\CoreBundle\Filter\SidFilter; |
||
33 | use Chamilo\CourseBundle\Repository\CDocumentRepository; |
||
34 | use Doctrine\Common\Collections\ArrayCollection; |
||
35 | use Doctrine\Common\Collections\Collection; |
||
36 | use Doctrine\ORM\Mapping as ORM; |
||
37 | use Stringable; |
||
38 | use Symfony\Component\Serializer\Annotation\Groups; |
||
39 | use Symfony\Component\Uid\Uuid; |
||
40 | use Symfony\Component\Validator\Constraints as Assert; |
||
41 | |||
42 | #[ApiResource( |
||
43 | shortName: 'Documents', |
||
44 | operations: [ |
||
45 | new Put( |
||
46 | controller: UpdateDocumentFileAction::class, |
||
47 | security: "is_granted('EDIT', object.resourceNode)", |
||
48 | validationContext: [ |
||
49 | 'groups' => ['media_object_create', 'document:write'], |
||
50 | ], |
||
51 | deserialize: false |
||
52 | ), |
||
53 | new Put( |
||
54 | uriTemplate: '/documents/{iid}/toggle_visibility', |
||
55 | controller: UpdateVisibilityDocument::class, |
||
56 | security: "is_granted('EDIT', object.resourceNode)", |
||
57 | deserialize: false |
||
58 | ), |
||
59 | new Put( |
||
60 | uriTemplate: '/documents/{iid}/move', |
||
61 | controller: UpdateDocumentFileAction::class, |
||
62 | security: "is_granted('EDIT', object.resourceNode)", |
||
63 | deserialize: true |
||
64 | ), |
||
65 | new Post( |
||
66 | uriTemplate: '/documents/{iid}/replace', |
||
67 | controller: ReplaceDocumentFileAction::class, |
||
68 | openapiContext: [ |
||
69 | 'summary' => 'Replace a document file, maintaining the same IDs.', |
||
70 | 'requestBody' => [ |
||
71 | 'content' => [ |
||
72 | 'multipart/form-data' => [ |
||
73 | 'schema' => [ |
||
74 | 'type' => 'object', |
||
75 | 'properties' => [ |
||
76 | 'file' => [ |
||
77 | 'type' => 'string', |
||
78 | 'format' => 'binary', |
||
79 | ], |
||
80 | ], |
||
81 | ], |
||
82 | ], |
||
83 | ], |
||
84 | ], |
||
85 | ], |
||
86 | security: "is_granted('ROLE_CURRENT_COURSE_TEACHER') or is_granted('ROLE_CURRENT_COURSE_SESSION_TEACHER') or is_granted('ROLE_TEACHER')", |
||
87 | validationContext: ['groups' => ['Default', 'media_object_create', 'document:write']], |
||
88 | deserialize: false |
||
89 | ), |
||
90 | new Get(security: "is_granted('VIEW', object.resourceNode)"), |
||
91 | new Delete(security: "is_granted('DELETE', object.resourceNode)"), |
||
92 | new Post( |
||
93 | controller: CreateDocumentFileAction::class, |
||
94 | openapiContext: [ |
||
95 | 'requestBody' => [ |
||
96 | 'content' => [ |
||
97 | 'multipart/form-data' => [ |
||
98 | 'schema' => [ |
||
99 | 'type' => 'object', |
||
100 | 'properties' => [ |
||
101 | 'title' => ['type' => 'string'], |
||
102 | 'filetype' => [ |
||
103 | 'type' => 'string', |
||
104 | 'enum' => ['folder', 'file'], |
||
105 | ], |
||
106 | 'comment' => ['type' => 'string'], |
||
107 | 'contentFile' => ['type' => 'string'], |
||
108 | 'uploadFile' => [ |
||
109 | 'type' => 'string', |
||
110 | 'format' => 'binary', |
||
111 | ], |
||
112 | 'parentResourceNodeId' => ['type' => 'integer'], |
||
113 | 'resourceLinkList' => [ |
||
114 | 'type' => 'array', |
||
115 | 'items' => [ |
||
116 | 'type' => 'object', |
||
117 | 'properties' => [ |
||
118 | 'visibility' => ['type' => 'integer'], |
||
119 | 'cid' => ['type' => 'integer'], |
||
120 | 'gid' => ['type' => 'integer'], |
||
121 | 'sid' => ['type' => 'integer'], |
||
122 | ], |
||
123 | ], |
||
124 | ], |
||
125 | 'isUncompressZipEnabled' => ['type' => 'boolean'], |
||
126 | 'fileExistsOption' => [ |
||
127 | 'type' => 'string', |
||
128 | 'enum' => ['overwrite', 'skip', 'rename'], |
||
129 | ], |
||
130 | ], |
||
131 | ], |
||
132 | ], |
||
133 | ], |
||
134 | ], |
||
135 | ], |
||
136 | security: "is_granted('ROLE_CURRENT_COURSE_TEACHER') or is_granted('ROLE_CURRENT_COURSE_SESSION_TEACHER') or is_granted('ROLE_TEACHER')", |
||
137 | validationContext: ['groups' => ['Default', 'media_object_create', 'document:write']], |
||
138 | deserialize: false |
||
139 | ), |
||
140 | new Post( |
||
141 | uriTemplate: '/documents/download-selected', |
||
142 | controller: DownloadSelectedDocumentsAction::class, |
||
143 | openapiContext: [ |
||
144 | 'summary' => 'Download selected documents as a ZIP file.', |
||
145 | 'requestBody' => [ |
||
146 | 'content' => [ |
||
147 | 'application/json' => [ |
||
148 | 'schema' => [ |
||
149 | 'type' => 'object', |
||
150 | 'properties' => [ |
||
151 | 'ids' => [ |
||
152 | 'type' => 'array', |
||
153 | 'items' => ['type' => 'integer'], |
||
154 | ], |
||
155 | ], |
||
156 | ], |
||
157 | ], |
||
158 | ], |
||
159 | ], |
||
160 | ], |
||
161 | security: "is_granted('ROLE_USER')", |
||
162 | ), |
||
163 | new GetCollection( |
||
164 | openapiContext: [ |
||
165 | 'parameters' => [ |
||
166 | [ |
||
167 | 'name' => 'resourceNode.parent', |
||
168 | 'in' => 'query', |
||
169 | 'required' => true, |
||
170 | 'description' => 'Resource node Parent', |
||
171 | 'schema' => ['type' => 'integer'], |
||
172 | ], |
||
173 | ], |
||
174 | ] |
||
175 | ), |
||
176 | ], |
||
177 | normalizationContext: [ |
||
178 | 'groups' => ['document:read', 'resource_node:read'], |
||
179 | ], |
||
180 | denormalizationContext: [ |
||
181 | 'groups' => ['document:write'], |
||
182 | ] |
||
183 | )] |
||
184 | #[ORM\Table(name: 'c_document')] |
||
185 | #[ORM\Index(columns: ['filetype'], name: 'idx_cdoc_type')] |
||
186 | #[ORM\Entity(repositoryClass: CDocumentRepository::class)] |
||
187 | #[ORM\EntityListeners([ResourceListener::class])] |
||
188 | #[ApiFilter(filterClass: PropertyFilter::class)] |
||
189 | #[ApiFilter(filterClass: SearchFilter::class, properties: ['title' => 'partial', 'resourceNode.parent' => 'exact', 'filetype' => 'exact'])] |
||
190 | #[ApiFilter( |
||
191 | filterClass: OrderFilter::class, |
||
192 | properties: [ |
||
193 | 'iid', |
||
194 | 'filetype', |
||
195 | 'resourceNode.title', |
||
196 | 'resourceNode.createdAt', |
||
197 | 'resourceNode.firstResourceFile.size', |
||
198 | 'resourceNode.updatedAt', |
||
199 | ] |
||
200 | )] |
||
201 | #[ApiFilter(filterClass: CidFilter::class)] |
||
202 | #[ApiFilter(filterClass: SidFilter::class)] |
||
203 | class CDocument extends AbstractResource implements ResourceInterface, ResourceShowCourseResourcesInSessionInterface, Stringable |
||
204 | { |
||
205 | #[ApiProperty(identifier: true)] |
||
206 | #[Groups(['document:read'])] |
||
207 | #[ORM\Column(name: 'iid', type: 'integer')] |
||
208 | #[ORM\Id] |
||
209 | #[ORM\GeneratedValue] |
||
210 | protected ?int $iid = null; |
||
211 | |||
212 | #[Groups(['document:read', 'document:write', 'document:browse', 'student_publication_rel_document:read'])] |
||
213 | #[Assert\NotBlank] |
||
214 | #[ORM\Column(name: 'title', type: 'string', length: 255, nullable: false)] |
||
215 | protected string $title; |
||
216 | |||
217 | #[Groups(['document:read', 'document:write'])] |
||
218 | #[ORM\Column(name: 'comment', type: 'text', nullable: true)] |
||
219 | protected ?string $comment; |
||
220 | |||
221 | #[Groups(['document:read', 'document:write'])] |
||
222 | #[Assert\Choice(['folder', 'file', 'certificate'], message: 'Choose a valid filetype.')] |
||
223 | #[ORM\Column(name: 'filetype', type: 'string', length: 15, nullable: false)] |
||
224 | protected string $filetype; |
||
225 | |||
226 | #[ORM\Column(name: 'readonly', type: 'boolean', nullable: false)] |
||
227 | protected bool $readonly; |
||
228 | |||
229 | #[Groups(['document:read', 'document:write'])] |
||
230 | #[ORM\Column(name: 'template', type: 'boolean', nullable: false)] |
||
231 | protected bool $template; |
||
232 | |||
233 | #[Groups(['document:read'])] |
||
234 | #[ORM\OneToMany(mappedBy: 'document', targetEntity: GradebookCategory::class)] |
||
235 | private Collection $gradebookCategories; |
||
236 | |||
237 | public function __construct() |
||
238 | { |
||
239 | $this->comment = ''; |
||
240 | $this->filetype = 'folder'; |
||
241 | $this->readonly = false; |
||
242 | $this->template = false; |
||
243 | $this->gradebookCategories = new ArrayCollection(); |
||
244 | } |
||
245 | |||
246 | public function __toString(): string |
||
247 | { |
||
248 | return $this->getTitle(); |
||
249 | } |
||
250 | |||
251 | public function getTitle(): string |
||
252 | { |
||
253 | return $this->title; |
||
254 | } |
||
255 | |||
256 | public function setTitle(string $title): self |
||
257 | { |
||
258 | $this->title = $title; |
||
259 | |||
260 | return $this; |
||
261 | } |
||
262 | |||
263 | public function isTemplate(): bool |
||
264 | { |
||
265 | return $this->template; |
||
266 | } |
||
267 | |||
268 | public function setTemplate(bool $template): self |
||
269 | { |
||
270 | $this->template = $template; |
||
271 | |||
272 | return $this; |
||
273 | } |
||
274 | |||
275 | public function getComment(): ?string |
||
276 | { |
||
277 | return $this->comment; |
||
278 | } |
||
279 | |||
280 | public function setComment(?string $comment): self |
||
281 | { |
||
282 | $this->comment = $comment; |
||
283 | |||
284 | return $this; |
||
285 | } |
||
286 | |||
287 | public function getFiletype(): string |
||
288 | { |
||
289 | return $this->filetype; |
||
290 | } |
||
291 | |||
292 | public function setFiletype(string $filetype): self |
||
293 | { |
||
294 | $this->filetype = $filetype; |
||
295 | |||
296 | return $this; |
||
297 | } |
||
298 | |||
299 | public function getReadonly(): bool |
||
300 | { |
||
301 | return $this->readonly; |
||
302 | } |
||
303 | |||
304 | public function setReadonly(bool $readonly): self |
||
305 | { |
||
306 | $this->readonly = $readonly; |
||
307 | |||
308 | return $this; |
||
309 | } |
||
310 | |||
311 | public function getResourceIdentifier(): int|Uuid |
||
312 | { |
||
313 | return $this->getIid(); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
314 | } |
||
315 | |||
316 | public function getIid(): ?int |
||
317 | { |
||
318 | return $this->iid; |
||
319 | } |
||
320 | |||
321 | public function getResourceName(): string |
||
322 | { |
||
323 | return $this->getTitle(); |
||
324 | } |
||
325 | |||
326 | public function setResourceName(string $name): self |
||
327 | { |
||
328 | return $this->setTitle($name); |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * @return Collection<int, GradebookCategory> |
||
333 | */ |
||
334 | public function getGradebookCategories(): Collection |
||
335 | { |
||
336 | return $this->gradebookCategories; |
||
337 | } |
||
338 | |||
339 | public function addGradebookCategory(GradebookCategory $gradebookCategory): static |
||
340 | { |
||
341 | if (!$this->gradebookCategories->contains($gradebookCategory)) { |
||
342 | $this->gradebookCategories->add($gradebookCategory); |
||
343 | $gradebookCategory->setDocument($this); |
||
344 | } |
||
345 | |||
346 | return $this; |
||
347 | } |
||
348 | |||
349 | public function removeGradebookCategory(GradebookCategory $gradebookCategory): static |
||
350 | { |
||
351 | if ($this->gradebookCategories->removeElement($gradebookCategory)) { |
||
352 | // set the owning side to null (unless already changed) |
||
353 | if ($gradebookCategory->getDocument() === $this) { |
||
354 | $gradebookCategory->setDocument(null); |
||
355 | } |
||
356 | } |
||
357 | |||
358 | return $this; |
||
359 | } |
||
360 | |||
361 | #[Groups(['document:read', 'document:fullPath'])] |
||
362 | public function getFullPath(): string |
||
363 | { |
||
364 | $pathParts = [$this->getTitle()]; |
||
365 | |||
366 | $parent = $this->getParent(); |
||
367 | while ($parent instanceof ResourceNode) { |
||
368 | array_unshift($pathParts, $parent->getTitle()); |
||
369 | $parent = $parent->getParent(); |
||
370 | } |
||
371 | |||
372 | return implode('/', $pathParts); |
||
373 | } |
||
374 | } |
||
375 |