• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

getUsers Order

Gossamer

Active member
#1
Hello! I'm attempting to output a list of users on a page and order them by user title. However, my list only ever seems to default to sorting by username. Is there a way to do this?

Here is my code:

PHP:
        // fetch characters for canon list
        $userModel = $controller->getModelFromCache('XenForo_Model_User');
        $response->params['users'] = $userModel->getUsers(array(
            'secondary_group_ids' => 5, 
            'user_state' => 'valid'
            ), array(
                'order' => 'user_title',
                'join' => XenForo_Model_User::FETCH_USER_PROFILE
            ));
 
#2
Hello! I'm attempting to output a list of users on a page and order them by user title. However, my list only ever seems to default to sorting by username. Is there a way to do this?

Here is my code:

PHP:
        // fetch characters for canon list
        $userModel = $controller->getModelFromCache('XenForo_Model_User');
        $response->params['users'] = $userModel->getUsers(array(
            'secondary_group_ids' => 5,
            'user_state' => 'valid'
            ), array(
                'order' => 'user_title',
                'join' => XenForo_Model_User::FETCH_USER_PROFILE
            ));
Where did you get a user title? i am looking at a default xenforo database and i don't have a user_title field. Did you create it by yourself?
 
#3
Hello! I'm attempting to output a list of users on a page and order them by user title. However, my list only ever seems to default to sorting by username. Is there a way to do this?

Here is my code:

PHP:
        // fetch characters for canon list
        $userModel = $controller->getModelFromCache('XenForo_Model_User');
        $response->params['users'] = $userModel->getUsers(array(
            'secondary_group_ids' => 5,
            'user_state' => 'valid'
            ), array(
                'order' => 'user_title',
                'join' => XenForo_Model_User::FETCH_USER_PROFILE
            ));
Also, Looking at the User Model getUsers model, the order is generated using the prepareUserOrderOptions method that allows one of the following options to sort on:

PHP:
$choices = array(
            'username' => 'user.username',
            'register_date' => 'user.register_date',
            'message_count' => 'user.message_count',
            'trophy_points' => 'user.trophy_points',
            'like_count' => 'user.like_count',
            'last_activity' => 'user.last_activity'
        );
So i don't think you can just specify any column to sort on if you want to use the getUsers method.
 

Liam W

Well-known member
#4
You would probably have to get the users normally, sorted in the default way, then sort them yourself using code.

Or, override the model function and change it in there...

Liam
 

Gossamer

Active member
#5
Aah, yes, I thought that might have been the problem. But after fumbling around a while, I managed to extend the User Model to put in my additional order option.

I'm only vaguely familiar with php and learning as I go, but any suggestions on how to add in a secondary sorting option? Ideally, I'd like it to sort first by user_title, and then by username.

This is my extended model:
PHP:
class Goss_CanonList_Model_User extends XFCP_Goss_CanonList_Model_User
{
    /**
     * Construct 'ORDER BY' clause
     *
     * @param array $fetchOptions (uses 'order' key)
     * @param string $defaultOrderSql Default order SQL
     *
     * @return string
     */
    public function prepareUserOrderOptions(array &$fetchOptions, $defaultOrderSql = '')
    {
        $choices = array(
            'username' => 'user.username',
            'register_date' => 'user.register_date',
            'message_count' => 'user.message_count',
            'trophy_points' => 'user.trophy_points',
            'like_count' => 'user.like_count',
            'last_activity' => 'user.last_activity',
            'custom_title' => 'user.custom_title'
        );
        return $this->getOrderByClause($choices, $fetchOptions, $defaultOrderSql);
    }
}
And this is my CanonList.php file:
PHP:
<?php

class Goss_CanonList_CanonList
{
    public static function respond(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract $response)
    {
        // fetch characters for canon list
        $userModel = $controller->getModelFromCache('XenForo_Model_User');
        $response->params['users'] = $userModel->getUsers(array(
            'secondary_group_ids' => 5,  /* Pull characters from this group # */
            'user_state' => 'valid'
            ), array(
                'order' => 'custom_title',
                'join' => XenForo_Model_User::FETCH_USER_PROFILE
            ));
      
        foreach ($response->params['users'] as &$user) {
            $user = $userModel->prepareUser($user);
        }
      
        $response->templateName = 'goss_canonlist';
    }
}
 
#6
Aah, yes, I thought that might have been the problem. But after fumbling around a while, I managed to extend the User Model to put in my additional order option.

I'm only vaguely familiar with php and learning as I go, but any suggestions on how to add in a secondary sorting option? Ideally, I'd like it to sort first by user_title, and then by username.

This is my extended model:
PHP:
class Goss_CanonList_Model_User extends XFCP_Goss_CanonList_Model_User
{
    /**
     * Construct 'ORDER BY' clause
     *
     * @param array $fetchOptions (uses 'order' key)
     * @param string $defaultOrderSql Default order SQL
     *
     * @return string
     */
    public function prepareUserOrderOptions(array &$fetchOptions, $defaultOrderSql = '')
    {
        $choices = array(
            'username' => 'user.username',
            'register_date' => 'user.register_date',
            'message_count' => 'user.message_count',
            'trophy_points' => 'user.trophy_points',
            'like_count' => 'user.like_count',
            'last_activity' => 'user.last_activity',
            'custom_title' => 'user.custom_title'
        );
        return $this->getOrderByClause($choices, $fetchOptions, $defaultOrderSql);
    }
}
And this is my CanonList.php file:
PHP:
<?php

class Goss_CanonList_CanonList
{
    public static function respond(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract $response)
    {
        // fetch characters for canon list
        $userModel = $controller->getModelFromCache('XenForo_Model_User');
        $response->params['users'] = $userModel->getUsers(array(
            'secondary_group_ids' => 5,  /* Pull characters from this group # */
            'user_state' => 'valid'
            ), array(
                'order' => 'custom_title',
                'join' => XenForo_Model_User::FETCH_USER_PROFILE
            ));
    
        foreach ($response->params['users'] as &$user) {
            $user = $userModel->prepareUser($user);
        }
    
        $response->templateName = 'goss_canonlist';
    }
}
To do that you'll have to extend the 'getOrderByClause' method within Xenforo_Model since that method supports only sorting on a single column, not multiple columns. Your best bet is to just write the SQL query yourself.

Something like this:

PHP:
$users = XenForo_Application::getDb()->fetchAll('
            SELECT u.*, f.*
            FROM xf_user u
            LEFT JOIN xf_user_profile f ON (u.user_id=f.user_id)
            WHERE u.user_state=? AND FIND_IN_SET(?, secondary_group_ids)
            ORDER BY u.custom_title ASC, u.username ASC
            ', array('valid', '5'));
 

Bob

Well-known member
#7
I'm only vaguely familiar with php and learning as I go, but any suggestions on how to add in a secondary sorting option? Ideally, I'd like it to sort first by user_title, and then by username.
PHP:
        $choices = array(
            'username' => 'user.username',
            ****SNIP****
            'custom_title' => 'user.custom_title %s, user.username ASC'
        );
 
Last edited: