Filter uncanny-learndash-groups

ulgm_group_quiz_query_args

Filters the arguments used to query quiz data for a specific group course.

add_filter( 'ulgm_group_quiz_query_args', $callback, 10, 1 );

Description

Allows modification of query arguments for fetching group quiz data. Developers can filter these arguments to customize which quizzes are included in group quiz reports, potentially based on course ID or other criteria. This hook fires when preparing the arguments for the quiz query within the group quiz reporting functionality.


Usage

add_filter( 'ulgm_group_quiz_query_args', 'your_function_name', 10, 1 );

Parameters

$course_id (mixed)
The `$course_id` parameter contains the ID of the course for which the group quiz query arguments are being filtered.

Return Value

The filtered value.


Examples

/**
 * Example of filtering the 'ulgm_group_quiz_query_args' hook.
 * This example adds an additional condition to only retrieve quizzes that
 * are marked as "beginner" level.
 */
add_filter(
	'ulgm_group_quiz_query_args',
	function ( $query_args, $course_id ) {
		// Ensure $query_args is an array before proceeding.
		if ( ! is_array( $query_args ) ) {
			return $query_args;
		}

		// Check if $course_id is provided and is a valid integer.
		if ( ! empty( $course_id ) && is_numeric( $course_id ) ) {
			// Add a meta query condition to filter for quizzes with 'quiz_level' set to 'beginner'.
			// We're assuming a custom meta key 'quiz_level' exists for this purpose.
			if ( ! isset( $query_args['meta_query'] ) || ! is_array( $query_args['meta_query'] ) ) {
				$query_args['meta_query'] = array();
			}

			// Add the new meta query condition. We'll use a 'relation' of 'AND'
			// to ensure this new condition is combined with any existing ones.
			$query_args['meta_query']['relation'] = 'AND';
			$query_args['meta_query'][] = array(
				'key'     => 'quiz_level', // Assuming a meta key for quiz level
				'value'   => 'beginner',
				'compare' => '=',
			);
		}

		return $query_args;
	},
	10, // Priority
	2  // Number of arguments accepted by the filter callback
);

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:590
src/classes/reports/group-quiz-report.php:1207

public static function group_quizzes( $group_id = 0 ) {
		$group_quiz_ids = array();

		$relationships = array();
		if ( ! empty( $group_id ) ) {

			$group_course_ids = LearndashFunctionOverrides::learndash_group_enrolled_courses( intval( $group_id ) );

			if ( ! empty( $group_course_ids ) ) {
				foreach ( $group_course_ids as $course_id ) {
					$group_quiz_query_args = apply_filters(
						'ulgm_group_quiz_query_args',
						array(
							'post_type'  => 'sfwd-quiz',
							'nopaging'   => true,
							'orderby'    => 'title',
							'order'      => 'ASC',
							'fields'     => 'ids',
							'meta_query' => array(
								array(
									'relation' => 'OR',
									array(
										'key'     => 'course_id',
										'value'   => $course_id,
										'compare' => '=',
									),
									array(
										'key'     => 'ld_course_' . $course_id,
										'value'   => $course_id,
										'compare' => '=',
									),
								),
							),
						)
					);

					$group_quiz_query = new WP_Query( $group_quiz_query_args );

					if ( ! empty( $group_quiz_query->posts ) ) {
						$group_quiz_ids = array_merge( $group_quiz_ids, $group_quiz_query->posts );
						$group_quiz_ids = array_unique( $group_quiz_ids );
						if ( isset( $relationships[ $course_id ] ) ) {
							$relationships[ $course_id ] = array_unique( array_merge( $relationships[ $course_id ], $group_quiz_query->posts ) );

						} else {
							$relationships[ $course_id ] = $group_quiz_query->posts;

						}
					}
				}
			}
		}

		return array(
			'group_quiz_ids'       => $group_quiz_ids,
			'group_course_quizzes' => $group_course_ids,
		);
	}


Scroll to Top