News:

Want to get involved in developing SMF? Why not lend a hand on our GitHub!

Main Menu

Getting user group ID from $context?

Started by Largo_npc, August 14, 2009, 05:27:54 AM

Previous topic - Next topic

Largo_npc

How do I get the user's group ID to use in my theme?

I can use things like $context['user']['avatar'] and $context['user']['name'] but $context['user']['group_id'] and $context['user']['id_group'] doesn't seem to work.

I could add the query in myself if I knew where $context was set.

Also, is there a list somewhere of what data I can get from these globals?

Cheers

EDIT: Using 2.2 RC1.2 btw.

Largo_npc

Found it. It's $context['member'] not ['user']

Arantor

Note that only covers the user's primary group, not any other groups they may be in.
Holder of controversial views, all of which my own.


Largo_npc

#3
That's true. That's exactly what I want in this case.

My problem is that it's suddenly stopped working though. It was fine and now it returning "", nothing.

EDIT: Found the problem. It only works if viewing the profile. So obviously it's getting the Group ID of who's profile you are viewing rather than your own. So I need another way to do it. Any ideas?

Arantor

The solution will be to call loadMemberData() with the id of the person you want. Offhand I can't remember which level of detail you need, whether 'minimal' or 'normal' is the answer, but loading via that function will always populate $user_profile[$user_id] with an array of useful information.
Holder of controversial views, all of which my own.


Largo_npc

I've tried using this: loadMemberData($context['user']['id']);
Followed by $user_profile['$user_id'] but it returns nothing. $context['user']['id'] is returning the correct ID so it must be the function not working for some reason, most likely I'm using it wrong.

Dragooon

Quote from: Largo_npc on August 14, 2009, 11:31:53 AM
I've tried using this: loadMemberData($context['user']['id']);
Followed by $user_profile['$user_id'] but it returns nothing. $context['user']['id'] is returning the correct ID so it must be the function not working for some reason, most likely I'm using it wrong.
Make sure $user_profile is globalled, also you should be good with minimal as it loads the primary group but not the other groups.

Largo_npc

Ok, this is what I have so far.

At the top of the function
global $context, $settings, $options, $scripturl, $txt, $modSettings, $user_profile;

where it needs to be above my switch statement:
$user_id = $context['user']['id'];
$set = 'minimal';
loadMemberData($user_id, $set);


