CGlossary::setDescription()   A
last analyzed

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\OpenApi\Model\Operation;
20
use ApiPlatform\OpenApi\Model\Parameter;
21
use ApiPlatform\OpenApi\Model\RequestBody;
22
use ApiPlatform\OpenApi\Model\Response;
23
use ArrayObject;
24
use Chamilo\CoreBundle\Controller\Api\CreateCGlossaryAction;
25
use Chamilo\CoreBundle\Controller\Api\ExportCGlossaryAction;
26
use Chamilo\CoreBundle\Controller\Api\ExportGlossaryToDocumentsAction;
27
use Chamilo\CoreBundle\Controller\Api\GetGlossaryCollectionController;
28
use Chamilo\CoreBundle\Controller\Api\ImportCGlossaryAction;
29
use Chamilo\CoreBundle\Controller\Api\UpdateCGlossaryAction;
30
use Chamilo\CoreBundle\Entity\AbstractResource;
31
use Chamilo\CoreBundle\Entity\ResourceInterface;
32
use Chamilo\CoreBundle\Entity\ResourceShowCourseResourcesInSessionInterface;
33
use Chamilo\CourseBundle\Repository\CGlossaryRepository;
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
/**
41
 * Course glossary.
42
 */
43
#[ApiResource(
44
    shortName: 'Glossary',
45
    operations: [
46
        new Put(
47
            controller: UpdateCGlossaryAction::class,
48
            security: "is_granted('EDIT', object.resourceNode)",
49
            validationContext: [
50
                'groups' => ['media_object_create', 'glossary:write'],
51
            ],
52
            deserialize: false
53
        ),
54
        new Get(security: "is_granted('VIEW', object.resourceNode)"),
55
        new Delete(security: "is_granted('DELETE', object.resourceNode)"),
56
        new Post(
57
            controller: CreateCGlossaryAction::class,
58
            openapi: new Operation(
59
                requestBody: new RequestBody(
60
                    content: new ArrayObject([
61
                        'application/json' => [
62
                            'schema' => [
63
                                'type' => 'object',
64
                                'properties' => [
65
                                    'name' => ['type' => 'string'],
66
                                    'description' => ['type' => 'string'],
67
                                    'parentResourceNodeId' => ['type' => 'integer'],
68
                                    'resourceLinkList' => [
69
                                        'type' => 'array',
70
                                        'items' => [
71
                                            'type' => 'object',
72
                                            'properties' => [
73
                                                'visibility' => ['type' => 'integer'],
74
                                                'cid' => ['type' => 'integer'],
75
                                                'gid' => ['type' => 'integer'],
76
                                                'sid' => ['type' => 'integer'],
77
                                            ],
78
                                        ],
79
                                    ],
80
                                ],
81
                                'required' => ['name'],
82
                            ],
83
                        ],
84
                    ]),
85
                ),
86
            ),
87
            security: "is_granted('ROLE_CURRENT_COURSE_TEACHER') or is_granted('ROLE_CURRENT_COURSE_SESSION_TEACHER') or is_granted('ROLE_TEACHER')",
88
            validationContext: ['groups' => ['Default', 'media_object_create', 'glossary:write']],
89
            deserialize: false
90
        ),
91
        new GetCollection(
92
            controller: GetGlossaryCollectionController::class,
93
            openapi: new Operation(
94
                parameters: [
95
                    new Parameter(
96
                        name: 'resourceNode.parent',
97
                        in: 'query',
98
                        description: 'Resource node Parent',
99
                        required: true,
100
                        schema: ['type' => 'integer'],
101
                    ),
102
                    new Parameter(
103
                        name: 'cid',
104
                        in: 'query',
105
                        description: 'Course id',
106
                        required: true,
107
                        schema: [
108
                            'type' => 'integer',
109
                        ],
110
                    ),
111
                    new Parameter(
112
                        name: 'sid',
113
                        in: 'query',
114
                        description: 'Session id',
115
                        required: false,
116
                        schema: [
117
                            'type' => 'integer',
118
                        ],
119
                    ),
120
                    new Parameter(
121
                        name: 'q',
122
                        in: 'query',
123
                        description: 'Search term',
124
                        required: false,
125
                        schema: [
126
                            'type' => 'string',
127
                        ],
128
                    ),
129
                ],
130
            )
131
        ),
132
        new Post(
133
            uriTemplate: '/glossaries/import',
134
            controller: ImportCGlossaryAction::class,
135
            openapi: new Operation(
136
                summary: 'Import a glossary',
137
                requestBody: new RequestBody(
138
                    content: new ArrayObject([
139
                        'multipart/form-data' => [
140
                            'schema' => [
141
                                'type' => 'object',
142
                                'properties' => [
143
                                    'file' => [
144
                                        'type' => 'string',
145
                                        'format' => 'binary',
146
                                    ],
147
                                ],
148
                            ],
149
                        ],
150
                    ]),
151
                ),
152
                responses: [
153
                    200 => new Response(
154
                        description: 'Glossaries imported successfully',
155
                    ),
156
                ],
157
            ),
158
            security: "is_granted('ROLE_CURRENT_COURSE_TEACHER') or is_granted('ROLE_CURRENT_COURSE_SESSION_TEACHER') or is_granted('ROLE_TEACHER')",
159
            validationContext: ['groups' => ['Default', 'media_object_create', 'glossary:write']],
160
            deserialize: false
161
        ),
162
        new Post(
163
            uriTemplate: '/glossaries/export',
164
            controller: ExportCGlossaryAction::class,
165
            security: "is_granted('ROLE_CURRENT_COURSE_TEACHER') or is_granted('ROLE_CURRENT_COURSE_SESSION_TEACHER') or is_granted('ROLE_TEACHER')",
166
            validationContext: ['groups' => ['Default', 'media_object_create', 'glossary:write']],
167
            deserialize: false
168
        ),
169
        new Post(
170
            uriTemplate: '/glossaries/export_to_documents',
171
            controller: ExportGlossaryToDocumentsAction::class,
172
            security: "is_granted('ROLE_CURRENT_COURSE_TEACHER') or is_granted('ROLE_CURRENT_COURSE_SESSION_TEACHER') or is_granted('ROLE_TEACHER')",
173
            validationContext: ['groups' => ['Default', 'media_object_create', 'glossary:write']],
174
            deserialize: false
175
        ),
176
    ],
