0

I have a plugin to fetch information user from wpdb. After minor update security Wordpress 4.8.2, plugin did not run well. My sellect button can not run and open data to the table.

add_action('wp_ajax_get_batch_course_stats',array($this,'get_batch_course_stats'));

This line if I put outside the class, it run but can not fetch the data because my query inside the class.

This is my code.

<?php


if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( ! defined( 'WPLMS_BATCH_STATISITCS_SLUG' ) )
        define( 'WPLMS_BATCH_STATISITCS_SLUG', 'stats' );

if ( class_exists( 'BP_Group_Extension' ) ) :

    class BP_Group_Course_Batch_Statistics extends BP_Group_Extension {

        var $message;

        function __construct() { 

            $args = array(
                'slug' => WPLMS_BATCH_STATISITCS_SLUG,
                'name' => __( 'Statistics', 'wplms-batches' ),
                'visibility'        => 'private',
                'nav_item_position' => 10,
                'enable_nav_item' => false,
                'screens' => array(
                    'admin' => array(
                        'metabox_context'  => 'side',
                        'metabox_priority' => 'core'
                    ),
                    'create' => array(
                        'enabled' => false,
                    ),
                    'edit' => array(
                        'enabled' => false,
                    ),
                )
            );
            $group_id = bp_get_group_id();
            global $wpdb, $bp;
            if(isset($bp->groups) && is_object($bp->groups->current_group)){
                $enable = groups_get_groupmeta($bp->groups->current_group->id, 'course_batch' );
            }else{
                $enable = 0;
            }

            if(!empty($enable)){
                $args['enable_nav_item'] = true;
            }

            $flag = 1;
            if(isset($bp->groups) && is_object($bp->groups->current_group)){
                $batch_stats_visibility = groups_get_groupmeta($bp->groups->current_group->id,'batch_stats_visibility');
            }


            if(!empty($batch_stats_visibility)){
                $user_id = get_current_user_ID();
                if($batch_stats_visibility == 'mods'){
                    if(!groups_is_user_mod($user_id,$bp->groups->current_group->id)){
                        $flag = 0; 
                    }
                }else if($batch_stats_visibility == 'admins'){
                    if(!groups_is_user_admin($user_id,$bp->groups->current_group->id)){
                        $flag = 0;
                    }
                }
            }

            if($flag){
                parent::init( $args );
                add_action('wplms_batch_statistics',array($this,'batch_statistics'),10,1);
                add_action('wplms_batch_moderator_statistics',array($this,'moderator_statistics'),10,1);             
                 add_action('wp_ajax_get_batch_course_stats', array($this,'get_batch_course_stats'));
                add_action('bp_enqueue_scripts', array( $this, 'cssjs' ) );


            }



        }



            function cssjs(){
                    if( bp_is_single_item() && bp_is_groups_component() && bp_is_current_action(WPLMS_BATCH_STATISITCS_SLUG) ){
                    wp_enqueue_script('datatables-js',plugins_url( '/assets/js/jquery.dataTables.min.js', __FILE__ ),array('jquery'));
                    wp_enqueue_script('bootstrap-datatables-js',plugins_url( '/assets/js/dataTables.bootstrap.min.js', __FILE__ ));
                        }
                        }




        /**
         * display() contains the markup that will be displayed on the main 
         * plugin tab
         */
        function display( $group_id = NULL ) {

            if(empty($group_id))
                $group_id = bp_get_group_id();


            do_action('wplms_batch_statistics',$group_id);
            if(bp_current_user_can( 'bp_moderate' )){
                do_action('wplms_batch_moderator_statistics',$group_id);
            }else{
                do_action('wplms_batch_student_statistics',$group_id);
            }

            $leaderboard = groups_get_groupmeta($group_id,'batch_leaderboard');
            if(bp_current_user_can( 'bp_moderate' ) || !empty($leaderboard)){
                //echo '<h3 class="heading"><span>'._x('Leaderboard','Batch Leaderboard','wplms-batches').'</span></h3>';
                $members = BP_Groups_Member::get_group_member_ids($group_id);
                $courses = groups_get_groupmeta($group_id,'batch_course',false);
                if(empty($courses)){
                    echo '<div class="message">'.__('No Courses connected to Batch','wplms-batches').'</div>';
                    return;
                }
                $member_ids = implode(',',$members);
                $course_ids = implode(',',$courses);
                $course_status_string = '';
                foreach($courses as $c){
                    $course_status_string .= "'course_status".$c."',";
                }
                $course_status_string =trim($course_status_string, ",");
                global $wpdb,$bp;
                $results = $wpdb->get_results($wpdb->prepare("SELECT p.meta_key as uid,sum(p.meta_value) as sum 
                    FROM {$wpdb->postmeta} as p
                    LEFT JOIN {$wpdb->usermeta} as u
                    ON p.meta_key = u.user_id
                    WHERE p.meta_key IN ($member_ids) 
                    AND p.post_id IN ($course_ids) 
                    AND u.meta_key IN ($course_status_string)
                    AND u.meta_value > %d
                    GROUP BY uid ORDER BY sum DESC",3));
                ?>

    <?php

            }
        }


        function batch_statistics($group_id){
            $courses = groups_get_groupmeta($group_id,'batch_course',false);
            if(empty($courses)){
                echo '<div class="message">'.__('No Courses connected to Batch','wplms-batches').'</div>';
                return;
            }
            $members = BP_Groups_Member::get_group_member_ids($group_id);
            $admins = groups_get_group_admins( $group_id );
            $mods = groups_get_group_mods( $group_id );


            global $wpdb,$bp;
            $batch_average = $batch_badge_count = $batch_certificate_count = 0;
            $batch_average = groups_get_groupmeta($group_id,'batch_average');
            $batch_badge_count = groups_get_groupmeta($group_id,'batch_badge_count');
            $batch_certificate_count = groups_get_groupmeta($group_id,'batch_certificate_count');
            global $wpdb, $bp;
            if(isset($_GET['recalculate'])){
                $batch_badge_count = $batch_certificate_count = 0;
                $total_marks = 0;$total_denom = 1;
                foreach($members as $member_id){
                    foreach($courses as $course_id){
                        $status = bp_course_get_user_course_status($member_id,$course_id);
                        if($status > 3){
                            $marks = get_post_meta($course_id,$member_id,true);
                            $total_marks += $marks;
                            $total_denom++;
                        }
                    }
                    $certificates = get_user_meta($member_id,'certificates',true);
                    $badges = get_user_meta($member_id,'badges',true);
                    if(!empty($certificates)){
                        foreach($certificates as $certificate_course_id){
                            if(in_array($certificate_course_id,$courses)){
                                $batch_certificate_count++;
                            }
                        }    
                    }
                    if(!empty($badges)){
                        foreach($badges as $badges_course_id){
                            if(in_array($badges_course_id,$courses)){
                                $batch_badge_count++;
                            }
                        }    
                    }
                }
                if($total_denom > 1){
                    $total_denom--;
                }
                $batch_average = round($total_marks/$total_denom,2);
                groups_update_groupmeta($group_id,'batch_average',$batch_average);
                groups_update_groupmeta($group_id,'batch_badge_count',$batch_badge_count);
                groups_update_groupmeta($group_id,'batch_certificate_count',$batch_certificate_count);
            }





            ?>
<?php /*
 ?>
        <div class="row">
            <div class="col-md-12">
                <div class="course_stats_form">
                    <h3 class="heading"><span><?php _e('Course Specific Statistics','wplms-batches'); ?></span></h3>
                    <select id="batch_course">
                        <?php
                            foreach($courses as $course){
                                echo '<option value="'.$course.'">'.get_the_title($course).'</option>';
                            }
                        ?>
                    </select>
                    <?php wp_nonce_field('batch_course_security'.$group_id,'batch_course_security'); 
                    echo '<input type="hidden" id="course_batch_id" value="'.$group_id.'" />'; ?>
                    <a id="get_batch_course_stats" class="button">
                        <?php _e('Get Stats','wplms-batches'); ?>
                    </a>
                </div>
            </div>


            <?php
        }
        function moderator_statistics($group_id){
            global $wpdb, $bp;
            $courses = groups_get_groupmeta($group_id,'batch_course',false);
            if(empty($courses)){
                echo '<div class="message">'.__('No Courses connected to Batch','wplms-batches').'</div>';
                return;
            }

            ?>

                <?php
            $members = BP_Groups_Member::get_group_member_ids($group_id);

        }

        function get_batch_course_stats(){


            $course_id = $_POST['course_id'];
            $batch_id = $_POST['batch_id'];
            $user_id = get_current_user_ID();
            global $wpdb, $bp;
            if ( !isset($_POST['security']) || !wp_verify_nonce($_POST['security'],'batch_course_security'.$batch_id) || !is_numeric($course_id) || !is_numeric($batch_id)){
                _e('Security check Failed. Contact Administrator.','wplms-batches');
                die();
            }

            echo '<h5>'.sprintf(__('Statistics for Course %s','wplms-batches'),'<a href="'.get_permalink($course_id).'" target="_blank">'.get_the_title($course_id)).'</a></h5>';

            if(function_exists('bp_course_get_curriculum_quizes')){
                global $wpdb, $bp;
                $quiz_ids = bp_course_get_curriculum_quizes($course_id);

                    // Sort Quiz id  
                    sort($quiz_ids);
                    $tlength = count($quiz_ids);
                    for($x = 0; $x < $tlength; $x++);

                        /*
                        $flag = 0;
                        if(!empty($quiz_ids)){
                            $flag++;
                            }
                         */  

                    ?>

                     <table class="course_leaderboard table table-hover table-bordered" id="table_course">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th valign="top">
                                    <?php _e('Student','wplms-batches') ?> <br>
                                </th>
                                <th>
                                    Progression (%)
                                </th>
                                <th>
                                    QCM terminés
                                </th>
                                <th>
                                    Sessions terminées
                                </th>
                                <?php

                             //if($flag >= 1){
                                foreach($quiz_ids as $quiz_id){
                                    echo '<th><a href="'.get_permalink($quiz_id).'" target="_blank">'.get_the_title($quiz_id).'</a></th>';                          
                                    }
                        //}

                        ?>
                            </tr>
                        </thead>
                        <tbody>

                            <?php
                         $members = BP_Groups_Member::get_group_member_ids($batch_id);

                         $i=1;
                         foreach($members as $member_id){


                             foreach ($courses as $course_id)
                                global $wpdb, $bp;
                                $progress = bp_course_get_user_progress($member_id,$course_id);

                        /*
                         * Process for QCM Value
                         */                     

                         global $wpdb, $bp;
                         $marks=$wpdb->get_results(sprintf("
                         SELECT rel.post_id as id, rel.meta_value as val
                             FROM {$wpdb->posts} AS posts
                             LEFT JOIN {$wpdb->postmeta} AS rel ON posts.ID = rel.post_id
                             WHERE   posts.post_type   = 'quiz'
                             AND     posts.post_title LIKE '%%QCM'
                             AND     posts.post_status   = 'publish'
                             AND     rel.meta_key   = %d
                             AND     rel.meta_value >= 0
                             ", $member_id)
                         );
                         if (is_array($marks)) {
                             foreach ($marks as $k=>$mark) {
                                 $user_marks[] = $mark->val;
                             }
                         }
                             else {
                                 $user_marks = array();
                             }
                             if (is_array($marks))
                             $value = count($marks);

                             if ( $title )
                             $label = $title;
                             else
                             $label = __('Quizzes Completed', 'wplms-dashboard');

                         if (is_array($user_marks)) {
                             foreach ($user_marks as $i=>$mark){
                                 if ($i<11) {
                                     if (!$i)
                                         $marks_string = $mark;
                                         else
                                         $marks_string .= ','.$mark;
                                 }
                             }  
                         }

                        if (!is_numeric($value))
                         $value = 0;


                         /*
                         * Process for Personality Value
                         */   


                         global $wpdb;
                         $marks=$wpdb->get_results(sprintf("
                         SELECT rel.post_id as id, rel.meta_value as val
                             FROM {$wpdb->posts} AS posts
                             LEFT JOIN {$wpdb->postmeta} AS rel ON posts.ID = rel.post_id
                             WHERE   posts.post_type   = 'quiz'
                             AND     posts.post_title LIKE '%%Test de Personnalité'
                             AND     posts.post_status   = 'publish'
                             AND     rel.meta_key   = %d
                             AND     rel.meta_value >= 0
                             ", $member_id)
                         );


                         if (is_array($marks)) {
                             foreach ($marks as $k=>$mark) {
                                 $user_marks[] = $mark->val;
                             }
                         }
                             else {
                                 $user_marks = array();
                             }

                         if ( $title )
                             $label2 = $title;
                             else
                             $label2 = __('Personality Tests Completed', 'wplms-dashboard');

                         if (is_array($marks))
                             $value2 = count($marks);

                         if (is_array($user_marks)) {
                             foreach ($user_marks as $i=>$mark){
                                 if ($i<11) {
                                     if (!$i)
                                         $marks_string = $mark;
                                         else
                                         $marks_string .= ','.$mark;
                                 }
                             }  
                         }


                         if (!is_numeric($value2))
                         $value2 = 0;


                         ?>


                                <tr>
                                    <td>
                                        <?php echo $i ?>
                                    </td>
                                    <td>
                                        <?php echo bp_core_get_user_displayname($member_id) ?>
                                    </td>
                                    <td>
                                        <?php echo $progress ?> %
                                    </td>
                                    <td>
                                        <?php echo $value ?>
                                    </td>
                                    <td>
                                        <?php echo $value2 ?>
                                    </td>

                                    <?php
                                        //if($flag >= 1){
                                        foreach($quiz_ids as $quiz_id){
                                            echo '<td>'.get_post_meta($quiz_id,$member_id,true).'</td>';
                                                }
                                        //}
                                        $sum=0;           
                                        $status = bp_course_get_user_course_status($member_id,$course_id);
                                        if($status > 3){
                                            $marks = get_post_meta($course_id,$member_id, true);
                                            $sum += $marks;
                                            echo '<td>'.$marks.'</td>';    
                                        }else{
                                            echo '<td>'._x('N.A','Marks not available in batch statisitcs leaderboard','wplms-batches').'</td>';
                                        }
                                        echo '</tr>';
                                        $i++;
                                        }
                                    ?>
                                </tr>

                                <?php }?>
                        </tbody>
                    </table>

                    <!-- END Custom Code KVDC -->
                    <?php
        die();
        } 


    } // END of Class

add_action( 'bp_init','wplms_batch_statistics_register_group_extension'); 
function wplms_batch_statistics_register_group_extension(){ 
    bp_register_group_extension( 'BP_Group_Course_Batch_Statistics' );
}
endif;

I use buddypress. and my wp_enqueue_script it side must be put after wp_enqueue_scripts and login_enqueue_scripts.

Before, Wordpress update, all my function work well!!

UPDATE

Here additional code

2
  • You're calling the function as if it is inside the class. array($this,'get_batch_course_stats') But I don't see that function in the class.
    – shanebp
    Commented Sep 23, 2017 at 15:35
  • Please see my update Commented Sep 25, 2017 at 5:11

1 Answer 1

-1

UPDATE:

After I saw an error massage it's all because unsupported operand type error.

  $sum += $marks;

its because store wrong type of value, I change to

  $sum->marks;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.