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) { |
|
|
|
|
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 $status, User $user, Course $course, Session $session): void |
85
|
|
|
{ |
86
|
|
|
if (!$session->isActive()) { |
87
|
|
|
throw new Exception('Session not active'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (!$user->isActive()) { |
91
|
|
|
throw new Exception('User not active'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (!$course->isActive()) { |
95
|
|
|
throw new Exception('Course not active'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (!$session->hasCourse($course)) { |
99
|
|
|
$msg = sprintf('Course %s is not subscribed to the session %s', $course->getTitle(), $session->getName()); |
100
|
|
|
|
101
|
|
|
throw new Exception($msg); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
switch ($status) { |
105
|
|
|
case Session::DRH: |
106
|
|
|
if ($user->hasRole('ROLE_RRHH')) { |
107
|
|
|
$session->addUserInSession(Session::DRH, $user); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
break; |
111
|
|
|
case Session::STUDENT: |
112
|
|
|
$session |
113
|
|
|
->addUserInSession(Session::STUDENT, $user) |
114
|
|
|
->addUserInCourse( |
115
|
|
|
Session::STUDENT, |
116
|
|
|
$user, |
117
|
|
|
$course |
118
|
|
|
) |
119
|
|
|
; |
120
|
|
|
|
121
|
|
|
break; |
122
|
|
|
case Session::COURSE_COACH: |
123
|
|
|
if ($user->hasRole('ROLE_TEACHER')) { |
124
|
|
|
$session->addUserInCourse( |
125
|
|
|
Session::COURSE_COACH, |
126
|
|
|
$user, |
127
|
|
|
$course |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
break; |
132
|
|
|
default: |
133
|
|
|
throw new Exception(sprintf('Cannot handle status %s', $status)); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return array<SessionRelCourse> |
139
|
|
|
*/ |
140
|
|
|
public function getSessionCoursesByStatusInUserSubscription(User $user, Session $session, int $relationType, AccessUrl $url = null): array |
141
|
|
|
{ |
142
|
|
|
$qb = $this->getEntityManager()->createQueryBuilder(); |
143
|
|
|
|
144
|
|
|
$qb->select('src') |
145
|
|
|
->from(SessionRelCourse::class, 'src') |
146
|
|
|
->innerJoin( |
147
|
|
|
SessionRelUser::class, |
148
|
|
|
'sru', |
149
|
|
|
Join::WITH, |
150
|
|
|
'src.session = sru.session' |
151
|
|
|
) |
152
|
|
|
->innerJoin('src.session', 'session') |
153
|
|
|
->where( |
154
|
|
|
$qb->expr()->eq('session', ':session') |
155
|
|
|
) |
156
|
|
|
->andWhere( |
157
|
|
|
$qb->expr()->eq('sru.user', ':user') |
158
|
|
|
) |
159
|
|
|
->andWhere( |
160
|
|
|
$qb->expr()->eq('sru.relationType', ':relation_type') |
161
|
|
|
) |
162
|
|
|
; |
163
|
|
|
|
164
|
|
|
$parameters = [ |
165
|
|
|
'session' => $session, |
166
|
|
|
'user' => $user, |
167
|
|
|
'relation_type' => $relationType, |
168
|
|
|
]; |
169
|
|
|
|
170
|
|
|
if ($url) { |
171
|
|
|
$qb->innerJoin('session.urls', 'urls') |
172
|
|
|
->andWhere( |
173
|
|
|
$qb->expr()->eq('urls.url', ':url') |
174
|
|
|
) |
175
|
|
|
; |
176
|
|
|
|
177
|
|
|
$parameters['url'] = $url; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$qb->setParameters($parameters); |
181
|
|
|
|
182
|
|
|
return $qb->getQuery()->getResult(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return array<SessionRelCourse> |
187
|
|
|
*/ |
188
|
|
|
public function getSessionCoursesByStatusInCourseSubscription(User $user, Session $session, int $status, AccessUrl $url = null): array |
189
|
|
|
{ |
190
|
|
|
$qb = $this->getEntityManager()->createQueryBuilder(); |
191
|
|
|
|
192
|
|
|
$qb->select('src') |
193
|
|
|
->from(SessionRelCourse::class, 'src') |
194
|
|
|
->innerJoin( |
195
|
|
|
SessionRelCourseRelUser::class, |
196
|
|
|
'srcru', |
197
|
|
|
Join::WITH, |
198
|
|
|
'src.session = srcru.session AND src.course = srcru.course' |
199
|
|
|
) |
200
|
|
|
->innerJoin('srcru.session', 'session') |
201
|
|
|
->where( |
202
|
|
|
$qb->expr()->eq('session', ':session') |
203
|
|
|
) |
204
|
|
|
->andWhere( |
205
|
|
|
$qb->expr()->eq('srcru.user', ':user') |
206
|
|
|
) |
207
|
|
|
->andWhere( |
208
|
|
|
$qb->expr()->eq('srcru.status', ':status') |
209
|
|
|
) |
210
|
|
|
; |
211
|
|
|
|
212
|
|
|
$parameters = [ |
213
|
|
|
'session' => $session, |
214
|
|
|
'user' => $user, |
215
|
|
|
'status' => $status, |
216
|
|
|
]; |
217
|
|
|
|
218
|
|
|
if ($url) { |
219
|
|
|
$qb->innerJoin('session.urls', 'urls') |
220
|
|
|
->andWhere( |
221
|
|
|
$qb->expr()->eq('urls.url', ':url') |
222
|
|
|
) |
223
|
|
|
; |
224
|
|
|
|
225
|
|
|
$parameters['url'] = $url; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$qb->setParameters($parameters); |
229
|
|
|
|
230
|
|
|
return $qb->getQuery()->getResult(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
private function addSessionRelUserFilterByUrl(Session $session, AccessUrl $url): QueryBuilder |
234
|
|
|
{ |
235
|
|
|
$qb = $this->getEntityManager()->createQueryBuilder(); |
236
|
|
|
$qb |
237
|
|
|
->select('sru') |
238
|
|
|
->from(User::class, 'u') |
239
|
|
|
->innerJoin(SessionRelUser::class, 'sru', Join::WITH, 'sru.user = u.id') |
240
|
|
|
->innerJoin(AccessUrlRelUser::class, 'uru', Join::WITH, 'uru.user = u.id') |
241
|
|
|
->andWhere('sru.session = :session AND uru.url = :url') |
242
|
|
|
->setParameters([ |
243
|
|
|
'session' => $session, |
244
|
|
|
'url' => $url, |
245
|
|
|
]) |
246
|
|
|
; |
247
|
|
|
|
248
|
|
|
return $qb; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
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.