Completed
Push — master ( 9fe4f1...0c649e )
by Julito
10:07
created

CDocument::getResourceFieldName()   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
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CourseBundle\Entity;
5
6
use APY\DataGridBundle\Grid\Mapping as GRID;
7
use Chamilo\CoreBundle\Entity\Course;
8
use Chamilo\CoreBundle\Entity\Resource\AbstractResource;
9
use Chamilo\CoreBundle\Entity\Resource\ResourceInterface;
10
use Chamilo\CoreBundle\Entity\Resource\ResourceLink;
11
use Chamilo\CoreBundle\Entity\Session;
12
use Doctrine\ORM\Event\LifecycleEventArgs;
13
use Doctrine\ORM\Mapping as ORM;
14
15
/**
16
 * CDocument.
17
 *
18
 * @ORM\Table(
19
 *  name="c_document",
20
 *  indexes={
21
 *      @ORM\Index(name="course", columns={"c_id"}),
22
 *      @ORM\Index(name="idx_cdoc_path", columns={"path"}),
23
 *      @ORM\Index(name="idx_cdoc_size", columns={"size"}),
24
 *      @ORM\Index(name="idx_cdoc_id", columns={"id"}),
25
 *      @ORM\Index(name="idx_cdoc_type", columns={"filetype"}),
26
 *      @ORM\Index(name="idx_cdoc_sid", columns={"session_id"}),
27
 *  }
28
 * )
29
 * @GRID\Source(columns="iid, title, filetype", filterable=false)
30
 * @ORM\Entity
31
 */
