Passed
Push — master ( 2117d3...1baf8e )
by
unknown
14:08 queued 06:15
created

CAttendanceSheetRepository::__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
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CourseBundle\Repository;
8
9
use Chamilo\CoreBundle\Repository\ResourceRepository;
10
use Chamilo\CourseBundle\Entity\CAttendanceSheet;
11
use Doctrine\Persistence\ManagerRegistry;
12
13
final class CAttendanceSheetRepository extends ResourceRepository
14
{
15
    public function __construct(ManagerRegistry $registry)
16
    {
17
        parent::__construct($registry, CAttendanceSheet::class);
18
    }
19
20
    public function getUserScore(int $userId, int $attendanceId, ?int $groupId = null): int
21
    {
22
        $qb = $this->createQueryBuilder('sheet')
23
            ->select('SUM(sheet.presence) as score')
24
            ->join('sheet.attendanceCalendar', 'calendar')
25
            ->where('calendar.attendance = :attendanceId')
26
            ->andWhere('sheet.user = :userId')
27
            ->setParameter('attendanceId', $attendanceId)
28
            ->setParameter('userId', $userId);
29
30
        if ($groupId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $groupId of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
31
            $qb->join('calendar.groups', 'groups')
32
                ->andWhere('groups.group = :groupId')
33
                ->setParameter('groupId', $groupId);
34
        }
35
36
        return (int) $qb->getQuery()->getSingleScalarResult();
37
    }
38
}
39