Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author myCred

    (@designbymerovingi)

    Hi.

    Unfortunately myCRED does not offer this. Leaderboards can either be current, today, this week or get updated on a specific date.

    Thread Starter kalesco

    (@kalesco)

    Hi,

    thanks for the quick reply.
    I managed to change the custom widget you posted for a weekly leaderboard to a monthly ranking. (current month)
    It is not perfect, but it does the job.

    Hi Kalesco, can you share the code? That’s all I need before joining mycred and leaving cubepoints.

    Thread Starter kalesco

    (@kalesco)

    I added this to the mycred-widgets.php.

    It shows either the current or the last month, depending on the widget setting. Don’t know if it will work for you, but I hope it helps to point you in the right direction.

    /**
     * CURRENT MONTH
     * @since 1.2
     * @version 1.0
     */
    if ( !class_exists( 'myCRED_Widget_This_Months_Leaderboard' ) ) {
            class myCRED_Widget_This_Months_Leaderboard extends WP_Widget {
    
                    // Constructor
                    public function __construct() {
                            $name = apply_filters( 'mycred_label', myCRED_NAME );
                            // Basic details about our widget
                            $widget_ops = array(
                                    'classname'   => 'widget-mycred-this-months-leaderboard',
                                    'description' => __( 'Show the leaderboard for the current month.', 'mycred' )
                            );
                            $this->WP_Widget( 'mycred_widget_this_months_leaderboard', __( 'This months Leaderboard', 'mycred' ), $widget_ops );
                            $this->alt_option_name = 'mycred_widget_this_months_leaderboard';
                    }
    
                    // Widget Output (what users see)
                    public function widget( $args, $instance ) {
                            extract( $args, EXTR_SKIP );
    
                            // Check if we want to show this to visitors
                            if ( !$instance['show_visitors'] && !is_user_logged_in() ) return;
    
                            // Get the leaderboard
                            $leaderboard = $this->get_leaderboard( $instance['number'], $widget_id, $instance['month'] );
                            //if ( !isset( $leaderboard[ date_i18n( 'W' ) ] ) ) return;
    
                            // Load myCRED
                            $mycred = mycred_get_settings();
    
                            // Start constructing Widget
                            echo $before_widget;
    
                            // Title (if not empty)
                            if ( !empty( $instance['title'] ) ) {
                                    echo $before_title;
                                    // Allow general tempalte tags in the title
                                    echo $mycred->template_tags_general( $instance['title'] );
                                    echo $after_title;
                            }
    						if ( !empty( $instance['month'] ) ) {
    							//echo $instance['month'];
    							//setlocale(LC_TIME, 'de_DE');
    							if ($instance['month'] == 'this') echo '<h5 style="text-align:center; margin: 1em;">'.strftime("%m/%Y", strtotime("this month")).'</h5>';
    							if ($instance['month'] == 'last') echo '<h5 style="text-align:center; margin: 1em;">'.strftime("%m/%Y", strtotime("last month")).'</h5>';
    						}
                            // Construct unorganized list for each row
                            echo '<table style="width: 70%;margin: 0 auto;text-align:center"><thead><tr><th style="text-align:center; width:10%;">Position</th><th style="text-align:center; width:60%;">Name</th><th style="text-align:center">Punkte</th></tr></thead>';
    						$rang = 1;
                            foreach ( $leaderboard as $position => $data ) {
                                   echo '<tr><td>'.$rang.'</td><td>' . $data->display_name . '</td><td> ' . $mycred->format_creds( $data->total ) . '</td></tr>';
    							   $rang++;
                            }
                            echo '</table>';
    
                            echo $after_widget;
                    }
    
                    // Widget Settings (when editing / setting up widget)
                    public function form( $instance ) {
                            $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'This Months Leaderboard', 'mycred' );
                            $number = isset( $instance['number'] ) ? abs( $instance['number'] ) : 5;
                            $show_visitors = isset( $instance['show_visitors'] ) ? 1 : 0;
    						$month = isset($instance['month']) ? $instance['month'] : 'this';
    						?>
    
                    <p class="myCRED-widget-field">
                            <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title', 'mycred' ); ?>:</label>
                            <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
                    </p>
    				<p>
    					<label for="something"><?php _e('Which month:', 'buddypress'); ?>
    					<select name="<?php echo $this->get_field_name( 'month' ) ?>">
    						<option value="this" <?php if ( $month == 'this' ) : ?>selected="selected"<?php endif; ?> >This month</option>
    						<option value="last" <?php if ( $month == 'last' ) : ?>selected="selected"<?php endif; ?>>Last month</option>
    
    					</select>
    					</label>
    				</p>
                    <p class="myCRED-widget-field">
                            <label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of users', 'mycred' ); ?>:</label>
                            <input id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" type="text" value="<?php echo $number; ?>" size="3" class="align-right" />
                    </p>
                    <p class="myCRED-widget-field">
                            <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'show_visitors' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'show_visitors' ) ); ?>" value="1"<?php checked( $show_visitors, 1 ); ?> class="checkbox" />
                            <label for="<?php echo esc_attr( $this->get_field_id( 'show_visitors' ) ); ?>"><?php _e( 'Visible to non-members', 'mycred' ); ?></label>
                    </p>
    <?php
                    }
    
                    // Save Widget Settings
                    public function update( $new_instance, $old_instance ) {
                            $instance = $old_instance;
                            $instance['number'] = (int) $new_instance['number'];
                            $instance['title'] = trim( $new_instance['title'] );
                            $instance['show_visitors'] = $new_instance['show_visitors'];
                            $instance['month'] = $new_instance['month'];
                            mycred_flush_widget_cache( 'mycred_widget_this_months_leaderboard' );
                            return $instance;
                    }
    
                    // Grabs the leaderboard
                    public function get_leaderboard( $number = 5, $widget_id = '', $month = 'this' ) {
    
                                    $data = array();
    
                                    // Load the wpdb class
                                    global $wpdb;
    
                                    $mycred = $wpdb->prefix . 'myCRED_log';
                                    $SQL = "
                                    SELECT m.user_id, u.display_name, sum( m.creds ) AS total
                                    FROM {$mycred} m
                                    LEFT JOIN {$wpdb->users} u
                                    ON u.ID = m.user_id
                                    WHERE ( m.time >= %d AND m.time <= %d )
                                    GROUP BY m.user_id
                                    ORDER BY total DESC ";
    
    								if ($month == 'this') {
    									$start_month = strtotime( "first day of this month" );
    									$end_month = strtotime("last day of this month");
    								} else {
    									$start_month = strtotime( "first day of last month" );
    									$end_month = strtotime("last day of last month");
    								}
    								$data = $wpdb->get_results( $wpdb->prepare( $SQL, $start_month, $end_month, $number ) );
                                    //echo $wpdb->prepare( $SQL, $start_month, $end_month, 10 );
    
                            // Excludes
    
    						$mycred = mycred_get_settings();
    
    						foreach ( $data as $row_id => $row_data ) {
    							$row = get_object_vars($row_data);
    							//print("<pre>".print_r($row,true)."</pre>");
    							// exclude users from myCred exclude list and deleted users (users with no displayname)
    							if ( $mycred->exclude_user( $row['user_id'] ) || $row['display_name'] == '' ) {
    								//echo '<br />excluded: '.$row['user_id'];
    								unset ($data[$row_id]);
    							}
    						}
    
    						$data = array_slice($data, 0,$number);
                            return $data;
                    }
            }
    }
    Plugin Author myCred

    (@designbymerovingi)

    Excellent work kalesco! Thanks for sharing!

    Hi Gabriel,

    You said in your reply that leaderboards can be current, today or this week. Is this possible within the shortcode?

    Thanks for your help,
    Sam

    Plugin Author myCred

    (@designbymerovingi)

    Hi MFSAM.

    That was an old feature where you could set how often the leaderboard is updated and not based on a specific timeframe. Basically you could show the leaderboard as it was a week ago for one week before it gets updated again. When it updates it would still be the total leaderboard and not the “last weeks”. So it was a useless feature that got removed.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Month Leaderboard’ is closed to new replies.