/**
 * Filters the bp_has_members() query to return only users who have unlocked the specific Achievement.
 *
 * @global object $bp BuddyPress global settings
 * @global nxtdb $nxtdb NXTClass database object
 * @since 2.0
 */
function dpa_filter_users_by_achievement($query, $sql_parts)
{
    global $bp, $nxtdb;
    // Hacktastic. To override filter text on pagination. See bp_members_pagination_count().
    $find = __('Viewing member %1$s to %2$s (of %3$s active members)', 'buddypress');
    // Intentionally uses BuddyPress' text domain.
    $replace = __('Viewing member %1$s to %2$s (of %3$s members) who have unlocked this Achievement', 'dpa');
    dpa_override_i18n($find, $replace);
    if ($bp->achievements->current_achievement->id) {
        // This function is hooked into both bp_core_get_total_users_sql and bp_core_get_paged_users_sql, modify the query appropiately.
        if (isset($sql_parts['select_main'])) {
            $sql_parts['select_main'] .= ', unlocked.achieved_at';
        }
        $sql_parts['where'] = ", {$bp->achievements->table_unlocked} as unlocked {$sql_parts['where']}" . $nxtdb->prepare(' AND unlocked.achievement_id = %d AND unlocked.user_id = u.ID', $bp->achievements->current_achievement->id);
        if ("ORDER BY um.meta_value DESC" == $sql_parts[0] || "ORDER BY u.user_registered DESC" == $sql_parts[0] || "ORDER BY pd.value ASC" == $sql_parts[0] || "ORDER BY rand()" == $sql_parts[0] || "ORDER BY CONVERT(um.meta_value, SIGNED) DESC" == $sql_parts[0]) {
            $sql_parts[0] = "ORDER BY unlocked.achieved_at DESC";
        }
    }
    // Remove the filters that invoke this function so it doesn't affect other usages of the members loop on the same page
    if (isset($sql_parts['select_main'])) {
        remove_filter('bp_core_get_paged_users_sql', 'dpa_filter_users_by_achievement', 10, 2);
    } else {
        remove_filter('bp_core_get_total_users_sql', 'dpa_filter_users_by_achievement', 10, 2);
    }
    // Switch back the translation
    $find = __('Viewing member %1$s to %2$s (of %3$s members) who have unlocked this Achievement', 'dpa');
    $replace = __('Viewing member %1$s to %2$s (of %3$s active members)', 'buddypress');
    // Intentionally uses BuddyPress' text domain.
    dpa_override_i18n($find, $replace);
    return apply_filters('dpa_filter_users_by_achievement', join(' ', (array) $sql_parts), $sql_parts);
}
/**
 * Changes the 'no activity' message on an achievement's activity page.
 *
 * @since 2.0
 */
function dpa_achievement_activity_il8n_filter()
{
    $find = __('Sorry, there was no activity found. Please try a different filter.', 'buddypress');
    // Intentionally uses BuddyPress' text domain.
    $replace = __('Sorry, there was no activity found.', 'dpa');
    dpa_override_i18n($find, $replace);
}