Passed
Push — master ( 1e00a2...f52220 )
by Angel Fernando Quiroz
07:44 queued 14s
created

XApiToolLaunchRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Chamilo\CoreBundle\Repository;
4
5
use Chamilo\CoreBundle\Entity\Course;
6
use Chamilo\CoreBundle\Entity\Session;
7
use Chamilo\CoreBundle\Entity\XApiToolLaunch;
8
use Chamilo\CourseBundle\Entity\CLp;
9
use Chamilo\CourseBundle\Entity\CLpItem;
10
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
11
use Doctrine\ORM\NonUniqueResultException;
12
use Doctrine\ORM\NoResultException;
13
use Doctrine\ORM\Query\Expr\Join;
14
use Doctrine\Persistence\ManagerRegistry;
15
16
/**
17
 * @extends ServiceEntityRepository<XApiToolLaunch>
18
 *
19
 * @method XApiToolLaunch|null find($id, $lockMode = null, $lockVersion = null)
20
 * @method XApiToolLaunch|null findOneBy(array $criteria, array $orderBy = null)
21
 * @method XApiToolLaunch[]    findAll()
22
 * @method XApiToolLaunch[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
23
 */
24
class XApiToolLaunchRepository extends ServiceEntityRepository
25
{
26
    public function __construct(ManagerRegistry $registry)
27
    {
28
        parent::__construct($registry, XApiToolLaunch::class);
29
    }
30
31
    /**
32
     * @return array<int, XApiToolLaunch>
33
     */
34
    public function findByCourseAndSession(
35
        Course $course,
36
        ?Session $session = null,
37
        array $orderBy = [],
38
        ?int $limit = null,
39
        ?int $start = null
40
    ): array {
41
        $criteria = [
42
            'course' => $course,
43
            'session' => null,
44
        ];
45
46
        if ($session) {
47
            $criteria['session'] = $session;
48
        }
49
50
        return $this->findBy($criteria, $orderBy, $limit, $start);
51
    }
52
53
    /**
54
     * @throws NonUniqueResultException
55
     * @throws NoResultException
56
     */
57
    public function countByCourseAndSession(
58
        Course $course,
59
        Session $session = null,
60
        bool $filteredForStudent = false
61
    ): int {
62
        $qb = $this->createQueryBuilder('tl');
63
        $qb->select($qb->expr()->count('tl'))
64
            ->where($qb->expr()->eq('tl.course', ':course'))
65
            ->setParameter('course', $course);
66
67
        if ($session) {
68
            $qb->andWhere($qb->expr()->eq('tl.session', ':session'))
69
                ->setParameter('session', $session);
70
        } else {
71
            $qb->andWhere($qb->expr()->isNull('tl.session'));
72
        }
73
74
        if ($filteredForStudent) {
75
            $qb
76
                ->leftJoin(
77
                    CLpItem::class,
78
                    'lpi',
79
                    Join::WITH,
80
                    "tl.id = lpi.path AND tl.course = lpi.cId AND lpi.itemType = 'xapi'"
81
                )
82
                ->andWhere($qb->expr()->isNull('lpi.path'));
83
        }
84
85
        $query = $qb->getQuery();
86
87
        return (int) $query->getSingleScalarResult();
88
    }
89
90
    /**
91
     * @throws NonUniqueResultException
92
     * @throws NoResultException
93
     */
94
    public function wasAddedInLp(XApiToolLaunch $toolLaunch): int
95
    {
96
        $qb = $this->getEntityManager()->createQueryBuilder();
97
98
        return (int) $qb->select($qb->expr()->count('lp'))
99
            ->from(CLp::class, 'lp')
100
            ->innerJoin(CLpItem::class, 'lpi', Join::WITH, 'lp.id = lpi.lpId')
101
            ->where('lpi.itemType = :type')
102
            ->andWhere('lpi.path = :tool_id')
103
            ->setParameter('type', TOOL_XAPI)
104
            ->setParameter('tool_id', $toolLaunch->getId())
105
            ->getQuery()
106
            ->getSingleScalarResult();
107
    }
108
}
109