Total Complexity | 88 |
Total Lines | 678 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Complex classes like SocialController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SocialController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | #[Route('/social-network')] |
||
42 | class SocialController extends AbstractController |
||
43 | { |
||
44 | #[Route('/personal-data/{userId}', name: 'chamilo_core_social_personal_data')] |
||
45 | public function getPersonalData( |
||
46 | int $userId, |
||
47 | SettingsManager $settingsManager, |
||
48 | UserToJsonNormalizer $userToJsonNormalizer |
||
49 | ): JsonResponse { |
||
50 | $propertiesToJson = $userToJsonNormalizer->serializeUserData($userId); |
||
51 | $properties = $propertiesToJson ? json_decode($propertiesToJson, true) : []; |
||
52 | |||
53 | $officerData = [ |
||
54 | ['name' => $settingsManager->getSetting('profile.data_protection_officer_name')], |
||
55 | ['role' => $settingsManager->getSetting('profile.data_protection_officer_role')], |
||
56 | ['email' => $settingsManager->getSetting('profile.data_protection_officer_email')], |
||
57 | ]; |
||
58 | $properties['officer_data'] = $officerData; |
||
59 | |||
60 | $dataForVue = [ |
||
61 | 'personalData' => $properties, |
||
62 | ]; |
||
63 | |||
64 | return $this->json($dataForVue); |
||
65 | } |
||
66 | |||
67 | #[Route('/terms-and-conditions/{userId}', name: 'chamilo_core_social_terms')] |
||
68 | public function getLegalTerms( |
||
69 | int $userId, |
||
70 | SettingsManager $settingsManager, |
||
71 | TranslatorInterface $translator, |
||
72 | LegalRepository $legalTermsRepo, |
||
73 | UserRepository $userRepo, |
||
74 | LanguageRepository $languageRepo |
||
75 | ): JsonResponse { |
||
76 | $user = $userRepo->find($userId); |
||
77 | if (!$user) { |
||
78 | return $this->json(['error' => 'User not found']); |
||
79 | } |
||
80 | |||
81 | $isoCode = $user->getLocale(); |
||
82 | $language = $languageRepo->findByIsoCode($isoCode); |
||
83 | $languageId = (int) $language->getId(); |
||
84 | |||
85 | $term = $legalTermsRepo->getLastConditionByLanguage($languageId); |
||
86 | |||
87 | if (!$term) { |
||
88 | $defaultLanguage = $settingsManager->getSetting('platform.platform_language'); |
||
89 | $term = $legalTermsRepo->getLastConditionByLanguage((int) $defaultLanguage); |
||
90 | } |
||
91 | |||
92 | if (!$term) { |
||
93 | return $this->json(['error' => 'Terms not found']); |
||
94 | } |
||
95 | |||
96 | $termExtraFields = new ExtraFieldValue('terms_and_condition'); |
||
97 | $values = $termExtraFields->getAllValuesByItem($term->getId()); |
||
98 | |||
99 | $termsContent = []; |
||
100 | foreach ($values as $value) { |
||
101 | if (!empty($value['value'])) { |
||
102 | $termsContent[] = [ |
||
103 | 'title' => $translator->trans($value['display_text'], [], 'messages', $isoCode), |
||
104 | 'content' => $value['value'], |
||
105 | ]; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | if (empty($termsContent)) { |
||
110 | $termsContent[] = [ |
||
111 | 'title' => $translator->trans('Terms and Conditions', [], 'messages', $isoCode), |
||
112 | 'content' => $term->getContent(), |
||
113 | ]; |
||
114 | } |
||
115 | |||
116 | $formattedDate = new DateTime('@'.$term->getDate()); |
||
117 | |||
118 | $dataForVue = [ |
||
119 | 'terms' => $termsContent, |
||
120 | 'date_text' => $translator->trans('PublicationDate', [], 'messages', $isoCode).': '.$formattedDate->format('Y-m-d H:i:s'), |
||
121 | ]; |
||
122 | |||
123 | return $this->json($dataForVue); |
||
124 | } |
||
125 | |||
126 | #[Route('/legal-status/{userId}', name: 'chamilo_core_social_legal_status')] |
||
127 | public function getLegalStatus( |
||
128 | int $userId, |
||
129 | SettingsManager $settingsManager, |
||
130 | TranslatorInterface $translator, |
||
131 | UserRepository $userRepo, |
||
132 | ): JsonResponse { |
||
133 | $allowTermsConditions = 'true' === $settingsManager->getSetting('registration.allow_terms_conditions'); |
||
134 | |||
135 | if (!$allowTermsConditions) { |
||
136 | return $this->json([ |
||
137 | 'message' => $translator->trans('No terms and conditions available', [], 'messages'), |
||
138 | ]); |
||
139 | } |
||
140 | |||
141 | $user = $userRepo->find($userId); |
||
142 | if (!$user) { |
||
143 | return $this->json(['error' => 'User not found']); |
||
144 | } |
||
145 | |||
146 | $extraFieldValue = new ExtraFieldValue('user'); |
||
147 | $value = $extraFieldValue->get_values_by_handler_and_field_variable($userId, 'legal_accept'); |
||
148 | |||
149 | if (empty($value['value'])) { |
||
150 | return $this->json([ |
||
151 | 'isAccepted' => false, |
||
152 | 'message' => $translator->trans('Send legal agreement', [], 'messages'), |
||
153 | ]); |
||
154 | } |
||
155 | |||
156 | [$legalId, $legalLanguageId, $legalTime] = explode(':', $value['value']); |
||
157 | $dateTime = new DateTime("@$legalTime"); |
||
158 | $response = [ |
||
159 | 'isAccepted' => true, |
||
160 | 'acceptDate' => $dateTime->format('Y-m-d H:i:s'), |
||
161 | 'message' => '', |
||
162 | ]; |
||
163 | |||
164 | return $this->json($response); |
||
165 | } |
||
166 | |||
167 | #[Route('/handle-privacy-request', name: 'chamilo_core_social_handle_privacy_request')] |
||
168 | public function handlePrivacyRequest( |
||
169 | Request $request, |
||
170 | SettingsManager $settingsManager, |
||
171 | UserRepository $userRepo, |
||
172 | TranslatorInterface $translator, |
||
173 | MailerInterface $mailer |
||
174 | ): JsonResponse { |
||
175 | $data = json_decode($request->getContent(), true); |
||
176 | $userId = $data['userId'] ? (int) $data['userId'] : null; |
||
177 | $explanation = $data['explanation'] ?? ''; |
||
178 | $requestType = $data['requestType'] ?? ''; |
||
179 | |||
180 | /** @var User $user */ |
||
181 | $user = $userRepo->find($userId); |
||
182 | |||
183 | if (!$user) { |
||
|
|||
184 | return $this->json(['success' => false, 'message' => 'User not found']); |
||
185 | } |
||
186 | |||
187 | if ('delete_account' === $requestType) { |
||
188 | $fieldToUpdate = 'request_for_delete_account'; |
||
189 | $justificationFieldToUpdate = 'request_for_delete_account_justification'; |
||
190 | $emailSubject = 'Request for account removal'; |
||
191 | $emailContent = sprintf($translator->trans('User %s asked for the deletion of his/her account, explaining that : ').$explanation, $user->getFullName()); |
||
192 | } elseif ('delete_legal' === $requestType) { |
||
193 | $fieldToUpdate = 'request_for_legal_agreement_consent_removal'; |
||
194 | $justificationFieldToUpdate = 'request_for_legal_agreement_consent_removal_justification'; |
||
195 | $emailSubject = 'Request for consent withdrawal on legal terms'; |
||
196 | $emailContent = sprintf($translator->trans('User %s asked for the removal of his/her consent to our legal terms, explaining that: ').$explanation, $user->getFullName()); |
||
197 | } else { |
||
198 | return $this->json(['success' => false, 'message' => 'Invalid action type']); |
||
199 | } |
||
200 | |||
201 | UserManager::createDataPrivacyExtraFields(); |
||
202 | UserManager::update_extra_field_value($userId, $fieldToUpdate, 1); |
||
203 | UserManager::update_extra_field_value($userId, $justificationFieldToUpdate, $explanation); |
||
204 | |||
205 | $emailPlatform = $settingsManager->getSetting('admin.administrator_email'); |
||
206 | |||
207 | $email = new Email(); |
||
208 | |||
209 | ->from($user->getEmail()) |
||
210 | ->to($emailPlatform) |
||
211 | ->subject($emailSubject) |
||
212 | ->html($emailContent) |
||
213 | ; |
||
214 | |||
215 | $mailer->send($email); |
||
216 | |||
217 | return $this->json([ |
||
218 | 'success' => true, |
||
219 | 'message' => $translator->trans('Your request has been received.'), |
||
220 | ]); |
||
221 | } |
||
222 | |||
223 | #[Route('/groups/{userId}', name: 'chamilo_core_social_groups')] |
||
224 | public function getGroups( |
||
225 | int $userId, |
||
226 | UsergroupRepository $usergroupRepository, |
||
227 | CForumThreadRepository $forumThreadRepository, |
||
228 | SettingsManager $settingsManager, |
||
229 | RequestStack $requestStack |
||
230 | ): JsonResponse { |
||
231 | $baseUrl = $requestStack->getCurrentRequest()->getBaseUrl(); |
||
232 | $cid = (int) $settingsManager->getSetting('forum.global_forums_course_id'); |
||
233 | $groupsArray = []; |
||
234 | $threadsArray = []; |
||
235 | if (!empty($cid)) { |
||
236 | $threads = $forumThreadRepository->getThreadsBySubscriptions($userId, $cid); |
||
237 | foreach ($threads as $thread) { |
||
238 | $threadId = $thread->getIid(); |
||
239 | $forumId = (int) $thread->getForum()->getIid(); |
||
240 | $threadsArray[] = [ |
||
241 | 'id' => $threadId, |
||
242 | 'name' => $thread->getTitle(), |
||
243 | 'description' => '', |
||
244 | 'url' => $baseUrl.'/main/forum/viewthread.php?cid='.$cid.'&sid=0&gid=0&forum='.$forumId.'&thread='.$threadId, |
||
245 | 'go_to' => $baseUrl.'/main/forum/index.php?cid='.$cid.'&sid=0&gid=0', |
||
246 | ]; |
||
247 | } |
||
248 | } else { |
||
249 | $groups = $usergroupRepository->getGroupsByUser($userId); |
||
250 | foreach ($groups as $group) { |
||
251 | $groupsArray[] = [ |
||
252 | 'id' => $group->getId(), |
||
253 | 'name' => $group->getTitle(), |
||
254 | 'description' => $group->getDescription(), |
||
255 | 'url' => $baseUrl.'/resources/usergroups/show/'.$group->getId(), |
||
256 | ]; |
||
257 | } |
||
258 | } |
||
259 | |||
260 | if (!empty($threadsArray)) { |
||
261 | return $this->json(['groups' => $threadsArray]); |
||
262 | } |
||
263 | |||
264 | return $this->json(['groups' => $groupsArray]); |
||
265 | } |
||
266 | |||
267 | #[Route('/get-forum-link', name: 'get_forum_link')] |
||
268 | public function getForumLink( |
||
269 | SettingsManager $settingsManager, |
||
270 | RequestStack $requestStack |
||
271 | ): JsonResponse { |
||
272 | $baseUrl = $requestStack->getCurrentRequest()->getBaseUrl(); |
||
273 | $cid = (int) $settingsManager->getSetting('forum.global_forums_course_id'); |
||
274 | |||
275 | $goToLink = ''; |
||
276 | if (!empty($cid)) { |
||
277 | $goToLink = $baseUrl.'/main/forum/index.php?cid='.$cid.'&sid=0&gid=0'; |
||
278 | } |
||
279 | |||
280 | return $this->json(['go_to' => $goToLink]); |
||
281 | } |
||
282 | |||
283 | #[Route('/invite-friends/{userId}/{groupId}', name: 'chamilo_core_social_invite_friends')] |
||
284 | public function inviteFriends( |
||
285 | int $userId, |
||
286 | int $groupId, |
||
287 | UserRepository $userRepository, |
||
288 | UsergroupRepository $usergroupRepository, |
||
289 | IllustrationRepository $illustrationRepository |
||
290 | ): JsonResponse { |
||
291 | $user = $userRepository->find($userId); |
||
292 | if (!$user) { |
||
293 | return $this->json(['error' => 'User not found'], Response::HTTP_NOT_FOUND); |
||
294 | } |
||
295 | |||
296 | $group = $usergroupRepository->find($groupId); |
||
297 | if (!$group) { |
||
298 | return $this->json(['error' => 'Group not found'], Response::HTTP_NOT_FOUND); |
||
299 | } |
||
300 | |||
301 | $friends = $userRepository->getFriendsNotInGroup($userId, $groupId); |
||
302 | |||
303 | $friendsList = array_map(function ($friend) use ($illustrationRepository) { |
||
304 | return [ |
||
305 | 'id' => $friend->getId(), |
||
306 | 'name' => $friend->getFirstName().' '.$friend->getLastName(), |
||
307 | 'avatar' => $illustrationRepository->getIllustrationUrl($friend), |
||
308 | ]; |
||
309 | }, $friends); |
||
310 | |||
311 | return $this->json(['friends' => $friendsList]); |
||
312 | } |
||
313 | |||
314 | #[Route('/add-users-to-group/{groupId}', name: 'chamilo_core_social_add_users_to_group')] |
||
326 | } |
||
327 | } |
||
328 | |||
329 | #[Route('/group/{groupId}/invited-users', name: 'chamilo_core_social_group_invited_users')] |
||
330 | public function groupInvitedUsers(int $groupId, UsergroupRepository $usergroupRepository, IllustrationRepository $illustrationRepository): JsonResponse |
||
331 | { |
||
332 | $invitedUsers = $usergroupRepository->getInvitedUsersByGroup($groupId); |
||
333 | |||
334 | $invitedUsersList = array_map(function ($user) { |
||
335 | return [ |
||
336 | 'id' => $user['id'], |
||
337 | 'name' => $user['username'], |
||
338 | // 'avatar' => $illustrationRepository->getIllustrationUrl($user), |
||
339 | ]; |
||
340 | }, $invitedUsers); |
||
341 | |||
342 | return $this->json(['invitedUsers' => $invitedUsersList]); |
||
343 | } |
||
344 | |||
345 | #[Route('/user-profile/{userId}', name: 'chamilo_core_social_user_profile')] |
||
393 | } |
||
394 | |||
395 | private function getExtraFieldBlock( |
||
396 | int $userId, |
||
397 | UserRepository $userRepository, |
||
398 | SettingsManager $settingsManager, |
||
399 | ExtraFieldRepository $extraFieldRepository, |
||
400 | ExtraFieldOptionsRepository $extraFieldOptionsRepository |
||
401 | ): array { |
||
402 | $user = $userRepository->find($userId); |
||
403 | if (!$user) { |
||
404 | return []; |
||
405 | } |
||
406 | |||
407 | $fieldVisibilityConfig = $settingsManager->getSetting('profile.profile_fields_visibility'); |
||
408 | $fieldVisibility = $fieldVisibilityConfig ? json_decode($fieldVisibilityConfig, true)['options'] : []; |
||
409 | |||
410 | $extraUserData = $userRepository->getExtraUserData($userId); |
||
411 | $extraFieldsFormatted = []; |
||
412 | foreach ($extraUserData as $key => $value) { |
||
413 | $fieldVariable = str_replace('extra_', '', $key); |
||
414 | |||
415 | $extraField = $extraFieldRepository->getHandlerFieldInfoByFieldVariable($fieldVariable, ExtraField::USER_FIELD_TYPE); |
||
416 | if (!$extraField || !isset($fieldVisibility[$fieldVariable]) || !$fieldVisibility[$fieldVariable]) { |
||
417 | continue; |
||
418 | } |
||
419 | |||
420 | $fieldValue = \is_array($value) ? implode(', ', $value) : $value; |
||
421 | |||
422 | switch ($extraField['type']) { |
||
423 | case ExtraField::FIELD_TYPE_RADIO: |
||
424 | case ExtraField::FIELD_TYPE_SELECT: |
||
425 | $extraFieldOptions = $extraFieldOptionsRepository->getFieldOptionByFieldAndOption($extraField['id'], $fieldValue, ExtraField::USER_FIELD_TYPE); |
||
426 | if (!empty($extraFieldOptions)) { |
||
427 | $optionTexts = array_map(function ($option) { |
||
428 | return $option['display_text']; |
||
429 | }, $extraFieldOptions); |
||
430 | $fieldValue = implode(', ', $optionTexts); |
||
431 | } |
||
432 | |||
433 | break; |
||
434 | |||
435 | case ExtraField::FIELD_TYPE_GEOLOCALIZATION_COORDINATES: |
||
436 | case ExtraField::FIELD_TYPE_GEOLOCALIZATION: |
||
437 | $geoData = explode('::', $fieldValue); |
||
438 | $locationName = $geoData[0]; |
||
439 | $coordinates = $geoData[1] ?? ''; |
||
440 | $fieldValue = $locationName; |
||
441 | |||
442 | break; |
||
443 | } |
||
444 | |||
445 | $extraFieldsFormatted[] = [ |
||
446 | 'variable' => $fieldVariable, |
||
447 | 'label' => $extraField['display_text'], |
||
448 | 'value' => $fieldValue, |
||
449 | ]; |
||
450 | } |
||
451 | |||
452 | return $extraFieldsFormatted; |
||
453 | } |
||
454 | |||
455 | #[Route('/invitations/{userId}', name: 'chamilo_core_social_invitations')] |
||
456 | public function getInvitations( |
||
457 | int $userId, |
||
458 | MessageRepository $messageRepository, |
||
459 | UsergroupRepository $usergroupRepository, |
||
460 | UserRepository $userRepository |
||
461 | ): JsonResponse { |
||
462 | $user = $this->getUser(); |
||
463 | if ($userId !== $user->getId()) { |
||
464 | return $this->json(['error' => 'Unauthorized'], Response::HTTP_UNAUTHORIZED); |
||
465 | } |
||
466 | |||
467 | $receivedMessages = $messageRepository->findReceivedInvitationsByUser($user); |
||
468 | $receivedInvitations = []; |
||
469 | foreach ($receivedMessages as $message) { |
||
470 | $sender = $message->getSender(); |
||
471 | $receivedInvitations[] = [ |
||
472 | 'id' => $message->getId(), |
||
473 | 'itemId' => $sender->getId(), |
||
474 | 'itemName' => $sender->getFirstName().' '.$sender->getLastName(), |
||
475 | 'itemPicture' => $userRepository->getUserPicture($sender->getId()), |
||
476 | 'content' => $message->getContent(), |
||
477 | 'date' => $message->getSendDate()->format('Y-m-d H:i:s'), |
||
478 | 'canAccept' => true, |
||
479 | 'canDeny' => true, |
||
480 | ]; |
||
481 | } |
||
482 | |||
483 | $sentMessages = $messageRepository->findSentInvitationsByUser($user); |
||
484 | $sentInvitations = []; |
||
485 | foreach ($sentMessages as $message) { |
||
486 | foreach ($message->getReceivers() as $receiver) { |
||
487 | $receiverUser = $receiver->getReceiver(); |
||
488 | $sentInvitations[] = [ |
||
489 | 'id' => $message->getId(), |
||
490 | 'itemId' => $receiverUser->getId(), |
||
491 | 'itemName' => $receiverUser->getFirstName().' '.$receiverUser->getLastName(), |
||
492 | 'itemPicture' => $userRepository->getUserPicture($receiverUser->getId()), |
||
493 | 'content' => $message->getContent(), |
||
494 | 'date' => $message->getSendDate()->format('Y-m-d H:i:s'), |
||
495 | 'canAccept' => false, |
||
496 | 'canDeny' => false, |
||
497 | ]; |
||
498 | } |
||
499 | } |
||
500 | |||
501 | $pendingGroupInvitations = []; |
||
502 | $pendingGroups = $usergroupRepository->getGroupsByUser($userId, Usergroup::GROUP_USER_PERMISSION_PENDING_INVITATION); |
||
503 | foreach ($pendingGroups as $group) { |
||
504 | $pendingGroupInvitations[] = [ |
||
505 | 'id' => $group->getId(), |
||
506 | 'itemId' => $group->getId(), |
||
507 | 'itemName' => $group->getTitle(), |
||
508 | 'itemPicture' => $usergroupRepository->getUsergroupPicture($group->getId()), |
||
509 | 'content' => $group->getDescription(), |
||
510 | 'date' => $group->getCreatedAt()->format('Y-m-d H:i:s'), |
||
511 | 'canAccept' => true, |
||
512 | 'canDeny' => true, |
||
513 | ]; |
||
514 | } |
||
515 | |||
516 | return $this->json([ |
||
517 | 'receivedInvitations' => $receivedInvitations, |
||
518 | 'sentInvitations' => $sentInvitations, |
||
519 | 'pendingGroupInvitations' => $pendingGroupInvitations, |
||
520 | ]); |
||
521 | } |
||
522 | |||
523 | #[Route('/search', name: 'chamilo_core_social_search')] |
||
524 | public function search( |
||
525 | Request $request, |
||
526 | UserRepository $userRepository, |
||
527 | UsergroupRepository $usergroupRepository, |
||
528 | TrackEOnlineRepository $trackOnlineRepository |
||
529 | ): JsonResponse { |
||
530 | $query = $request->query->get('query', ''); |
||
531 | $type = $request->query->get('type', 'user'); |
||
532 | $from = $request->query->getInt('from', 0); |
||
533 | $numberOfItems = $request->query->getInt('number_of_items', 1000); |
||
534 | |||
535 | $formattedResults = []; |
||
536 | if ('user' === $type) { |
||
537 | /** @var User $user */ |
||
538 | $user = $this->getUser(); |
||
539 | $results = $userRepository->searchUsersByTags($query, $user->getId(), 0, $from, $numberOfItems); |
||
540 | foreach ($results as $item) { |
||
541 | $isUserOnline = $trackOnlineRepository->isUserOnline($item['id']); |
||
542 | $relation = $userRepository->getUserRelationWithType($user->getId(), $item['id']); |
||
543 | $formattedResults[] = [ |
||
544 | 'id' => $item['id'], |
||
545 | 'name' => $item['firstname'].' '.$item['lastname'], |
||
546 | 'avatar' => $userRepository->getUserPicture($item['id']), |
||
547 | 'role' => 5 === $item['status'] ? 'student' : 'teacher', |
||
548 | 'status' => $isUserOnline ? 'online' : 'offline', |
||
549 | 'url' => '/social?id='.$item['id'], |
||
550 | 'relationType' => $relation['relationType'] ?? null, |
||
551 | ]; |
||
552 | } |
||
553 | } elseif ('group' === $type) { |
||
554 | // Perform group search |
||
555 | $results = $usergroupRepository->searchGroupsByTags($query, $from, $numberOfItems); |
||
556 | foreach ($results as $item) { |
||
557 | $formattedResults[] = [ |
||
558 | 'id' => $item['id'], |
||
559 | 'name' => $item['title'], |
||
560 | 'description' => $item['description'] ?? '', |
||
561 | 'image' => $usergroupRepository->getUsergroupPicture($item['id']), |
||
562 | 'url' => '/resources/usergroups/show/'.$item['id'], |
||
563 | ]; |
||
564 | } |
||
565 | } |
||
566 | |||
567 | return $this->json(['results' => $formattedResults]); |
||
568 | } |
||
569 | |||
570 | #[Route('/group-details/{groupId}', name: 'chamilo_core_social_group_details')] |
||
571 | public function groupDetails( |
||
572 | int $groupId, |
||
573 | UsergroupRepository $usergroupRepository, |
||
574 | TrackEOnlineRepository $trackOnlineRepository |
||
575 | ): JsonResponse { |
||
576 | /** @var User $user */ |
||
577 | $user = $this->getUser(); |
||
578 | if (!$user) { |
||
579 | return $this->json(['error' => 'User not authenticated'], Response::HTTP_UNAUTHORIZED); |
||
580 | } |
||
581 | |||
582 | /** @var Usergroup $group */ |
||
583 | $group = $usergroupRepository->find($groupId); |
||
584 | if (!$group) { |
||
585 | return $this->json(['error' => 'Group not found'], Response::HTTP_NOT_FOUND); |
||
586 | } |
||
587 | |||
588 | $isMember = $usergroupRepository->isGroupMember($groupId, $user); |
||
589 | $role = $usergroupRepository->getUserGroupRole($groupId, $user->getId()); |
||
590 | $isUserOnline = $trackOnlineRepository->isUserOnline($user->getId()); |
||
591 | $isModerator = $usergroupRepository->isGroupModerator($groupId, $user->getId()); |
||
592 | |||
593 | $groupDetails = [ |
||
594 | 'id' => $group->getId(), |
||
595 | 'title' => $group->getTitle(), |
||
596 | 'description' => $group->getDescription(), |
||
597 | 'image' => $usergroupRepository->getUsergroupPicture($group->getId()), |
||
598 | 'isMember' => $isMember, |
||
599 | 'isModerator' => $isModerator, |
||
600 | 'role' => $role, |
||
601 | 'isUserOnline' => $isUserOnline, |
||
602 | 'visibility' => (int) $group->getVisibility(), |
||
603 | ]; |
||
604 | |||
605 | return $this->json($groupDetails); |
||
606 | } |
||
607 | |||
608 | #[Route('/group-action', name: 'chamilo_core_social_group_action')] |
||
609 | public function groupAction(Request $request, UsergroupRepository $usergroupRepository, EntityManagerInterface $em): JsonResponse |
||
610 | { |
||
611 | $data = json_decode($request->getContent(), true); |
||
612 | |||
613 | $userId = $data['userId'] ?? null; |
||
614 | $groupId = $data['groupId'] ?? null; |
||
615 | $action = $data['action'] ?? null; |
||
616 | |||
617 | if (!$userId || !$groupId || !$action) { |
||
618 | return $this->json(['error' => 'Missing parameters'], Response::HTTP_BAD_REQUEST); |
||
619 | } |
||
620 | |||
621 | try { |
||
622 | switch ($action) { |
||
623 | case 'join': |
||
624 | $usergroupRepository->addUserToGroup($userId, $groupId); |
||
625 | |||
626 | break; |
||
627 | |||
628 | case 'leave': |
||
629 | $usergroupRepository->removeUserFromGroup($userId, $groupId); |
||
630 | |||
631 | break; |
||
632 | |||
633 | default: |
||
634 | return $this->json(['error' => 'Invalid action'], Response::HTTP_BAD_REQUEST); |
||
635 | } |
||
636 | |||
637 | $em->flush(); |
||
638 | |||
639 | return $this->json(['success' => 'Action completed successfully']); |
||
640 | } catch (Exception $e) { |
||
641 | return $this->json(['error' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR); |
||
642 | } |
||
643 | } |
||
644 | |||
645 | #[Route('/user-action', name: 'chamilo_core_social_user_action')] |
||
711 | } |
||
712 | } |
||
713 | |||
714 | private function checkUserStatus(int $userId, UserRepository $userRepository): bool |
||
715 | { |
||
719 | } |
||
720 | } |
||
721 |