Last active
June 11, 2021 13:32
-
-
Save MrVibe/33425b188c5516c70c4bb40d3f0eee35 to your computer and use it in GitHub Desktop.
Course leaderboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class WPLMS_Course_Leaderboard{ | |
| public static $instance; | |
| public static function init(){ | |
| if ( is_null( self::$instance ) ) | |
| self::$instance = new WPLMS_Course_Leaderboard(); | |
| return self::$instance; | |
| } | |
| private function __construct(){ | |
| add_filter('wplms_get_course_tabs',array($this,'add_course_tab')); | |
| add_filter('wplms_course_tab_data',array($this,'capture_course_id'),10,3); | |
| add_shortcode('course_leaderboard',array($this,'course_leaderboard')); | |
| } | |
| function add_course_tab($tabs){ | |
| $tabs['internal__leaderboard'] = 'Leaderboard'; | |
| return $tabs; | |
| } | |
| function capture_course_id($data,$body,$course_id){ | |
| if($body['tab'] == 'internal__leaderboard'){ | |
| $this->course_id = $course_id; // [1] CACHE the COURSE ID into this Class | |
| ob_start(); | |
| echo do_shortcode('[course_leaderboard course="'.$this->course_id.'"]'); | |
| $data = ob_get_clean(); | |
| } | |
| return $data; | |
| } | |
| function course_leaderboard($atts,$content=null){ | |
| ob_start(); | |
| global $wpdb; | |
| if(!isset($atts['count'])){ | |
| $atts['count'] =10; | |
| } | |
| $students = $wpdb->get_results($wpdb->prepare(" | |
| SELECT pm.meta_key,pm.meta_value FROM {$wpdb->postmeta} AS pm | |
| LEFT JOIN {$wpdb->posts} as p ON pm.post_id = p.ID | |
| WHERE p.post_status = %s AND p.post_type = %s | |
| AND p.ID = %d | |
| AND pm.meta_key REGEXP '^[0-9]+$' ORDER BY pm.meta_value DESC LIMIT 0,{$atts['count']}",'publish','course',$atts['course']) | |
| ); | |
| if(!empty($students)){ | |
| echo '<ul class="student_list_title"> | |
| <li> | |
| <span style="display: inline-block;background: #313b3d40;width: 50% !important;"> Student Name </span> | |
| <span style="display: inline-block;background: #313b3d40;width: 49% !important;"> Marks </span> | |
| </li> | |
| </ul>'; | |
| echo '<ol class="students">'; | |
| foreach ($students as $key => $stu) { | |
| $student = get_user_by('id',$stu->meta_key); | |
| if(!empty($student) && !is_wp_error($student)){ | |
| echo '<li><span style="width: 50% !important;display: inline-block; font-size: 18px;">'.bp_core_get_user_displayname($student->ID).'</span><span style="width: 50% !important;display: inline-block; font-size: 18px;">'.$stu->meta_value.'</span></li>'; | |
| } | |
| } | |
| echo '</ul>'; | |
| }else{ | |
| echo '<div class="vbp_message">No students found.</div>'; | |
| } | |
| $html = ob_get_clean(); | |
| return $html; | |
| } | |
| } | |
| WPLMS_Course_Leaderboard::init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment