1
LiliVG
Get user avatar from Userid function?
  • 2006/11/6 2:28

  • LiliVG

  • Just popping in

  • Posts: 65

  • Since: 2005/12/25


Is this possible? In functions.php there is a function that will get create a linked username when given the userid. Would it be possible to modify this function to get the users avatar? That way, site owners could include the user's avatars for any module or block, system-wide. I have seen a lot of posts requesting hacks to include users avatars in various modules and blocks. This function would accomplish that wouldn't it? Just add the new function to the functions.php file, and call the function where ever you want the users avatars to show up?

Here's the function in functions.php:

function xoops_getLinkedUnameFromId($userid)
{
    
$userid intval($userid);
    if (
$userid 0) {
        
$member_handler =& xoops_gethandler('member');
        
$user =& $member_handler->getUser($userid);
        if (
is_object($user)) {
            
$linkeduser '<a href="'.XOOPS_URL.'/userinfo.php?uid='.$userid.'">'$user->getVar('uname').'</a>';
            return 
$linkeduser;
        }
    }
    return 
$GLOBALS['xoopsConfig']['anonymous'];
}


Would this work, and can it be done?

2
LiliVG
Re: Get user avatar from Userid function?
  • 2006/11/6 6:53

  • LiliVG

  • Just popping in

  • Posts: 65

  • Since: 2005/12/25


Ok, I have edited the original function to create this:

(as a note, I didn't replace the original function, it is still there, I copied it, and then edited the copy in order to create a new function)

function xoops_getLinkedUserAvatarFromId($userid)
{
    
$userid intval($userid);
    if (
$userid 0) {
        
$member_handler =& xoops_gethandler('member');
        
$user =& $member_handler->getUser($userid);
        if (
is_object($user)) {
            
$linkedavatar '<a href="'.XOOPS_URL.'/userinfo.php?uid='.$userid.'">'.'<img src="'.XOOPS_URL.'/uploads/'.$user->getVar('user_avatar').'">'.'</a>';
            return 
$linkedavatar;
        }
    }
    return 
$GLOBALS['xoopsConfig']['anonymous'];
}


That's where I'm at right now. I don't know where to go from there. Can anyone help me with this?

My goal is then to be able to put
<{linkedavatar}>
in whatever template you want the users avatars to appear in.

3
Nathan74
Re: Get user avatar from Userid function?
  • 2006/11/6 10:12

  • Nathan74

  • Just popping in

  • Posts: 2

  • Since: 2005/6/6 1


This is indeed a feature I'd like to use too...

4
Nathan74
Re: Get user avatar from Userid function?
  • 2006/11/13 21:58

  • Nathan74

  • Just popping in

  • Posts: 2

  • Since: 2005/6/6 1


... gona have to push this up again ...

Anybody an idea to solve this?!?

5
irmtfan
Re: Get user avatar from Userid function?
  • 2006/11/14 3:24

  • irmtfan

  • Module Developer

  • Posts: 3419

  • Since: 2003/12/7


1- open class/theme.php
2- in XOOPS 2.2.4 around line 448 and in XOOPS 2.0.15 around line 192 insert this line:
'xoops_userid' => $xoopsUser->getVar('uid'),
            
'xoops_avatar' => $xoopsUser->getVar('user_avatar'),  //insert this               
            
'xoops_uname' => $xoopsUser->getVar('uname')));


then in theme/template just use it like this:
< img src="<{$xoops_upload_url}>/<{$xoops_avatar}>" alt="" width="32" />

Demo: www.jadoogaran.org

6
LiliVG
Re: Get user avatar from Userid function?
  • 2006/11/14 3:59

  • LiliVG

  • Just popping in

  • Posts: 65

  • Since: 2005/12/25


What I'm looking for is a way to have each posters avatar show up in the CBB Recent Topics/Posts Block next to their username. I'd also like to know there would be a way to put users avatars next to their names on other blocks as well (in the same way the different users avatars can show up in the system blocks - top posters, newest members, etc,) but the forum block is my main concern right now.

So will that code show the avatars of the many different posters, or will it show the avatar of the account you are logged in as?

7
irmtfan
Re: Get user avatar from Userid function?
  • 2006/11/14 5:08

  • irmtfan

  • Module Developer

  • Posts: 3419

  • Since: 2003/12/7


Ah sorry
I think it is not so easy.
but in cbb i think you can do it like this:

<img src="<{$xoops_upload_url}>/<{$topic_post.poster.avatar}>" alt="" />

8
LiliVG
Re: Get user avatar from Userid function?
  • 2006/11/14 5:18

  • LiliVG

  • Just popping in

  • Posts: 65

  • Since: 2005/12/25


That was the very first thing I tried. It seems very logical, that's how it's called in the forum thread template, but the coresponding php code isn't in the blocks php file, so it doesn't work. And I don't know php, so I don't know how to find the corresponding code. :(

9
LiliVG
Re: Get user avatar from Userid function?
  • 2006/11/16 7:47

  • LiliVG

  • Just popping in

  • Posts: 65

  • Since: 2005/12/25


Ok, I got the user avatars to show up on the CBB Recent Topics, and Recent Topic Replies blocks (CBB 3.07). I could probably add it to the recent posts one too, I just don't use that block on my site, so I didn't bother. But here's the hacks for those two if anyone's interested:

In newbb/blocks/newbb_block.php:
function b_newbb_show($options) (this is the function for the block that shows recently replied topics) Add the lines in red (there are 3):
function b_newbb_show($options)
{
    global 
$xoopsConfig;
    global 
$access_forums;

    
$db =& Database::getInstance();
    
$myts =& MyTextSanitizer::getInstance();
    
$block = array();
    
$i 0;
    
$order "";
    
$extra_criteria "";
    if(!empty(
$options[2])) {
        
$time_criteria time() - newbb_getSinceTime($options[2]);
        
$extra_criteria " AND p.post_time>".$time_criteria;
    }
    
$time_criteria null;
    switch (
$options[0]) {
        case 
'time':
        default:
            
$order 'p.post_time';
            
$extra_criteria .= " AND p.approved=1";
            break;
    }
    
$newbbConfig getConfigForBlock();
                
    if(!isset(
$access_forums)){
        
$forum_handler =& xoops_getmodulehandler('forum''newbb');
        if(!
$access_obj =& $forum_handler->getForums(0'access', array('forum_id''cat_id''forum_type')) ){
            return 
null;
        }
        
$access_forums array_keys$access_obj ); // get all accessible forums
        
unset($access_obj );
    }
    if (!empty(
$options[6])) {
        
$allowedforums array_filter(array_slice($options6), "b_newbb_array_filter"); // get allowed forums
        
$allowed_forums array_intersect($allowedforums$access_forums);
    }else{
        
$allowed_forums $access_forums;
    }

    
$forum_criteria ' AND t.forum_id IN (' implode(','$allowed_forums) . ')';
    
$approve_criteria ' AND t.approved = 1';

    
$query 'SELECT'.
            
'    DISTINCT t.topic_id, t.topic_replies, t.forum_id, t.topic_title, t.topic_views, t.topic_subject,'.
            
'    f.forum_name, f.allow_subject_prefix,'.

[
color=FF0000]
            
'    u.user_avatar,'.
[/
color]

            
'    p.post_id, p.post_time, p.icon, p.uid, p.poster_name'.
            
'    FROM ' $db->prefix('bb_posts') . ' AS p '.
            
'    LEFT JOIN ' $db->prefix('bb_topics') . ' AS t ON t.topic_last_post_id=p.post_id'.
            
'    LEFT JOIN ' $db->prefix('bb_forums') . ' AS f ON f.forum_id=t.forum_id'.

[
color=FF0000]
                
'    LEFT JOIN ' $db->prefix('users') . ' AS u ON u.uid=p.uid'.
[/
color]

            
'    WHERE 1=1 ' .
                
$forum_criteria .
                
$approve_criteria .
                
$extra_criteria .
                
' ORDER BY ' $order ' DESC';


    
$result $db->query($query$options[1], 0);
    if (!
$result) {
        
newbb_message("newbb block query error: ".$query);
        return 
false;
    }
    
$block['disp_mode'] = $options[3]; // 0 - full view; 1 - compact view; 2 - lite view;
    
$rows = array();
    
$author = array();
    while (
$row $db->fetchArray($result)) {
        
$rows[] = $row;
        
$author[$row["uid"]] = 1;
    }
    if (
count($rows) < 1) return false;
    
$author_name newbb_getUnameFromIds(array_keys($author), $newbbConfig['show_realname'], true);

    foreach (
$rows as $arr) {
        
$topic_page_jump '';
        if (
$arr['allow_subject_prefix']) {
            
$subjectpres explode(','$newbbConfig['subject_prefix']);
            if (
count($subjectpres) > 1) {
                foreach(
$subjectpres as $subjectpre) {
                    
$subject_array[] = $subjectpre;
                }
                   
$subject_array[0] = null;
            }
            
$topic['topic_subject'] = $subject_array[$arr['topic_subject']];
        } else {
            
$topic['topic_subject'] = "";
        }
        
$topic['post_id'] = $arr['post_id'];
        
$topic['forum_id'] = $arr['forum_id'];
        
$topic['forum_name'] = $myts->htmlSpecialChars($arr['forum_name']);
        
$topic['id'] = $arr['topic_id'];

[
color=FF0000]
        
$topic['poster_avatar'] = $arr['user_avatar'];
[/
color]

        
$title $myts->htmlSpecialChars($arr['topic_title']);
        if(!empty(
$options[5])){
            
$title xoops_substr($title0$options[5]);
        }
        
$topic['title'] = $title;
        
$topic['replies'] = $arr['topic_replies'];
        
$topic['views'] = $arr['topic_views'];
        
$topic['time'] = newbb_formatTimestamp($arr['post_time']);
        if (!empty(
$author_name[$arr['uid']])) {
            
$topic_poster $author_name[$arr['uid']];
        } else {
            
$topic_poster $myts->htmlSpecialChars( ($arr['poster_name'])?$arr['poster_name']:$GLOBALS["xoopsConfig"]["anonymous"] );
        }

        
$topic['topic_poster'] = $topic_poster;
        
$topic['topic_page_jump'] = $topic_page_jump;
        
$block['topics'][] = $topic;
        unset(
$topic);

    }
    
$block['indexNav'] = intval($options[4]);


    return 
$block;
}




