'WP Learndash plugin, get userdata related to courses and lessons

Hi I am using learndash Wordpress plugin. I want to get the data related to a user tht how many courses he is enrolled in and how many has he completed. Is there a way to check this? does learndash provide any solution for this or should I query data myself?

Any help is appreciated. Thanks in advance. Please ask for any more details if you want.



Solution 1:[1]

I found that all is stored inside the table learndash_user_activity and you can query that table to get user stats.

For example I get the list of the in-progress users for a given course with the following query:

public static function get_inprogress_users_for_course( $course_id )
{
    global $wpdb;
    
    if( empty( $course_id ) ) return [];
                                    
    $results = $wpdb->get_results( "SELECT `user_id` FROM `" . $wpdb->prefix . "learndash_user_activity` "
                                    ."WHERE `course_id` = '" . intval( $course_id ) . "' "
                                    ."AND `activity_type` = 'lesson' "
                                    ."GROUP BY `user_id`" );
                                    
    return $results;
}

In the same way, you can get all the IDs of the users ever enrolled to a course changing activity_type from 'lesson' to 'course' in the query, and if you want to only get enrolled course by a user, you can add the user_id to the query, like this:

public static function get_courses_for_user( $user_id )
{
    global $wpdb;
    
    if( empty( $course_id ) ) return [];
                                    
    $results = $wpdb->get_results( "SELECT * FROM `" . $wpdb->prefix . "learndash_user_activity` "
                                    ."WHERE `user_id` = '" . intval( $user_id ) . "' "
                                    ."AND `activity_type` = 'course' "
                                    ."GROUP BY `course_id`" );
                                    
    return $results;
}

I know this is not exactly what you were searching for, but it could still be useful.

Solution 2:[2]

You can use the following to get all course ID's the current user is currently enrolled to:

learndash_user_get_enrolled_courses(get_current_user_id())

Solution 3:[3]

You can return anything using wp_query. Try this:

function wpso49370180_get_course_name($courseid) {
global $wpdb;
$user_id = the_author_meta( 'ID' ); //alt method below
$query_course = "SELECT post_title 
                 FROM wp_posts 
                 WHERE post_type = 'sfwd-courses'   
                 AND post_status NOT IN ( 'trash','auto-draft','inherit' ) 
                 AND post_author='$user_id' LIMIT 10";

      return $wpdb->get_var($query_course);
}

You will need to either know the user_id or get it from the post (sfwd-quiz, sfwd-course, sfwd-lesson) -see below.

The data you want can be all (*) You will have to do a meta_query if you want deeper data that is not in the post_type tables.

/**
 * Gets the author of the specified post. Can also be used inside the loop
 * to get the ID of the author of the current post, by not passing a post ID.
 * Outside the loop you must pass a post ID.
 *
 * @param int $post_id ID of post
 * @return int ID of post author
*/
function wpso49370180_get_author( $post_id = 0 ){
    $post = get_post( $post_id );
        return $post->post_author;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 tradesouthwest