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\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface; |
10
|
|
|
//use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface; |
11
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; |
12
|
|
|
use Chamilo\CoreBundle\Entity\ResourceLink; |
13
|
|
|
use Chamilo\CourseBundle\Entity\CToolIntro; |
14
|
|
|
use Doctrine\ORM\QueryBuilder; |
15
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
16
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
17
|
|
|
use Symfony\Component\Security\Core\Security; |
18
|
|
|
|
19
|
|
|
final class CToolIntroExtension implements QueryCollectionExtensionInterface |
20
|
|
|
{ |
21
|
|
|
private Security $security; |
22
|
|
|
private RequestStack $requestStack; |
23
|
|
|
|
24
|
|
|
public function __construct(Security $security, RequestStack $request) |
25
|
|
|
{ |
26
|
|
|
$this->security = $security; |
27
|
|
|
$this->requestStack = $request; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null): void |
31
|
|
|
{ |
32
|
|
|
$this->addWhere($queryBuilder, $resourceClass); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
private function addWhere(QueryBuilder $queryBuilder, string $resourceClass): void |
36
|
|
|
{ |
37
|
|
|
if (CToolIntro::class !== $resourceClass) { |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/*if ($this->security->isGranted('ROLE_ADMIN')) { |
42
|
|
|
return; |
43
|
|
|
}*/ |
44
|
|
|
|
45
|
|
|
if (null === $user = $this->security->getUser()) { |
46
|
|
|
throw new AccessDeniedException('Access Denied.'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
50
|
|
|
|
51
|
|
|
$courseId = $request->query->get('cid'); |
52
|
|
|
$sessionId = $request->query->get('sid'); |
53
|
|
|
$groupId = $request->query->get('gid'); |
54
|
|
|
|
55
|
|
|
$rootAlias = $queryBuilder->getRootAliases()[0]; |
56
|
|
|
|
57
|
|
|
$queryBuilder |
58
|
|
|
->innerJoin("$rootAlias.resourceNode", 'node') |
59
|
|
|
->innerJoin('node.resourceLinks', 'links') |
60
|
|
|
; |
61
|
|
|
|
62
|
|
|
// Do not show deleted resources. |
63
|
|
|
$queryBuilder |
64
|
|
|
->andWhere('links.visibility != :visibilityDeleted') |
65
|
|
|
->setParameter('visibilityDeleted', ResourceLink::VISIBILITY_DELETED) |
66
|
|
|
; |
67
|
|
|
|
68
|
|
|
$allowDraft = |
69
|
|
|
$this->security->isGranted('ROLE_ADMIN') || |
70
|
|
|
$this->security->isGranted('ROLE_CURRENT_COURSE_TEACHER') |
71
|
|
|
; |
72
|
|
|
|
73
|
|
|
if (!$allowDraft) { |
74
|
|
|
$queryBuilder |
75
|
|
|
->andWhere('links.visibility != :visibilityDraft') |
76
|
|
|
->setParameter('visibilityDraft', ResourceLink::VISIBILITY_DRAFT) |
77
|
|
|
; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$queryBuilder |
81
|
|
|
->andWhere('links.course = :course') |
82
|
|
|
->setParameter('course', $courseId) |
83
|
|
|
; |
84
|
|
|
|
85
|
|
|
if (empty($sessionId)) { |
86
|
|
|
$queryBuilder->andWhere('links.session IS NULL'); |
87
|
|
|
} else { |
88
|
|
|
$queryBuilder |
89
|
|
|
->andWhere('links.session = :session') |
90
|
|
|
->setParameter('session', $sessionId) |
91
|
|
|
; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (empty($groupId)) { |
95
|
|
|
$queryBuilder->andWhere('links.group IS NULL'); |
96
|
|
|
} else { |
97
|
|
|
$queryBuilder |
98
|
|
|
->andWhere('links.group = :group') |
99
|
|
|
->setParameter('group', $groupId) |
100
|
|
|
; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|