Filter uncanny-automator

automator_learndash_user_quiz_category_scores_token

Filters the calculated user quiz category scores before they are potentially used as tokens.

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

Description

Filters the data array containing quiz category scores for a specific user and quiz before it's returned by the LearnDash integration. Developers can modify this array to alter the displayed category score results, add custom data, or exclude certain categories.


Usage

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

Parameters

$result (mixed)
This parameter contains the filtered array of quiz category scores for the user and quiz.
$user_id (mixed)
This parameter contains the calculated category scores for the user's quiz attempts, which can be filtered or modified by other plugins.
$quiz_post_id (mixed)
This parameter represents the unique identifier of the WordPress user whose quiz category scores are being retrieved.

Return Value

The filtered value.


Examples

<?php
/**
 * Example: Modify the user quiz category scores before they are returned.
 * This function might add a custom status or modify an existing score based on some logic.
 *
 * @param array $result The original array containing 'result' and 'scores'.
 * @param int   $user_id The ID of the user who took the quiz.
 * @param int   $quiz_post_id The ID of the LearnDash quiz.
 * @return array The modified $result array.
 */
add_filter( 'automator_learndash_user_quiz_category_scores_token', function( $result, $user_id, $quiz_post_id ) {

    // Check if the 'scores' key exists and is an array.
    if ( isset( $result['scores'] ) && is_array( $result['scores'] ) ) {

        // Example: Add a custom 'overall_performance' key to the scores.
        // This is a hypothetical calculation. In a real scenario, this would
        // involve more complex logic or fetching additional data.
        $total_score_sum = 0;
        $total_possible_score = 0;

        foreach ( $result['scores'] as $category_id => $category_data ) {
            if ( isset( $category_data['score'] ) && isset( $category_data['possible_score'] ) ) {
                $total_score_sum += $category_data['score'];
                $total_possible_score += $category_data['possible_score'];
            }
        }

        if ( $total_possible_score > 0 ) {
            $overall_percentage = ( $total_score_sum / $total_possible_score ) * 100;
            $result['scores']['overall_performance'] = array(
                'score' => round( $overall_percentage, 2 ),
                'label' => __( 'Overall Performance', 'your-text-domain' ),
                'possible_score' => 100, // Representing a percentage as possible score
            );
        }

        // Example: Add a custom status based on the total score.
        if ( $total_score_sum > 50 ) {
            $result['result']['custom_status'] = __( 'Passed', 'your-text-domain' );
        } else {
            $result['result']['custom_status'] = __( 'Needs Improvement', 'your-text-domain' );
        }
    }

    return $result;
}, 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/integrations/learndash/tokens/ld-tokens.php:948

public function get_user_quiz_category_scores( $user_id, $quiz_post_id ) {

		$user_quiz_data = $this->get_quiz_data( 'data', $user_id, $quiz_post_id, array() );
		if ( empty( $user_quiz_data ) ) {
			return array();
		}

		$quiz_pro_id  = $user_quiz_data['pro_quizid'];
		$reference_id = $user_quiz_data['statistic_ref_id'];

		$statistics = $this->get_user_quiz_statistics( $reference_id, $quiz_post_id, $quiz_pro_id );
		if ( empty( $statistics ) ) {
			return array();
		}

		$scores = array();
		foreach ( $statistics as $statistic ) {
			$category_id = (int) $statistic->getCategoryId();

			// Add new category if not set.
			if ( ! isset( $scores[ $category_id ] ) ) {
				$scores[ $category_id ] = array(
					'id'           => $category_id,
					'name'         => $category_id ? $statistic->getCategoryName() : esc_html__( 'No category', 'learndash' ), // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
					'points'       => 0,
					'total_points' => 0,
					'score'        => 0,
				);
			}

			// Points Tally.
			$scores[ $category_id ]['points']       += $statistic->getPoints();
			$scores[ $category_id ]['total_points'] += $statistic->getGPoints();
		}

		// Calculate the score & generate output.
		$results = '';
		foreach ( $scores as $category_id => $score ) {
			$scores[ $category_id ]['score'] = $score['total_points'] ? ( $score['points'] / $score['total_points'] ) * 100 : 0;
			// Generate String Output.
			$results .= sprintf(
			/* Translators: %1$s = category name, %2$s = score.*/
				'<div><strong>%s:</strong> %s</div>',
				$scores[ $category_id ]['name'],
				round( $scores[ $category_id ]['score'], 2 ) . '%'
			);
		}

		$result = array(
			'result' => $results,
			'scores' => $scores,
		);

		return apply_filters( 'automator_learndash_user_quiz_category_scores_token', $result, $user_id, $quiz_post_id );
	}


Scroll to Top