Filter tin-canny-learndash-reporting

uotc_quiz_report_user_score

Filters the user's score before it's displayed in the quiz report.

add_filter( 'uotc_quiz_report_user_score', $callback, 10, 4 );

Description

Filters the user's calculated quiz score before it's displayed in the group quiz report. Developers can modify the score, add custom data, or adjust the reporting logic based on user or quiz details. Fires after the score is initially determined.


Usage

add_filter( 'uotc_quiz_report_user_score', 'your_function_name', 10, 4 );

Parameters

$this (mixed)
This parameter likely represents the current object or instance that the hook is being applied to, providing access to its properties and methods.
$user_data (mixed)
This parameter represents the current object or class instance.
$quiz (mixed)
This parameter contains the user's data, likely including their ID, login, email, and name.
$score_type (mixed)
This parameter contains the quiz object for which the user's score is being reported.

Return Value

The filtered value.


Examples

<?php
/**
 * Modify the quiz score displayed in the user report.
 *
 * This example demonstrates how to modify the calculated quiz score.
 * For instance, we might want to round the score to two decimal places
 * or apply a specific bonus/penalty if certain conditions are met.
 *
 * @param float $quiz_score    The original calculated quiz score.
 * @param object $user_data    The WordPress user object for the current user.
 * @param object $quiz         The quiz object.
 * @param string $score_type   The type of score being retrieved (e.g., 'percentage', 'raw').
 * @return float The modified quiz score.
 */
add_filter( 'uotc_quiz_report_user_score', function( $quiz_score, $user_data, $quiz, $score_type ) {

	// Example: If the score type is percentage, let's ensure it's always displayed with two decimal places.
	if ( 'percentage' === $score_type ) {
		$quiz_score = round( $quiz_score, 2 );
	}

	// Example: Apply a 5% bonus to users with a specific role.
	if ( user_can( $user_data->ID, 'administrator' ) ) {
		$quiz_score = $quiz_score * 1.05;
		// Ensure the score doesn't exceed 100% if it's a percentage.
		if ( 'percentage' === $score_type && $quiz_score > 100 ) {
			$quiz_score = 100;
		}
	}

	return $quiz_score;

}, 10, 4 );

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

// Add the quiz data to the results.
					$results[] = array(
						'ID'         => $user_data->ID,
						'user_name'  => $user_data->user_login,
						'user_email' => $user_data->user_email,
						'first_name' => $user_data->first_name,
						'last_name'  => $user_data->last_name,
						'quiz_score' => apply_filters( 'uotc_quiz_report_user_score', $this->get_quiz_score( $quiz, $score_type ), $user_data, $quiz, $score_type ),
						'quiz_modal' => $this->get_quiz_modal_link( $user_data->ID, $quiz ),
						'quiz_date'  => $this->get_quiz_date_column( $quiz ),
					);

					$user_attempt_added = true;
				}


Scroll to Top