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