Filter uncanny-learndash-groups

ulgm_quiz_report_user_score

Filters the user's score for a quiz attempt before it's displayed in reports.

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

Description

This filter hook allows modification of the user's quiz score before it's displayed. Developers can alter the score's value, format, or even assign custom statuses like 'Pending'. It's useful for customizing how quiz results are presented to users and administrators, especially when dealing with ungraded questions or specific scoring types.


Usage

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

Parameters

$score (mixed)
This parameter contains the current calculated score for the quiz attempt.
$user (mixed)
This parameter contains the calculated score for the user's quiz attempt, which can be modified by the filter.
$quiz_attempt (mixed)
This parameter contains information about the user attempting the quiz.
$score_type (mixed)
This parameter contains information about a specific attempt made by a user to complete a quiz.

Return Value

The filtered value.


Examples

<?php
/**
 * Modify the user's quiz score display based on specific conditions.
 *
 * This example demonstrates how to adjust the displayed score for a user's quiz attempt.
 * It checks if the score is already set to 'Pending' and if so, maintains it.
 * Otherwise, it formats the score as a percentage, and adds a custom suffix if the user
 * is an administrator.
 *
 * @param mixed $score The current score value.
 * @param mixed $user The user object or array.
 * @param mixed $quiz_attempt The quiz attempt data.
 * @param mixed $score_type The type of score ('percent', 'points', etc.).
 * @return mixed The modified score.
 */
add_filter( 'ulgm_quiz_report_user_score', function( $score, $user, $quiz_attempt, $score_type ) {

	// If the score is already 'Pending', don't change it.
	if ( 'Pending' === $score ) {
		return $score;
	}

	// If the score type is percentage, format it.
	if ( 'percent' === $score_type ) {
		// Assuming $score is already a numerical percentage value here.
		$formatted_score = round( floatval( $score ), 2 ) . '%';

		// Add a custom suffix for administrators.
		if ( current_user_can( 'administrator' ) ) {
			$formatted_score .= ' (Admin Override)';
		}
		return $formatted_score;
	}

	// For other score types, return the original score.
	return $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/classes/reports/group-quiz-report.php:825

if ( ( isset( $quiz_attempt['has_graded'] ) ) && ( true === $quiz_attempt['has_graded'] ) && ( true === LD_QuizPro::quiz_attempt_has_ungraded_question( $quiz_attempt ) ) ) {
						$score = _x( 'Pending', 'Pending Certificate Status Label', 'learndash' );
					} elseif ( 'percent' === $score_type ) {
						$score = round( $quiz_attempt['percentage'], 2 ) . __( '%', 'uncanny-learndash-groups' );
					} elseif ( 'points' === $score_type ) {
						$score = sprintf( '%d/%d', $quiz_attempt['points'], $quiz_attempt['total_points'] );
					}
					$score = apply_filters( 'ulgm_quiz_report_user_score', $score, $user, $quiz_attempt, $score_type );

					if ( isset( $quiz_attempt['statistic_ref_id'] ) && intval( $quiz_attempt['statistic_ref_id'] ) ) {
						$modal_link = '<a class="user_statistic"
									     data-statistic_nonce="' . wp_create_nonce( 'statistic_nonce_' . $quiz_attempt['statistic_ref_id'] . '_' . get_current_user_id() . '_' . $user['ID'] ) . '"
									     data-user_id="' . $user['ID'] . '"
									     data-quiz_id="' . $quiz_attempt['pro_quizid'] . '"
									     data-ref_id="' . intval( $quiz_attempt['statistic_ref_id'] ) . '"

Scroll to Top