Filter tin-canny-learndash-reporting

tc_api_get_userSingleCoursesOverviewTable

Filters the user's single courses overview table data before it's returned, allowing modification of course rows and data.

add_filter( 'tc_api_get_userSingleCoursesOverviewTable', $callback, 10, 3 );

Description

Filter the JSON response for the user's single courses overview table. Developers can modify the `$json_return` array to alter the data, message, or success status before it's sent back. This hook fires after fetching and formatting course data for a specific user.


Usage

add_filter( 'tc_api_get_userSingleCoursesOverviewTable', 'your_function_name', 10, 3 );

Parameters

$json_return (mixed)
This parameter holds the JSON response array that will be returned by the API.
$user_id (mixed)
This parameter holds the JSON response structure which will be returned by the API.
$rows (mixed)
This parameter holds the ID of the user whose course overview data is being requested.

Return Value

The filtered value.


Examples

add_filter( 'tc_api_get_userSingleCoursesOverviewTable', 'my_custom_user_courses_overview_table_data', 10, 3 );

/**
 * Example of modifying the user single courses overview table data.
 * This function demonstrates how to add extra data or alter the existing data
 * before it's returned by the filter.
 *
 * @param array $json_return The default JSON return array.
 * @param int   $user_id     The ID of the user.
 * @param array $rows        An array of rows for the table.
 * @return array Modified JSON return array.
 */
function my_custom_user_courses_overview_table_data( $json_return, $user_id, $rows ) {
    // Let's add a custom message if the user has no courses displayed.
    if ( empty( $json_return['data'] ) ) {
        $json_return['message'] = sprintf(
            esc_html__( 'No course data found for user ID %d.', 'your-text-domain' ),
            $user_id
        );
        $json_return['success'] = false; // Mark as unsuccessful if no data.
    } else {
        // Example: Add an extra field to each course row if data exists.
        // This assumes $json_return['data'] is an array of course data,
        // and each course has a structure that can be modified.
        // For demonstration, let's assume each item in $json_return['data']
        // is an array representing a course and has a 'course_id'.
        foreach ( $json_return['data'] as &$course_data ) {
            if ( isset( $course_data['course_id'] ) ) {
                // Let's add a placeholder for a custom completion percentage.
                // In a real scenario, you'd fetch or calculate this.
                $course_data['custom_completion_status'] = 'N/A';
            }
        }
        unset( $course_data ); // Unset the reference after the loop.

        $json_return['message'] = esc_html__( 'Course overview data retrieved successfully.', 'your-text-domain' );
        $json_return['success'] = true;
    }

    // You can also log the user ID and rows for debugging purposes.
    // error_log( 'Filtering tc_api_get_userSingleCoursesOverviewTable for user: ' . $user_id );
    // error_log( 'Rows passed: ' . print_r( $rows, true ) );

    return $json_return;
}

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-table-data.php:276

public static function user_single_courses_overview_table() {
		$json_return = array(
			'message' => '',
			'success' => false,
			'data'    => array(),
		);

		if ( ! ultc_filter_has_var( 'userId', INPUT_POST ) ) {
			$json_return['message'] = 'userId or rowsIds not set';
			return $json_return;
		}

		$user_id     = absint( ultc_filter_input( 'userId', INPUT_POST ) );
		$rows        = array();
		$posted_rows = ultc_filter_input_array( 'rows', INPUT_POST );
		if ( is_null( $posted_rows ) || false === $posted_rows ) {
			$posted_rows = json_decode( stripslashes( ultc_filter_input( 'rows', INPUT_POST ) ), true );
		}

		if ( is_array( $posted_rows ) ) {
			foreach ( $posted_rows as $row ) {
				$rows[ absint( $row['rowId'] ) ] = absint( $row['ID'] );
			}
		}
		$json_return['success'] = true;
		$json_return['user_id'] = $user_id;
		$json_return['data']    = self::get_user_single_overview( $user_id, $rows );

		return apply_filters( 'tc_api_get_userSingleCoursesOverviewTable', $json_return, $user_id, $rows ); // phpcs:ignore WordPress.NamingConventions.ValidHookName
	}

Scroll to Top