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\Session; |
11
|
|
|
use Chamilo\CoreBundle\Entity\SessionRelUser; |
12
|
|
|
use Chamilo\CoreBundle\Entity\SysAnnouncement; |
13
|
|
|
use Chamilo\CoreBundle\Entity\User; |
14
|
|
|
use Datetime; |
15
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
16
|
|
|
use Doctrine\DBAL\Types\Types; |
17
|
|
|
use Doctrine\ORM\QueryBuilder; |
18
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
19
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
20
|
|
|
use Symfony\Component\Security\Core\Security; |
21
|
|
|
|
22
|
|
|
class SysAnnouncementRepository extends ServiceEntityRepository |
23
|
|
|
{ |
24
|
|
|
protected ParameterBagInterface $parameterBag; |
25
|
|
|
protected Security $security; |
26
|
|
|
|
27
|
|
|
public function __construct(ManagerRegistry $registry, ParameterBagInterface $parameterBag, Security $security) |
28
|
|
|
{ |
29
|
|
|
parent::__construct($registry, SysAnnouncement::class); |
30
|
|
|
$this->parameterBag = $parameterBag; |
31
|
|
|
$this->security = $security; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getVisibilityList() |
35
|
|
|
{ |
36
|
|
|
$hierarchy = $this->parameterBag->get('security.role_hierarchy.roles'); |
37
|
|
|
$roles = []; |
38
|
|
|
array_walk_recursive($hierarchy, function ($role) use (&$roles): void { |
39
|
|
|
$roles[$role] = $role; |
40
|
|
|
}); |
41
|
|
|
|
42
|
|
|
return $roles; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getAnnouncementsQueryBuilder(string $iso, AccessUrl $url, ?User $user = null): QueryBuilder |
46
|
|
|
{ |
47
|
|
|
$qb = $this->createQueryBuilder('s'); |
48
|
|
|
$qb |
49
|
|
|
->andWhere('s.lang IS NULL OR s.lang = :lang OR s.lang = :empty') |
50
|
|
|
->andWhere('s.url = :url') |
51
|
|
|
->setParameters( |
52
|
|
|
['url' => $url, 'lang' => $iso, 'empty' => ''] |
53
|
|
|
) |
54
|
|
|
; |
55
|
|
|
|
56
|
|
|
$this->addDateQueryBuilder($qb); |
57
|
|
|
|
58
|
|
|
if (null !== $user) { |
59
|
|
|
$this->addRoleListQueryBuilder($user->getRoles(), $qb); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$qb->orderBy('s.dateStart', 'DESC'); |
63
|
|
|
|
64
|
|
|
return $qb; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getAnnouncements(User $user, AccessUrl $url, string $iso): array |
68
|
|
|
{ |
69
|
|
|
$qb = $this->getAnnouncementsQueryBuilder($iso, $url, $user); |
70
|
|
|
|
71
|
|
|
$announcements = $qb->getQuery()->getResult(); |
72
|
|
|
|
73
|
|
|
$cutSize = 500; |
74
|
|
|
$list = []; |
75
|
|
|
if (!empty($announcements)) { |
76
|
|
|
/** @var SysAnnouncement $announcement */ |
77
|
|
|
foreach ($announcements as $announcement) { |
78
|
|
|
if ($announcement->hasCareer()) { |
79
|
|
|
$promotionList = []; |
80
|
|
|
if ($announcement->hasPromotion()) { |
81
|
|
|
$promotionList[] = $announcement->getPromotion(); |
82
|
|
|
} else { |
83
|
|
|
$promotionList = $announcement->getCareer()->getPromotions(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$show = false; |
87
|
|
|
foreach ($promotionList as $promotion) { |
88
|
|
|
$sessionList = $promotion->getSessions(); |
89
|
|
|
foreach ($sessionList as $session) { |
90
|
|
|
$subscription = (new SessionRelUser()) |
91
|
|
|
->setUser($user) |
92
|
|
|
->setSession($session) |
93
|
|
|
->setRelationType(0) |
94
|
|
|
; |
95
|
|
|
|
96
|
|
|
// Check student |
97
|
|
|
if ($this->security->isGranted('ROLE_STUDENT') && |
98
|
|
|
$session->hasUser($subscription) |
99
|
|
|
//\SessionManager::isUserSubscribedAsStudent($sessionId, $userId) |
100
|
|
|
) { |
101
|
|
|
$show = true; |
102
|
|
|
|
103
|
|
|
break 2; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($this->security->isGranted('ROLE_TEACHER') && |
107
|
|
|
$session->isUserGeneralCoach($user) |
108
|
|
|
//SessionManager::user_is_general_coach($userId, $sessionId) |
109
|
|
|
) { |
110
|
|
|
$show = true; |
111
|
|
|
|
112
|
|
|
break 2; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// Check course coach |
116
|
|
|
//$coaches = \SessionManager::getCoachesBySession($sessionId); |
117
|
|
|
if ($this->security->isGranted('ROLE_TEACHER') && |
118
|
|
|
$session->getSessionRelCourseByUser($user, Session::COACH)->count() > 0 |
119
|
|
|
) { |
120
|
|
|
$show = true; |
121
|
|
|
|
122
|
|
|
break 2; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if (false === $show) { |
128
|
|
|
continue; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$announcementData = [ |
133
|
|
|
'id' => $announcement->getId(), |
134
|
|
|
'title' => $announcement->getTitle(), |
135
|
|
|
'content' => $announcement->getContent(), |
136
|
|
|
'readMore' => null, |
137
|
|
|
]; |
138
|
|
|
|
139
|
|
|
if (api_strlen(strip_tags($announcement->getContent())) > $cutSize) { |
140
|
|
|
$announcementData['content'] = cut($announcement->getContent(), $cutSize); |
141
|
|
|
$announcementData['readMore'] = true; |
142
|
|
|
} |
143
|
|
|
$list[] = $announcementData; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if (0 === \count($list)) { |
148
|
|
|
return []; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $list; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function addRoleListQueryBuilder(array $roles, QueryBuilder $qb = null): QueryBuilder |
155
|
|
|
{ |
156
|
|
|
$qb = $this->getOrCreateQueryBuilder($qb); |
157
|
|
|
|
158
|
|
|
$conditions[] = $qb->expr()->like('s.roles', $qb->expr()->literal('%ROLE_ANONYMOUS%')); |
|
|
|
|
159
|
|
|
|
160
|
|
|
if (!empty($roles)) { |
161
|
|
|
foreach ($roles as $role) { |
162
|
|
|
$conditions[] = $qb->expr()->like('s.roles', $qb->expr()->literal('%'.$role.'%')); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$orX = $qb->expr()->orX(); |
167
|
|
|
$orX->addMultiple($conditions); |
168
|
|
|
$qb->andWhere($orX); |
169
|
|
|
|
170
|
|
|
return $qb; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function addDateQueryBuilder(QueryBuilder $qb = null): QueryBuilder |
174
|
|
|
{ |
175
|
|
|
$qb = $this->getOrCreateQueryBuilder($qb); |
176
|
|
|
$qb |
177
|
|
|
->andWhere('s.dateStart <= :now AND s.dateEnd > :now') |
178
|
|
|
->setParameter('now', new Datetime(), Types::DATETIME_MUTABLE) |
179
|
|
|
; |
180
|
|
|
|
181
|
|
|
return $qb; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function update(SysAnnouncement $sysAnnouncement, $andFlush = true): void |
185
|
|
|
{ |
186
|
|
|
$this->getEntityManager()->persist($sysAnnouncement); |
187
|
|
|
if ($andFlush) { |
188
|
|
|
$this->getEntityManager()->flush(); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
protected function getOrCreateQueryBuilder(QueryBuilder $qb = null, string $alias = 's'): QueryBuilder |
193
|
|
|
{ |
194
|
|
|
return $qb ?: $this->createQueryBuilder($alias); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function delete($id): void |
198
|
|
|
{ |
199
|
|
|
$announcement = $this->find($id); |
200
|
|
|
if (null !== $announcement) { |
201
|
|
|
$em = $this->getEntityManager(); |
202
|
|
|
$em->remove($announcement); |
203
|
|
|
$em->flush(); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|