diff --git a/main/inc/lib/group_portal_manager.lib.php b/main/inc/lib/group_portal_manager.lib.php index a72376da5e..f8e8f7d683 100755 --- a/main/inc/lib/group_portal_manager.lib.php +++ b/main/inc/lib/group_portal_manager.lib.php @@ -631,20 +631,20 @@ class GroupPortalManager * */ public static function get_user_group_role($user_id, $group_id) { - $table_group_rel_user = Database :: get_main_table(TABLE_MAIN_USER_REL_GROUP); - $return_value = 0; - if (!empty($user_id) && !empty($group_id)) { - $sql = "SELECT relation_type FROM $table_group_rel_user - WHERE - group_id = ".intval($group_id)." AND - user_id = ".intval($user_id)." "; - $result = Database::query($sql); - if (Database::num_rows($result) > 0) { - $row = Database::fetch_array($result, 'ASSOC'); - $return_value = $row['relation_type']; - } + $em = Database::getManager(); + + $result = $em + ->getRepository('ChamiloCoreBundle:UsergroupRelUser') + ->findOneBy([ + 'usergroup' => intval($group_id), + 'user' => intval($user_id) + ]); + + if (!$result) { + return 0; } - return $return_value; + + return $result->getRelationType(); } /** @@ -657,35 +657,49 @@ class GroupPortalManager public static function add_user_to_group($user_id, $group_id, $relation_type = GROUP_USER_PERMISSION_READER) { $result = false; - $table_url_rel_group = Database :: get_main_table(TABLE_MAIN_USER_REL_GROUP); - if (!empty($user_id) && !empty($group_id)) { - $role = self::get_user_group_role($user_id, $group_id); - if ($role == 0) { - $sql = "INSERT INTO $table_url_rel_group - SET - user_id = ".intval($user_id).", - group_id = ".intval($group_id).", - relation_type = ".intval($relation_type); - $result = Database::query($sql); - Event::addEvent( - LOG_GROUP_PORTAL_USER_SUBSCRIBED, - LOG_GROUP_PORTAL_REL_USER_ARRAY, - array( - 'user_id' => $user_id, - 'group_id' => $group_id, - 'relation_type' => $relation_type, - ) - ); - - } elseif ($role == GROUP_USER_PERMISSION_PENDING_INVITATION) { - // If somebody already invited me I can be added - self::update_user_role( - $user_id, - $group_id, - GROUP_USER_PERMISSION_READER - ); - } + + if (empty($user_id) || empty($group_id)) { + return false; + } + + $em = Database::getManager(); + + $user = $em->find('ChamiloCoreBundle:User', $user_id); + $usergroup = $em->find('ChamiloCoreBundle:Usergroup', $group_id); + $role = self::get_user_group_role($user_id, $group_id); + + if ($role == 0) { + $usergroupRelUser = new \Chamilo\CoreBundle\Entity\UsergroupRelUser(); + $usergroupRelUser + ->setUser($user) + ->setUsergroup($usergroup) + ->setRelationType($relation_type); + + $em->persist($usergroupRelUser); + $em->flush(); + + Event::addEvent( + LOG_GROUP_PORTAL_USER_SUBSCRIBED, + LOG_GROUP_PORTAL_REL_USER_ARRAY, + array( + 'user_id' => $user_id, + 'group_id' => $group_id, + 'relation_type' => $relation_type, + ) + ); + + return true; + } else if ($role == GROUP_USER_PERMISSION_PENDING_INVITATION) { + // If somebody already invited me I can be added + self::update_user_role( + $user_id, + $group_id, + GROUP_USER_PERMISSION_READER + ); + + return true; } + return $result; } @@ -753,10 +767,21 @@ class GroupPortalManager * */ public static function delete_user_rel_group($user_id, $group_id) { - $table = Database :: get_main_table(TABLE_MAIN_USER_REL_GROUP); - $sql = "DELETE FROM $table - WHERE user_id = ".intval($user_id)." AND group_id=".intval($group_id); - $result = Database::query($sql); + $em = Database::getManager(); + + $result = $em + ->getRepository('ChamiloCoreBundle:UsergroupRelUser') + ->findOneBy([ + 'usergroup' => intval($group_id), + 'user' => intval($user_id) + ]); + + if (!$result) { + return false; + } + + $em->remove($result); + $em->flush(); Event::addEvent( LOG_GROUP_PORTAL_USER_UNSUBSCRIBED, @@ -764,7 +789,7 @@ class GroupPortalManager array('user_id' => $user_id, 'group_id' => $group_id) ); - return $result; + return true; } /** @@ -778,20 +803,29 @@ class GroupPortalManager **/ public static function update_user_role($user_id, $group_id, $relation_type = GROUP_USER_PERMISSION_READER) { - $table_group_rel_user = Database::get_main_table(TABLE_MAIN_USER_REL_GROUP); if (empty($user_id) || empty($group_id) || empty($relation_type)) { return false; } + + $em = Database::getManager(); $group_id = intval($group_id); $user_id = intval($user_id); - $sql = "UPDATE $table_group_rel_user - SET relation_type = ".intval($relation_type)." - WHERE - user_id = $user_id AND - group_id = $group_id - "; - Database::query($sql); + $usergroupUser = $em + ->getRepository('ChamiloCoreBundle:UsergroupRelUser') + ->findOneBy([ + 'user' => $user_id, + 'usergroup' => $group_id + ]); + + if (!$usergroupUser) { + return false; + } + + $usergroupUser->setRelationType($relation_type); + + $em->merge($usergroupUser); + $em->flush(); Event::addEvent( LOG_GROUP_PORTAL_USER_UPDATE_ROLE, diff --git a/main/social/invitations.php b/main/social/invitations.php index 92cf78359b..61f579d05d 100755 --- a/main/social/invitations.php +++ b/main/social/invitations.php @@ -19,9 +19,67 @@ $this_section = SECTION_SOCIAL; $interbreadcrumb[] = array ('url' =>'profile.php','name' => get_lang('SocialNetwork')); $interbreadcrumb[] = array ('url' =>'#','name' => get_lang('Invitations')); +if (is_array($_GET) && count($_GET) > 0) { + foreach ($_GET as $key => $value) { + switch ($key) { + case 'accept': + $useRole = GroupPortalManager::get_user_group_role(api_get_user_id(), $value); + + if (in_array( + $useRole, + array( + GROUP_USER_PERMISSION_PENDING_INVITATION_SENT_BY_USER, + GROUP_USER_PERMISSION_PENDING_INVITATION + ) + )) { + GroupPortalManager::update_user_role(api_get_user_id(), $value, GROUP_USER_PERMISSION_READER); + + Display::addFlash( + Display::return_message(get_lang('UserIsSubscribedToThisGroup'), 'success') + ); + + header('Location: ' . api_get_path(WEB_CODE_PATH) . 'social/invitations.php'); + exit; + } + + if (in_array( + $useRole, + array( + GROUP_USER_PERMISSION_READER, + GROUP_USER_PERMISSION_ADMIN, + GROUP_USER_PERMISSION_MODERATOR + ) + )) { + Display::addFlash( + Display::return_message(get_lang('UserIsAlreadySubscribedToThisGroup'), 'warning') + ); + + header('Location: ' . api_get_path(WEB_CODE_PATH) . 'social/invitations.php'); + exit; + } + + Display::addFlash( + Display::return_message(get_lang('UserIsNotSubscribedToThisGroup'), 'warning') + ); + + header('Location: ' . api_get_path(WEB_CODE_PATH) . 'social/invitations.php'); + exit; + break; + case 'deny': + GroupPortalManager::delete_user_rel_group(api_get_user_id(), $value); + + Display::addFlash( + Display::return_message(get_lang('GroupInvitationWasDeny')) + ); + + header('Location: ' . api_get_path(WEB_CODE_PATH) . 'social/invitations.php'); + exit; + } + } +} + $userGroup = new UserGroup(); -$show_message = null; $content = null; // Block Menu Social @@ -148,8 +206,20 @@ if (count($pending_invitations) > 0) { $waitingInvitation .= '