• Page:
  • 1

Assign community groups to user

OFFLINE

Assign community groups to user

3 months ago
Hi All,

I have written below function which Assign Community Groups to user. Thanks to vicky sir to adding this into core code of uKnowva too.
You can use this function like this -
$groups_to_assign = array(7,8,9); //7,8,9 are group ids which needs to assign
$user = uKnowvaFactory::getUser(); // assigning to current user, you can pass id of any of the user
$user->assignCommunityGroups($groups_to_assign);


Below is definition of the function -
function assignCommunityGroups($community_groups = array()){
		if(!is_array($community_groups)){
			$community_groups = explode(',',$community_groups);
		}
		$community_groups = array_unique(array_filter($community_groups));
		if(empty($community_groups)){
			return false;
		}
		JPluginHelper::importPlugin('users');
		JPluginHelper::importPlugin('community');
		$dispatcher = JDispatcher::getInstance();
		
		require_once JPATH_SITE.DS.'components'.DS.'com_community'.DS.'models'.DS.'groups.php';
		CFactory::load('libraries', 'groups');
		$groups_model = CFactory::getModel('groups');
		
		foreach ($community_groups as $group_id){
			if(empty($group_id))
				continue;
			if(!$groups_model->isMember($this->id, $group_id)){ 
				$obj = new stdClass();
				$obj->groupid = $group_id;
				$obj->memberid = $this->id;
				$obj->approved = 1;
				$obj->permissions = 0;
				JFactory::getDbo()->insertObject('#__community_groups_members',$obj);
				CGroups::joinApproved($group_id, $this->id);
				uKnowvaFactory::getUser($this->id)->updateGroupList(true); 
				$dispatcher->trigger('onGroupJoin',array($group_id,$this->id));
			}
		}
	}


All the uKnowva Developers use this function and save your time.

Thanks
Last Edit: 3 months ago by Rajat Pal.
ONLINE

Re: Assign community groups to user

3 months ago
write a function for removing too
  • Page:
  • 1

Upcoming Events

Who's Online

5 users online