Passed
Push — master ( d1c2a2...5885e4 )
by Angel Fernando Quiroz
17:30 queued 15s
created

addPersonalCalendarConditions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 14
rs 10
c 1
b 0
f 0
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);
39
    }
40
41
    private function addWhere(QueryBuilder $qb, string $resourceClass): void
42
    {
43
        if (CCalendarEvent::class !== $resourceClass) {
44
            return;
45
        }
46
47
        $courseId = $this->cidReqHelper->getCourseId();
48
        $sessionId = $this->cidReqHelper->getSessionId();
49
        $groupId = $this->cidReqHelper->getGroupId();
50
51
        /** @var ?User $user */
52
        $user = $this->security->getUser();
53
54
        $inCourseBase = !empty($courseId);
55
        $inSession = !empty($sessionId);
56
        $inCourseSession = $inCourseBase && $inSession;
57
58
        $inPersonalList = !$inCourseBase && !$inCourseSession;
59
60
        $alias = $qb->getRootAliases()[0];
61
62
        $qb
63
            ->innerJoin("$alias.resourceNode", 'node')
64
            ->leftJoin('node.resourceLinks', 'resource_links')
65
        ;
66
67
        if ($inPersonalList && $user) {
68
            $this->addPersonalCalendarConditions($qb, $user);
69
        }
70
    }
71
72
    private function addPersonalCalendarConditions(QueryBuilder $qb, User $user): void
73
    {
74
        $qb
75
            ->andWhere(
76
                $qb->expr()->orX(
77
                    $qb->expr()->eq('resource_links.user', ':user'),
78
                    $qb->expr()->eq('node.creator', ':user')
79
                )
80
            )
81
            ->setParameter('user', $user->getId())
82
        ;
83
84
        if ('true' === $this->settingsManager->getSetting('agenda.agenda_event_subscriptions')) {
85
            $this->addSubscriptionsConditions($qb, $user);
86
        }
87
    }
88
89
    private function addSubscriptionsConditions(QueryBuilder $qb, User $user): void
90
    {
91
        $groupList = (new UserGroupModel())->getUserGroupListByUser($user->getId(), Usergroup::NORMAL_CLASS);
92
        $groupIdList = $groupList ? array_column($groupList, 'id') : [];
93
94
        $alias = $qb->getRootAliases()[0];
95
96
        $expr = $qb->expr()->orX(
97
            $qb->expr()->eq("$alias.subscriptionVisibility", ':visibility_all'),
98
        );
99
100
        if ($groupIdList) {
101
            $expr->add(
102
                $qb->expr()->orX(
103
                    $qb->expr()->eq("$alias.subscriptionVisibility", ':visibility_class'),
104
                    $qb->expr()->in("$alias.subscriptionItemId", ':item_id_list')
105
                )
106
            );
107
108
            $qb->setParameter('visibility_class', CCalendarEvent::SUBSCRIPTION_VISIBILITY_CLASS);
109
            $qb->setParameter('item_id_list', $groupIdList);
110
        }
111
112
        $qb
113
            ->orWhere($expr)
114
            ->setParameter(':visibility_all', CCalendarEvent::SUBSCRIPTION_VISIBILITY_ALL)
115
        ;
116
    }
117
}
118