Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.

[Badges Plugin] How do I display overall points and badges below the user avatar on the profile page?

+1 vote
1,897 views
asked Apr 14, 2015 in Plugins by gold.developer

How can I display the overall points and badges achieved below of the user avatar on the profile page?
Something like this:

2 Answers

+1 vote
answered Apr 15, 2015 by gold.developer
edited Apr 19, 2015 by gold.developer
 
Best answer

Thank you very much for your help @sama55 !
I've found another way of displaying the user points and badges on the profile page, by adding this code on the user-profile.php file.


Update

For those trying to do the same as I did, here's how I did it step by step.
You'll need to go to: qa-include > pages > user-profile.php , and around line 368 you'll find a bit of code that looks something like this:

'fields' => array(
    'avatar' => array(
        'type' => 'image',
         'style' => 'tall',
        'label' => '',
        'html' => qa_get_user_avatar_html($useraccount['flags'], $useraccount['email'], $useraccount['handle'],
            $useraccount['avatarblobid'], $useraccount['avatarwidth'], $useraccount['avatarheight'], qa_opt('avatar_profile_size')),
        'id' => 'avatar',
    ),

notice that there's a line that begins with  'label'  which I made it bold to highlight it, and there's another one with  'html' , you'll need to reverse the order of these 2 lines and add an extra code like I'll show on the example below. (You can just copy and replace the two lines with the following code):

    'label' => qa_get_user_avatar_html($useraccount['flags'], $useraccount['email'], $useraccount['handle'],
        $useraccount['avatarblobid'], $useraccount['avatarwidth'], $useraccount['avatarheight'], qa_opt('avatar_profile_size')),
    'html' => '<div class="repField"><div class="hmrepField"><span class="rePnumber">'.qa_html(number_format(@$userpoints['points'])). '</span><span>Reputation</span></div>' .qa_badge_plugin_user_widget($handle).'</div>',

With this being done we are including our new div  repField  (and the rest of the code of course) inside of the  table  where the user avatar is on the profile page. The HTML output will look like this:

<table class="qa-form-wide-table">
    <tbody id="avatar">
        <tr><img src="THE USER AVATAR"><tr>
        <tr><div class="repField">OUR NEW REPUTATION FIELD</div><tr>
    </tbody>
</table>

If you don't want the user points to appear, just remove this from the code:

<span class="rePnumber">'.qa_html(number_format(@$userpoints['points'])). '</span>

+1 vote
answered Apr 14, 2015 by sama55

Example of function that gets the number of each badges of specific user. This is a part of Banner Pro.

Input:

  • $handle: Handle of specific user

Output:

  • $bcount[2]: Gold badge count
  • $bcount[1]: Silver badge count
  • $bcount[0]: Bronze badge count

function qa_bnp_cb_badge_count($handle) {
    $bcount = array(0,0,0);
    $result = qa_db_read_all_values(qa_db_query_sub('SHOW TABLES LIKE \'^userbadges\''));
    if(count($result)) {
        $userids = qa_handles_to_userids(array($handle));
        $userid = $userids[$handle];
        $result = qa_db_read_all_values(qa_db_query_sub('SELECT badge_slug FROM ^userbadges WHERE user_id=#', $userid));
        if(count($result) == 0)
             return $bcount;
        if(function_exists('qa_get_badge_list')) {
            $badges = qa_get_badge_list();
            foreach($result as $slug)
                $bcount[$badges[$slug]['type']] = isset($bcount[$badges[$slug]['type']])?$bcount[$badges[$slug]['type']]+1:1;
        }
    }
    return $bcount;
}

commented Apr 14, 2015 by gold.developer
edited Apr 14, 2015 by gold.developer
Thanks @sama55 . I pasted this on qa-theme-php in a random place, but it's not showing anything on the users profile :S
commented Apr 14, 2015 by sama55
No. You should hack badge plugin source (qa-badge-layer.php).
For example:

    function main() {
        if (qa_opt('badge_active') && $this->template == 'user' && isset($this->content['raw']['userid'])) {
                $userid = $this->content['raw']['userid'];
                if(qa_get('tab')=='badges') {
                    $handles = qa_userids_to_handles(array($userid));
                    $handle = $handles[$userid];
                    $this->content['title'] = 'Badge of '.$handle;
                    foreach($this->content as $i => $v)
                        if(strpos($i,'form') === 0 || strpos($i,'message') === 0)
                            unset($this->content[$i]);
                    $this->content['form-badges-list'] = qa_badge_plugin_user_form($userid, false);
                } else {
                    if(qa_opt('badge_admin_user_field_no_tab'))
                        $this->content['form-badges-list'] = qa_badge_plugin_user_form($userid);
                }
                // Add [Start]
                if(isset($this->content['form_activity']['fields'])) {
                    $activities = &$this->content['form_activity']['fields'];
                    $bcount = $this->qa_get_badge_count($userid);
                    $newactivities = array();
                    foreach($activities as $key => $activity) {
                        $newactivities[$key] = $activity;
                        if($key == 'points') {
                            $newactivities['badges'] = array(
                                'type' => 'static',
                                'label' => 'Badges:',
                                'value' => 'Gold:'.$bcount[2].', Silver:'.$bcount[1].', Bronze:'.$bcount[0],
                                'id' => 'badges',
                            );
                        }
                    }
                    $activities = $newactivities;
                }
                // Add [End]
        }
        qa_html_theme_base::main();
    }
    // Add [Start]
    function qa_get_badge_count($userid) {
        $bcount = array(0,0,0);
        $result = qa_db_read_all_values(qa_db_query_sub('SELECT badge_slug FROM ^userbadges WHERE user_id=#', $userid));
        if(count($result) == 0)
             return $bcount;
        $badges = qa_get_badge_list();
        foreach($result as $slug)
            $bcount[$badges[$slug]['type']] = isset($bcount[$badges[$slug]['type']])?$bcount[$badges[$slug]['type']]+1:1;
        return $bcount;
    }
    // Add [End]
commented Apr 15, 2015 by gold.developer
still nothing :S I've added this chunk of code right below of the function main_parts($content){ ... } but doesn't seems to make any changes :S
...