Completed
Push — master ( b582f4...6580e5 )
by
unknown
02:29 queued 01:29
created

CDocument::getReadonly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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