WordPress.org

Ready to get started?Download WordPress

Forums

teachPress
[resolved] PHP conditional test for TeachPress access (3 posts)

  1. Elder_Richmond
    Member
    Posted 1 year ago #

    Can anyone help write a simple conditional test in PHP that will be true or false based upon whether the actively logged in WordPress user is successfully enrolled in a particular course or not.

    In other words, Boolean true or false based upon if the user is enrolled in TeachPress's Course ID=1.

    NOTE, THE TPPOST SHORTCUT YIELDS THE WRONG RESULTS:

    `[tppost id="1"]
    You can only see this text if registered for Course ID 1
    [/tppost]`

    I am trying to display different executable buttons (embeded as shortcode from another plugin) based upon php conditional tests. I am unable to utilize the TPPOST shortcut for this scenario because the TPPOST simply outputs the text string of the said embedded button shortcode to the screen. So, instead of seeing a functional button, I see the shortcode text string. Hence, TPPOST shortcode is functioning successfully as it is designed, but doesn't allow me to process the code within the embedded shortcode for the said buttons.

    Hence, I need assistance finding a PHP test parameter that I can use to test if a user is registered for a course (e.g. Course ID=1). Perhaps, someone can specify an appropriate parameter to go with the current_user_can scope, such as:

    <?php if (!current_user_can('teachpress_user')) ...

    However, 'TEACHPRESS_USER' is the wrong parameter.

    Thanks for any assistance.

    http://wordpress.org/extend/plugins/teachpress/

  2. Michael
    Member
    Posted 1 year ago #

    The following function will be a part of the next major version and implements this test:

    /**
     * Return true if the user is subscribed in the course or false of not
     * @param integer course_id
     * @param boolean consider_subcourses   -->
     * @return boolean
     */
    function tp_is_user_subscribed ($course_id, $consider_subcourses = false) {
        global $wpdb;
        global $teachpress_signup;
        global $teachpress_courses;
        global $user_ID;
        get_currentuserinfo();
        $course_id = intval($course_id);
        if ( $course_id == 0 ) {
            return false;
        }
        // simple case
        if ( $consider_subcourses == false ) {
            $test = $wpdb->query("SELECT con_id FROM $teachpress_signup WHERE course_id = '$course_id' AND wp_id = '$user_ID' AND waitinglist = '0'");
        }
        // consider subcourses
        if ( $consider_subcourses == true ) {
            $where = "";
            $courses = $wpdb->get_results("SELECT course_id FROM $teachpress_courses WHERE parent = '$course_id'");
            foreach ( $courses as $row ) {
                $where = $where == "" ? "course_id = '$row->course_id'" : $where . " OR course_id = '$row->course_id'";
            }
            if ( $where != "" ) {
                $where = " WHERE wp_id = '$user_ID' AND waitinglist = '0' AND ( $where OR course_id = '$course_id' )";
                $test = $wpdb->query("SELECT con_id FROM $teachpress_signup $where");
            }
            // Fallback if there are no subcourses
            else {
                $test = $wpdb->query("SELECT con_id FROM $teachpress_signup WHERE course_id = '$course_id' AND wp_id = '$user_ID' AND waitinglist = '0'");
            }
        }
    
        if ( $test >= 1 ) {
            return true;
        }
        return false;
    }
  3. Elder_Richmond
    Member
    Posted 1 year ago #

    Thanks, Michael.

    You are a true godsend. Your solution is very empowering because it gives us the ability to perform critical conditional testing with your plugin.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic