Filter tin-canny-learndash-reporting

tc_api_get_courseSingleTable

Filters course data for the single course table before it is returned to the API.

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

Description

Filters the JSON data returned for a single course's user table report. Developers can modify the success status, message, or the actual course data array before it's sent to the client. Useful for custom data manipulation or error handling.


Usage

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

Parameters

$json_return (mixed)
This parameter is the JSON response array that will be filtered, initially containing default success and message keys.
$course_id (mixed)
This parameter is used to hold the JSON response that will be returned by the API call, including status messages and the actual data.
$rows (mixed)
This parameter contains the ID of the course for which the single table data is being retrieved.

Return Value

The filtered value.


Examples

/**
 * Example filter callback for tc_api_get_courseSingleTable.
 *
 * This callback adds an extra field to the course data, indicating if the course
 * has any enrolled users.
 *
 * @param array $json_return The original JSON return data.
 * @param int   $course_id   The ID of the course.
 * @param array $rows        An array of row IDs and their corresponding post IDs.
 * @return array The modified JSON return data.
 */
function my_custom_course_single_table_data( $json_return, $course_id, $rows ) {
	// Only proceed if the original call was successful and has data.
	if ( $json_return['success'] && ! empty( $json_return['data'] ) ) {

		// Assume $json_return['data'] is an array of course user data.
		// Let's add a custom field to each user in the data.
		foreach ( $json_return['data'] as &$user_data ) {
			// Example: Check if the user has completed a specific lesson.
			// Replace 'your_lesson_id' with an actual lesson ID relevant to your logic.
			$lesson_completed = false;
			if ( isset( $user_data['lessons'] ) && is_array( $user_data['lessons'] ) ) {
				foreach ( $user_data['lessons'] as $lesson ) {
					if ( $lesson['id'] == 'your_lesson_id' && $lesson['status'] == 'completed' ) {
						$lesson_completed = true;
						break;
					}
				}
			}
			$user_data['custom_lesson_status'] = $lesson_completed;
		}
		unset( $user_data ); // Unset the reference to avoid potential issues.

		// You could also modify the top-level message or success status.
		// $json_return['message'] = 'Course data enriched with custom status!';
	}

	return $json_return;
}
add_filter( 'tc_api_get_courseSingleTable', 'my_custom_course_single_table_data', 10, 3 );

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:82

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

		//      $json_return['success'] = false;
		//      $json_return['data']    = $_POST; // phpcs:ignore WordPress.Security

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

			$course_id = absint( ultc_filter_input( 'courseId', INPUT_POST ) );
			$rows      = array();
			$post_rows = array();

			// phpcs:disable WordPress.Security
		if ( is_string( $_POST['rows'] ) ) {
			$post_rows = json_decode( stripslashes( ultc_filter_input( 'rows', INPUT_POST ) ), true );
		} elseif ( is_array( $_POST['rows'] ) ) {
			$post_rows = ultc_filter_input_array( 'rows', INPUT_POST );
		}

		if ( ! empty( $post_rows ) ) {
			foreach ( $post_rows as $row ) {
				if ( isset( $row['rowId'], $row['ID'] ) ) {
					$rows[ absint( $row['rowId'] ) ] = absint( $row['ID'] );
				}
			}
		}
			// phpcs:enable WordPress.Security

			$json_return['success'] = true;
			$json_return['data']    = self::get_course_single_overview( $course_id, $rows );

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

Scroll to Top