Bug: is_instructor function fails when you try to check when gloabl post not set

Home Forums Legacy Support Support queries How-to & Troubleshooting Bug: is_instructor function fails when you try to check when gloabl post not set

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #240955
    Makarand Mane
    Spectator
    @logan I think its Bug: is_instructor function fails when you try to check instructor for course by passing course id (when gloabl post not set or i mean out of WP loop) vibe-course-module.zip\vibe-course-module\includes\bp-course-template.php Passing $id is useless in this function as it is doing nothing with that,
    function is_instructor($id=NULL){
        
        if(!is_user_logged_in())
            return false;
    
        global $post;
        if(!isset($id)){
            $id= $post->ID;
        }
        $uid = bp_loggedin_user_id();
        $authors=array($post->post_author);
        $authors = apply_filters('wplms_course_instructors',$authors,$post->ID);
        if(in_array($uid,$authors) )
            return true;
    
        return false;
    }
    So I think It should be like this.
    function is_instructor( $id = NULL ){
        
        if(!is_user_logged_in())
            return false;
    
        if( $id ){
            $post = get_post( $id );
        }else{
            global $post;
        }
        $uid        = bp_loggedin_user_id();
        $authors    = array($post->post_author);
        $authors    = apply_filters('wplms_course_instructors', $authors, $post->ID);
        if( in_array( $uid, $authors ) )
            return true;
    
        return false;
    }
    Well then I can use it like to check instructor anywhere.. is_instructor( $course_id);
    #241009
    Scott Lang
    Moderator
    refer this code to check user is course instructor:
    function check_user_is_instructor($course_id,$user_id){
    if(!empty($user_id) && !empty($course_id)){
    $instructor_ids = apply_filters('wplms_course_instructors',get_post_field('post_author', $course_id),$course_id);
    if(!is_array($instructor_ids)){
    $instructor_ids = array($instructor_ids);
    }
    if(empty($instructor_ids)){
    $instructor_ids = array();
    }
    if(in_array($user_id, $instructor_ids)){
    return true;
    }
    }
    return false;
    }
    #241024
    Makarand Mane
    Spectator
    @scott Thank for this code. Also check my code. I was trying to tell you that inside is_instructor function, parameter $id is useless as it was not used if it is not NULL or set. You should make that useful in next version. Also Is this function check_user_is_instructor available in WPLMS or should I add this function in my code?
    #241128
    Scott Lang
    Moderator
    You can use our one. That is not a global function and not yet use in wplms. we have created it only for you.  
    #241144
    Makarand Mane
    Spectator
    Thank you
    #241146
    logan
    Member
    welcome.
Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Bug: is_instructor function fails when you try to check when gloabl post not set’ is closed to new replies.