Commit 4f7605ce by Scott

Refactor UsersController to avoid duplication

parent fbd032ff
......@@ -1562,7 +1562,7 @@ function qa_db_newest_users_selectspec($start, $count = null)
function qa_db_users_from_level_selectspec($level)
{
return array(
'columns' => array('^users.userid', 'handle', 'level'),
'columns' => array('^users.userid', 'handle', 'flags', 'level', 'email', 'avatarblobid' => 'BINARY avatarblobid', 'avatarwidth', 'avatarheight'),
'source' => '^users WHERE level>=# ORDER BY level DESC',
'arguments' => array($level),
'sortdesc' => 'level',
......@@ -1589,7 +1589,7 @@ function qa_db_users_with_flag_selectspec($flag, $start = 0, $limit = null)
}
return array(
'columns' => array('^users.userid', 'handle', 'flags', 'level'),
'columns' => array('^users.userid', 'handle', 'flags', 'level', 'email', 'avatarblobid' => 'BINARY avatarblobid', 'avatarwidth', 'avatarheight'),
'source' => $source,
'arguments' => $arguments,
);
......
......@@ -18,265 +18,212 @@
namespace Q2A\Controllers;
require_once QA_INCLUDE_DIR . 'db/users.php';
require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'app/users.php';
require_once QA_INCLUDE_DIR . 'app/format.php';
class UsersController extends BaseController
{
/**
* Display top users page (ordered by points)
* @return array $qa_content
*/
public function top()
{
require_once QA_INCLUDE_DIR . 'db/users.php';
require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'app/format.php';
// Get list of all users
$start = qa_get_start();
$users = qa_db_select_with_pending(qa_db_top_users_selectspec($start, qa_opt_if_loaded('page_size_users')));
$usercount = qa_opt('cache_userpointscount');
$pagesize = qa_opt('page_size_users');
$users = array_slice($users, 0, $pagesize);
$usershtml = qa_userids_handles_html($users);
// Prepare content for theme
$qa_content = qa_content_prepare();
$qa_content['title'] = qa_lang_html('main/highest_users');
$qa_content['ranking'] = array(
'items' => array(),
'rows' => ceil($pagesize / qa_opt('columns_users')),
'type' => 'users',
'sort' => 'points',
);
if (count($users)) {
foreach ($users as $userid => $user) {
if (QA_FINAL_EXTERNAL_USERS)
$avatarhtml = qa_get_external_avatar_html($user['userid'], qa_opt('avatar_users_size'), true);
else {
$avatarhtml = qa_get_user_avatar_html($user['flags'], $user['email'], $user['handle'],
$user['avatarblobid'], $user['avatarwidth'], $user['avatarheight'], qa_opt('avatar_users_size'), true);
}
// avatar and handle now listed separately for use in themes
$qa_content['ranking']['items'][] = array(
'avatar' => $avatarhtml,
'label' => $usershtml[$user['userid']],
'score' => qa_html(qa_format_number($user['points'], 0, true)),
'raw' => $user,
);
}
} else {
$qa_content['title'] = qa_lang_html('main/no_active_users');
}
// set the canonical url based on possible pagination
$qa_content['canonical'] = qa_path_html(qa_request(), ($start > 0 ? array('start' => $start) : null), qa_opt('site_url'));
// callables to fetch user data
$fetchUsers = function($start, $pageSize) {
return array(
qa_opt('cache_userpointscount'),
qa_db_select_with_pending(qa_db_top_users_selectspec($start, $pageSize))
);
};
$userScore = function($user) {
return qa_html(qa_format_number($user['points'], 0, true));
};
$qa_content['page_links'] = qa_html_page_links(qa_request(), $start, $pagesize, $usercount, qa_opt('pages_prev_next'));
$qa_content = $this->rankedUsersContent($fetchUsers, $userScore);
$qa_content['navigation']['sub'] = qa_users_sub_navigation();
$qa_content['title'] = empty($qa_content['ranking']['items'])
? qa_lang_html('main/no_active_users')
: qa_lang_html('main/highest_users');
$qa_content['ranking']['sort'] = 'points';
return $qa_content;
}
/**
* Display newest users page
* @return array $qa_content
*/
public function newest()
{
require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'app/format.php';
// Check we're not using single-sign on integration
// check we're not using single-sign on integration
if (QA_FINAL_EXTERNAL_USERS)
qa_fatal_error('User accounts are handled by external code');
// Check we have permission to view this page (moderator or above)
// check we have permission to view this page (moderator or above)
if (qa_user_permit_error('permit_view_new_users_page')) {
$qa_content = qa_content_prepare();
$qa_content['error'] = qa_lang_html('users/no_permission');
return $qa_content;
}
// callables to fetch user data
$fetchUsers = function($start, $pageSize) {
return array(
qa_opt('cache_userpointscount'),
qa_db_select_with_pending(qa_db_newest_users_selectspec($start, $pageSize))
);
};
$userDate = function($user) {
$when = qa_when_to_html($user['created'], 7);
return $when['data'];
};
// Get list of all users
$start = qa_get_start();
$users = qa_db_select_with_pending(qa_db_newest_users_selectspec($start, qa_opt_if_loaded('page_size_users')));
$userCount = qa_opt('cache_userpointscount');
$pageSize = qa_opt('page_size_users');
$users = array_slice($users, 0, $pageSize);
$usersHtml = qa_userids_handles_html($users);
// Prepare content for theme
$qa_content = qa_content_prepare();
$qa_content['title'] = qa_lang_html('main/newest_users');
$qa_content['ranking'] = array(
'items' => array(),
'rows' => ceil($pageSize / qa_opt('columns_users')),
'type' => 'users',
'sort' => 'date',
);
if (!empty($users)) {
foreach ($users as $user) {
$avatarHtml = qa_get_user_avatar_html($user['flags'], $user['email'], $user['handle'],
$user['avatarblobid'], $user['avatarwidth'], $user['avatarheight'], qa_opt('avatar_users_size'), true);
$when = qa_when_to_html($user['created'], 7);
$qa_content['ranking']['items'][] = array(
'avatar' => $avatarHtml,
'label' => $usersHtml[$user['userid']],
'score' => $when['data'],
'raw' => $user,
);
}
} else {
$qa_content['title'] = qa_lang_html('main/no_active_users');
}
// set the canonical url based on possible pagination
$qa_content['canonical'] = qa_path_html(qa_request(), ($start > 0 ? array('start' => $start) : null), qa_opt('site_url'));
$qa_content = $this->rankedUsersContent($fetchUsers, $userDate);
$qa_content['page_links'] = qa_html_page_links(qa_request(), $start, $pageSize, $userCount, qa_opt('pages_prev_next'));
$qa_content['title'] = empty($qa_content['ranking']['items'])
? qa_lang_html('main/no_active_users')
: qa_lang_html('main/newest_users');
$qa_content['navigation']['sub'] = qa_users_sub_navigation();
$qa_content['ranking']['sort'] = 'date';
return $qa_content;
}
/**
* Display special users page (admins, moderators, etc)
* @return array $qa_content
*/
public function special()
{
require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'app/users.php';
require_once QA_INCLUDE_DIR . 'app/format.php';
// Check we're not using single-sign on integration
// check we're not using single-sign on integration
if (QA_FINAL_EXTERNAL_USERS)
qa_fatal_error('User accounts are handled by external code');
// Get list of special users
$users = qa_db_select_with_pending(qa_db_users_from_level_selectspec(QA_USER_LEVEL_EXPERT));
// Check we have permission to view this page (moderator or above)
// check we have permission to view this page (moderator or above)
if (qa_user_permit_error('permit_view_special_users_page')) {
$qa_content = qa_content_prepare();
$qa_content['error'] = qa_lang_html('users/no_permission');
return $qa_content;
}
// callables to fetch user data
$fetchUsers = function($start, $pageSize) {
// here we fetch *all* users to get the total instead of a separate query; there are unlikely to be many special users
$users = qa_db_select_with_pending(qa_db_users_from_level_selectspec(QA_USER_LEVEL_EXPERT));
return array(count($users), $users);
};
$userLevel = function($user) {
return qa_html(qa_user_level_string($user['level']));
};
// Get userids and handles of retrieved users
$usershtml = qa_userids_handles_html($users);
// Prepare content for theme
$qa_content = qa_content_prepare();
$qa_content = $this->rankedUsersContent($fetchUsers, $userLevel);
$qa_content['title'] = qa_lang_html('users/special_users');
$qa_content['ranking'] = array(
'items' => array(),
'rows' => ceil(qa_opt('page_size_users') / qa_opt('columns_users')),
'type' => 'users',
'sort' => 'level',
);
foreach ($users as $user) {
$qa_content['ranking']['items'][] = array(
'label' => $usershtml[$user['userid']],
'score' => qa_html(qa_user_level_string($user['level'])),
'raw' => $user,
);
}
$qa_content['navigation']['sub'] = qa_users_sub_navigation();
$qa_content['ranking']['sort'] = 'level';
return $qa_content;
}
/**
* Display blocked users page
* @return array $qa_content
*/
public function blocked()
{
require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'app/users.php';
require_once QA_INCLUDE_DIR . 'app/format.php';
// Check we're not using single-sign on integration
// check we're not using single-sign on integration
if (QA_FINAL_EXTERNAL_USERS)
qa_fatal_error('User accounts are handled by external code');
// Get list of blocked users
$start = qa_get_start();
$pagesize = qa_opt('page_size_users');
$userSpecCount = qa_db_selectspec_count(qa_db_users_with_flag_selectspec(QA_USER_FLAGS_USER_BLOCKED));
$userSpec = qa_db_users_with_flag_selectspec(QA_USER_FLAGS_USER_BLOCKED, $start, $pagesize);
list($numUsers, $users) = qa_db_select_with_pending($userSpecCount, $userSpec);
$count = $numUsers['count'];
// Check we have permission to view this page (moderator or above)
// check we have permission to view this page (moderator or above)
if (qa_get_logged_in_level() < QA_USER_LEVEL_MODERATOR) {
$qa_content = qa_content_prepare();
$qa_content['error'] = qa_lang_html('users/no_permission');
return $qa_content;
}
// callables to fetch user data
$fetchUsers = function($start, $pageSize) {
list($totalUsers, $users) = qa_db_select_with_pending(
qa_db_selectspec_count(qa_db_users_with_flag_selectspec(QA_USER_FLAGS_USER_BLOCKED)),
qa_db_users_with_flag_selectspec(QA_USER_FLAGS_USER_BLOCKED, $start, $pageSize)
);
return array($totalUsers['count'], $users);
};
$userLevel = function($user) {
return qa_html(qa_user_level_string($user['level']));
};
$qa_content = $this->rankedUsersContent($fetchUsers, $userLevel);
$qa_content['title'] = empty($qa_content['ranking']['items'])
? qa_lang_html('users/no_blocked_users')
: qa_lang_html('users/blocked_users');
$qa_content['ranking']['sort'] = 'level';
return $qa_content;
}
// Get userids and handles of retrieved users
/**
* Fetch $qa_content array for a set of ranked users.
* @param callable $fnUsersAndCount Function that returns the list of users for a page and the user total.
* @param callable $fnUserScore Function that returns the "score" (points, date, etc) that will be displayed.
* @return array $qa_content
*/
private function rankedUsersContent($fnUsersAndCount, $fnUserScore)
{
// get the users to display on this page
$usershtml = qa_userids_handles_html($users);
$request = qa_request();
$start = qa_get_start();
$pageSize = qa_opt('page_size_users');
list($totalUsers, $users) = $fnUsersAndCount($start, $pageSize);
// Prepare content for theme
// get userids and handles of retrieved users
$usersHtml = qa_userids_handles_html($users);
$qa_content = qa_content_prepare();
// prepare content for theme
$qa_content['title'] = $count > 0 ? qa_lang_html('users/blocked_users') : qa_lang_html('users/no_blocked_users');
$content = qa_content_prepare();
$qa_content['ranking'] = array(
$content['ranking'] = array(
'items' => array(),
'rows' => ceil(count($users) / qa_opt('columns_users')),
'rows' => ceil($pageSize / qa_opt('columns_users')),
'type' => 'users',
'sort' => 'level',
// 'sort' => '',
);
foreach ($users as $user) {
$qa_content['ranking']['items'][] = array(
'label' => $usershtml[$user['userid']],
'score' => qa_html(qa_user_level_string($user['level'])),
if (QA_FINAL_EXTERNAL_USERS) {
$avatarHtml = qa_get_external_avatar_html($user['userid'], qa_opt('avatar_users_size'), true);
} else {
$avatarHtml = qa_get_user_avatar_html($user['flags'], $user['email'], $user['handle'],
$user['avatarblobid'], $user['avatarwidth'], $user['avatarheight'], qa_opt('avatar_users_size'), true);
}
$content['ranking']['items'][] = array(
'avatar' => $avatarHtml,
'label' => $usersHtml[$user['userid']],
'score' => $fnUserScore($user),
'raw' => $user,
);
}
$qa_content['page_links'] = qa_html_page_links(qa_request(), $start, $pagesize, $count, qa_opt('pages_prev_next'));
$content['page_links'] = qa_html_page_links($request, $start, $pageSize, $totalUsers, qa_opt('pages_prev_next'));
$qa_content['navigation']['sub'] = qa_users_sub_navigation();
// set the canonical url based on possible pagination
$params = $start > 0 ? array('start' => $start) : null;
$content['canonical'] = qa_path_html($request, $params, qa_opt('site_url'));
$content['navigation']['sub'] = qa_users_sub_navigation();
return $qa_content;
return $content;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment