Topic-icon Is it possible to add new facebook users to a group?

Active Subscriptions:

None
I need to add all of Facebook connect users to "Facebook" user group. How can I do that?
The topic has been locked.
Support Specialist
12 years 10 months ago #34754 by alzander
You can't automatically add them to a group. There are ways, from a quick investigation, to invite users to your group, but they still need to approve that request on Facebook.com.

Currently, JFBConnect doesn't have much support for groups. It's something we want to add in the future, but there are a lot of limits that Facebook places on groups due to privacy and sharing restrictions that groups have.

I hope that helps explain, even if it's not what you were looking to hear,
Alex
The topic has been locked.
Active Subscriptions:

None
12 years 10 months ago #34770 by julsen
Sorry, my question was not clear. When I said "Facebook" group, that is the name of my Joomla user group! :D

So, my problem is: when a user register in my Joomla system using JFBConnect, I need to add this user to one (or more) specific Joomla user groups. Is it possible?
The topic has been locked.
Active Subscriptions:

None
Hi @julsen

What you're trying to achieve is not possible without custom coding I'm afraid. However, If you were to create either a Joomla or Social Profile plugin, you could easily use the JUserHelper class' addUserToGroup() method to add the facebook created users to a specified group.

Here are a couple of methods from a custom user profile plugin that I wrote recently for a client. The plugin was written to account for 2 different types of user to be able to register from the frontend - 'Project Creators' and 'Project Investors' - where each had it's own group and login redirects for new & returning users:

1. This is the core onUserLogin($user, $options) event.
Any plugins with this method that are in the plugins/user directory (user plugin group) will be executed when the user logs in. As you can see, there is a call to the second method (runCrowdfundingLoginOverrides($userId)) on the line, $redirect = $this->runCrowdfundingLoginOverrides($userId);. It is in this method that all of the groups are checked/moved.
/**
 * LOGIN
 *
 * If you wanted to do any custom checks in the database for the user,
 * here is where you'd do it. For example, below you can see the basic calls to retrieve
 * the user id using the $user object passed in to the method then get the user profile data
 * 
 * @param      obj     $user       The user object
 * @param      obj     $options    The login options
 * @return     bool    true/false  The 'success' for login
 */
function onUserLogin($user, $options)
{
    //Get App & User
    $app =& JFactory::getApplication();
       
    //only run in site
    if($app->isSite()){
       //INIT some vars to use
       //Get the user ID with helper method and the $user object
       $userId = intval(JUserHelper::getUserId($user['username']));

       //Get Profile with helper method and userId
       $profile = JUserHelper::getProfile($userId);        

       // Check the users groups against the configured Investor/Creator groups to get login redirect
       $redirect = $this->runCrowdfundingLoginOverrides($userId);
       
       // Only redirect if the config is not empty (fallback to default behaviour)
       if($redirect != ""){
           // Get config to pass in the site's SEF setting in redirect
           $config = JFactory::getConfig();
           
           // Set the redirect 
           $app->setUserState(
               'users.login.form.return', 
               JRoute::_($redirect, false, $config->get('usesecure'))
           );
       }//END IF redirect isn't empty
    }// END IF IN SITE
   
   //RETURN
   return true;
}

2. Custom method to get & set user groups and redirects
You could easily adapt this and add it to the onUserAfterSave($data, $isNew, $result, $error) method in a custom user plugin.
/**
* Get the custom user/group data to use
* 
* Method to get the Custom Crowdfunding user groups & their custom redirect urls for each from config
* and compare with the users current groups to determine the redirect
* 
* As its name suggests, this method (function) You could conceivably add additional 'sub' routines in here to get additional info &
* 'Do' stuff with it. You could do this by writing your logic (code) or 'trigger'
* other public or private methods (in this file) - both of which are demonstrated in the
* function 'runCrowdfundingLoginOverrides($userId, $userGroups)'.
* 
* Please see bottom of this file for examples.
* 
* @param   integer     $userId         The user id whose groups to chec
* @return  bool        true/false      True on success/False on fail
*/
public function runCrowdfundingLoginOverrides($userId)
{   // INIT Vars 
    $return=""; // to return
   
    // Login Type - first OR any
    $loginType = "any";
    
    // Groups
    $userGroups = JUserHelper::getUserGroups($userId);
    
    // Get the configured custom user groups to check against;
    $check = array(
       "custom_groups" => array(
              $this->params->get('investor_group', 2),
              $this->params->get('creator_group', 3)
       ),
       "first" => array(
              $this->params->get('investor_group', 2) => $this->params->get('investorredirect1', ''), 
              $this->params->get('creator_group', 3) => $this->params->get('creatorredirect1', '')    
       ),
       "any" => array(
              $this->params->get('investor_group', 2) => $this->params->get('investorredirect2', ''), 
              $this->params->get('creator_group', 3) => $this->params->get('creatorredirect2', '')
       )
    );    
       
    // Get user type
    $profile = JUserHelper::getProfile($userId);
    $userType = ((int)$profile->profile['usertype'] > 0) ? (int)$profile->profile['usertype'] : 0;
    $otherGroup = ((int)$userType > 0 && (int)$userType == $check['custom_groups'][0]) ? $check['custom_groups'][1] : $check['custom_groups'][0];
    
    // =============== Check & Set the user's groups =====================//
    // Check usergroups is array & not empty 
    if(is_array($userGroups) && !empty($userGroups)){        
           // The user is in their correct group
           if($userType > 0){
               if(in_array($userType, $userGroups)){
                   //Check if they are alo in the opposing group
                   if(in_array($otherGroup, $userGroups)){
                       JUserHelper::removeUserFromGroup($userId, $otherGroup);
                       //unset($userGroups[$otherGroup]);
                   }
               }else{// User isn't in their correct group
                   // Add the user to their group
                   JUserHelper::addUserToGroup($userId, $userType);
                   //$userGroups[$userType] = $userType;
                   
                   //Remove from opposing
                   if(in_array($otherGroup, $userGroups)){
                       JUserHelper::removeUserFromGroup($userId, $otherGroup);
                       //unset($userGroups[$otherGroup]);
                   }
               }
           }else{
                // User Doesn't have a usertype set - ADD IT based on their group
               //If creator
               if(in_array($this->params->get('creator_group', 3), $userGroups))
                   // Add to creator type
                   $profile->profile['usertype'] = $this->params->get('creator_group', 3);
               
               //If investor
               if(in_array($this->params->get('investor_group', 2), $userGroups))
                   //Set to investor type
                   $profile->profile['usertype'] = $this->params->get('investor_group', 2);
               
               // RESET usertype var
               $userType = ((int)$profile->profile['usertype'] > 0) ? (int)$profile->profile['usertype'] : 0;
               if($userType > 0)  // if their field is set
                   // Set the Group based on the usertype field
                   JUserHelper::addUserToGroup($userId, $userType);
               
           }//END Else (usertype <= 0)
   }//END IF $userGroups is array and not empty
   
   else{ //The user is not in any groups - check if they have a usertype in profile info and add to that group
       if($userType > 0){
           // Add the user to their group
           JUserHelper::addUserToGroup($userId, $userType);
       }else{
           // Add the users to the default Registration Group
           jimport('joomla.application.component.helper');
           $config = JComponentHelper::getParams('com_users');
           // Default to Registered.
           $defaultUserGroup = $config->get('new_usertype', 2);
           JUserHelper::addUserToGroup($userId, $defaultUserGroup);
       }
   }
           
   // Get the groups again        
   $userGroups = JUserHelper::getUserGroups($userId);
   
   // Get fresh session/user object
   $session = JFactory::getSession();
   $session->set('user', new JUser($userId));
   $user = JFactory::getUser($userId);

   //Check if this is the first login or not and set $loginType
   if($user->guest || $user->lastvisitDate == "0000-00-00 00:00:00" || $user->lastvisitDate == NULL){
       $loginType = "first";
   }
   
   //Check the user groups again now we've added/removed
   if(is_array($userGroups) && !empty($userGroups)){
       //Loop through the users groups to check which group they are in
       foreach($userGroups as $k => $v){
           if(in_array($k, $check['custom_groups'])) //&& $g = $userType
               $return = $check[$loginType][$k];
       }
   }

   // Return $return
   return $return;
}
Anyway, I hope this helps you somewhat! If you need any further assistance, the docs are available here:
1. User Plugin Events: docs.joomla.org/Plugin/Events/User
2. User/Profile plugin: docs.joomla.org/Creating_a_profile_plugin

If you need any assistance with this, you ca also contact me via my facebook page: www.facebook.com/MindYourBizOnline

Best of luck,

Gez
The topic has been locked.
Support Specialist
12 years 10 months ago #34780 by alzander
Gez,
Thanks so much for your feedback on this and for helping respond to user requests! It's always appreciated.

Julio (and Gez),
We actually created a "Joomla Group" social profile plugin already, but didn't include it in the last v5.0.1 release due to a glitch in our build system. We'll be releasing it in the full 5.1 version coming out in August. If you'd like to download and use it now, you can do so from the following link:
www.sourcecoast.com/index.php?option=com_ars&view=release&id=6

I'm not sure if this will do exactly what you're looking for right now though. When enabled, you'll see a new tab in the "Profiles" area of JFBConnect. From there, you can select which group a user *can choose* to be a part of when they register. This only works when using the "Normal Registration" flow, where the user will see the registration fields page. The idea is for a user to be able to choose their group, like on a school site where there are teachers and students.

If you're looking to always add a user to a specific group on registration, especially if using the Automatic Registration flow, this wouldn't work for you. However, it should be pretty easy to update the plugin to let you select a group or groups that all Facebook registrants are automatically added too.

Let us know if that helps or any feedback you have and we'll gladly help further.

Thanks,
Alex
The topic has been locked.
Active Subscriptions:

None
@alzander no problem! I'll always try to share when time allows it!

Awesome news about the user groups plugin! I'm sure a lot of folks will want to use that! ;)

Cheers,

Gez
The topic has been locked.
Support Specialist
12 years 10 months ago #34791 by alzander
Gez,
Yeah, it's been a long time coming, though it really hasn't been requested as much as we expected. Still planning for more features along those lines and generally gearing up for v5.1 in August. It's going to be a big release!

Julio,
If you do need anything or have any feedback, just let me know.

Thanks again,
Alex
The topic has been locked.