1
irmtfan
a simple method to return the groups names of a user
  • 2012/9/4 11:09

  • irmtfan

  • Module Developer

  • Posts: 3419

  • Since: 2003/12/7


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.

2
zyspec
Re: a simple method to return the groups names of a user
  • 2012/9/4 14:06

  • zyspec

  • Module Developer

  • Posts: 1092

  • Since: 2004/9/21


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.

3
Shine
Re: a simple method to return the groups names of a user
  • 2012/9/4 14:41

  • Shine

  • Just can't stay away

  • Posts: 822

  • Since: 2002/7/22


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.

4
zyspec
Re: a simple method to return the groups names of a user
  • 2012/9/4 16:28

  • zyspec

  • Module Developer

  • Posts: 1092

  • Since: 2004/9/21


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.

5
Mazarin
Re: a simple method to return the groups names of a user
  • 2012/9/4 16:30

  • Mazarin

  • Just can't stay away

  • Posts: 533

  • Since: 2008/12/10


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'); 
}

6
irmtfan
Re: a simple method to return the groups names of a user
  • 2012/9/5 4:22

  • irmtfan

  • Module Developer

  • Posts: 3419

  • Since: 2003/12/7


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.

7
mjoel
Re: a simple method to return the groups names of a user
  • 2020/2/1 3:56

  • mjoel

  • Quite a regular

  • Posts: 331

  • Since: 2006/12/9


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

8
mjoel
Re: a simple method to return the groups names of a user
  • 2020/2/1 5:28

  • mjoel

  • Quite a regular

  • Posts: 331

  • Since: 2006/12/9


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

9
Mamba
Re: a simple method to return the groups names of a user
  • 2020/2/1 19:19

  • Mamba

  • Moderator

  • Posts: 11211

  • Since: 2004/4/23


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

Login

Username:
Password:

Lost Password? Register now!

Who's Online

59 user(s) are online (35 user(s) are browsing Support Forums)


Members: 0


Guests: 59


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Dec 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits