myCRED Forums How-to Questions [mycred_leaderboard] based on different point type

This topic contains 3 replies, has 2 voices, and was last updated by Avatar of Gabriel Merovingi Gabriel Merovingi 2 days, 11 hours ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #6979
    Avatar of oomskaap
    oomskaap
    Member

    Hi Gabriel. I have a custom leaderboard, which works great on the mycred_leaderboard shortcode.
    In my template I have the following code:
    ——just to define some custom meta to be used ———-
    <?php
    global $wpdb, $user_ID ;
    $results = get_user_meta ( $user_ID, ‘qty’, false ) ;
    $totalQty = array_sum ( $results ) ;
    $results = get_user_meta ( $user_ID, ‘grand_total’, false ) ;
    $gTotal = array_sum ( $results ) ;
    ?>

    ——-output of shortcode—————-

    <?php $html =  do_shortcode('[mycred_leaderboard wrap="" template="%user_id%" number="50"]
        <tr class="">
            <td class="col-rank"> <div class="rank">%ranking%</div></td>
            <td class="col-user">
    
                <a data-role="hovercard" data-direction="right" data-user-id="4642580" class="name
                    
                        "  "="">%display_name%
                </a>
            </td>
    
            <td class="col-owns">%user_id% </td>
    		<td class="col-img">img</td>
    
            <td class="col-points">%cred_f%</td>
        </tr>
    [/mycred_leaderboard]'); 
    $matches = array();
    preg_match_all('/<tr class="">(.*?)<\/tr>/s', $html, $matches);
    foreach($matches[1] as $output)
    {
    	preg_match_all('/<td class="col-owns">(.*?) <\/td>/s', $output, $userid);
    	$user = $userid[1][0];
    	$results1 = get_user_meta ( $user, 'qty', false ) ; 
    	$totalQty = array_sum ( $results1 ) ; 
    	$results2 = get_user_meta ( $user, 'product_name', false ) ; 
    	$products = implode("<br />", $results2 ) ;
    	$results3 = get_user_meta ( $user, 'product_id', false ) ; 
    	$img='';
    	foreach($results3 as $productid)
    	{
    		$image = wp_get_attachment_image_src( get_post_thumbnail_id( $productid ), 'single-post-thumbnail' );
    		$img .= '<a href="'.get_permalink($productid).'"><img src="'.$image[0].'" width="100px" height="100px" /></a>';
    	}
    	
    	$str = str_replace("<td class=\"col-owns\">".$user, "<td class=\"col-owns\">".$totalQty, $output);
    	$str = str_replace("<td class=\"col-img\">img", "<td class=\"col-img\">".$img, $str);
    	echo  '<tr class="">'.$str.'</tr>';
    }
    ?>

    This will give me the leaderboard based on store credits.

    Now, i want to duplicate this template, but the difference is it should display the leader board based on people who has the most REFERRAL points.

    I already know how to echo a current user’s referral points via:

    Referral points</i><b><?php echo mycred_total_per_ref( 'visitor_referring', get_current_user_id() ); ?> points

    But I am not sure how to modify the leaderboard shortcode above to be referral specific points.

    Is this possible?

    #7042

    Hey.

    This is how I would do it:

    First, I would add a new shortcode attribute called reference which then would hold the reference the leaderboard should be based on. As you correctly pointed out, the reference here is “visitor_referring”.

    So my shortcode would look like this when used:

    [mycred_leaderboard reference="visitor_referring"]
    

    Next we need to adjust the SQL Query which can do via the mycred_ranking_sql filter. It has two parameters: the query string and the attributes passed to the shortcode.

    add_filter( 'mycred_ranking_sql', 'mycred_custom_leaderboard_base', 10, 2 );
    function mycred_custom_leaderboard_base( $query, $attr = array() ) {
    	// Default
    	if ( ! isset( $attr['reference'] ) || $attr['reference'] == '' ) return $query;
    
    	extract( shortcode_atts( array(
    		'number'    => '-1',
    		'order'     => 'DESC',
    		'offset'    => 0,
    		'type'      => 'mycred_default',
    		'wrap'      => 'li',
    		'template'  => '#%position% %user_profile_link% %cred_f%',
    		'nothing'   => __( 'Leaderboard is empty.', 'mycred' ),
    		'current'   => 0,
    		'reference' => ''
    	), $attr ) );
    
    	if ( ! in_array( $order, array( 'ASC', 'DESC' ) ) )
    		$order = 'DESC';
    
    	if ( $number != '-1' )
    		$limit = 'LIMIT ' . absint( $offset ) . ',' . absint( $number );
    	else
    		$limit = '';
    
    	global $wpdb;
    
    	$mycred_log = $wpdb->prefix . 'myCRED_log';
    
    	return "SELECT DISTINCT u.ID, SUM( log.creds ) AS cred 
    			FROM {$wpdb->users} u
    			INNER JOIN {$mycred_log} log
    				ON ( u.ID = log.user_id )
    			WHERE log.ref = '{$reference}'
    			AND log.ctype = %s  
    			GROUP BY log.user_id 
    			ORDER BY SUM( log.creds ) {$order} {$limit};";
    }
    

    All other shortcodes are still usable here it’s just that we override the query with a new one.

    In 1.5 I am going to redo the leaderboard to try and make it more customizable. Preferably making it easier to create leaderboards based on different point information.

    #7626
    Avatar of oomskaap
    oomskaap
    Member

    This works very good. Thank you!

    #7627

    This will become obsolete in 1.5 as the new leaderboard widget will allow you to create leaderbaords based on these references at will.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.