|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
|
4
|
|
|
|
|
5
|
|
|
declare(strict_types=1); |
|
6
|
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\DataProvider\Extension; |
|
8
|
|
|
|
|
9
|
|
|
use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface; |
|
10
|
|
|
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface; |
|
11
|
|
|
use ApiPlatform\Metadata\Operation; |
|
12
|
|
|
use Chamilo\CoreBundle\Entity\User; |
|
13
|
|
|
use Chamilo\CoreBundle\Entity\Usergroup; |
|
14
|
|
|
use Chamilo\CoreBundle\ServiceHelper\CidReqHelper; |
|
15
|
|
|
use Chamilo\CoreBundle\Settings\SettingsManager; |
|
16
|
|
|
use Chamilo\CourseBundle\Entity\CCalendarEvent; |
|
17
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
18
|
|
|
use Symfony\Component\Security\Core\Security; |
|
19
|
|
|
use UserGroupModel; |
|
20
|
|
|
|
|
21
|
|
|
final class CCalendarEventExtension implements QueryCollectionExtensionInterface |
|
22
|
|
|
{ |
|
23
|
|
|
use CourseLinkExtensionTrait; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct( |
|
26
|
|
|
private readonly Security $security, |
|
27
|
|
|
private readonly CidReqHelper $cidReqHelper, |
|
28
|
|
|
private readonly SettingsManager $settingsManager, |
|
29
|
|
|
) {} |
|
30
|
|
|
|
|
31
|
|
|
public function applyToCollection( |
|
32
|
|
|
QueryBuilder $queryBuilder, |
|
33
|
|
|
QueryNameGeneratorInterface $queryNameGenerator, |
|
34
|
|
|
string $resourceClass, |
|
35
|
|
|
?Operation $operation = null, |
|
36
|
|
|
array $context = [] |
|
37
|
|
|
): void { |
|
38
|
|
|
$this->addWhere($queryBuilder, $resourceClass, $context); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
private function addWhere(QueryBuilder $qb, string $resourceClass, array $context): void |
|
42
|
|
|
{ |
|
43
|
|
|
if (CCalendarEvent::class !== $resourceClass) { |
|
44
|
|
|
return; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$isGlobalType = isset($context['filters']['type']) && $context['filters']['type'] === 'global'; |
|
48
|
|
|
if ($isGlobalType) { |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$courseId = $this->cidReqHelper->getCourseId(); |
|
53
|
|
|
$sessionId = $this->cidReqHelper->getSessionId(); |
|
54
|
|
|
$groupId = $this->cidReqHelper->getGroupId(); |
|
55
|
|
|
|
|
56
|
|
|
/** @var ?User $user */ |
|
57
|
|
|
$user = $this->security->getUser(); |
|
58
|
|
|
|
|
59
|
|
|
$inCourseBase = !empty($courseId); |
|
60
|
|
|
$inSession = !empty($sessionId); |
|
61
|
|
|
$inCourseSession = $inCourseBase && $inSession; |
|
62
|
|
|
|
|
63
|
|
|
$inPersonalList = !$inCourseBase && !$inCourseSession; |
|
64
|
|
|
|
|
65
|
|
|
$alias = $qb->getRootAliases()[0]; |
|
66
|
|
|
|
|
67
|
|
|
$qb |
|
68
|
|
|
->innerJoin("$alias.resourceNode", 'node') |
|
69
|
|
|
->leftJoin('node.resourceLinks', 'resource_links') |
|
70
|
|
|
; |
|
71
|
|
|
|
|
72
|
|
|
if ($inPersonalList && $user) { |
|
73
|
|
|
$this->addPersonalCalendarConditions($qb, $user); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function addPersonalCalendarConditions(QueryBuilder $qb, User $user): void |
|
78
|
|
|
{ |
|
79
|
|
|
$qb |
|
80
|
|
|
->andWhere( |
|
81
|
|
|
$qb->expr()->orX( |
|
82
|
|
|
$qb->expr()->eq('resource_links.user', ':user'), |
|
83
|
|
|
$qb->expr()->eq('node.creator', ':user') |
|
84
|
|
|
) |
|
85
|
|
|
) |
|
86
|
|
|
->setParameter('user', $user->getId()) |
|
87
|
|
|
; |
|
88
|
|
|
|
|
89
|
|
|
if ('true' === $this->settingsManager->getSetting('agenda.agenda_event_subscriptions')) { |
|
90
|
|
|
$this->addSubscriptionsConditions($qb, $user); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function addSubscriptionsConditions(QueryBuilder $qb, User $user): void |
|
95
|
|
|
{ |
|
96
|
|
|
$groupList = (new UserGroupModel())->getUserGroupListByUser($user->getId(), Usergroup::NORMAL_CLASS); |
|
97
|
|
|
$groupIdList = $groupList ? array_column($groupList, 'id') : []; |
|
98
|
|
|
|
|
99
|
|
|
$alias = $qb->getRootAliases()[0]; |
|
100
|
|
|
|
|
101
|
|
|
$expr = $qb->expr()->orX( |
|
102
|
|
|
$qb->expr()->eq("$alias.subscriptionVisibility", ':visibility_all'), |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
if ($groupIdList) { |
|
106
|
|
|
$expr->add( |
|
107
|
|
|
$qb->expr()->orX( |
|
108
|
|
|
$qb->expr()->eq("$alias.subscriptionVisibility", ':visibility_class'), |
|
109
|
|
|
$qb->expr()->in("$alias.subscriptionItemId", ':item_id_list') |
|
110
|
|
|
) |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
|
|
$qb->setParameter('visibility_class', CCalendarEvent::SUBSCRIPTION_VISIBILITY_CLASS); |
|
114
|
|
|
$qb->setParameter('item_id_list', $groupIdList); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$qb |
|
118
|
|
|
->orWhere($expr) |
|
119
|
|
|
->setParameter(':visibility_all', CCalendarEvent::SUBSCRIPTION_VISIBILITY_ALL) |
|
120
|
|
|
; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|