[RFC] Improved user_rank function

Note: We are moving the topics of this forum and it will be deleted at some point

Publish your own request for comments/change or patches for the next version of phpBB. Discuss the contributions and proposals of others. Upcoming releases are 3.2/Rhea and 3.3.
Post Reply
User avatar
John P
Posts: 157
Joined: Sun Nov 04, 2012 7:39 am
Location: Netherlands
Contact:

[RFC] Improved user_rank function

Post by John P »

I propose the following in order to do something with function get_user_rank:

Code: Select all

/**
* Get user rank title and image
*
* @param array $user_data the current stored users data
* @param int $user_posts the users number of posts
* @param string &$rank_title the rank title will be stored here after execution
* @param string &$rank_img the rank image as full img tag is stored here after execution
* @param string &$rank_img_src the rank image source is stored here after execution
*
* Note: since we do not want to break backwards-compatibility, this function will only properly assign ranks to guests if you call it for them with user_posts == false
*/
function get_user_rank($user_data, $user_posts, &$rank_title, &$rank_img, &$rank_img_src)
{
    global $ranks, $config, $phpbb_root_path, $phpbb_path_helper, $phpbb_dispatcher;

    /**
    * Preparing a user's rank before displaying
    *
    * @event core.modify_user_rank
    * @var    array    user_data        Array with user's data
    * @var    int        user_posts        User_posts to change
    * @since 3.1.0-rc3
    */

    $vars = array('user_data', 'user_posts');
    extract($phpbb_dispatcher->trigger_event('core.modify_user_rank', compact($vars)));

    if (empty($ranks))
    {
        global $cache;
        $ranks = $cache->obtain_ranks();
    }

    if (!empty($user_data['user_rank']))
    {

        $rank_title = (isset($ranks['special'][$user_data['user_rank']]['rank_title'])) ? $ranks['special'][$user_data['user_rank']]['rank_title'] : '';

        $rank_img_src = (!empty($ranks['special'][$user_data['user_rank']]['rank_image'])) ? $phpbb_path_helper->update_web_root_path($phpbb_root_path . $config['ranks_path'] . '/' . $ranks['special'][$user_data['user_rank']]['rank_image']) : '';

        $rank_img = (!empty($ranks['special'][$user_data['user_rank']]['rank_image'])) ? '<img src="' . $rank_img_src . '" alt="' . $ranks['special'][$user_data['user_rank']]['rank_title'] . '" title="' . $ranks['special'][$user_data['user_rank']]['rank_title'] . '" />' : '';
    }
    else if ($user_posts !== false)
    {
        if (!empty($ranks['normal']))
        {
            foreach ($ranks['normal'] as $rank)
            {
                if ($user_posts >= $rank['rank_min'])
                {
                    $rank_title = $rank['rank_title'];
                    $rank_img_src = (!empty($rank['rank_image'])) ? $phpbb_path_helper->update_web_root_path($phpbb_root_path . $config['ranks_path'] . '/' . $rank['rank_image']) : '';
                    $rank_img = (!empty($rank['rank_image'])) ? '<img src="' . $rank_img_src . '" alt="' . $rank['rank_title'] . '" title="' . $rank['rank_title'] . '" />' : '';
                    break;
                }
            }
        }
    }
}
 
Where $user_rank is changed in $userdata in which $user_data['user_rank'] is.
In this way we can do stuff with ranks in one place.

Event 'core.modify_user_rank' can be used for manipulating.

Memberlist.php, viewtopic.php, mcp_warn.php, ucp_pm_viewmessage.php, user_loader.php and functions_display.php -> function phpbb_show_profile have to send the user_data instead of only the user_rank

User avatar
John P
Posts: 157
Joined: Sun Nov 04, 2012 7:39 am
Location: Netherlands
Contact:

Re: [RFC] Improved user_rank function

Post by John P »

Seems the change is approved

Post Reply