Completed
Push — master ( c6cfcb...a88956 )
by Julito
11:58
created

CDocument   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 230
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 39
dl 0
loc 230
rs 10
c 3
b 0
f 0
wmc 20

20 Methods

Rating   Name   Duplication   Size   Complexity  
A getResourceName() 0 3 1
A getResourceIdentifier() 0 3 1
A getIid() 0 3 1
A getSession() 0 3 1
A setCourse() 0 5 1
A getCourse() 0 3 1
A setReadonly() 0 5 1
A isTemplate() 0 3 1
A __toString() 0 3 1
A setTitle() 0 5 1
A getFiletype() 0 3 1
A getTitle() 0 3 1
A setFiletype() 0 5 1
A setTemplate() 0 5 1
A getComment() 0 3 1
A setResourceName() 0 3 1
A setComment() 0 5 1
A __construct() 0 5 1
A setSession() 0 5 1
A getReadonly() 0 3 1
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CourseBundle\Entity;
6
7
use ApiPlatform\Core\Annotation\ApiFilter;
8
use ApiPlatform\Core\Annotation\ApiResource;
9
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
10
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
11
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
12
use Chamilo\CoreBundle\Controller\Api\CreateResourceNodeFileAction;
13
use Chamilo\CoreBundle\Controller\Api\UpdateResourceNodeFileAction;
14
use Chamilo\CoreBundle\Entity\AbstractResource;
15
use Chamilo\CoreBundle\Entity\Course;
16
use Chamilo\CoreBundle\Entity\ResourceInterface;
17
use Chamilo\CoreBundle\Entity\Session;
18
use Chamilo\CourseBundle\Traits\ShowCourseResourcesInSessionTrait;
19
use Doctrine\ORM\Mapping as ORM;
20
use Symfony\Component\Serializer\Annotation\Groups;
21
use Symfony\Component\Validator\Constraints as Assert;
22
23
//*      attributes={"security"="is_granted('ROLE_ADMIN')"},
24
/**
25
 * @ApiResource(
26
 *      shortName="Documents",
27
 *      normalizationContext={"groups"={"document:read", "resource_node:read"}},
28
 *      denormalizationContext={"groups"={"document:write"}},
29
 *     itemOperations={
30
 *     "put" ={
31
 *             "controller"=UpdateResourceNodeFileAction::class,
32
 *             "deserialize"=false,
33
 *             "security"="is_granted('ROLE_USER')",
34
 *             "validation_groups"={"Default", "media_object_create", "document:write"},
35
 *         },
36
 *     "get",
37
 *     "delete"
38
 *     },
39
 *      collectionOperations={
40
 *         "post"={
41
 *             "controller"=CreateResourceNodeFileAction::class,
42
 *             "deserialize"=false,
43
 *             "security"="is_granted('ROLE_USER')",
44
 *             "validation_groups"={"Default", "media_object_create", "document:write"},
45
 *             "openapi_context"={
46
 *                 "requestBody"={
47
 *                     "content"={
48
 *                         "multipart/form-data"={
49
 *                             "schema"={
50
 *                                 "type"="object",
51
 *                                 "properties"={
52
 *                                     "title"={
53
 *                                         "type"="string",
54
 *                                     },
55
 *                                     "filetype"={
56
 *                                         "type"="string",
57
 *                                         "enum"={"folder", "file"},
58
 *                                     },
59
 *                                     "comment"={
60
 *                                         "type"="string",
61
 *                                     },
62
 *                                     "contentFile"={
63
 *                                         "type"="string",
64
 *                                     },
65
 *                                     "uploadFile"={
66
 *                                         "type"="string",
67
 *                                         "format"="binary"
68
 *                                     },
69
 *                                     "parentResourceNodeId"={
70
 *                                         "type"="integer",
71
 *                                     },
72
 *                                     "resourceLinkList"={
73
 *                                         "type"="array",
74
 *                                         "items": {
75
 *                                              "type": "object",
76
 *                                              "properties"={
77
 *                                                  "visibility"={
78
 *                                                       "type"="integer",
79
 *                                                   },
80
 *                                                  "c_id"={
81
 *                                                       "type"="integer",
82
 *                                                   },
83
 *                                                   "session_id"={
84
 *                                                       "type"="integer",
85
 *                                                   },
86
 *                                              }
87
 *                                         }
88
 *                                     },
89
 *                                 }
90
 *                             }
91
 *                         }
92
 *                     }
93
 *                 }
94
 *             }
95
 *         },
96
 *         "get",
97
 *     },
98
 * )
99
 * @ApiFilter(SearchFilter::class, properties={"title": "partial", "resourceNode.parent": "exact"})
100
 * @ApiFilter(PropertyFilter::class)
101
 * @ApiFilter(
102
 *     OrderFilter::class,
103
 *     properties={
104
 *          "id",
105
 *          "filetype",
106
 *          "resourceNode.title",
107
 *          "resourceNode.createdAt",
108
 *          "resourceNode.resourceFile.size",
109
 *          "resourceNode.updatedAt"
110
 *      }
111
 * )
112
 *
113
 * @ORM\Table(
114
 *  name="c_document",
115
 *  indexes={
116
 *      @ORM\Index(name="idx_cdoc_size", columns={"size"}),
117
 *      @ORM\Index(name="idx_cdoc_type", columns={"filetype"}),
118
 *  }
119
 * )
120
 * GRID\Source(columns="iid, title, resourceNode.createdAt", filterable=false, groups={"resource"})
121
 * GRID\Source(columns="iid, title", filterable=false, groups={"editor"})
122
 * @ORM\EntityListeners({"Chamilo\CoreBundle\Entity\Listener\ResourceListener"})
123
 * @ORM\Entity
124
 */
