Filter uncanny-toolkit-pro

uo_quiz_completion_setup_parameters

Filters the parameters used to set up a quiz completion, allowing modifications before it's rendered.

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

Description

Allows modification of setup parameters before generating a quiz completion PDF or email. Developers can alter user details, quiz information, or add custom data to the setup array. This filter is applied just before the PDF/email generation process begins.


Usage

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

Parameters

$setup_parameters (mixed)
This parameter is an array that will be populated with various setup details for the quiz completion process.
$id (mixed)
This parameter contains an array of setup parameters for the quiz completion process, which is being filtered and modified.
$user_id (mixed)
This parameter contains the unique identifier for the user who completed the quiz.
$setup_parameters (mixed)
This parameter is an array that will be populated with various setup details for the quiz completion process.

Return Value

The filtered value.


Examples

add_filter( 'uo_quiz_completion_setup_parameters', 'my_custom_quiz_completion_setup', 10, 4 );

/**
 * Modifies the setup parameters for quiz completion, potentially adding custom data
 * or altering existing values based on specific conditions.
 *
 * @param array $setup_parameters The existing setup parameters array.
 * @param int   $id               The ID of the quiz.
 * @param int   $user_id          The ID of the user completing the quiz.
 * @param mixed $certificate_post The post ID of the certificate, if available.
 * @return array The modified setup parameters.
 */
function my_custom_quiz_completion_setup( $setup_parameters, $id, $user_id, $certificate_post ) {

	// Example: Add a custom flag if the quiz is a specific type.
	if ( get_post_meta( $id, '_custom_quiz_type_flag', true ) === 'special' ) {
		$setup_parameters['is_special_quiz'] = true;
	}

	// Example: Modify the 'print-certificate' value based on user role.
	if ( user_can( $user_id, 'administrator' ) ) {
		// Administrators always get the certificate regardless of score.
		$setup_parameters['print-certificate'] = 1;
	}

	// Example: Add the quiz title directly to the parameters if it wasn't already there.
	if ( ! isset( $setup_parameters['quiz-title'] ) ) {
		$setup_parameters['quiz-title'] = get_the_title( $id );
	}

	// Example: Log the parameters for debugging purposes (remove in production).
	// error_log( print_r( $setup_parameters, true ) );

	return $setup_parameters;
}

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/generate-p-d-f-email.php:394

public static function setup_parameters( $id, $user_id, $get_request_results ) {

		$setup_parameters = array();

		$meta = get_post_meta( $id, '_sfwd-quiz' );

		$setup_parameters['userID']    = $user_id;
		$setup_parameters['user']      = get_user_by( 'ID', $user_id );
		$setup_parameters['quiz-id']   = $id;
		$setup_parameters['quiz-name'] = html_entity_decode( get_the_title( $id ) );
		// Get course id from quiz data - $get_request_results.
		$maybe_course_id = key_exists( 'course-id', $get_request_results ) ? $get_request_results['course-id'] : 0;
		// If 'learndash_quiz_completed' is called from LD_QuizPro::wp_pro_quiz_completed() the course is a WP_Post, else
		// on 'approve_essay' or 'editpost' action, the 'learndash_quiz_completed' is called from learndash_update_quiz_data() the course is an int.
		$setup_parameters['course-id']         = is_a( $maybe_course_id, 'WP_Post' ) ? $maybe_course_id->ID : $maybe_course_id;
		$setup_parameters['print-certificate'] = 0;
		$setup_parameters['timespent']         = $get_request_results['timespent'];
		$setup_parameters['points']            = $get_request_results['results']['comp']['points'];
		$setup_parameters['total-points']      = $get_request_results['results']['comp']['total-points'];
		$setup_parameters['count']             = $get_request_results['count'];

		if ( is_array( $meta ) && ! empty( $meta ) ) {
			$meta = $meta[0];
			if ( is_array( $meta ) && ( ! empty( $meta['sfwd-quiz_certificate'] ) ) ) {
				//Setting Certificate Post ID
				$setup_parameters['certificate-post'] = $meta['sfwd-quiz_certificate'];
				//Setting Course Post ID
				if ( 0 === absint( $setup_parameters['course-id'] ) ) {
					$setup_parameters['course-id'] = $meta['sfwd-quiz_course'];
				}
			}
		}

		if ( empty( $setup_parameters['certificate-post'] ) ) {
			return $setup_parameters;
		}

		$result                = $get_request_results['results']['comp']['result'];
		$certificate_threshold = ( learndash_get_setting( $id, 'threshold' ) * 100 );

		$setup_parameters['result']                = $result;
		$setup_parameters['certificate_threshold'] = $certificate_threshold;

		if ( ( isset( $result ) && $result >= $certificate_threshold ) ) {
			// All Set. User & Quiz good to go to print pdf certificate.
			$setup_parameters['print-certificate'] = 1;
		}

		return apply_filters( 'uo_quiz_completion_setup_parameters', $setup_parameters, $id, $user_id, $setup_parameters['certificate-post'] );
	}

Scroll to Top