From d4b74099eeb7421a0c6ef58f3d941944e8c8287d Mon Sep 17 00:00:00 2001 From: pupi1985 Date: Fri, 9 Feb 2018 21:36:25 -0300 Subject: [PATCH] Fix some scenarios where ^userpoints records are not present and generate issues --- qa-include/app/users.php | 11 +++++++++++ qa-include/db/selects.php | 10 +++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/qa-include/app/users.php b/qa-include/app/users.php index 3df963f8a..8d7c064ce 100644 --- a/qa-include/app/users.php +++ b/qa-include/app/users.php @@ -456,6 +456,17 @@ function qa_get_logged_in_user_cache() require_once QA_INCLUDE_DIR . 'db/selects.php'; $qa_cached_logged_in_user = qa_db_get_pending_result('loggedinuser', qa_db_user_account_selectspec($userid, true)); + // If the site is configured to share the ^users table then there might not be a record in the + // ^userpoints table so this creates it + if ($qa_cached_logged_in_user['points'] === null) { + require_once QA_INCLUDE_DIR . 'db/points.php'; + require_once QA_INCLUDE_DIR . 'db/users.php'; + + qa_db_points_update_ifuser($userid, null); + qa_db_uapprovecount_update(); + $qa_cached_logged_in_user = qa_db_single_select(qa_db_user_account_selectspec($userid, true)); + } + if (!isset($qa_cached_logged_in_user)) { // the user can no longer be found (should only apply to deleted users) qa_clear_session_user(); diff --git a/qa-include/db/selects.php b/qa-include/db/selects.php index a356b866f..030fb2706 100644 --- a/qa-include/db/selects.php +++ b/qa-include/db/selects.php @@ -1523,9 +1523,17 @@ function qa_db_top_users_selectspec($start, $count = null) ); } + // If the site is configured to share the ^users table then there might not be a record in the ^userpoints table + if (defined('QA_MYSQL_USERS_PREFIX')) { + $basePoints = (int)qa_opt('points_base'); + $source = '^users JOIN (SELECT ^users.userid, COALESCE(points,' . $basePoints . ') AS points FROM ^users LEFT JOIN ^userpoints ON ^users.userid=^userpoints.userid ORDER BY points DESC LIMIT #,#) y ON ^users.userid=y.userid'; + } else { + $source = '^users JOIN (SELECT userid FROM ^userpoints ORDER BY points DESC LIMIT #,#) y ON ^users.userid=y.userid JOIN ^userpoints ON ^users.userid=^userpoints.userid';; + } + return array( 'columns' => array('^users.userid', 'handle', 'points', 'flags', '^users.email', 'avatarblobid' => 'BINARY avatarblobid', 'avatarwidth', 'avatarheight'), - 'source' => '^users JOIN (SELECT userid FROM ^userpoints ORDER BY points DESC LIMIT #,#) y ON ^users.userid=y.userid JOIN ^userpoints ON ^users.userid=^userpoints.userid', + 'source' => $source, 'arguments' => array($start, $count), 'arraykey' => 'userid', 'sortdesc' => 'points',