Search code examples
variablesjoomlajoomla3.0usertype

Joomla 3.0 not echoing usertype


I made the following code to get some user variables in a flash app:

<?php
$user =& JFactory::getUser();
echo $user->get('username') ;
echo $user->get('id') ;
echo $user->get('name') ;
echo $user->get('usertype') ;
?>

Everything but usertype works, for some reason. Usertype is vital to be able to monetize my app. I followed this as a reference, so it seems alright:

http://docs.joomla.org/Accessing_the_current_user_object

Whats wrong here?


Solution

  • Right, I've had a look around and I can't actually find a decent solution that simply provides you with the name of the group that the user belongs to. Everything else gives you an array or the ID, so I have written a simple function that will get you exactly what you want:

    function getUserGroup($userId){     
         $db = JFactory::getDbo();
    
         $query  = $db->getQuery(true);
         $query->select('title')
         ->from('#__user_usergroup_map AS map')
         ->where('map.user_id = '.(int) $userId)
         ->leftJoin('#__usergroups AS a ON a.id = map.group_id');
    
         $db->setQuery($query);
         $result = $db->loadResult();
    
         return $result;
    }
    
    echo getUserGroup($user->id);
    

    Hope this helps