Filter tin-canny-learndash-reporting

uo_get_dashboard_data

Filters the course overview data Filters the course overview data before it is displayed on the dashboard.

add_filter( 'uo_get_dashboard_data', $callback, 10, 1 );

Description

Fires after the core course overview data has been gathered. Developers can use this filter to modify or extend the course data object before it's returned. This is useful for adding custom metrics or filtering the data based on specific criteria. The `$dashboard_data_object` contains the entire dataset for the course overview.


Usage

add_filter( 'uo_get_dashboard_data', 'your_function_name', 10, 1 );

Parameters

$dashboard_data_object (mixed)

Return Value

The filtered value.


Examples

add_filter( 'uo_get_dashboard_data', 'my_custom_dashboard_data_filter', 10, 1 );

/**
 * Example filter to modify the dashboard data object.
 * This function demonstrates how to add a custom count of active courses for each user.
 *
 * @param object $dashboard_data_object The original dashboard data object.
 * @return object The modified dashboard data object.
 */
function my_custom_dashboard_data_filter( $dashboard_data_object ) {

    // Ensure the dashboard data object is in the expected format, or handle gracefully.
    if ( ! isset( $dashboard_data_object->users ) || ! is_array( $dashboard_data_object->users ) ) {
        return $dashboard_data_object;
    }

    // Iterate through each user in the dashboard data.
    foreach ( $dashboard_data_object->users as &$user_data ) {
        // Assuming $user_data is an object with at least a 'user_id' property.
        if ( isset( $user_data->user_id ) ) {
            $user_id = $user_data->user_id;

            // Use LearnDash's functions to get courses associated with the user.
            // Adjust query parameters as needed for your specific definition of "active".
            $user_courses = learndash_get_users_courses( $user_id, true ); // 'true' for enrolled courses

            // Count the number of enrolled courses.
            $active_course_count = count( $user_courses );

            // Add the new data point to the user's data object.
            $user_data->active_course_count = $active_course_count;
        }
    }

    // Return the modified dashboard data object.
    return $dashboard_data_object;
}

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/reporting/learndash/courses-users-report/build-report-data.php:248

self::$all_user_ids = array();
		self::$user_data = array();
		self::$groups_list = array();

		/**
		 * Filters the course overview data
		 */
		return apply_filters( 'uo_get_dashboard_data', self::$dashboard_data_object );
	}

	/**
	 * Inject group leader id
	 *
	 * @param $user_ids
	 * @param $current_user_id


Scroll to Top