• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
  • This forum is for release discussion only. Discussions that do not relate specifically to the resource release should be discussed in another, more appropriate forum.
  • This forum has been archived. New threads and replies may not be made. All add-ons/resources that are active should be migrated to the Resource Manager. See this thread for more information.

Sort Member List By Last Activity

Jake Bunce

XenForo moderator
Staff member
#1
(in response to this thread)

Edit this file:

library/XenForo/ControllerPublic/Member.php

Find this code in actionIndex() and add the red code:

Code:
		// users for the member list
		$users = $userModel->getUsers($criteria, array(
			'join' => XenForo_Model_User::FETCH_USER_FULL,
			'perPage' => $usersPerPage,
			'page' => $page,
			'order' => 'last_activity',
			'direction' => 'DESC'
		));
Now the member list will default to being sorted by last activity. Active users will be at the top while inactive users will fall to the bottom.

Allowed values are:

order:
username
register_date
message_count
last_activity


direction:
ASC
DESC
 

kkm323

Well-known member
#2
error

Code:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in /home/xxxxxxx/public_html/library/XenForo/ControllerPublic/Member.php on line 57
 

Jake Bunce

XenForo moderator
Staff member
#4
No comma after the last option. Here is the added code by itself:

Code:
					,
			'order' => 'last_activity',
			'direction' => 'DESC'
Or just copy the entire block of code and replace the existing code.
 

EasyTarget

Well-known member
#5
(in response to this thread)
Code:
// users for the member list
$users = $userModel->getUsers($criteria, array(
'join' => XenForo_Model_User::FETCH_USER_FULL,
'perPage' => $usersPerPage,
'page' => $page,
 'order' => 'last_activity',
 'direction' => 'DESC'
));
How do we add a where clause to the query? I want to exclude banned members and members in specific user group.
 

Jake Bunce

XenForo moderator
Staff member
#6
How do we add a where clause to the query? I want to exclude banned members and members in specific user group.
There is no option to specify "not in usergroup." There is only "in usergroup."

Edit the same file:

library/XenForo/ControllerPublic/Member.php

You need to set the criteria which is directly above the previous code:

Code:
		$criteria = array(
			'user_state' => 'valid',
			'is_banned' => 0,
			'user_group_id' => array(1,2,3),
			'secondary_group_ids' => array(1,2,3)
		);

		// users for the member list
		$users = $userModel->getUsers($criteria, array(
			'join' => XenForo_Model_User::FETCH_USER_FULL,
			'perPage' => $usersPerPage,
			'page' => $page,
			'order' => 'last_activity',
			'direction' => 'DESC'
		));
is_banned is 0 or 1. The group criteria take group ids.

All of the allowed "where" conditions are defined in this file:

library/XenForo/Model/User.php

In this function:

Code:
	public function prepareUserConditions(array $conditions, array &$fetchOptions)
	{
		$db = $this->_getDb();
		$sqlConditions = array();

		if (!empty($conditions['username']))
		{
			if (is_array($conditions['username']))
			{
				$sqlConditions[] = 'user.username LIKE ' . XenForo_Db::quoteLike($conditions['username'][0], $conditions['username'][1], $db);
			}
			else
			{
				$sqlConditions[] = 'user.username LIKE ' . XenForo_Db::quoteLike($conditions['username'], 'lr', $db);
			}
		}

		// this is mainly for dynamically filtering a search that already matches user names
		if (!empty($conditions['username2']))
		{
			if (is_array($conditions['username2']))
			{
				$sqlConditions[] = 'user.username LIKE ' . XenForo_Db::quoteLike($conditions['username2'][0], $conditions['username2'][1], $db);
			}
			else
			{
				$sqlConditions[] = 'user.username LIKE ' . XenForo_Db::quoteLike($conditions['username2'], 'lr', $db);
			}
		}

		if (!empty($conditions['usernames']) && is_array($conditions['usernames']))
		{
			$sqlConditions[] = 'user.username IN (' . $db->quote($conditions['usernames']) . ')';
		}

		if (!empty($conditions['email']))
		{
			if (is_array($conditions['email']))
			{
				$sqlConditions[] = 'user.email LIKE ' . XenForo_Db::quoteLike($conditions['email'][0], $conditions['email'][1], $db);
			}
			else
			{
				$sqlConditions[] = 'user.email LIKE ' . XenForo_Db::quoteLike($conditions['email'], 'lr', $db);
			}
		}
		if (!empty($conditions['emails']) && is_array($conditions['emails']))
		{
			$sqlConditions[] = 'user.email IN (' . $db->quote($conditions['emails']) . ')';
		}

		if (!empty($conditions['user_group_id']))
		{
			if (is_array($conditions['user_group_id']))
			{
				$sqlConditions[] = 'user.user_group_id IN (' . $db->quote($conditions['user_group_id']) . ')';
			}
			else
			{
				$sqlConditions[] = 'user.user_group_id = ' . $db->quote($conditions['user_group_id']);
			}
		}

		if (!empty($conditions['secondary_group_ids']))
		{
			if (is_array($conditions['secondary_group_ids']))
			{
				$groupConds = array();
				foreach ($conditions['secondary_group_ids'] AS $groupId)
				{
					$groupConds[] = 'FIND_IN_SET(' . $db->quote($groupId) . ', user.secondary_group_ids)';
				}
				$sqlConditions[] = '(' . implode(' OR ', $groupConds) . ')';
			}
			else
			{
				$sqlConditions[] = 'FIND_IN_SET(' . $db->quote($conditions['secondary_group_ids']) . ', user.secondary_group_ids)';
			}
		}

		if (!empty($conditions['last_activity']) && is_array($conditions['last_activity']))
		{
			list($operator, $cutOff) = $conditions['last_activity'];

			$this->assertValidCutOffOperator($operator);
			$sqlConditions[] = "user.last_activity $operator " . $db->quote($cutOff);
		}

		if (!empty($conditions['message_count']) && is_array($conditions['message_count']))
		{
			list($operator, $cutOff) = $conditions['message_count'];

			$this->assertValidCutOffOperator($operator);
			$sqlConditions[] = "user.message_count $operator " . $db->quote($cutOff);
		}

		if (!empty($conditions['user_state']) && $conditions['user_state'] !== 'any')
		{
			if (is_array($conditions['user_state']))
			{
				$sqlConditions[] = 'user.user_state IN (' . $db->quote($conditions['user_state']) . ')';
			}
			else
			{
				$sqlConditions[] = 'user.user_state = ' . $db->quote($conditions['user_state']);
			}
		}

		if (isset($conditions['is_admin']))
		{
			$sqlConditions[] = 'user.is_admin = ' . ($conditions['is_admin'] ? 1 : 0);
		}

		if (isset($conditions['is_moderator']))
		{
			$sqlConditions[] = 'user.is_moderator = ' . ($conditions['is_moderator'] ? 1 : 0);
		}

		if (isset($conditions['is_banned']))
		{
			$sqlConditions[] = 'user.is_banned = ' . ($conditions['is_banned'] ? 1 : 0);
		}

		if (!empty($conditions['receive_admin_email']))
		{
			$sqlConditions[] = 'user_option.receive_admin_email = 1';
			$this->addFetchOptionJoin($fetchOptions, self::FETCH_USER_OPTION);
		}

		return $this->getConditionsForClause($sqlConditions);
	}