Filter tin-canny-learndash-reporting

uo_tincanny_reporting_questions_get_question_total_percent

Filters the percentage of correctly answered questions, allowing modification before display in reporting.

add_filter( 'uo_tincanny_reporting_questions_get_question_total_percent', $callback, 10, 5 );

Description

Filters the calculated percentage of correct answers for a specific question. Developers can modify the formatted output, question ID, correct answer count, total attempts, or the final percentage. Useful for custom display or altering the scoring logic.


Usage

add_filter( 'uo_tincanny_reporting_questions_get_question_total_percent', 'your_function_name', 10, 5 );

Parameters

$formatted (mixed)
This parameter is the formatted output of the percentage of correct answers for a given question.
$question_id (mixed)
This parameter is a boolean value that determines whether only the percentage value should be returned, or if a formatted string including the percentage should be returned.
$answered_correctly (mixed)
This parameter represents the unique identifier for the question whose completion percentage is being calculated.
$total (mixed)
This parameter indicates whether the question was answered correctly.
$percent (mixed)
This parameter contains the calculated percentage of times the question was answered correctly out of the total times it was asked, formatted as a string.

Return Value

The filtered value.


Examples

// Add a filter to modify the formatted percentage of correct answers for a question.
// This example will round the percentage down to the nearest whole number if it's below 50%.
add_filter(
	'uo_tincanny_reporting_questions_get_question_total_percent',
	function( $formatted, $question_id, $answered_correctly, $total, $percent ) {
		// Check if the percentage is less than 50% and if it's not already a whole number.
		if ( $percent < 50 && $percent !== floor( $percent ) ) {
			// If so, round the percentage down and reformat it.
			$formatted = floor( $percent );
		}
		// Return the potentially modified formatted percentage.
		return $formatted;
	},
	10, // Priority
	5  // Accepted arguments count
);

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/uncanny-question-analysis-report/question-analysis-report.php:807

private static function get_question_total_percent( $question_id, $percent_only = false ) {
		$answered_correctly = self::get_answered_correctly( $question_id );
		$total              = self::get_times_question_asked( $question_id );
		if ( 0 === absint( $answered_correctly ) || 0 === absint( $total ) ) {
			return 0;
		}
		$percent = ( $answered_correctly / $total ) * 100;
		if ( $percent_only ) {
			return $percent;
		}
		$formatted = number_format( $percent, 2 );

		return apply_filters(
			'uo_tincanny_reporting_questions_get_question_total_percent',
			$formatted,
			$question_id,
			$answered_correctly,
			$total,
			$percent
		);
	}


Scroll to Top