Where the output goes (I'm using in image sorce, so it should put out 1.jpg if group id is 1)
<img src="'.$user_profile[$user_id]['group_id'].'.jpg" alt="" \>

The image I get though is just /.jpg instead of /1.jpg




Dragooon

Its id_group not group_id, also are all the code in same function?

Largo_npc

Yup. All the code is in function template_body_above().

Here's the code down to the relevant point.

function template_body_above()
{
global $context, $settings, $options, $scripturl, $txt, $modSettings, $user_profile;

echo '
<div id="body-top">
<div id="wrapper">
<div id="header">
<div id="headerleft">
<div id="headerright">
<div id="menubar">
',template_menu(),'
</div>
<div id="userarea" class="smalltext">';
if(!empty($context['user']['avatar']))
echo '
<div class="user-avatar">'.$context['user']['avatar']['image'].'</div>';
else
{
$user_id = $context['user']['id'];
$set = 'minimal';
loadMemberData($user_id, $set);
switch($user_profile[$user_id]['group_id'])
{
case 1 : $npc_ava = "default_avatars/elder.gif"; break;
case 2 : $npc_ava = "default_avatars/council.gif"; break;
case 9 : $npc_ava = "default_avatars/member.gif"; break;
case 11 : $npc_ava = "default_avatars/srmember.gif"; break;
case 10 : $npc_ava = "default_avatars/trialee.gif"; break;
default : $npc_ava = "default_avatars/newb.gif";
}
echo '
<div class="user-avatar"><img src="'.$user_profile[$user_id]['id_group'].'.jpg" alt="" \></div>';
}


Now I know the switch statement will fail (Go to default). The reason the $user_profile bit is in the image tags is for debugging. Once it works it will go into the switch statement and $npc_ava will replace it in the image tag.

Kays

loadMemberData(() needs to be an array of ids.

If this is to check the secondary group of a user, have you tried using $user_info['groups']['0']

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Largo_npc

It's for the user's main group.

I've tried using $user_id = array("id" => $context['user']['id']); but that didn't work either.

Dragooon

Quote from: Kays on August 14, 2009, 12:59:32 PM
loadMemberData(() needs to be an array of ids.
Doesn't matter.

As I said, it has to be id_group not group_id which you're currently using...

Largo_npc

As I said, Only in the switch statement, which is currently irrelevant. It's this line which puts the output out...
<div class="user-avatar"><img src="'.$user_profile[$user_id]['id_group'].'.jpg" alt="" \></div>';

Largo_npc

It's now working.
I took the $set out. Maybe I was passing that wrong or minimal doesn't supply main group.

$user_id = $context['user']['id'];
//$set = 'minimal';
//loadMemberData($user_id, $set);
loadMemberData($user_id);
switch($user_profile[$user_id]['id_group'])
{
case 1 : $npc_ava = "default_avatars/elder.gif"; break;
case 2 : $npc_ava = "default_avatars/council.gif"; break;
case 9 : $npc_ava = "default_avatars/member.gif"; break;
case 11 : $npc_ava = "default_avatars/srmember.gif"; break;
case 10 : $npc_ava = "default_avatars/trialee.gif"; break;
default : $npc_ava = "default_avatars/newb.gif";
}
echo '
<div class="user-avatar"><img src="'.$npc_ava.'" alt="" \></div>';
}

varmandra

i try something similar....but i am a complete noob ;)

I want an aditional picture for the secondery group in the box where the information about the poster is shown.

i find out where i have to put it, but i don´t know how to specify for a group (non primary).

i know the goup has the ID 20, so i think a simple "if"  should solve

if ($id_group == 20) echo '<img scr=..........><br />';

id_group: what is the correct value?  what else i have to do?

I want to put it under //Honk of the day anzeige

Code (Take out of the display.template.php from the Default theme) Select

// Show the post group if and only if they have no other group or the option is on, and they are in a post group.
if ((empty($settings['hide_post_group']) || $message['member']['group'] == '') && $message['member']['post_group'] != '')
echo '
', $message['member']['post_group'], '<br />';
echo '
', $message['member']['group_stars'], '<br />';

//Honk oof the day anzeige


// Is karma display enabled?  Total or +/-?
if ($modSettings['karmaMode'] == '1')
echo '
<br />
', $modSettings['karmaLabel'], ' ', $message['member']['karma']['good'] - $message['member']['karma']['bad'], '<br />';
elseif ($modSettings['karmaMode'] == '2')
echo '
<br />
', $modSettings['karmaLabel'], ' +', $message['member']['karma']['good'], '/-', $message['member']['karma']['bad'], '<br />';

// Is this user allowed to modify this member's karma?
if ($message['member']['karma']['allow'])
echo '
<a href="', $scripturl, '?action=modifykarma;sa=applaud;uid=', $message['member']['id'], ';topic=', $context['current_topic'], '.' . $context['start'], ';m=', $message['id'], ';sesc=', $context['session_id'], '">', $modSettings['karmaApplaudLabel'], '</a>



Arantor

Is it the user's primary group you're checking?
Holder of controversial views, all of which my own.


varmandra

Quote from: varmandra on August 21, 2009, 10:48:21 AM
i find out where i have to put it, but i don´t know how to specify for a group (non primary).

Arantor

And where exactly are you calling the above code from?

It isn't populated into $context by default.
Holder of controversial views, all of which my own.


varmandra

it is in the display.template.php from the default theme.

I think you mean this, but i am not sure...
Code (This is the beginning of the function) Select

function template_main()
{
global $context, $settings, $options, $txt, $scripturl, $modSettings;


the complete file is attached.

if this is not the thing you want to know, can you describe more what mean.

Advertisement: