1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Repository; |
8
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\DataFixtures\AccessGroupFixtures; |
10
|
|
|
use Chamilo\CoreBundle\Entity\Group; |
11
|
|
|
use Chamilo\CoreBundle\Entity\User; |
12
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
13
|
|
|
use Doctrine\Common\Collections\Collection; |
14
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
15
|
|
|
|
16
|
|
|
class GroupRepository extends ServiceEntityRepository |
17
|
|
|
{ |
18
|
|
|
public function __construct(ManagerRegistry $registry) |
19
|
|
|
{ |
20
|
|
|
parent::__construct($registry, Group::class); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function delete(Group $group): void |
24
|
|
|
{ |
25
|
|
|
$em = $this->getEntityManager(); |
26
|
|
|
$em->remove($group); |
27
|
|
|
$em->flush(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return User[]|Collection|array |
32
|
|
|
*/ |
33
|
|
|
public function getAdmins() |
34
|
|
|
{ |
35
|
|
|
$criteria = [ |
36
|
|
|
'name' => 'admins', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** @var Group|null $group */ |
40
|
|
|
$group = $this->findOneBy($criteria); |
41
|
|
|
if (null !== $group) { |
42
|
|
|
return $group->getUsers(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return []; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getDefaultGroups(): array |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
|
|
[ |
52
|
|
|
'code' => 'ADMIN', |
53
|
|
|
'title' => 'Administrators', |
54
|
|
|
'roles' => ['ROLE_ADMIN'], |
55
|
|
|
], |
56
|
|
|
[ |
57
|
|
|
'code' => 'STUDENT', |
58
|
|
|
'title' => 'Students', |
59
|
|
|
'roles' => ['ROLE_STUDENT'], |
60
|
|
|
], |
61
|
|
|
[ |
62
|
|
|
'code' => 'TEACHER', |
63
|
|
|
'title' => 'Teachers', |
64
|
|
|
'roles' => ['ROLE_TEACHER'], |
65
|
|
|
], |
66
|
|
|
[ |
67
|
|
|
'code' => 'RRHH', |
68
|
|
|
'title' => 'Human resources manager', |
69
|
|
|
'roles' => ['ROLE_RRHH'], |
70
|
|
|
], |
71
|
|
|
[ |
72
|
|
|
'code' => 'SESSION_MANAGER', |
73
|
|
|
'title' => 'Session', |
74
|
|
|
'roles' => ['ROLE_SESSION_MANAGER'], |
75
|
|
|
], |
76
|
|
|
[ |
77
|
|
|
'code' => 'QUESTION_MANAGER', |
78
|
|
|
'title' => 'Question manager', |
79
|
|
|
'roles' => ['ROLE_QUESTION_MANAGER'], |
80
|
|
|
], |
81
|
|
|
[ |
82
|
|
|
'code' => 'STUDENT_BOSS', |
83
|
|
|
'title' => 'Student boss', |
84
|
|
|
'roles' => ['ROLE_STUDENT_BOSS'], |
85
|
|
|
], |
86
|
|
|
[ |
87
|
|
|
'code' => 'INVITEE', |
88
|
|
|
'title' => 'Invitee', |
89
|
|
|
'roles' => ['ROLE_INVITEE'], |
90
|
|
|
], |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function createDefaultGroups(AccessGroupFixtures $accessGroupFixtures = null): void |
95
|
|
|
{ |
96
|
|
|
$em = $this->getEntityManager(); |
97
|
|
|
$groups = $this->getDefaultGroups(); |
98
|
|
|
|
99
|
|
|
foreach ($groups as $groupItem) { |
100
|
|
|
$groupExists = $this->findOneBy(['code' => $groupItem['code']]); |
101
|
|
|
if (null === $groupExists) { |
102
|
|
|
$group = (new Group($groupItem['title'])) |
103
|
|
|
->setCode($groupItem['code']) |
104
|
|
|
; |
105
|
|
|
|
106
|
|
|
foreach ($groupItem['roles'] as $role) { |
107
|
|
|
$group->addRole($role); |
108
|
|
|
} |
109
|
|
|
$em->persist($group); |
110
|
|
|
|
111
|
|
|
if (null !== $accessGroupFixtures) { |
112
|
|
|
$accessGroupFixtures->addReference('GROUP_'.$groupItem['code'], $group); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$em->flush(); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|