And for function b_newbb_topic_show($options) (shows the most recently created topics) within the same file (also 3 lines, very similar to the first 3):
function b_newbb_topic_show($options)
{
    global 
$xoopsConfig;
    global 
$access_forums;

    
$db = &Database::getInstance();
    
$myts = &MyTextSanitizer::getInstance();
    
$block = array();
    
$i 0;
    
$order "";
    
$extra_criteria "";
    
$time_criteria null;
    if(!empty(
$options[2])) {
        
$time_criteria time() - newbb_getSinceTime($options[2]);
        
$extra_criteria " AND t.topic_time>".$time_criteria;
    }
    switch (
$options[0]) {
        case 
'views':
            
$order 't.topic_views';
            break;
        case 
'replies':
            
$order 't.topic_replies';
            break;
        case 
'digest':
            
$order 't.digest_time';
            
$extra_criteria " AND t.topic_digest=1";
            if(
$time_criteria)
            
$extra_criteria .= " AND t.digest_time>".$time_criteria;
            break;
        case 
'sticky':
            
$order 't.topic_time';
            
$extra_criteria .= " AND t.topic_sticky=1";
            break;
        case 
'time':
        default:
            
$order 't.topic_time';
            break;
    }
    
$newbbConfig getConfigForBlock();

    if(!isset(
$access_forums)){
        
$forum_handler =& xoops_getmodulehandler('forum''newbb');
        if(!
$access_obj =& $forum_handler->getForums(0'access', array('forum_id''cat_id''forum_type')) ){
            return 
null;
        }
        
$access_forums array_keys$access_obj ); // get all accessible forums
        
unset($access_obj );
    }

    if (!empty(
$options[6])) {
        
$allowedforums array_filter(array_slice($options6), "b_newbb_array_filter"); // get allowed forums
        
$allowed_forums array_intersect($allowedforums$access_forums);
    }else{
        
$allowed_forums $access_forums;
    }

    
