1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* For licensing terms, see /license.txt */ |
||
6 | |||
7 | namespace Chamilo\CourseBundle\Entity; |
||
8 | |||
9 | use ApiPlatform\Doctrine\Orm\Filter\SearchFilter; |
||
10 | use ApiPlatform\Metadata\ApiFilter; |
||
11 | use ApiPlatform\Metadata\ApiResource; |
||
12 | use ApiPlatform\Metadata\Get; |
||
13 | use ApiPlatform\Metadata\GetCollection; |
||
14 | use Chamilo\CoreBundle\Entity\AbstractResource; |
||
15 | use Chamilo\CoreBundle\Entity\ResourceInterface; |
||
16 | use Chamilo\CoreBundle\Entity\User; |
||
17 | use Chamilo\CourseBundle\Repository\CGroupRepository; |
||
18 | use Doctrine\Common\Collections\ArrayCollection; |
||
19 | use Doctrine\Common\Collections\Collection; |
||
20 | use Doctrine\ORM\Mapping as ORM; |
||
21 | use Stringable; |
||
22 | use Symfony\Component\Serializer\Annotation\Groups; |
||
23 | use Symfony\Component\Uid\Uuid; |
||
24 | use Symfony\Component\Validator\Constraints as Assert; |
||
25 | |||
26 | #[ApiResource( |
||
27 | shortName: 'Groups', |
||
28 | operations: [ |
||
29 | new GetCollection( |
||
30 | uriTemplate: '/groups', |
||
31 | openapiContext: [ |
||
32 | 'parameters' => [ |
||
33 | [ |
||
34 | 'name' => 'resourceNode.parent', |
||
35 | 'in' => 'query', |
||
36 | 'required' => true, |
||
37 | 'description' => 'Filter groups by the parent resource node (course)', |
||
38 | 'schema' => ['type' => 'integer'], |
||
39 | ], |
||
40 | ], |
||
41 | ] |
||
42 | ), |
||
43 | new Get(security: "is_granted('VIEW', object.resourceNode)"), |
||
44 | ], |
||
45 | normalizationContext: ['groups' => ['group:read']], |
||
46 | denormalizationContext: ['groups' => ['group:write']], |
||
47 | paginationEnabled: true |
||
48 | )] |
||
49 | #[ApiFilter(SearchFilter::class, properties: ['resourceNode.parent' => 'exact'])] |
||
50 | #[ORM\Table(name: 'c_group_info')] |
||
51 | #[ORM\Entity(repositoryClass: CGroupRepository::class)] |
||
52 | class CGroup extends AbstractResource implements ResourceInterface, Stringable |
||
53 | { |
||
54 | public const TOOL_NOT_AVAILABLE = 0; |
||
55 | public const TOOL_PUBLIC = 1; |
||
56 | public const TOOL_PRIVATE = 2; |
||
57 | public const TOOL_PRIVATE_BETWEEN_USERS = 3; |
||
58 | #[ORM\Column(name: 'iid', type: 'integer')] |
||
59 | #[ORM\Id] |
||
60 | #[ORM\GeneratedValue] |
||
61 | #[Groups(['group:read', 'group:write'])] |
||
62 | protected ?int $iid = null; |
||
63 | #[Assert\NotBlank] |
||
64 | #[ORM\Column(name: 'title', type: 'string', length: 100, nullable: false)] |
||
65 | #[Groups(['group:read', 'group:write'])] |
||
66 | protected string $title; |
||
67 | #[Assert\NotNull] |
||
68 | #[ORM\Column(name: 'status', type: 'boolean', nullable: false)] |
||
69 | #[Groups(['group:read', 'group:write'])] |
||
70 | protected bool $status; |
||
71 | #[ORM\ManyToOne(targetEntity: CGroupCategory::class, cascade: ['persist'])] |
||
72 | #[ORM\JoinColumn(name: 'category_id', referencedColumnName: 'iid', onDelete: 'CASCADE')] |
||
73 | protected ?CGroupCategory $category = null; |
||
74 | #[ORM\Column(name: 'description', type: 'text', nullable: true)] |
||
75 | #[Groups(['group:read', 'group:write'])] |
||
76 | protected ?string $description = null; |
||
77 | #[Assert\NotBlank] |
||
78 | #[ORM\Column(name: 'max_student', type: 'integer')] |
||
79 | protected int $maxStudent; |
||
80 | #[ORM\Column(name: 'doc_state', type: 'integer')] |
||
81 | protected int $docState; |
||
82 | #[ORM\Column(name: 'calendar_state', type: 'integer')] |
||
83 | protected int $calendarState; |
||
84 | #[ORM\Column(name: 'work_state', type: 'integer')] |
||
85 | protected int $workState; |
||
86 | #[ORM\Column(name: 'announcements_state', type: 'integer')] |
||
87 | protected int $announcementsState; |
||
88 | #[ORM\Column(name: 'forum_state', type: 'integer')] |
||
89 | protected int $forumState; |
||
90 | #[ORM\Column(name: 'wiki_state', type: 'integer')] |
||
91 | protected int $wikiState; |
||
92 | #[ORM\Column(name: 'chat_state', type: 'integer')] |
||
93 | protected int $chatState; |
||
94 | #[ORM\Column(name: 'self_registration_allowed', type: 'boolean')] |
||
95 | protected bool $selfRegistrationAllowed; |
||
96 | #[ORM\Column(name: 'self_unregistration_allowed', type: 'boolean')] |
||
97 | protected bool $selfUnregistrationAllowed; |
||
98 | #[ORM\Column(name: 'document_access', type: 'integer', options: ['default' => 0])] |
||
99 | protected int $documentAccess; |
||
100 | |||
101 | /** |
||
102 | * @var Collection<int, CGroupRelUser> |
||
103 | */ |
||
104 | #[ORM\OneToMany(mappedBy: 'group', targetEntity: CGroupRelUser::class)] |
||
105 | protected Collection $members; |
||
106 | |||
107 | /** |
||
108 | * @var Collection<int, CGroupRelTutor> |
||
109 | */ |
||
110 | #[ORM\OneToMany(mappedBy: 'group', targetEntity: CGroupRelTutor::class)] |
||
111 | protected Collection $tutors; |
||
112 | public function __construct() |
||
113 | { |
||
114 | $this->status = true; |
||
115 | $this->members = new ArrayCollection(); |
||
116 | $this->tutors = new ArrayCollection(); |
||
117 | // Default values |
||
118 | $defaultVisibility = self::TOOL_PRIVATE; |
||
119 | $this->docState = $defaultVisibility; |
||
120 | $this->calendarState = $defaultVisibility; |
||
121 | $this->workState = $defaultVisibility; |
||
122 | $this->announcementsState = $defaultVisibility; |
||
123 | $this->forumState = $defaultVisibility; |
||
124 | $this->wikiState = $defaultVisibility; |
||
125 | $this->chatState = $defaultVisibility; |
||
126 | $this->documentAccess = $defaultVisibility; |
||
127 | $this->selfRegistrationAllowed = false; |
||
128 | $this->selfUnregistrationAllowed = false; |
||
129 | } |
||
130 | public function __toString(): string |
||
131 | { |
||
132 | return $this->getTitle(); |
||
133 | } |
||
134 | |||
135 | public function getIid(): ?int |
||
136 | { |
||
137 | return $this->iid; |
||
138 | } |
||
139 | |||
140 | public function setTitle(string $title): self |
||
141 | { |
||
142 | $this->title = $title; |
||
143 | |||
144 | return $this; |
||
145 | } |
||
146 | |||
147 | public function getTitle(): string |
||
148 | { |
||
149 | return $this->title; |
||
150 | } |
||
151 | public function setStatus(bool $status): self |
||
152 | { |
||
153 | $this->status = $status; |
||
154 | |||
155 | return $this; |
||
156 | } |
||
157 | public function getStatus(): bool |
||
158 | { |
||
159 | return $this->status; |
||
160 | } |
||
161 | public function setDescription(string $description): self |
||
162 | { |
||
163 | $this->description = $description; |
||
164 | |||
165 | return $this; |
||
166 | } |
||
167 | public function getDescription(): ?string |
||
168 | { |
||
169 | return $this->description; |
||
170 | } |
||
171 | public function setMaxStudent(int $maxStudent): self |
||
172 | { |
||
173 | $this->maxStudent = $maxStudent; |
||
174 | |||
175 | return $this; |
||
176 | } |
||
177 | public function getMaxStudent(): int |
||
178 | { |
||
179 | return $this->maxStudent; |
||
180 | } |
||
181 | public function setDocState(int $docState): self |
||
182 | { |
||
183 | $this->docState = $docState; |
||
184 | |||
185 | return $this; |
||
186 | } |
||
187 | |||
188 | public function getDocState(): int |
||
189 | { |
||
190 | return $this->docState; |
||
191 | } |
||
192 | public function setCalendarState(int $calendarState): self |
||
193 | { |
||
194 | $this->calendarState = $calendarState; |
||
195 | |||
196 | return $this; |
||
197 | } |
||
198 | |||
199 | public function getCalendarState(): int |
||
200 | { |
||
201 | return $this->calendarState; |
||
202 | } |
||
203 | public function setWorkState(int $workState): self |
||
204 | { |
||
205 | $this->workState = $workState; |
||
206 | |||
207 | return $this; |
||
208 | } |
||
209 | |||
210 | public function getWorkState(): int |
||
211 | { |
||
212 | return $this->workState; |
||
213 | } |
||
214 | public function setAnnouncementsState(int $announcementsState): self |
||
215 | { |
||
216 | $this->announcementsState = $announcementsState; |
||
217 | |||
218 | return $this; |
||
219 | } |
||
220 | |||
221 | public function getAnnouncementsState(): int |
||
222 | { |
||
223 | return $this->announcementsState; |
||
224 | } |
||
225 | public function setForumState(int $forumState): self |
||
226 | { |
||
227 | $this->forumState = $forumState; |
||
228 | |||
229 | return $this; |
||
230 | } |
||
231 | public function getForumState(): int |
||
232 | { |
||
233 | return $this->forumState; |
||
234 | } |
||
235 | public function setWikiState(int $wikiState): self |
||
236 | { |
||
237 | $this->wikiState = $wikiState; |
||
238 | |||
239 | return $this; |
||
240 | } |
||
241 | |||
242 | public function getWikiState(): int |
||
243 | { |
||
244 | return $this->wikiState; |
||
245 | } |
||
246 | public function setChatState(int $chatState): self |
||
247 | { |
||
248 | $this->chatState = $chatState; |
||
249 | |||
250 | return $this; |
||
251 | } |
||
252 | |||
253 | public function getChatState(): int |
||
254 | { |
||
255 | return $this->chatState; |
||
256 | } |
||
257 | public function setSelfRegistrationAllowed(bool $selfRegistrationAllowed): self |
||
258 | { |
||
259 | $this->selfRegistrationAllowed = $selfRegistrationAllowed; |
||
260 | |||
261 | return $this; |
||
262 | } |
||
263 | |||
264 | public function getSelfRegistrationAllowed(): bool |
||
265 | { |
||
266 | return $this->selfRegistrationAllowed; |
||
267 | } |
||
268 | public function setSelfUnregistrationAllowed(bool $selfUnregistrationAllowed): self |
||
269 | { |
||
270 | $this->selfUnregistrationAllowed = $selfUnregistrationAllowed; |
||
271 | |||
272 | return $this; |
||
273 | } |
||
274 | |||
275 | public function getSelfUnregistrationAllowed(): bool |
||
276 | { |
||
277 | return $this->selfUnregistrationAllowed; |
||
278 | } |
||
279 | public function getDocumentAccess(): int |
||
280 | { |
||
281 | return $this->documentAccess; |
||
282 | } |
||
283 | public function setDocumentAccess(int $documentAccess): self |
||
284 | { |
||
285 | $this->documentAccess = $documentAccess; |
||
286 | |||
287 | return $this; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * @return Collection<int, CGroupRelUser> |
||
292 | */ |
||
293 | public function getMembers(): Collection |
||
294 | { |
||
295 | return $this->members; |
||
296 | } |
||
297 | |||
298 | public function setMembers(Collection $members): self |
||
299 | { |
||
300 | $this->members = $members; |
||
301 | |||
302 | return $this; |
||
303 | } |
||
304 | public function hasMembers(): bool |
||
305 | { |
||
306 | return $this->members->count() > 0; |
||
307 | } |
||
308 | public function hasMember(User $user): bool |
||
309 | { |
||
310 | if (!$this->hasMembers()) { |
||
311 | return false; |
||
312 | } |
||
313 | $list = $this->members->filter(fn (CGroupRelUser $member) => $member->getUser()->getId() === $user->getId()); |
||
314 | |||
315 | return $list->count() > 0; |
||
316 | } |
||
317 | public function hasTutor(User $user): bool |
||
318 | { |
||
319 | if (!$this->hasTutors()) { |
||
320 | return false; |
||
321 | } |
||
322 | $list = $this->tutors->filter(fn (CGroupRelTutor $tutor) => $tutor->getUser()->getId() === $user->getId()); |
||
323 | |||
324 | return $list->count() > 0; |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * @return Collection<int, CGroupRelTutor> |
||
329 | */ |
||
330 | public function getTutors(): Collection |
||
331 | { |
||
332 | return $this->tutors; |
||
333 | } |
||
334 | |||
335 | public function setTutors(Collection $tutors): self |
||
336 | { |
||
337 | $this->tutors = $tutors; |
||
338 | |||
339 | return $this; |
||
340 | } |
||
341 | public function hasTutors(): bool |
||
342 | { |
||
343 | return $this->tutors->count() > 0; |
||
344 | } |
||
345 | public function getCategory(): ?CGroupCategory |
||
346 | { |
||
347 | return $this->category; |
||
348 | } |
||
349 | public function setCategory(?CGroupCategory $category = null): self |
||
350 | { |
||
351 | $this->category = $category; |
||
352 | |||
353 | return $this; |
||
354 | } |
||
355 | |||
356 | public function getResourceIdentifier(): int|Uuid |
||
357 | { |
||
358 | return $this->iid; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
359 | } |
||
360 | public function getResourceName(): string |
||
361 | { |
||
362 | return $this->getTitle(); |
||
363 | } |
||
364 | public function setResourceName(string $name): self |
||
365 | { |
||
366 | return $this->setTitle($name); |
||
367 | } |
||
368 | } |
||
369 |