Completed
Push — master ( c9546d...95f607 )
by Julito
09:41
created

SkillRelUser::getIssueUrl()   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
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Entity;
6
7
use Chamilo\CoreBundle\Traits\UserTrait;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use Doctrine\Common\Collections\Criteria;
10
use Doctrine\ORM\Mapping as ORM;
11
12
/**
13
 * SkillRelUser.
14
 *
15
 * @ORM\Table(
16
 *  name="skill_rel_user",
17
 *  indexes={
18
 *      @ORM\Index(name="idx_select_cs", columns={"course_id", "session_id"}),
19
 *      @ORM\Index(name="idx_select_s_c_u", columns={"session_id", "course_id", "user_id"}),
20
 *      @ORM\Index(name="idx_select_sk_u", columns={"skill_id", "user_id"})
21
 *  }
22
 * )
23
 * @ORM\Entity
24
 */
25
class SkillRelUser
26
{
27
    use UserTrait;
28
29
    /**
30
     * @var int
31
     *
32
     * @ORM\Column(name="id", type="integer")
33
     * @ORM\Id
34
     * @ORM\GeneratedValue
35
     */
36
    protected $id;
37
38
    /**
39
     * @ORM\OneToMany(
40
     *     targetEntity="SkillRelUserComment", mappedBy="skillRelUser",
41
     *     cascade={"persist", "remove"},
42
     *     orphanRemoval=true
43
     * )
44
     */
45
    protected $comments;
46
47
    /**
48
     * Whether this has been confirmed by a teacher or not
49
     * Only set to 0 when the skill_rel_item says requires_validation = 1.
50
     *
51
     * @var int
52
     *
53
     * @ORM\Column(name="validation_status", type="integer")
54
     */
55
    protected $validationStatus;
56
57
    /**
58
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\User", inversedBy="achievedSkills", cascade={"persist"})
59
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
60
     */
61
    protected $user;
62
63
    /**
64
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Skill", inversedBy="issuedSkills", cascade={"persist"})
65
     * @ORM\JoinColumn(name="skill_id", referencedColumnName="id", nullable=false)
66
     */
67
    protected $skill;
68
69
    /**
70
     * @var \DateTime
71
     *
72
     * @ORM\Column(name="acquired_skill_at", type="datetime", nullable=false)
73
     */
74
    protected $acquiredSkillAt;
75
76
    /**
77
     * @var int
78
     *
79
     * @ORM\Column(name="assigned_by", type="integer", nullable=false)
80
     */
81
    protected $assignedBy;
82
83
    /**
84
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course", inversedBy="issuedSkills", cascade={"persist"})
85
     * @ORM\JoinColumn(name="course_id", referencedColumnName="id", nullable=true)
86
     */
87
    protected $course;
88
89
    /**
90
     * @var Session
91
     *
92
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Session", inversedBy="issuedSkills", cascade={"persist"})
93
     * @ORM\JoinColumn(name="session_id", referencedColumnName="id", nullable=true)
94
     */
95
    protected $session;
96
97
    /**
98
     * @var Level
99
     *
100
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Level")
101
     * @ORM\JoinColumn(name="acquired_level", referencedColumnName="id")
102
     */
103
    protected $acquiredLevel;
104
105
    /**
106
     * @var string
107
     *
108
     * @ORM\Column(name="argumentation", type="text")
109
     */
110
    protected $argumentation;
111
112
    /**
113
     * @var int
114
     *
115
     * @ORM\Column(name="argumentation_author_id", type="integer")
116
     */
117
    protected $argumentationAuthorId;
118
119
    /**
120
     * SkillRelUser constructor.
121
     */
122
    public function __construct()
123
    {
124
        $this->comments = new ArrayCollection();
125
    }
126
127
    /**
128
     * Set skill.
129
     *
130
     * @return SkillRelUser
131
     */
132
    public function setSkill(Skill $skill)
133
    {
134
        $this->skill = $skill;
135
136
        return $this;
137
    }
138
139
    /**
140
     * Get skill.
141
     *
142
     * @return Skill
143
     */
144
    public function getSkill()
145
    {
146
        return $this->skill;
147
    }
148
149
    /**
150
     * Set course.
151
     *
152
     * @return SkillRelUser
153
     */
154
    public function setCourse(Course $course)
155
    {
156
        $this->course = $course;
157
158
        return $this;
159
    }
160
161
    /**
162
     * Get course.
163
     *
164
     * @return Course
165
     */
166
    public function getCourse()
167
    {
168
        return $this->course;
169
    }
170
171
    /**
172
     * Set session.
173
     *
174
     * @return SkillRelUser
175
     */
176
    public function setSession(Session $session)
177
    {
178
        $this->session = $session;
179
180
        return $this;
181
    }
182
183
    /**
184
     * Get session.
185
     *
186
     * @return Session
187
     */
188
    public function getSession()
189
    {
190
        return $this->session;
191
    }
192
193
    /**
194
     * Set acquiredSkillAt.
195
     *
196
     * @param \DateTime $acquiredSkillAt
197
     *
198
     * @return SkillRelUser
199
     */
200
    public function setAcquiredSkillAt($acquiredSkillAt)
201
    {
202
        $this->acquiredSkillAt = $acquiredSkillAt;
203
204
        return $this;
205
    }
206
207
    /**
208
     * Get acquiredSkillAt.
209
     *
210
     * @return \DateTime
211
     */
212
    public function getAcquiredSkillAt()
213
    {
214
        return $this->acquiredSkillAt;
215
    }
216
217
    /**
218
     * Set assignedBy.
219
     *
220
     * @param int $assignedBy
221
     *
222
     * @return SkillRelUser
223
     */
224
    public function setAssignedBy($assignedBy)
225
    {
226
        $this->assignedBy = $assignedBy;
227
228
        return $this;
229
    }
230
231
    /**
232
     * Get assignedBy.
233
     *
234
     * @return int
235
     */
236
    public function getAssignedBy()
237
    {
238
        return $this->assignedBy;
239
    }
240
241
    /**
242
     * Get id.
243
     *
244
     * @return int
245
     */
246
    public function getId()
247
    {
248
        return $this->id;
249
    }
250
251
    /**
252
     * Set acquiredLevel.
253
     *
254
     * @param Level $acquiredLevel
255
     *
256
     * @return SkillRelUser
257
     */
258
    public function setAcquiredLevel($acquiredLevel)
259
    {
260
        $this->acquiredLevel = $acquiredLevel;
261
262
        return $this;
263
    }
264
265
    /**
266
     * Get acquiredLevel.
267
     *
268
     * @return Level
269
     */
270
    public function getAcquiredLevel()
271
    {
272
        return $this->acquiredLevel;
273
    }
274
275
    /**
276
     * Set argumentationAuthorId.
277
     *
278
     * @param int $argumentationAuthorId
279
     *
280
     * @return SkillRelUser
281
     */
282
    public function setArgumentationAuthorId($argumentationAuthorId)
283
    {
284
        $this->argumentationAuthorId = $argumentationAuthorId;
285
286
        return $this;
287
    }
288
289
    /**
290
     * Get argumentationAuthorId.
291
     *
292
     * @return int
293
     */
294
    public function getArgumentationAuthorId()
295
    {
296
        return $this->argumentationAuthorId;
297
    }
298
299
    /**
300
     * Set argumentation.
301
     *
302
     * @param string $argumentation
303
     *
304
     * @return SkillRelUser
305
     */
306
    public function setArgumentation($argumentation)
307
    {
308
        $this->argumentation = $argumentation;
309
310
        return $this;
311
    }
312
313
    /**
314
     * Get argumentation.
315
     *
316
     * @return string
317
     */
318
    public function getArgumentation()
319
    {
320
        return $this->argumentation;
321
    }
322
323
    /**
324
     * Get the source which the skill was obtained.
325
     *
326
     * @return string
327
     */
328
    public function getSourceName()
329
    {
330
        $source = '';
331
332
        if ($this->session && 0 != $this->session->getId()) {
333
            $source .= "[{$this->session->getName()}] ";
334
        }
335
336
        if ($this->course) {
337
            $source .= $this->course->getTitle();
338
        }
339
340
        return $source;
341
    }
342
343
    /**
344
     * Get the URL for the issue.
345
     *
346
     * @return string
347
     */
348
    public function getIssueUrl()
349
    {
350
        return api_get_path(WEB_PATH)."badge/{$this->id}";
351
    }
352
353
    /**
354
     * Get the URL for the All issues page.
355
     *
356
     * @return string
357
     */
358
    public function getIssueUrlAll()
359
    {
360
        return api_get_path(WEB_PATH)."skill/{$this->skill->getId()}/user/{$this->user->getId()}";
361
    }
362
363
    /**
364
     * Get the URL for the assertion.
365
     *
366
     * @return string
367
     */
368
    public function getAssertionUrl()
369
    {
370
        $url = api_get_path(WEB_CODE_PATH).'badge/assertion.php?';
371
372
        $url .= http_build_query([
373
            'user' => $this->user->getId(),
374
            'skill' => $this->skill->getId(),
375
            'course' => $this->course ? $this->course->getId() : 0,
376
            'session' => $this->session ? $this->session->getId() : 0,
377
        ]);
378
379
        return $url;
380
    }
381
382
    /**
383
     * Get comments.
384
     *
385
     * @param bool $sortDescByDateTime
386
     *
387
     * @return ArrayCollection
388
     */
389
    public function getComments($sortDescByDateTime = false)
390
    {
391
        if ($sortDescByDateTime) {
392
            $criteria = Criteria::create();
393
            $criteria->orderBy([
394
                'feedbackDateTime' => Criteria::DESC,
395
            ]);
396
397
            return $this->comments->matching($criteria);
398
        }
399
400
        return $this->comments;
401
    }
402
403
    /**
404
     * Calculate the average value from the feedback comments.
405
     *
406
     * @return string
407
     */
408
    public function getAverage()
409
    {
410
        $sum = 0;
411
        $countValues = 0;
412
413
        foreach ($this->comments as $comment) {
414
            if (!$comment->getFeedbackValue()) {
415
                continue;
416
            }
417
418
            $sum += $comment->getFeedbackValue();
419
            $countValues++;
420
        }
421
422
        $average = $countValues > 0 ? $sum / $countValues : 0;
423
424
        return number_format($average, 2);
425
    }
426
427
    /**
428
     * @return int
429
     */
430
    public function getValidationStatus()
431
    {
432
        return $this->validationStatus;
433
    }
434
435
    /**
436
     * @param int $validationStatus
437
     *
438
     * @return SkillRelUser
439
     */
440
    public function setValidationStatus($validationStatus)
441
    {
442
        $this->validationStatus = $validationStatus;
443
444
        return $this;
445
    }
446
}
447