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

countByAttendanceAndGroup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 14
rs 9.9666
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\CAttendanceCalendar;
11
use Chamilo\CourseBundle\Entity\CAttendanceSheet;
12
use Doctrine\Persistence\ManagerRegistry;
13
14
final class CAttendanceCalendarRepository extends ResourceRepository
15
{
16
    public function __construct(ManagerRegistry $registry)
17
    {
18
        parent::__construct($registry, CAttendanceCalendar::class);
19
    }
20
21
    /**
22
     * Retrieves all calendar events for a specific attendance.
23
     *
24
     * @param int $attendanceId
25
     * @return CAttendanceCalendar[]
26
     */
27
    public function findByAttendanceId(int $attendanceId): array
28
    {
29
        return $this->createQueryBuilder('c')
30
            ->where('c.attendance = :attendanceId')
31
            ->setParameter('attendanceId', $attendanceId)
32
            ->orderBy('c.dateTime', 'ASC')
33
            ->getQuery()
34
            ->getResult();
35
    }
36
37
    /**
38
     * Deletes all calendar events associated with a specific attendance.
39
     *
40
     * @param int $attendanceId
41
     * @return int The number of deleted records
42
     */
43
    public function deleteAllByAttendance(int $attendanceId): int
44
    {
45
        return $this->createQueryBuilder('c')
46
            ->delete()
47
            ->where('c.attendance = :attendanceId')
48
            ->setParameter('attendanceId', $attendanceId)
49
            ->getQuery()
50
            ->execute();
51
    }
52
53
    /**
54
     * Finds a specific calendar event by its ID and the associated attendance ID.
55
     *
56
     * @param int $calendarId
57
     * @param int $attendanceId
58
     * @return CAttendanceCalendar|null
59
     */
60
    public function findByIdAndAttendance(int $calendarId, int $attendanceId): ?CAttendanceCalendar
61
    {
62
        return $this->createQueryBuilder('c')
63
            ->where('c.id = :calendarId')
64
            ->andWhere('c.attendance = :attendanceId')
65
            ->setParameters([
66
                'calendarId' => $calendarId,
67
                'attendanceId' => $attendanceId,
68
            ])
69
            ->getQuery()
70
            ->getOneOrNullResult();
71
    }
72
73
    /**
74
     * Retrieves calendar events filtered by a date range.
75
     *
76
     * @param int $attendanceId
77
     * @param \DateTime|null $startDate
78
     * @param \DateTime|null $endDate
79
     * @return CAttendanceCalendar[]
80
     */
81
    public function findByDateRange(
82
        int $attendanceId,
83
        ?\DateTime $startDate,
84
        ?\DateTime $endDate
85
    ): array {
86
        $qb = $this->createQueryBuilder('c')
87
            ->where('c.attendance = :attendanceId')
88
            ->setParameter('attendanceId', $attendanceId);
89
90
        if ($startDate) {
91
            $qb->andWhere('c.dateTime >= :startDate')
92
                ->setParameter('startDate', $startDate);
93
        }
94
95
        if ($endDate) {
96
            $qb->andWhere('c.dateTime <= :endDate')
97
                ->setParameter('endDate', $endDate);
98
        }
99
100
        return $qb->orderBy('c.dateTime', 'ASC')
101
            ->getQuery()
102
            ->getResult();
103
    }
104
105
    /**
106
     * Checks if a calendar event is blocked.
107
     *
108
     * @param int $calendarId
109
     * @return bool
110
     */
111
    public function isBlocked(int $calendarId): bool
112
    {
113
        return (bool) $this->createQueryBuilder('c')
114
            ->select('c.blocked')
115
            ->where('c.id = :calendarId')
116
            ->setParameter('calendarId', $calendarId)
117
            ->getQuery()
118
            ->getSingleScalarResult();
119
    }
120
121
    public function findAttendanceWithData(int $attendanceId): array
122
    {
123
        $calendars = $this->createQueryBuilder('calendar')
124
            ->andWhere('calendar.attendance = :attendanceId')
125
            ->setParameter('attendanceId', $attendanceId)
126
            ->orderBy('calendar.dateTime', 'ASC')
127
            ->getQuery()
128
            ->getResult();
129
130
        $attendanceDates = array_map(function (CAttendanceCalendar $calendar) {
131
            return [
132
                'id' => $calendar->getIid(),
133
                'label' => $calendar->getDateTime()->format('M d, Y - h:i A'),
134
            ];
135
        }, $calendars);
136
137
        $attendanceData = [];
138
        foreach ($calendars as $calendar) {
139
            /* @var CAttendanceSheet $sheet */
140
            foreach ($calendar->getSheets() as $sheet) {
141
                $key = $sheet->getUser()->getId() . '-' . $calendar->getIid();
142
                $attendanceData[$key] = (int) $sheet->getPresence(); // Status: 1 (Present), 0 (Absent), null (No Status)
143
            }
144
        }
145
146
        return [
147
            'attendanceDates' => $attendanceDates,
148
            'attendanceData' => $attendanceData,
149
        ];
150
    }
151
152
    public function countByAttendanceAndGroup(int $attendanceId, ?int $groupId = null): int
153
    {
154
        $qb = $this->createQueryBuilder('calendar')
155
            ->select('COUNT(calendar.iid)')
156
            ->where('calendar.attendance = :attendanceId')
157
            ->setParameter('attendanceId', $attendanceId);
158
159
        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...
160
            $qb->join('calendar.groups', 'groups')
161
                ->andWhere('groups.group = :groupId')
162
                ->setParameter('groupId', $groupId);
163
        }
164
165
        return (int) $qb->getQuery()->getSingleScalarResult();
166
    }
167
168
    public function countDoneAttendanceByAttendanceAndGroup(int $attendanceId, ?int $groupId = null): int
169
    {
170
        $qb = $this->createQueryBuilder('calendar')
171
            ->select('COUNT(calendar.iid)')
172
            ->where('calendar.attendance = :attendanceId')
173
            ->andWhere('calendar.doneAttendance = true')
174
            ->setParameter('attendanceId', $attendanceId);
175
176
        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...
177
            $qb->join('calendar.groups', 'groups')
178
                ->andWhere('groups.group = :groupId')
179
                ->setParameter('groupId', $groupId);
180
        }
181
182
        return (int) $qb->getQuery()->getSingleScalarResult();
183
    }
184
}
185