Passed
Pull Request — master (#5329)
by Angel Fernando Quiroz
08:20
created

CToolStateProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 6
dl 0
loc 8
rs 10
c 0
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\State;
8
9
use ApiPlatform\Doctrine\Orm\State\CollectionProvider;
10
use ApiPlatform\Metadata\Operation;
11
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
12
use ApiPlatform\State\ProviderInterface;
13
use Chamilo\CoreBundle\Entity\ResourceLink;
14
use Chamilo\CoreBundle\Entity\User;
15
use Chamilo\CoreBundle\Settings\SettingsManager;
16
use Chamilo\CoreBundle\Tool\ToolChain;
17
use Chamilo\CoreBundle\Traits\CourseFromRequestTrait;
18
use Chamilo\CourseBundle\Entity\CTool;
19
use Doctrine\ORM\EntityManagerInterface;
20
use Symfony\Bundle\SecurityBundle\Security;
21
use Symfony\Component\HttpFoundation\RequestStack;
22
23
/**
24
 * @template-implements ProviderInterface<CTool>
25
 */
26
final class CToolStateProvider implements ProviderInterface
27
{
28
    use CourseFromRequestTrait;
29
30
    public function __construct(
31
        private readonly CollectionProvider $provider,
32
        protected EntityManagerInterface $entityManager,
33
        private readonly SettingsManager $settingsManager,
34
        private readonly Security $security,
35
        private readonly ToolChain $toolChain,
36
        protected RequestStack $requestStack,
37
    ) {}
38
39
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): array
40
    {
41
        /** @var PartialPaginatorInterface $result */
42
        $result = $this->provider->provide($operation, $uriVariables, $context);
43
44
        $request = $this->requestStack->getMainRequest();
45
46
        $studentView = $request ? $request->getSession()->get('studentview') : 'studentview';
47
48
        /** @var User|null $user */
49
        $user = $this->security->getUser();
50
51
        $isAllowToEdit = $user && ($user->hasRole('ROLE_ADMIN') || $user->hasRole('ROLE_CURRENT_COURSE_TEACHER'));
52
        $isAllowToEditBack = $user && ($user->hasRole('ROLE_ADMIN') || $user->hasRole('ROLE_CURRENT_COURSE_TEACHER'));
53
        $isAllowToSessionEdit = $user && ($user->hasRole('ROLE_ADMIN') || $user->hasRole('ROLE_CURRENT_COURSE_TEACHER') || $user->hasRole('ROLE_CURRENT_COURSE_SESSION_TEACHER'));
54
55
        $allowVisibilityInSession = $this->settingsManager->getSetting('course.allow_edit_tool_visibility_in_session');
56
        $session = $this->getSession();
57
58
        $results = [];
59
60
        /** @var CTool $cTool */
61
        foreach ($result as $cTool) {
62
            $toolModel = $this->toolChain->getToolFromName(
63
                $cTool->getTool()->getTitle()
64
            );
65
66
            if (!$isAllowToEdit && 'admin' === $toolModel->getCategory()) {
0 ignored issues
show
Bug introduced by
The method getCategory() does not exist on Chamilo\CoreBundle\Tool\AbstractTool. It seems like you code against a sub-type of said class. However, the method does not exist in Chamilo\CoreBundle\Tool\AbstractCourseTool. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
            if (!$isAllowToEdit && 'admin' === $toolModel->/** @scrutinizer ignore-call */ getCategory()) {
Loading history...
67
                continue;
68
            }
69
70
            $resourceLinks = $cTool->getResourceNode()->getResourceLinks();
71
72
            if ($session && $allowVisibilityInSession) {
73
                $sessionLink = $resourceLinks->findFirst(
74
                    fn (int $key, ResourceLink $resourceLink): bool => $resourceLink->getSession()?->getId() === $session->getId()
75
                );
76
77
                if ($sessionLink) {
78
                    // Set the session link as unique to include in repsonse
79
                    $resourceLinks->clear();
80
                    $resourceLinks->add($sessionLink);
81
82
                    $isAllowToEdit = $isAllowToSessionEdit;
83
                } else {
84
                    $isAllowToEdit = $isAllowToEditBack;
85
                }
86
            }
87
88
            if (!$isAllowToEdit || 'studentview' === $studentView) {
89
                $notPublishedLink = ResourceLink::VISIBILITY_PUBLISHED !== $resourceLinks->first()->getVisibility();
90
91
                if ($notPublishedLink) {
92
                    continue;
93
                }
94
            }
95
96
            $results[] = $cTool;
97
        }
98
99
        return $results;
100
    }
101
}
102