Passed
Push — master ( 13e36e...a9fdac )
by Yannick
06:45 queued 14s
created

CDocument::setTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
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\ResourceShowCourseResourcesInSessionInterface;
28
use Chamilo\CourseBundle\Repository\CDocumentRepository;
29
use Doctrine\ORM\Mapping as ORM;
30
use Stringable;
31
use Symfony\Component\Serializer\Annotation\Groups;
32
use Symfony\Component\Uid\Uuid;
33
use Symfony\Component\Validator\Constraints as Assert;
34
35
#[ApiResource(
36
    shortName: 'Documents',
37
    operations: [
38
        new Put(
39
            controller: UpdateDocumentFileAction::class,
40
            security: "is_granted('EDIT', object.resourceNode)",
41
            validationContext: [
42
                'groups' => ['media_object_create', 'document:write'],
43
            ],
44
            deserialize: false
45
        ),
46
        new Put(
47
            uriTemplate: '/documents/{iid}/toggle_visibility',
48
            controller: UpdateVisibilityDocument::class,
49
            security: "is_granted('EDIT', object.resourceNode)",
50
            deserialize: false
51
        ),
52
        new Get(security: "is_granted('VIEW', object.resourceNode)"),
53
        new Delete(security: "is_granted('DELETE', object.resourceNode)"),
54
        new Post(
55
            controller: CreateDocumentFileAction::class,
56
            openapiContext: [
57
                'requestBody' => [
58
                    'content' => [
59
                        'multipart/form-data' => [
60
                            'schema' => [
61
                                'type' => 'object',
62
                                'properties' => [
63
                                    'title' => ['type' => 'string'],
64
                                    'filetype' => [
65
                                        'type' => 'string',
66
                                        'enum' => ['folder', 'file'],
67
                                    ],
68
                                    'comment' => ['type' => 'string'],
69
                                    'contentFile' => ['type' => 'string'],
70
                                    'uploadFile' => [
71
                                        'type' => 'string',
72
                                        'format' => 'binary',
73
                                    ],
74
                                    'parentResourceNodeId' => ['type' => 'integer'],
75
                                    'resourceLinkList' => [
76
                                        'type' => 'array',
77
                                        'items' => [
78
                                            'type' => 'object',
79
                                            'properties' => [
80
                                                'visibility' => ['type' => 'integer'],
81
                                                'cid' => ['type' => 'integer'],
82
                                                'gid' => ['type' => 'integer'],
83
                                                'sid' => ['type' => 'integer'],
84
                                            ],
85
                                        ],
86
                                    ],
87
                                ],
88
                            ],
89
                        ],
90
                    ],
91
                ],
92
            ],
93
            security: "is_granted('ROLE_CURRENT_COURSE_TEACHER') or is_granted('ROLE_CURRENT_COURSE_SESSION_TEACHER')",
94
            validationContext: ['groups' => ['Default', 'media_object_create', 'document:write']],
95
            deserialize: false
96
        ),
97
        new GetCollection(
98
            openapiContext: [
99
                'parameters' => [
100
                    [
101
                        'name' => 'resourceNode.parent',
102
                        'in' => 'query',
103
                        'required' => true,
104
                        'description' => 'Resource node Parent',
105
                        'schema' => ['type' => 'integer'],
106
                    ],
107
                    [
108
                        'name' => 'cid',
109
                        'in' => 'query',
110
                        'required' => true,
111
                        'description' => 'Course id',
112
                        'schema' => [
113
                            'type' => 'integer',
114
                        ],
115
                    ],
116
                    [
117
                        'name' => 'sid',
118
                        'in' => 'query',
119
                        'required' => false,
120
                        'description' => 'Session id',
121
                        'schema' => [
122
                            'type' => 'integer',
123
                        ],
124
                    ],
125
                ],
126
            ]
127
        ),
128
    ],
129
    normalizationContext: [
130
        'groups' => ['document:read', 'resource_node:read'],
131
    ],
