Filter tin-canny-learndash-reporting

tc_api_get_courses_overview

Filters the JSON response before it's returned for the courses overview API request.

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

Description

Filters the course overview data returned by the API. Developers can modify the `$json_return` array before it's sent, allowing customization of course progress information, labels, and links. This hook fires after initial data retrieval and before the response is finalized.


Usage

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

Parameters

$json_return (mixed)
This parameter holds the array of data that will be returned by the `tc_api_get_courses_overview` filter, containing labels, links, messages, and success status.

Return Value

The filtered value.


Examples

/**
 * Example of how to modify the course overview data returned by the tc_api_get_courses_overview filter.
 * This example adds a custom field 'completion_rate_percentage' to each course in the data array.
 *
 * @param array $json_return The original array of data returned by the filter.
 * @return array The modified array of data.
 */
add_filter( 'tc_api_get_courses_overview', function( $json_return ) {

	// Ensure the 'data' key exists and is an array before proceeding.
	if ( isset( $json_return['data'] ) && is_array( $json_return['data'] ) ) {

		// Loop through each course in the 'data' array.
		foreach ( $json_return['data'] as &$course_data ) {
			// Calculate completion rate if lesson and overall progress are available.
			if ( isset( $course_data['lesson_progress'] ) && isset( $course_data['overall_progress'] ) ) {
				$completion_rate = ( $course_data['overall_progress'] / $course_data['lesson_progress'] ) * 100;
				// Add the calculated completion rate to the course data.
				$course_data['completion_rate_percentage'] = round( $completion_rate, 2 );
			} else {
				// Set a default value if progress data is not available.
				$course_data['completion_rate_percentage'] = 0;
			}
		}
	}

	// Always return the modified (or original if conditions weren't met) $json_return array.
	return $json_return;

}, 10, 1 ); // Priority 10, accepts 1 argument.

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/endpoint-courses-overview.php:45

public static function get_courses_overview( WP_REST_Request $request ) {

		$json_return = array();

		$json_return['learnDashLabels'] = MiscFunctions::get_labels();
		$json_return['links']           = MiscFunctions::get_links();

		$json_return['message'] = '';
		$json_return['success'] = true;
		$json_return['data']    = Config::profile_function(array(__CLASS__, 'course_progress_data'));

		return apply_filters( 'tc_api_get_courses_overview', $json_return );
	}


Scroll to Top