Passed
Push — master ( f19064...ccb5fd )
by Julito
09:49
created

CDocument::isVisible()   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\Resource\ResourceNode;
12
use Chamilo\CoreBundle\Entity\Resource\ResourceRight;
13
use Chamilo\CoreBundle\Entity\Session;
14
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter;
15
use Doctrine\Common\Collections\Criteria;
16
use Doctrine\ORM\Event\LifecycleEventArgs;
17
use Doctrine\ORM\Mapping as ORM;
18
19
/**
20
 * CDocument.
21
 *
22
 * @ORM\Table(
23
 *  name="c_document",
24
 *  indexes={
25
 *      @ORM\Index(name="course", columns={"c_id"})
26
 *  }
27
 * )
28
 * @GRID\Source(columns="iid, id, title, filetype", filterable=false)
29
 *
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=false)
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|null
94
     *
95
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course", cascade={"persist"})
96
     * @ORM\JoinColumn(name="c_id", referencedColumnName="id")
97
     */
98
    protected $course;
99
100
    /**
101
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Session", cascade={"persist"})
102
     * @ORM\JoinColumn(name="session_id", referencedColumnName="id")
103
     */
104
    protected $session;
105
106
    /**
107
     * CDocument constructor.
108
     */
109
    public function __construct()
110
    {
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function __toString()
117
    {
118
        return (string) $this->title;
119
    }
120
121
    /**
122
     * Set path.
123
     *
124
     * @param string $path
125
     *
126
     * @return CDocument
127
     */
128
    public function setPath($path)
129
    {
130
        $this->path = $path;
131
132
        return $this;
133
    }
134
135
    /**
136
     * Get path.
137
     *
138
     * @return string
139
     */
140
    public function getPath()
141
    {
142
        return $this->path;
143
    }
144
145
    /**
146
     * Set comment.
147
     *
148
     * @param string $comment
149
     *
150
     * @return CDocument
151
     */
152
    public function setComment($comment)
153
    {
154
        $this->comment = $comment;
155
156
        return $this;
157
    }
158
159
    /**
160
     * Get comment.
161
     *
162
     * @return string
163
     */
164
    public function getComment()
165
    {
166
        return $this->comment;
167
    }
168
169
    /**
170
     * Set title.
171
     *
172
     * @param string $title
173
     *
174
     * @return CDocument
175
     */
176
    public function setTitle($title)
177
    {
178
        $this->title = $title;
179
180
        return $this;
181
    }
182
183
    /**
184
     * Get title.
185
     *
186
     * @return string
187
     */
188
    public function getTitle()
189
    {
190
        return $this->title;
191
    }
192
193
    /**
194
     * Set filetype.
195
     *
196
     * @param string $filetype
197
     *
198
     * @return CDocument
199
     */
200
    public function setFiletype($filetype)
201
    {
202
        $this->filetype = $filetype;
203
204
        return $this;
205
    }
206
207
    /**
208
     * Get filetype.
209
     *
210
     * @return string
211
     */
212
    public function getFiletype()
213
    {
214
        return $this->filetype;
215
    }
216
217
    /**
218
     * Set size.
219
     *
220
     * @param int $size
221
     *
222
     * @return CDocument
223
     */
224
    public function setSize($size)
225
    {
226
        $this->size = $size;
227
228
        return $this;
229
    }
230
231
    /**
232
     * Get size.
233
     *
234
     * @return int
235
     */
236
    public function getSize()
237
    {
238
        return $this->size;
239
    }
240
241
    /**
242
     * Set readonly.
243
     *
244
     * @param bool $readonly
245
     *
246
     * @return CDocument
247
     */
248
    public function setReadonly($readonly)
249
    {
250
        $this->readonly = $readonly;
251
252
        return $this;
253
    }
254
255
    /**
256
     * Get readonly.
257
     *
258
     * @return bool
259
     */
260
    public function getReadonly()
261
    {
262
        return $this->readonly;
263
    }
264
265
    /**
266
     * Set id.
267
     *
268
     * @param int $id
269
     *
270
     * @return CDocument
271
     */
272
    public function setId($id)
273
    {
274
        $this->id = $id;
275
276
        return $this;
277
    }
278
279
    /**
280
     * Get id.
281
     *
282
     * @return int
283
     */
284
    public function getId()
285
    {
286
        return $this->id;
287
    }
288
289
    /**
290
     * @return Course
291
     */
292
    public function getCourse(): Course
293
    {
294
        return $this->course;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->course could return the type null which is incompatible with the type-hinted return Chamilo\CoreBundle\Entity\Course. Consider adding an additional type-check to rule them out.
Loading history...
295
    }
296
297
    /**
298
     * @param Course $course
299
     *
300
     * @return CDocument
301
     */
302
    public function setCourse($course)
303
    {
304
        $this->course = $course;
305
306
        return $this;
307
    }
308
309
    /**
310
     * @return int
311
     */
312
    public function getIid()
313
    {
314
        return $this->iid;
315
    }
316
317
    /**
318
     * @return Session
319
     */
320
    public function getSession()
321
    {
322
        return $this->session;
323
    }
324
325
    /**
326
     * @param Session $session
327
     *
328
     * @return CDocument
329
     */
330
    public function setSession($session)
331
    {
332
        $this->session = $session;
333
334
        return $this;
335
    }
336
337
    /**
338
     * @return ResourceLink
339
     */
340
    public function getCourseSessionResourceLink()
341
    {
342
        $criteria = Criteria::create();
343
        $criteria
344
            ->where(Criteria::expr()->eq('course', $this->getCourse()))
345
            ->andWhere(
346
                Criteria::expr()->eq('session', $this->getSession())
347
            );
348
        $resourceNode = $this->getResourceNode();
349
350
        $result = null;
351
        if ($resourceNode && $resourceNode->getResourceLinks()) {
352
            $result = $resourceNode->getResourceLinks()->matching($criteria)->first();
353
        }
354
355
        return $result;
356
    }
357
358
    /**
359
     * Visiblity types ResourceLink::VISIBILITY_DELETED
360
     *
361
     * @return int
362
     */
363
    public function getVisibility()
364
    {
365
        return $this->getCourseSessionResourceLink()->getVisibility();
366
    }
367
368
369
    public function isVisible()
370
    {
371
        return $this->getVisibility() === ResourceLink::VISIBILITY_PUBLISHED;
372
    }
373
374
    /**
375
     * @ORM\PostPersist()
376
     *
377
     * @param LifecycleEventArgs $args
378
     */
379
    public function postPersist(LifecycleEventArgs $args)
380
    {
381
        // Update id with iid value
382
        $em = $args->getEntityManager();
383
        $this->setId($this->iid);
384
        $em->persist($this);
385
        $em->flush($this);
386
    }
387
388
    /**
389
     * Resource identifier.
390
     *
391
     * @return int
392
     */
393
    public function getResourceIdentifier(): int
394
    {
395
        return $this->getIid();
396
    }
397
398
    /**
399
     * @return string
400
     */
401
    public function getResourceName(): string
402
    {
403
        return $this->getTitle();
404
    }
405
406
    /**
407
     * @return string
408
     */
409
    public function getToolName(): string
410
    {
411
        return 'document';
412
    }
413
}
414