$forum_criteria ' AND t.forum_id IN (' implode(','$allowed_forums) . ')';
    
$approve_criteria ' AND t.approved = 1';

    
$query 'SELECT'.
            
'    t.topic_id, t.topic_replies, t.forum_id, t.topic_title, t.topic_views, t.topic_subject, t.topic_time, t.topic_poster, t.poster_name,'.

[
color=FF0000]
            
'    u.user_avatar,'.
[/
color]

            
'    f.forum_name, f.allow_subject_prefix'.
            
'    FROM ' $db->prefix('bb_topics') . ' AS t '.
            
'    LEFT JOIN ' $db->prefix('bb_forums') . ' AS f ON f.forum_id=t.forum_id'.

[
color=FF0000]
                
'    LEFT JOIN ' $db->prefix('users') . ' AS u ON u.uid=t.topic_poster'.
[/
color]

            
'    WHERE 1=1 ' .
                
$forum_criteria .
                
$approve_criteria .
                
$extra_criteria .
                
' ORDER BY ' $order ' DESC';

    
$result $db->query($query$options[1], 0);
    if (!
$result) {
        
newbb_message("newbb block query error: ".$query);
        return 
false;
    }
    
$block['disp_mode'] = $options[3]; // 0 - full view; 1 - compact view; 2 - lite view;
    
$rows = array();
    
$author = array();
    while (
$row $db->fetchArray($result)) {
        
$rows[] = $row;
        
$author[$row["topic_poster"]] = 1;
    }
    if (
count($rows) < 1) return false;
    
$author_name newbb_getUnameFromIds(array_keys($author), $newbbConfig['show_realname'], true);

    foreach (
$rows as $arr) {
        
$topic_page_jump '';
        if (
$arr['allow_subject_prefix']) {
            
$subjectpres explode(','$newbbConfig['subject_prefix']);
            if (
count($subjectpres) > 1) {
                foreach(
$subjectpres as $subjectpre) {
                    
$subject_array[] = $subjectpre;
                }
                   
$subject_array[0] = null;
            }
            
$topic['topic_subject'] = $subject_array[$arr['topic_subject']];
        } else {
            
$topic['topic_subject'] = "";
        }
        
$topic['forum_id'] = $arr['forum_id'];
        
$topic['forum_name'] = $myts->htmlSpecialChars($arr['forum_name']);
        
$topic['id'] = $arr['topic_id'];

[
color=FF0000]
        
$topic['poster_avatar'] = $arr['user_avatar'];
[/
color]

        
$title $myts->htmlSpecialChars($arr['topic_title']);
        if(!empty(
$options[5])){
            
$title xoops_substr($title0$options[5]);
        }
        
$topic['title'] = $title;
        
$topic['replies'] = $arr['topic_replies'];
        
$topic['views'] = $arr['topic_views'];
        
$topic['time'] = newbb_formatTimestamp($arr['topic_time']);
        if (!empty(
$author_name[$arr['topic_poster']])) {
            
$topic_poster $author_name[$arr['topic_poster']];
        } else {
            
$topic_poster $myts->htmlSpecialChars( ($arr['poster_name'])?$arr['poster_name']:$GLOBALS["xoopsConfig"]["anonymous"] );
        }
        
$topic['topic_poster'] = $topic_poster;
        
$topic['topic_page_jump'] = $topic_page_jump;
        
$block['topics'][] = $topic;
        unset(
$topic);
    }
    
$block['indexNav'] = intval($options[4]);

    return 
$block;
}



Then, you will be able to add this code to newbb/templates/blocks/newbb_block.html and newbb/templates/blocks/newbb_block_topic.html wherever you want the posters' avatars to appear in the template:

<img src="<{xoops_url}>/uploads/<{topic.poster_avatar}>" width="50px" alt="" />


And you're done!

Login

Who's Online

240 user(s) are online (146 user(s) are browsing Support Forums)


Members: 0


Guests: 240


more...

Donat-O-Meter

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

Latest GitHub Commits