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