Passed
Push — master ( 1e0b50...40a262 )
by Angel Fernando Quiroz
09:11
created

getSessionCoursesByStatusInUserSubscription()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 26
nc 2
nop 4
dl 0
loc 43
rs 9.504
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\AccessUrl;
10
use Chamilo\CoreBundle\Entity\AccessUrlRelUser;
11
use Chamilo\CoreBundle\Entity\Course;
12
use Chamilo\CoreBundle\Entity\Session;
13
use Chamilo\CoreBundle\Entity\SessionRelCourse;
14
use Chamilo\CoreBundle\Entity\SessionRelCourseRelUser;
15
use Chamilo\CoreBundle\Entity\SessionRelUser;
16
use Chamilo\CoreBundle\Entity\User;
17
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
18
use Doctrine\ORM\Query\Expr\Join;
19
use Doctrine\ORM\QueryBuilder;
20
use Doctrine\Persistence\ManagerRegistry;
21
use Exception;
22
23
/**
24
 * @author Julio Montoya <[email protected]>
25
 */
26
class SessionRepository extends ServiceEntityRepository
27
{
28
    public function __construct(ManagerRegistry $registry)
29
    {
30
        parent::__construct($registry, Session::class);
31
    }
32
33
    public function create(): ?Session
34
    {
35
        return new Session();
36
    }
37
38
    public function update(Session $session): void
39
    {
40
        $this->getEntityManager()->persist($session);
41
        $this->getEntityManager()->flush();
42
    }
43
44
    /**
45
     * @return array<SessionRelUser>
46
     */
47
    public function getUsersByAccessUrl(Session $session, AccessUrl $url, array $relationTypeList = []): array
48
    {
49
        if (0 === $session->getUsers()->count()) {
50
            return [];
51
        }
52
53
        $qb = $this->addSessionRelUserFilterByUrl($session, $url);
54
        $qb->orderBy('sru.relationType');
55
56
        if ($relationTypeList) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $relationTypeList of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
57
            $qb->andWhere(
58
                $qb->expr()->in('sru.relationType', $relationTypeList)
59
            );
60
        }
61
62
        return $qb->getQuery()->getResult();
63
    }
64
65
    /**
66
     * @return Session[]
67
     */
68
    public function getSessionsByUser(User $user, AccessUrl $url)
69
    {
70
        $qb = $this->createQueryBuilder('s');
71
        $qb
72
            ->innerJoin('s.users', 'sru')
73
            ->leftJoin(AccessUrlRelUser::class, 'uru', Join::WITH, 'uru.user = sru.user')
74
            ->andWhere('sru.user = :user AND uru.url = :url')
75
            ->setParameters([
76
                'user' => $user,
77
                'url' => $url,
78
            ])
79
        ;
80
81
        return $qb->getQuery()->getResult();
82
    }
83
84
    public function addUserInCourse(int $relationType, User $user, Course $course, Session $session): void
85
    {
86
        if (!$user->isActive()) {
87
            throw new Exception('User not active');
88
        }
89
90
        if (!$session->hasCourse($course)) {
91
            $msg = sprintf('Course %s is not subscribed to the session %s', $course->getTitle(), $session->getTitle());
92
93
            throw new Exception($msg);
94
        }
95
96
        if (!\in_array($relationType, Session::getRelationTypeList(), true)) {
97
            throw new Exception(sprintf('Cannot handle relationType %s', $relationType));
98
        }
99
100
        switch ($relationType) {
101
            case Session::DRH:
102
                if ($user->hasRole('ROLE_RRHH')) {
103
                    $session->addUserInSession(Session::DRH, $user);
104
                }
105
106
                break;
107
108
            case Session::STUDENT:
109
                $session
110
                    ->addUserInSession(Session::STUDENT, $user)
111
                    ->addUserInCourse(Session::STUDENT, $user, $course)
112
                ;
113
114
                break;
115
116
            case Session::COURSE_COACH:
117
                if ($user->hasRole('ROLE_TEACHER')) {
118
                    $session
119
                        ->addUserInSession(Session::COURSE_COACH, $user)
120
                        ->addUserInCourse(
121
                            Session::COURSE_COACH,
122
                            $user,
123
                            $course
124
                        )
125
                    ;
126
                }
127
128
                break;
129
        }
130
    }
131
132
    /**
133
     * @return array<SessionRelCourse>
134
     */
135
    public function getSessionCoursesByStatusInUserSubscription(User $user, Session $session, int $relationType, ?AccessUrl $url = null): array