132
    denormalizationContext: [
133
        'groups' => ['document:write'],
134
    ]
135
)]
136
#[ORM\Table(name: 'c_document')]
137
#[ORM\Index(columns: ['filetype'], name: 'idx_cdoc_type')]
138
#[ORM\Entity(repositoryClass: CDocumentRepository::class)]
139
#[ORM\EntityListeners([ResourceListener::class])]
140
#[ApiFilter(filterClass: PropertyFilter::class)]
141
#[ApiFilter(filterClass: SearchFilter::class, properties: ['title' => 'partial', 'resourceNode.parent' => 'exact'])]
142
#[ApiFilter(
143
    filterClass: OrderFilter::class,
144
    properties: [
145
        'iid',
146
        'filetype',
147
        'resourceNode.title',
148
        'resourceNode.createdAt',
149
        'resourceNode.resourceFile.size',
150
        'resourceNode.updatedAt',
151
    ]
152
)]
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
    #[Groups(['document:read', 'document:write', 'document:browse'])]
162
    #[Assert\NotBlank]
163
    #[ORM\Column(name: 'title', type: 'string', length: 255, nullable: false)]
164
    protected string $title;
165
    #[Groups(['document:read', 'document:write'])]
166
    #[ORM\Column(name: 'comment', type: 'text', nullable: true)]
167
    protected ?string $comment;
168
    #[Groups(['document:read', 'document:write'])]
169
    #[Assert\Choice(['folder', 'file', 'certificate'], message: 'Choose a valid filetype.')]
170
    #[ORM\Column(name: 'filetype', type: 'string', length: 15, nullable: false)]
171
    protected string $filetype;
172
    #[ORM\Column(name: 'readonly', type: 'boolean', nullable: false)]
173
    protected bool $readonly;
174
    #[ORM\Column(name: 'template', type: 'boolean', nullable: false)]
175
    protected bool $template;
176
    #[ORM\OneToOne(mappedBy: 'document', targetEntity: GradebookCategory::class)]
177
    #[Groups(['document:read'])]
178
    protected GradebookCategory|null $gradebookCategory = null;
179
180
    public function __construct()
181
    {
182
        $this->comment = '';
183
        $this->filetype = 'folder';
184
        $this->readonly = false;
185
        $this->template = false;
186
    }
187
    public function __toString(): string
188
    {
189
        return $this->getTitle();
190
    }
191
    public function isTemplate(): bool
192
    {
193
        return $this->template;
194
    }
195
    public function setTemplate(bool $template): self
196
    {
197
        $this->template = $template;
198
199
        return $this;
200
    }
201
    public function setComment(?string $comment): self
202
    {
203
        $this->comment = $comment;
204
205
        return $this;
206
    }
207
    public function getComment(): ?string
208
    {
209
        return $this->comment;
210
    }
211
    public function setTitle(string $title): self
212
    {
213
        $this->title = $title;
214
215
        return $this;
216
    }
217
    public function getTitle(): string
218
    {
219
        return $this->title;
220
    }
221
    public function setFiletype(string $filetype): self
222
    {
223
        $this->filetype = $filetype;
224
225
        return $this;
226
    }
227
    public function getFiletype(): string
228
    {
229
        return $this->filetype;
230
    }
231
    public function setReadonly(bool $readonly): self
232
    {
233
        $this->readonly = $readonly;
234
235
        return $this;
236
    }
237
    public function getReadonly(): bool
238
    {
239
        return $this->readonly;
240
    }
241
242
    public function getIid(): ?int
243
    {
244
        return $this->iid;
245
    }
246
    public function getResourceIdentifier(): int|Uuid
247
    {
248
        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...
249
    }
250
    public function getResourceName(): string
251
    {
252
        return $this->getTitle();
253
    }
254
    public function setResourceName(string $name): self
255
    {
256
        return $this->setTitle($name);
257
    }
258
259
    public function getGradebookCategory(): GradebookCategory|null
260
    {
261
        return $this->gradebookCategory;
262
    }
263
}
264