function cjtheme_user_role_display($user_id, $icon = null)
{
    global $wpdb;
    $table_custom_roles = $wpdb->prefix . 'gamf_custom_roles';
    $table_user_points = $wpdb->prefix . 'gamf_user_points';
    $modify_roles_query = $wpdb->get_results("SELECT * FROM {$table_custom_roles} ORDER BY points ASC");
    $user_points = $wpdb->get_row("SELECT SUM(points) as points FROM {$table_user_points} WHERE user_id = '{$user_id}'");
    $user_points = $user_points->points + 1;
    $role_points = $wpdb->get_row("SELECT * FROM {$table_custom_roles} WHERE points <= '{$user_points}' ORDER BY points DESC");
    $current_user_role = get_user_meta($user_id, 'cjtheme_user_role', true);
    if ($current_user_role != $role_points->role_display_name) {
        update_user_meta($user_id, 'cjtheme_user_role', $role_points->role_display_name);
        $nmessage = 'Your role is upgraded to ' . strtoupper($role_points->role_display_name);
        cjtheme_notification_add($user_id, $nmessage);
    }
    return '<span class="role">' . $icon . $role_points->role_display_name . '</span>';
    if (in_array(cjtheme_user_role($user_id), $modify_roles)) {
        return '<span class="role">' . $icon . cjtheme_user_role($user_id) . '</span>';
    }
}
function cjtheme_insert_answer()
{
    global $wpdb;
    $errors = null;
    parse_str($_POST['postdata'], $postdata);
    $user_info = cjtheme_user_info($postdata['user_id']);
    $time = current_time('mysql');
    $comment_data = array('comment_post_ID' => $postdata['post_id'], 'comment_author' => $user_info['user_login'], 'comment_author_email' => $user_info['user_email'], 'comment_author_url' => '', 'comment_content' => esc_textarea($postdata['comment']), 'user_id' => $user_info['ID'], 'comment_date' => $time, 'comment_approved' => 1);
    $comment_id = wp_insert_comment($comment_data);
    if (strlen($postdata['comment']) >= 140) {
        gamf_add_points($user_info['ID'], 1, 'comment-140', 'You answered a question with more than 140 characters.');
    }
    $post_info = cjtheme_post_info($postdata['post_id']);
    $nmessage = '<a href="' . site_url($user_info['user_login']) . '">' . $user_info['display_name'] . '</a> answered your <a href="' . get_permalink($post_info['ID']) . '">question</a>.';
    $nmessage .= '<br>' . cjtheme_trim_text($post_info['post_content'], 160, '...');
    cjtheme_notification_add($post_info['post_author'], $nmessage);
    $return['success'] = get_permalink($post_info['ID']) . '#answer-' . $comment_id;
    echo json_encode($return);
    die;
}
function cjtheme_update_bp_acmeta_thanks()
{
    global $wpdb;
    $table_bp_acmeta = $wpdb->prefix . 'cjtheme_likes_thanks';
    $check_existing = $wpdb->get_row("SELECT * FROM {$table_bp_acmeta} WHERE user_id = '{$_POST['user_id']}' AND comment_id = '{$_POST['comment_id']}'");
    if (is_null($check_existing)) {
        $acmeta_data = array('comment_id' => $_POST['comment_id'], 'user_id' => $_POST['user_id'], 'thanks' => 1);
        cjtheme_insert($table_bp_acmeta, $acmeta_data);
        $comment_thanks = cjtheme_activity_comment_thanks($_POST['comment_id']);
        $comment_thanks = !is_null($comment_thanks) ? ' ' . $comment_thanks : '';
        $comment = get_comment($_POST['comment_id']);
        if ($comment->user_id != $_POST['user_id']) {
            gamf_add_points(cjtheme_user_info($comment->user_id, 'ID'), '3', 'thank-answer', 'You received a thanks on your answer.');
        }
        echo ' ' . $comment_thanks;
    } elseif ($check_existing->thanks == 0) {
        $acmeta_data = array('comment_id' => $_POST['comment_id'], 'user_id' => $_POST['user_id'], 'thanks' => 1);
        cjtheme_update($table_bp_acmeta, $acmeta_data, 'id', $check_existing->id);
        $comment_thanks = cjtheme_activity_comment_thanks($_POST['comment_id']);
        $comment_thanks = !is_null($comment_thanks) ? ' ' . $comment_thanks : '';
        $comment = get_comment($_POST['comment_id']);
        if ($comment->user_id != $_POST['user_id']) {
            gamf_add_points(cjtheme_user_info($comment->user_id, 'ID'), '3', 'thank-answer', 'You received a thanks on your answer.');
            $nmessage = '<a href="' . site_url(cjtheme_user_info($_POST['user_id'], 'user_login')) . '">' . cjtheme_user_info($_POST['user_id'], 'display_name') . '</a> said thanks for your <a href="' . get_permalink($comment->comment_post_ID) . '#answer-' . $comment->comment_ID . '">answer</a>.';
            cjtheme_notification_add(cjtheme_user_info($comment->user_id, 'ID'), $nmessage);
        }
        echo ' ' . $comment_thanks;
    } else {
        $acmeta_data = array('comment_id' => $_POST['comment_id'], 'user_id' => $_POST['user_id'], 'thanks' => 0);
        cjtheme_update($table_bp_acmeta, $acmeta_data, 'id', $check_existing->id);
        $comment_thanks = cjtheme_activity_comment_thanks($_POST['comment_id']);
        $comment_thanks = !is_null($comment_thanks) ? ' ' . $comment_thanks : '';
        $comment = get_comment($_POST['comment_id']);
        if ($comment->user_id != $_POST['user_id']) {
            gamf_add_points(cjtheme_user_info($comment->user_id, 'ID'), '-3', 'unthank-answer', 'You received a Thanks  on your answer.');
            $nmessage = '<a href="' . site_url(cjtheme_user_info($_POST['user_id'], 'user_login')) . '">' . cjtheme_user_info($_POST['user_id'], 'display_name') . '</a> unthanked your <a href="' . get_permalink($comment->comment_post_ID) . '#answer-' . $comment->comment_ID . '">answer</a>.';
            cjtheme_notification_add(cjtheme_user_info($comment->user_id, 'ID'), $nmessage);
        }
        echo '' . $comment_thanks;
    }
    die;
}