125
class CDocument extends AbstractResource implements ResourceInterface
126
{
127
    use ShowCourseResourcesInSessionTrait;
128
129
    /**
130
     * @var int
131
     * @Groups({"document:read"})
132
     * @ORM\Column(name="iid", type="integer")
133
     * @ORM\Id
134
     * @ORM\GeneratedValue
135
     */
136
    protected $iid;
137
138
    /**
139
     * @var string
140
     * @Groups({"document:read", "document:write"})
141
     * @ORM\Column(name="title", type="string", length=255, nullable=true)
142
     */
143
    protected $title;
144
145
    /**
146
     * @var string
147
     * @Groups({"document:read", "document:write"})
148
     * @ORM\Column(name="comment", type="text", nullable=true)
149
     */
150
    protected $comment;
151
152
    /**
153
     * @var string File type, it can be 'folder' or 'file'
154
     * @Groups({"document:read", "document:write"})
155
     * @Assert\Choice({"folder", "file"}, message="Choose a valid filetype.")
156
     * @ORM\Column(name="filetype", type="string", length=10, nullable=false)
157
     */
158
    protected $filetype;
159
160
    /**
161
     * @var bool
162
     * @ORM\Column(name="readonly", type="boolean", nullable=false)
163
     */
164
    protected $readonly;
165
166
    /**
167
     * @var bool
168
     * @ORM\Column(name="template", type="boolean", nullable=false)
169
     */
170
    protected $template;
171
172
    /**
173
     * @var Course
174
     *
175
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course", cascade={"persist"})
176
     * @ORM\JoinColumn(name="c_id", referencedColumnName="id", onDelete="CASCADE" )
177
     */
178
    protected $course;
179
180
    /**
181
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Session", cascade={"persist"})
182
     * @ORM\JoinColumn(name="session_id", referencedColumnName="id", onDelete="CASCADE" )
183
     */
184
    protected $session;
185
186
    /**
187
     * CDocument constructor.
188
     */
189
    public function __construct()
190
    {
191
        $this->filetype = 'folder';
192
        $this->readonly = false;
193
        $this->template = false;
194
    }
195
196
    public function __toString(): string
197
    {
198
        return $this->getTitle();
199
    }
200
201
    public function isTemplate(): bool
202
    {
203
        return $this->template;
204
    }
205
206
    public function setTemplate(bool $template): self
207
    {
208
        $this->template = $template;
209
210
        return $this;
211
    }
212
213
    /**
214
     * Set comment.
215
     *
216
     * @param string $comment
217
     */
218
    public function setComment($comment): self
219
    {
220
        $this->comment = $comment;
221
222
        return $this;
223
    }
224
225
    /**
226
     * Get comment.
227
     *
228
     * @return string
229
     */
230
    public function getComment()
231
    {
232
        return $this->comment;
233
    }
234
235
    public function setTitle(string $title): self
236
    {
237
        $this->title = $title;
238
239
        return $this;
240
    }
241
242
    /**
243
     * Document title.
244
     */
245
    public function getTitle(): string
246
    {
247
        return (string) $this->title;
248
    }
249
250
    /**
251
     * Set filetype.
252
     */
253
    public function setFiletype(string $filetype): self
254
    {
255
        $this->filetype = $filetype;
256
257
        return $this;
258
    }
259
260
    /**
261
     * Get filetype.
262
     *
263
     * @return string
264
     */
265
    public function getFiletype()
266
    {
267
        return $this->filetype;
268
    }
269
270
    /**
271
     * Set readonly.
272
     *
273
     * @param bool $readonly
274
     *
275
     * @return CDocument
276
     */
277
    public function setReadonly($readonly)
278
    {
279
        $this->readonly = $readonly;
280
281
        return $this;
282
    }
283
284
    /**
285
     * Get readonly.
286
     *
287
     * @return bool
288
     */
289
    public function getReadonly()
290
    {
291
        return $this->readonly;
292
    }
293
294
    public function getCourse(): Course
295
    {
296
        return $this->course;
297
    }
298
299
    /**
300
     * @param Course $course
301
     *
302
     * @return CDocument
303
     */
304
    public function setCourse($course)
305
    {
306
        $this->course = $course;
307
308
        return $this;
309
    }
310
311
    /**
312
     * @return int
313
     */
314
    public function getIid()
315
    {
316
        return $this->iid;
317
    }
318
319
    /**
320
     * @return Session
321
     */
322
    public function getSession()
323
    {
324
        return $this->session;
325
    }
326
327
    /**
328
     * @param Session $session
329
     *
330
     * @return CDocument
331
     */
332
    public function setSession($session)
333
    {
334
        $this->session = $session;
335
336
        return $this;
337
    }
338
339
    /**
340
     * Resource identifier.
341
     */
342
    public function getResourceIdentifier(): int
343
    {
344
        return $this->getIid();
345
    }
346
347
    public function getResourceName(): string
348
    {
349
        return $this->getTitle();
350
    }
351
352
    public function setResourceName(string $name): self
353
    {
354
        return $this->setTitle($name);
355
    }
356
}
357