32
class CDocument extends AbstractResource implements ResourceInterface
33
{
34
    /**
35
     * @var int
36
     *
37
     * @ORM\Column(name="iid", type="integer")
38
     * @ORM\Id
39
     * @ORM\GeneratedValue
40
     */
41
    protected $iid;
42
43
    /**
44
     * @var int
45
     *
46
     * @ORM\Column(name="id", type="integer", nullable=true)
47
     */
48
    protected $id;
49
50
    /**
51
     * @var string
52
     *
53
     * @ORM\Column(name="path", type="string", length=255, nullable=true)
54
     */
55
    protected $path;
56
57
    /**
58
     * @var string
59
     *
60
     * @ORM\Column(name="comment", type="text", nullable=true)
61
     */
62
    protected $comment;
63
64
    /**
65
     * @var string
66
     *
67
     * @ORM\Column(name="title", type="string", length=255, nullable=true)
68
     */
69
    protected $title;
70
71
    /**
72
     * @var string
73
     *
74
     * @ORM\Column(name="filetype", type="string", length=10, nullable=false)
75
     */
76
    protected $filetype;
77
78
    /**
79
     * @var int
80
     *
81
     * @ORM\Column(name="size", type="integer", nullable=false)
82
     */
83
    protected $size;
84
85
    /**
86
     * @var bool
87
     *
88
     * @ORM\Column(name="readonly", type="boolean", nullable=false)
89
     */
90
    protected $readonly;
91
92
    /**
93
     * @var Course
94
     *
95
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course", cascade={"persist"})
96
     * @ORM\JoinColumn(name="c_id", referencedColumnName="id", onDelete="CASCADE" )
97
     */
98
    protected $course;
99
100
    /**
101
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Session", cascade={"persist"})
102
     * @ORM\JoinColumn(name="session_id", referencedColumnName="id", onDelete="CASCADE" )
103
     */
104
    protected $session;
105
106
    /**
107
     * CDocument constructor.
108
     */
109
    public function __construct()
110
    {
111
        $this->readonly = false;
112
        $this->size = 0;
113
        $this->id = 0;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function __toString()
120
    {
121
        return (string) $this->title;
122
    }
123
124
    /**
125
     * Set path.
126
     *
127
     * @param string $path
128
     *
129
     * @return CDocument
130
     */
131
    public function setPath($path)
132
    {
133
        $this->path = $path;
134
135
        return $this;
136
    }
137
138
    /**
139
     * Get path.
140
     *
141
     * @return string
142
     */
143
    public function getPath()
144
    {
145
        return $this->path;
146
    }
147
148
    /**
149
     * Set comment.
150
     *
151
     * @param string $comment
152
     *
153
     * @return CDocument
154
     */
155
    public function setComment($comment)
156
    {
157
        $this->comment = $comment;
158
159
        return $this;
160
    }
161
162
    /**
163
     * Get comment.
164
     *
165
     * @return string
166
     */
167
    public function getComment()
168
    {
169
        return $this->comment;
170
    }
171
172
    /**
173
     * Set title.
174
     *
175
     * @param string $title
176
     *
177
     * @return CDocument
178
     */
179
    public function setTitle($title)
180
    {
181
        $this->title = $title;
182
183
        return $this;
184
    }
185
186
    /**
187
     * Get title.
188
     *
189
     * @return string
190
     */
191
    public function getTitle()
192
    {
193
        return $this->title;
194
    }
195
196
    /**
197
     * Set filetype.
198
     *
199
     * @param string $filetype
200
     *
201
     * @return CDocument
202
     */
203
    public function setFiletype($filetype)
204
    {
205
        $this->filetype = $filetype;
206
207
        return $this;
208
    }
209
210
    /**
211
     * Get filetype.
212
     *
213
     * @return string
214
     */
215
    public function getFiletype()
216
    {
217
        return $this->filetype;
218
    }
219
220
    /**
221
     * Set size.
222
     *
223
     * @return CDocument
224
     */
225
    public function setSize(int $size)
226
    {
227
        $this->size = $size ?: 0;
228
229
        return $this;
230
    }
231
232
    /**
233
     * Get size.
234
     *
235
     * @return int
236
     */
237
    public function getSize()
238
    {
239
        return $this->size;
240
    }
241
242
    /**
243
     * Set readonly.
244
     *
245
     * @param bool $readonly
246
     *
247
     * @return CDocument
248
     */
249
    public function setReadonly($readonly)
250
    {
251
        $this->readonly = $readonly;
252
253
        return $this;
254
    }
255
256
    /**
257
     * Get readonly.
258
     *
259
     * @return bool
260
     */
261
    public function getReadonly()
262
    {
263
        return $this->readonly;
264
    }
265
266
    /**
267
     * Set id.
268
     *
269
     * @param int $id
270
     *
271
     * @return CDocument
272
     */
273
    public function setId($id)
274
    {
275
        $this->id = $id;
276
277
        return $this;
278
    }
279
280
    /**
281
     * Get id.
282
     *
283
     * @return int
284
     */
285
    public function getId()
286
    {
287
        return $this->id;
288
    }
289
290
    public function getCourse(): Course
291
    {
292
        return $this->course;
293
    }
294
295
    /**
296
     * @param Course $course
297
     *
298
     * @return CDocument
299
     */
300
    public function setCourse($course)
301
    {
302
        $this->course = $course;
303
304
        return $this;
305
    }
306
307
    /**
308
     * @return int
309
     */
310
    public function getIid()
311
    {
312
        return $this->iid;
313
    }
314
315
    /**
316
     * @return Session
317
     */
318
    public function getSession()
319
    {
320
        return $this->session;
321
    }
322
323
    /**
324
     * @param Session $session
325
     *
326
     * @return CDocument
327
     */
328
    public function setSession($session)
329
    {
330
        $this->session = $session;
331
332
        return $this;
333
    }
334
335
    /**
336
     * @return ResourceLink
337
     */
338
    public function getCourseSessionResourceLink()
339
    {
340
        return $this->getFirstResourceLinkFromCourseSession($this->getCourse(), $this->getSession());
341
    }
342
343
    /**
344
     * See ResourceLink to see the visibility constants. Example: ResourceLink::VISIBILITY_DELETED.
345
     *
346
     * @return int
347
     */
348
    public function getVisibility()
349
    {
350
        return $this->getCourseSessionResourceLink()->getVisibility();
351
    }
352
353
    /**
354
     * @return bool
355
     */
356
    public function isVisible(): bool
357
    {
358
        return $this->getCourseSessionResourceLink() === ResourceLink::VISIBILITY_PUBLISHED;
359
    }
360
361
    /**
362
     *
363
     */
364
    public function postPersist(LifecycleEventArgs $args)
365
    {
366
        // Update id with iid value
367
        $em = $args->getEntityManager();
368
        $this->setId($this->getIid());
369
        $em->persist($this);
370
        $em->flush();
371
    }
372
373
    /**
374
     * Resource identifier.
375
     */
376
    public function getResourceIdentifier(): int
377
    {
378
        return $this->getIid();
379
    }
380
381
    /**
382
     * @return string
383
     */
384
    public function getResourceName(): string
385
    {
386
        return $this->getTitle();
387
    }
388
389
    public function getResourceFieldName(): string
390
    {
391
        return 'title';
392
    }
393
}
394