136
    {
137
        $qb = $this->getEntityManager()->createQueryBuilder();
138
139
        $qb->select('src')
140
            ->from(SessionRelCourse::class, 'src')
141
            ->innerJoin(
142
                SessionRelUser::class,
143
                'sru',
144
                Join::WITH,
145
                'src.session = sru.session'
146
            )
147
            ->innerJoin('src.session', 'session')
148
            ->where(
149
                $qb->expr()->eq('session', ':session')
150
            )
151
            ->andWhere(
152
                $qb->expr()->eq('sru.user', ':user')
153
            )
154
            ->andWhere(
155
                $qb->expr()->eq('sru.relationType', ':relation_type')
156
            )
157
        ;
158
159
        $parameters = [
160
            'session' => $session,
161
            'user' => $user,
162
            'relation_type' => $relationType,
163
        ];
164
165
        if ($url) {
166
            $qb->innerJoin('session.urls', 'urls')
167
                ->andWhere(
168
                    $qb->expr()->eq('urls.url', ':url')
169
                )
170
            ;
171
172
            $parameters['url'] = $url;
173
        }
174
175
        $qb->setParameters($parameters);
176
177
        return $qb->getQuery()->getResult();
178
    }
179
180
    /**
181
     * @return array<SessionRelCourse>
182
     */
183
    public function getSessionCoursesByStatusInCourseSubscription(User $user, Session $session, int $status, ?AccessUrl $url = null): array
184
    {
185
        $qb = $this->getEntityManager()->createQueryBuilder();
186
187
        $qb->select('src')
188
            ->from(SessionRelCourse::class, 'src')
189
            ->innerJoin(
190
                SessionRelCourseRelUser::class,
191
                'srcru',
192
                Join::WITH,
193
                'src.session = srcru.session AND src.course = srcru.course'
194
            )
195
            ->innerJoin('srcru.session', 'session')
196
            ->where(
197
                $qb->expr()->eq('session', ':session')
198
            )
199
            ->andWhere(
200
                $qb->expr()->eq('srcru.user', ':user')
201
            )
202
            ->andWhere(
203
                $qb->expr()->eq('srcru.status', ':status')
204
            )
205
        ;
206
207
        $parameters = [
208
            'session' => $session,
209
            'user' => $user,
210
            'status' => $status,
211
        ];
212
213
        if ($url) {
214
            $qb->innerJoin('session.urls', 'urls')
215
                ->andWhere(
216
                    $qb->expr()->eq('urls.url', ':url')
217
                )
218
            ;
219
220
            $parameters['url'] = $url;
221
        }
222
223
        $qb->setParameters($parameters);
224
225
        return $qb->getQuery()->getResult();
226
    }
227
228
    private function addSessionRelUserFilterByUrl(Session $session, AccessUrl $url): QueryBuilder
229
    {
230
        $qb = $this->getEntityManager()->createQueryBuilder();
231
        $qb
232
            ->select('sru')
233
            ->from(SessionRelUser::class, 'sru')
234
            ->innerJoin('sru.user', 'u')
235
            ->innerJoin('u.portals', 'p')
236
            ->andWhere('sru.session = :session AND p.url = :url')
237
            ->setParameters([
238
                'session' => $session,
239
                'url' => $url,
240
            ])
241
        ;
242
243
        return $qb;
244
    }
245
246
    public function getUserFollowedSessionsInAccessUrl(User $user, AccessUrl $url): QueryBuilder
247
    {
248
        $callback = fn (Session $session) => $session->getId();
249
250
        if ($user->isHRM()) {
251
            $idList = array_map($callback, $user->getDRHSessions());
252
        } elseif ($user->isTeacher() || COURSEMANAGER === $user->getStatus()) {
253
            $idListAsCoach = $user
254
                ->getSessionsByStatusInCourseSubscription(Session::COURSE_COACH)
255
                ->map($callback)
256
                ->getValues()
257
            ;
258
            $idListAsGeneralCoach = array_map($callback, $user->getSessionsAsGeneralCoach());
259
            $idList = array_merge($idListAsCoach, $idListAsGeneralCoach);
260
        } elseif ($user->isSessionAdmin()) {
261
            $idList = array_map($callback, $user->getSessionsAsAdmin());
262
        } else {
263
            $idList = array_map($callback, $user->getSessionsAsStudent());
264
        }
265
266
        $qb = $this->createQueryBuilder('s');
267
        $qb
268
            ->innerJoin('s.urls', 'u')
269
            ->where($qb->expr()->eq('u.url', $url->getId()))
270
            ->andWhere($qb->expr()->in('s.id', ':id_list'))
271
            ->setParameter('id_list', $idList)
272
        ;
273
274
        return $qb;
275
    }
276
}
277