Passed
Push — master ( 94394b...67cab2 )
by Julito
09:48
created

CDocument::getCourseSessionResourceLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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