xoops forums

irmtfan

Module Developer
Posted on: 2012/9/4 11:09
irmtfan
irmtfan (Show more)
Module Developer
Posts: 3419
Since: 2003/12/7
#1

a simple method to return the groups names of a user

as you may know we can get the user groups IDs array with this function:
$XoopsUser -> getGroups()
but sometime we need to display the user group names not their IDs.

I sent this feature request to the core:
https://sourceforge.net/tracker/index. ... roup_id=41586&atid=430843

as you can see zyspec suggest this simpler method:
$member_handler =& xoops_gethandler('member');
$group_names $member_handler->getGroupList(new Criteria('uid',
$GLOBALS['xoopsUser']->uid()));

I didnt aware of that function and zyspec is really the master of Xoops API.
but the above is not work for me.
Please help we need your opinion.

zyspec

Module Developer
Posted on: 2012/9/4 14:06
zyspec
zyspec (Show more)
Module Developer
Posts: 1077
Since: 2004/9/21
#2

Re: a simple method to return the groups names of a user

I apologize irmtfan... I'm not as much of a 'master of Xoops API' as indicated
In my haste I posted the above code too quickly. Here is updated code, which is a little more involved, so maybe this does warrant a new method in the class, as you originally suggested...

$member_handler =& xoops_gethandler('member');
$group_ids $member_handler->getGroupsByUser($GLOBALS['xoopsUser']->uid());
$group_criteria = new Criteria('groupid''(' implode(','$group_ids) . ')''IN');
$group_names $member_handler->getGroupList($group_criteria);


I tested this code, it works in 2.5.5. Sorry for the confusion.

Shine

Just can't stay away
Posted on: 2012/9/4 14:41
Shine
Shine (Show more)
Just can't stay away
Posts: 822
Since: 2002/7/22
#3

Re: a simple method to return the groups names of a user

Uid depend on what you prefer. Within my website and registered users the uid is very important and not the usergroup. After a year they can extend their registration. For this they have to give their uid, because this is a unique number for that registered person. For me easy to see as soon they have made their small payment. Because they have to menton their uid-number.

Why not use both (possibility) in future instead one or the other.

zyspec

Module Developer
Posted on: 2012/9/4 16:28
zyspec
zyspec (Show more)
Module Developer
Posts: 1077
Since: 2004/9/21
#4

Re: a simple method to return the groups names of a user

Shine,

I don't think we're proposing one or the other. I think what irmtfan wanted was the added capability of easily displaying the user's group names.

Mazarin

Just can't stay away
Posted on: 2012/9/4 16:30
Mazarin
Mazarin (Show more)
Just can't stay away
Posts: 533
Since: 2008/12/10
#5

Re: a simple method to return the groups names of a user

Seems this is already solved, but still, I use this code (which some nice person on the forums probably helped me with) to get the names of the user groups in an array:
global $xoopsUser;

//get current user id 
$uid is_object($xoopsUser) ? $xoopsUser->getVar('uid') : t

//get groups from this user, if any 
$member_handler =& xoops_gethandler('member'); 
$groups =& $member_handler->getGroupsByUser($uidtrue); 

//the above method will give as an array of objects, lets get the name of each group  
$usergroups = array(); 
foreach (
$groups as $group) { 
    
$usergroups[] = $group->getVar('name'); 
}

irmtfan

Module Developer
Posted on: 2012/9/5 4:22
irmtfan
irmtfan (Show more)
Module Developer
Posts: 3419
Since: 2003/12/7
#6

Re: a simple method to return the groups names of a user

zyspec:
IMO you are more familira with Xoops API than many other developers. so You are the master of Xoops API.
another evidence here: Xoops 2.5.5 depricated functions in 2.6.0
Many developer didnt know even what is getGroups()

Thank you it seems the original solution that i found in forums (just like Mazarin) is good enough.
IMO the best solution is extending the current functionality of getGroups :
$user -> getGroups('id'); // or getGroups();
$user -> getGroups('name');
anyway it is a very simple task for core team or any developer.
I sent many more important features in tracker that takes 1000 hours from developers for implementation.

Shine:
xoops permission system is group base and gid is more important in this area than uid.
for individuals a ticket system module like xhelp is good.
I hope somebody could debug Xhelp 0.8

Mazarin:
Thank you. I used this codes in newbb. It seems now only newbb show the group names of a user.
IMO group names should be used/shown more general in some other modules like profile, comments, news, gallery, download modules and ...
It is a good feature.

mjoel

Quite a regular
Posted on: 2/1 3:56
mjoel
mjoel (Show more)
Quite a regular
Posts: 307
Since: 2006/12/9
#7

Re: a simple method to return the groups names of a user

i tried it like this

global $xoopsUser;
//get current user id 
//$uid = is_object($xoopsUser) ? $xoopsUser->getVar('uid') : t; 
$uid=$row['uid'];
//get groups from this user, if any 
$member_handler =& xoops_gethandler('member'); 
$groups =& $member_handler->getGroupsByUser($uidtrue); 

//the above method will give as an array of objects, lets get the name of each group  
$usergroups = array(); 
foreach (
$groups as $group) { 
    
$usergroups[] = $group->getVar('name'); 
}
$usergroupsArray implode(', '$usergroups);
echo 
$usergroupsArray;


i have more than 400 users ..only the group for 31 users is displayed before i get this error

Fatal errorCall to a member function getVar() on a non-object in custom.php on line 354


line 354 in my file custom.php refers to
$usergroups[] = $group->getVar('name');


is the code above working for XOOPS 2.5.10..help

XOOPS Version XOOPS 2.5.10
PHP Version 5.4.27
mySQL Version 5.5.36

mjoel

Quite a regular
Posted on: 2/1 5:28
mjoel
mjoel (Show more)
Quite a regular
Posts: 307
Since: 2006/12/9
#8

Re: a simple method to return the groups names of a user

Quote:

mjoel wrote:
i tried it like this

global $xoopsUser;
//get current user id 
//$uid = is_object($xoopsUser) ? $xoopsUser->getVar('uid') : t; 
$uid=$row['uid'];
//get groups from this user, if any 
$member_handler =& xoops_gethandler('member'); 
$groups =& $member_handler->getGroupsByUser($uidtrue); 

//the above method will give as an array of objects, lets get the name of each group  
$usergroups = array(); 
foreach (
$groups as $group) { 
    
$usergroups[] = $group->getVar('name'); 
}
$usergroupsArray implode(', '$usergroups);
echo 
$usergroupsArray;


i have more than 400 users ..only the group for 31 users is displayed before i get this error

Fatal errorCall to a member function getVar() on a non-object in custom.php on line 354


line 354 in my file custom.php refers to
$usergroups[] = $group->getVar('name');


is the code above working for XOOPS 2.5.10..help

XOOPS Version XOOPS 2.5.10
PHP Version 5.4.27
mySQL Version 5.5.36


i figured this out

in my database groups_users_link table there is user with groupid=0 .. im not sure how that happen

but i deleted all the user with groupid=0 ..and now all is well..the code above is working fine

Mamba

Moderator
Posted on: 2/1 19:19
Mamba
Mamba (Show more)
Moderator
Posts: 10944
Since: 2004/4/23
#9

Re: a simple method to return the groups names of a user

Thanks for reporting back and sharing your solution!
Support XOOPS => DONATE
Use 2.5.10 | Docs | Modules | Bugs