Filter tin-canny-learndash-reporting

uotc_rest_api_get_quiz_data

Filters the data retrieved for a quiz, allowing modification before it's returned by the REST API.

add_filter( 'uotc_rest_api_get_quiz_data', $callback, 10, 2 );

Description

Filters the quiz data returned by the REST API for group quiz reports. Developers can modify the quiz results and group ID before they are sent to the frontend. This hook is ideal for customizing how quiz data is displayed in reports or for adding/altering information related to quiz attempts and scores.


Usage

add_filter( 'uotc_rest_api_get_quiz_data', 'your_function_name', 10, 2 );

Parameters

$results (mixed)
This parameter holds the data that will be returned by the REST API endpoint for quiz data.
$group_id (mixed)
This parameter holds the data structure for the quiz report results being processed.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to use the 'uotc_rest_api_get_quiz_data' filter.
 *
 * This filter allows you to modify the quiz data before it's returned by the REST API.
 * In this example, we'll add a custom field to each quiz result if the user has
 * not attempted the quiz.
 *
 * @param mixed $results   The original quiz data.
 * @param mixed $group_id  The ID of the group the quiz belongs to.
 * @return mixed The modified quiz data.
 */
add_filter( 'uotc_rest_api_get_quiz_data', 'my_custom_quiz_data_modifier', 10, 2 );

function my_custom_quiz_data_modifier( $results, $group_id ) {
	// Check if $results is an array and not empty.
	if ( ! is_array( $results ) || empty( $results ) ) {
		return $results;
	}

	// Check if $group_id is provided and is a valid group ID.
	if ( ! $group_id || ! is_numeric( $group_id ) ) {
		return $results;
	}

	// Loop through each quiz result and add custom data.
	foreach ( $results as &$quiz_data ) {
		// Example: If the quiz has not been attempted, add a custom status.
		if ( isset( $quiz_data['quiz_status'] ) && 'not_attempted' === $quiz_data['quiz_status'] ) {
			$quiz_data['custom_message'] = sprintf(
				__( 'User has not attempted quiz "%s" in group %d.', 'my-text-domain' ),
				$quiz_data['quiz_title'] ?? 'Unknown Quiz',
				$group_id
			);
		}

		// Example: Modify an existing field based on conditions.
		if ( isset( $quiz_data['quiz_score'] ) && is_numeric( $quiz_data['quiz_score'] ) ) {
			if ( $quiz_data['quiz_score'] < 50 ) {
				$quiz_data['quiz_score_category'] = 'Needs Improvement';
			} else {
				$quiz_data['quiz_score_category'] = 'Passing';
			}
		}
	}
	unset( $quiz_data ); // Unset the reference after the loop.

	return $results;
}

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/frontend-reports/group-quiz-report.php:507

'quiz_score' => apply_filters( 'uotc_quiz_report_quiz_score_not_attempted_label', __( 'Not Attempted', 'uncanny-learndash-reporting' ), $user_data ),
					'quiz_modal' => apply_filters( 'uotc_quiz_report_quiz_modal_not_attempted_label', __( 'Not Attempted', 'uncanny-learndash-reporting' ), $user_data ),
					'quiz_date'  => apply_filters( 'uotc_quiz_report_quiz_date_not_attempted_label', __( 'Not Attempted', 'uncanny-learndash-reporting' ), $user_data ),
				);
			}
		}

		$results = apply_filters(
			'uotc_rest_api_get_quiz_data',
			$results,
			array(
				'group_id'   => $group_id,
				'quiz_id'    => $quiz_id,
				'score_type' => $score_type,
			)


Scroll to Top