Passed
Push — master ( aacfc9...8578c1 )
by Julito
09:03
created

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