Passed
Pull Request — master (#5832)
by
unknown
08:28
created

CourseRelUserRepository::getCourseUsers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 16
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 21
rs 9.7333
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Repository;
8
9
10
use Chamilo\CoreBundle\Entity\CourseRelUser;
11
use Chamilo\CoreBundle\Entity\SessionRelCourseRelUser;
12
use Chamilo\CourseBundle\Entity\CLpView;
13
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
14
use Doctrine\Persistence\ManagerRegistry;
15
16
class CourseRelUserRepository extends ServiceEntityRepository
17
{
18
    public function __construct(ManagerRegistry $registry)
19
    {
20
        parent::__construct($registry, CourseRelUser::class);
21
    }
22
23
    /**
24
     * Retrieves users from a course, with or without a session, and their LP progress.
25
     */
26
    public function getCourseUsers(int $courseId, array $lpIds, bool $checkSession = false): array
27
    {
28
        $qb = $this->createQueryBuilder('cu')
29
            ->select('u.id AS userId, c.title AS courseTitle, lp.iid AS lpId, lpv.progress')
30
            ->innerJoin('cu.user', 'u')
31
            ->innerJoin('cu.course', 'c')
32
            ->innerJoin(CLpView::class, 'lpv', 'WITH', 'lpv.course = c AND lpv.user = u.id')
33
            ->innerJoin('lpv.lp', 'lp')
34
            ->where('cu.course = :courseId')
35
            ->andWhere('lp.iid IN (:lpIds)')
36
            ->setParameter('courseId', $courseId)
37
            ->setParameter('lpIds', $lpIds)
38
            ->andWhere('(lpv.progress < 100 OR lpv.progress IS NULL)');
39
40
        if ($checkSession) {
41
            $qb->from(SessionRelCourseRelUser::class, 'scu')
42
                ->andWhere('scu.course = :courseId')
43
                ->andWhere('scu.session IS NOT NULL');
44
        }
45
46
        return $qb->getQuery()->getResult();
47
    }
48
}
49