177
    normalizationContext: [
178
        'groups' => ['glossary:read', 'resource_node:read'],
179
    ],
180
    denormalizationContext: [
181
        'groups' => ['glossary:write'],
182
    ],
183
)]
184
#[ApiFilter(SearchFilter::class, properties: ['name' => 'partial'])]
185
#[ApiFilter(OrderFilter::class, properties: ['iid', 'name', 'createdAt', 'updatedAt'])]
186
#[ORM\Table(name: 'c_glossary')]
187
#[ORM\Entity(repositoryClass: CGlossaryRepository::class)]
188
class CGlossary extends AbstractResource implements ResourceInterface, ResourceShowCourseResourcesInSessionInterface, Stringable
189
{
190
    #[ApiProperty(identifier: true)]
191
    #[Groups(['glossary:read'])]
192
    #[ORM\Column(name: 'iid', type: 'integer')]
193
    #[ORM\Id]
194
    #[ORM\GeneratedValue]
195
    protected ?int $iid = null;
196
197
    #[Groups(['glossary:read', 'glossary:write'])]
198
    #[Assert\NotBlank]
199
    #[ORM\Column(name: 'title', type: 'text', nullable: false)]
200
    protected string $title;
201
202
    #[Groups(['glossary:read', 'glossary:write'])]
203
    #[ORM\Column(name: 'description', type: 'text', nullable: false)]
204
    protected ?string $description = null;
205
206
    public function __toString(): string
207
    {
208
        return $this->getTitle();
209
    }
210
211
    public function setTitle(string $title): self
212
    {
213
        $this->title = $title;
214
215
        return $this;
216
    }
217
218
    public function getTitle(): string
219
    {
220
        return $this->title;
221
    }
222
223
    public function setDescription(string $description): self
224
    {
225
        $this->description = $description;
226
227
        return $this;
228
    }
229
230
    /**
231
     * Get description.
232
     */
233
    public function getDescription(): ?string
234
    {
235
        return $this->description;
236
    }
237
238
    public function getIid(): ?int
239
    {
240
        return $this->iid;
241
    }
242
243
    public function getResourceIdentifier(): int|Uuid
244
    {
245
        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...
246
    }
247
248
    public function getResourceName(): string
249
    {
250
        return $this->getTitle();
251
    }
252
253
    public function setResourceName(string $name): self
254
    {
255
        return $this->setTitle($name);
256
    }
257
}
258