Passed
Pull Request — master (#7013)
by
unknown
09:18
created

CourseRelUserRepository::__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
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Repository;
8
9
use Chamilo\CoreBundle\Entity\CourseRelUser;
10
use Chamilo\CoreBundle\Entity\User;
11
use Chamilo\CourseBundle\Entity\CLp;
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 and their LP progress (without session).
25
     */
26
    public function getCourseUsers(int $courseId, array $lpIds): array
27
    {
28
        $qb = $this->createQueryBuilder('cu')
29
            ->select('u.id AS userId, c.title AS courseTitle, lp.iid AS lpId, COALESCE(lpv.progress, 0) AS progress')
30
            ->innerJoin('cu.user', 'u')
31
            ->innerJoin('cu.course', 'c')
32
            ->leftJoin(CLpView::class, 'lpv', 'WITH', 'lpv.user = u.id AND lpv.course = cu.course AND lpv.lp IN (:lpIds)')
33
            ->leftJoin(CLp::class, 'lp', 'WITH', 'lp.iid IN (:lpIds)')
34
            ->innerJoin('lp.resourceNode', 'rn')
35
            ->where('cu.course = :courseId')
36
            ->andWhere('rn.parent = c.resourceNode')
37
            ->andWhere('(lpv.progress < 100 OR lpv.progress IS NULL)')
38
            ->setParameter('courseId', $courseId)
39
            ->setParameter('lpIds', $lpIds)
40
        ;
41
42
        return $qb->getQuery()->getResult();
43
    }
44
45
    /**
46
     * Count distinct courses where the given user is a teacher (status == TEACHER).
47
     */
48
    public function countTaughtCoursesForUser(User $user): int
49
    {
50
        return (int) $this->createQueryBuilder('cru')
51
            ->select('COUNT(DISTINCT c.id)')
52
            ->innerJoin('cru.course', 'c')
53
            ->andWhere('cru.user = :user')
54
            ->andWhere('cru.status = :teacher')
55
            ->setParameter('user', $user)
56
            ->setParameter('teacher', CourseRelUser::TEACHER)
57
            ->getQuery()
58
            ->getSingleScalarResult();
59
    }
60
}
61