Filter uncanny-toolkit-pro

uo_download_certificates_in_bulk_quiz_data

Filters quiz data for bulk certificate downloads, allowing customization of results and associated information.

add_filter( 'uo_download_certificates_in_bulk_quiz_data', $callback, 10, 2 );

Description

Filters the quiz data before certificates are downloaded in bulk. Developers can modify the `$results` and `$data` parameters to customize which quizzes are included or alter the data associated with them. This hook fires within the REST API response for fetching quiz data.


Usage

add_filter( 'uo_download_certificates_in_bulk_quiz_data', 'your_function_name', 10, 2 );

Parameters

$results (mixed)
This parameter contains the results of the certificate generation process, which could be an array of file paths or an error message.
$data (mixed)
This parameter contains the results of the certificate download process.

Return Value

The filtered value.


Examples

/**
 * Example of how to filter the quiz data before it's returned for bulk certificate downloads.
 * This example adds a new key to each quiz result, indicating if it has a certificate associated with it.
 *
 * @param array $results The original quiz data returned by ld_get_quizzes_list.
 * @param array $data    The data from the request, including 'course_id' and 'certificates'.
 * @return array The modified quiz data.
 */
add_filter( 'uo_download_certificates_in_bulk_quiz_data', function( $results, $data ) {
	// Check if the 'certificates' parameter was provided in the request.
	// If not, we can't determine which quizzes have certificates, so return original results.
	if ( empty( $data['certificates'] ) || ! is_array( $data['certificates'] ) ) {
		return $results;
	}

	// Iterate through each quiz result and add a flag indicating if it has a certificate.
	foreach ( $results as &$quiz ) {
		$quiz['has_certificate'] = false;
		if ( isset( $quiz['id'] ) && in_array( $quiz['id'], $data['certificates'] ) ) {
			$quiz['has_certificate'] = true;
		}
	}

	return $results;
}, 10, 2 );

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/download-certificates-in-bulk.php:459

public static function uo_generate_quizzes_dropdowns( $request ) {
		// check if its a valid request.
		$data = $request->get_params();

		if ( isset( $data['action'] ) && 'get_uo_quizzes' === $data['action'] ) {
			if ( ! self::validate_user_id( $data ) ) {
				return new WP_REST_Response(
					array(
						'msg'     => __( 'You do not have sufficient permission to perform this action.', 'uncanny-pro-toolkit' ),
						'success' => false,
					),
					200
				);
			}
			$results           = self::ld_get_quizzes_list( absint( $data['course_id'] ), $data['certificates'], $data );
			$results           = apply_filters( 'uo_download_certificates_in_bulk_quiz_data', $results, $data );
			$return            = array();
			$return['success'] = false;
			if ( ! empty( $results ) ) {
				$return['success'] = true;
				$return['data']    = $results;
			} else {
				$return['msg'] = sprintf( __( 'Sorry, no %s with certificates were found for the selected course. Please choose another %s.', 'uncanny-pro-toolkit' ), LearnDash_Custom_Label::get_label( 'quizzes' ), LearnDash_Custom_Label::get_label( 'course' ) );
			}

			return new WP_REST_Response(
				$return,
				200
			);
		}

		return new WP_REST_Response(
			array(
				'msg'     => 'Course ID not passed or no courses found.',
				'success' => false,
			),
			200
		);
	}

Scroll to Top