Filter tin-canny-learndash-reporting

uotc_quiz_report_group_options

Filters quiz report group options displayed for a specific user or user role.

add_filter( 'uotc_quiz_report_group_options', $callback, 10, 3 );

Description

Filter to modify the options for the group dropdown in LearnDash group quiz reports. Developers can add, remove, or alter group options displayed to users based on their role and associated groups. This hook fires when generating the report's group selection UI.


Usage

add_filter( 'uotc_quiz_report_group_options', 'your_function_name', 10, 3 );

Parameters

$options (mixed)
This parameter contains an array of options that will be displayed in the group quiz report interface, which can be filtered and modified by other plugins or themes.
$user_id (mixed)
This parameter contains an array of options for grouping quiz reports, which can be filtered by plugins.
$user_role (mixed)
This parameter contains the ID of the user for whom the quiz report is being generated.

Return Value

The filtered value.


Examples

<?php
/**
 * Example function to modify the group options for the quiz report.
 * This function might be used to add a specific group option or filter out existing ones
 * based on certain conditions.
 *
 * @param array  $options    The current array of group options. Each option is an associative array
 *                           with 'value' (group ID) and 'text' (group title).
 * @param int    $user_id    The ID of the current user.
 * @param string $user_role  The role of the current user (e.g., 'administrator', 'group_leader').
 * @return array The modified array of group options.
 */
function my_custom_quiz_report_group_options( $options, $user_id, $user_role ) {

	// Example: If the user is a 'group_leader' and has specific permissions,
	// we might want to prepend an option for "My Assigned Groups Only".
	if ( 'group_leader' === $user_role ) {
		// In a real scenario, you would check $user_id against a custom meta key
		// or another mechanism to determine if this special option should be shown.
		// For this example, we'll just add it if it's a group leader.

		// Check if the 'All Groups' option is already present and remove it if we want to restrict.
		// This is just a hypothetical scenario.
		$options = array_filter( $options, function( $option ) {
			// Remove the 'All Groups' option if its value is -2
			return isset( $option['value'] ) && $option['value'] !== -2;
		} );

		// Add a new custom option at the beginning.
		array_unshift( $options, array(
			'value' => 'my_assigned_groups',
			'text'  => __( 'My Assigned Groups Only', 'my-text-domain' ),
		) );
	}

	// Example: If the user is an administrator, we might want to ensure
	// the 'Any Group' option is always at the top.
	if ( 'administrator' === $user_role ) {
		// Find the 'Any Group' option and move it to the front if it exists.
		$any_group_option = false;
		$other_options    = array();

		foreach ( $options as $key => $option ) {
			if ( isset( $option['value'] ) && '-1' === (string) $option['value'] ) {
				$any_group_option = $option;
			} else {
				$other_options[] = $option;
			}
		}

		if ( $any_group_option ) {
			// Rebuild the array with 'Any Group' at the start.
			$options = array_merge( array( $any_group_option ), $other_options );
		}
	}

	// Always return the modified options array.
	return $options;
}
add_filter( 'uotc_quiz_report_group_options', 'my_custom_quiz_report_group_options', 10, 3 );
?>

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/reporting/learndash/frontend-reports/group-quiz-report.php:812

private function get_group_options( $user_id, $user_role ) {

		$user_groups  = $this->get_user_group_data( $user_id );
		$group_label  = LearnDash_Custom_Label::get_label( 'group' );
		$groups_label = LearnDash_Custom_Label::get_label( 'groups' );
		$options      = array();
		$admin_option = false;

		// Generate admin option to be used in the dropdown.
		if ( 'administrator' === $user_role ) {
			$admin_option = array(
				'value' => '-1',
				'text'  => sprintf(
				// translators: %1$s: group Label
					__( 'Any %1$s, including no %1$s', 'uncanny-learndash-reporting' ),
					strtolower( $group_label )
				),
			);
		}

		// Groups found.
		if ( ! empty( $user_groups ) ) {

			// Add placeholders.
			if ( count( $user_groups ) > 1 ) {
				$options[] = array(
					'value' => 0,
					'text'  => sprintf(
					// translators: %s: Group Label
						__( 'Select a %s', 'uncanny-learndash-reporting' ),
						strtolower( $group_label )
					),
				);
			}

			// Allow Admin Any Group including No group.
			if ( $admin_option ) {
				$options[] = $admin_option;
			}

			// Allow Group Leader All Groups.
			if ( 'group_leader' === $user_role ) {
				$options[] = array(
					'value' => -2,
					'text'  => sprintf(
					// translators: %s: Plural Groups Label
						__( 'All %s', 'uncanny-learndash-reporting' ),
						strtolower( $groups_label )
					),
				);
			}

			foreach ( $user_groups as $group ) {
				$options[] = array(
					'value' => $group['ID'],
					'text'  => $group['post_title'],
				);
			}
		}

		// No groups found.
		if ( empty( $user_groups ) ) {
			// If Admin, show Any Group including No group.
			if ( $admin_option ) {
				$options[] = $admin_option;
			} else {
				// No groups found.
				$options[] = array(
					'value' => 0,
					'text'  => sprintf(
					// translators: %s: Plural Groups Label
						__( 'No %s', 'uncanny-learndash-reporting' ),
						strtolower( $groups_label )
					),
				);
			}
		}

		return apply_filters( 'uotc_quiz_report_group_options', $options, $user_id, $user_role );
	}

Scroll to Top