Passed
Push — master ( 76abd2...ab5d59 )
by Julito
10:01
created

SkillRepository::__construct()   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 1
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\CoreBundle\Repository;
5
6
use Chamilo\CoreBundle\Entity\Course;
7
use Chamilo\CoreBundle\Entity\Session;
8
use Chamilo\CoreBundle\Entity\Skill;
9
use Chamilo\UserBundle\Entity\User;
10
use Doctrine\ORM\EntityRepository;
11
use Doctrine\ORM\Query\Expr\Join;
12
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
13
use Doctrine\Common\Persistence\ManagerRegistry;
14
15
/**
16
 * SkillRepository class.
17
 *
18
 * @author Angel Fernando Quiroz Campos <[email protected]>
19
 */
20
class SkillRepository extends ServiceEntityRepository
21
{
22
    /**
23
     * SkillRepository constructor.
24
     *
25
     * @param ManagerRegistry $registry
26
     */
27
    public function __construct(ManagerRegistry $registry)
28
    {
29
        parent::__construct($registry, Skill::class);
30
    }
31
32
    /**
33
     * Get the last acquired skill by a user on course and/or session.
34
     *
35
     * @param User    $user    The user
36
     * @param Course  $course  The course
37
     * @param Session $session The session
38
     *
39
     * @return Skill
40
     */
41
    public function getLastByUser(User $user, Course $course = null, Session $session = null)
42
    {
43
        $qb = $this->createQueryBuilder('s');
44
45
        $qb
46
            ->innerJoin(
47
                'ChamiloCoreBundle:SkillRelUser',
48
                'su',
49
                Join::WITH,
50
                's.id = su.skill'
51
            )
52
            ->where(
53
                $qb->expr()->eq('su.user', $user->getId())
54
            );
55
56
        if ($course) {
57
            $qb->andWhere(
58
                $qb->expr()->eq('su.course', $course->getId())
59
            );
60
        }
61
62
        if ($session) {
63
            $qb->andWhere(
64
                $qb->expr()->eq('su.session', $session->getId())
65
            );
66
        }
67
68
        $qb
69
            ->setMaxResults(1)
70
            ->orderBy('su.id', 'DESC');
71
72
        return $qb->getQuery()->getOneOrNullResult();
73
    }
74
}
75