Passed
Push — master ( f7d79c...3c939b )
by Angel Fernando Quiroz
07:27
created

CDocument::addGradebookCategory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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