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\Metadata\Operation; |
10
|
|
|
use ApiPlatform\State\ProviderInterface; |
11
|
|
|
use Chamilo\CoreBundle\Entity\User; |
12
|
|
|
use Chamilo\CoreBundle\Entity\Usergroup; |
13
|
|
|
use Chamilo\CoreBundle\Repository\Node\IllustrationRepository; |
14
|
|
|
use Chamilo\CoreBundle\Repository\Node\UsergroupRepository; |
15
|
|
|
use Exception; |
16
|
|
|
use Symfony\Bundle\SecurityBundle\Security; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @template-implements ProviderInterface<array<int, Usergroup>> |
20
|
|
|
*/ |
21
|
|
|
final class UsergroupStateProvider implements ProviderInterface |
22
|
|
|
{ |
23
|
|
|
public function __construct( |
24
|
|
|
private readonly Security $security, |
25
|
|
|
private readonly UsergroupRepository $usergroupRepository, |
26
|
|
|
private readonly IllustrationRepository $illustrationRepository |
27
|
|
|
) {} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @throws Exception |
31
|
|
|
*/ |
32
|
|
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable |
33
|
|
|
{ |
34
|
|
|
$operationName = $operation->getName(); |
35
|
|
|
if ('get_usergroup' === $operationName) { |
36
|
|
|
$groupId = $uriVariables['id'] ?? null; |
37
|
|
|
|
38
|
|
|
if (!$groupId) { |
39
|
|
|
throw new Exception("Group ID is required for 'get_usergroup' operation"); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$group = $this->usergroupRepository->findGroupById($groupId); |
43
|
|
|
|
44
|
|
|
if (!$group) { |
45
|
|
|
throw new Exception('Group not found'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$this->setGroupDetails($group); |
49
|
|
|
|
50
|
|
|
return [$group]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ('search_usergroups' === $operationName) { |
54
|
|
|
$searchTerm = $context['filters']['search'] ?? ''; |
55
|
|
|
$groups = $this->usergroupRepository->searchGroups($searchTerm); |
56
|
|
|
foreach ($groups as $group) { |
57
|
|
|
$this->setGroupDetails($group); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $groups; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
switch ($operationName) { |
64
|
|
|
case 'get_my_usergroups': |
65
|
|
|
$userId = $context['request_attributes']['_api_filters']['userId'] ?? null; |
66
|
|
|
if (!$userId) { |
67
|
|
|
/** @var User $user */ |
68
|
|
|
$user = $this->security->getUser(); |
69
|
|
|
$userId = $user?->getId(); |
70
|
|
|
} |
71
|
|
|
if (!$userId) { |
72
|
|
|
throw new Exception('User ID is required'); |
73
|
|
|
} |
74
|
|
|
$groups = $this->usergroupRepository->getGroupsByUser($userId, 0); |
75
|
|
|
|
76
|
|
|
break; |
77
|
|
|
|
78
|
|
|
case 'get_newest_usergroups': |
79
|
|
|
$groups = $this->usergroupRepository->getNewestGroups(); |
80
|
|
|
|
81
|
|
|
break; |
82
|
|
|
|
83
|
|
|
case 'get_popular_usergroups': |
84
|
|
|
$groups = $this->usergroupRepository->getPopularGroups(); |
85
|
|
|
|
86
|
|
|
break; |
87
|
|
|
|
88
|
|
|
default: |
89
|
|
|
$groups = []; |
90
|
|
|
|
91
|
|
|
break; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (\in_array($operationName, ['get_my_usergroups', 'get_newest_usergroups', 'get_popular_usergroups'])) { |
95
|
|
|
/** @var Usergroup $group */ |
96
|
|
|
foreach ($groups as $group) { |
97
|
|
|
$this->setGroupDetails($group); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $groups; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function supports(Operation $operation, array $uriVariables = [], array $context = []): bool |
105
|
|
|
{ |
106
|
|
|
return Usergroup::class === $operation->getClass(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function setGroupDetails(Usergroup $group): void |
110
|
|
|
{ |
111
|
|
|
$memberCount = $this->usergroupRepository->countMembers($group->getId()); |
112
|
|
|
$group->setMemberCount($memberCount); |
113
|
|
|
|
114
|
|
|
if ($this->illustrationRepository->hasIllustration($group)) { |
115
|
|
|
$picture = $this->illustrationRepository->getIllustrationUrl($group); |
116
|
|
|
$group->setPictureUrl($picture); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|