Filter tin-canny-learndash-reporting

tincanny_view_all_reports_permission

Filters whether the current user has permission to view all reports, allowing for custom access control logic.

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

Description

This filter allows developers to control whether the current user has permission to view all reports. It defaults to checking if the current user is an administrator. Developers can return `true` or `false` to grant or deny access, or implement custom logic for more granular control.


Usage

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

Return Value

The filtered value.


Examples

<?php
/**
 * Example: Allow users with a custom role to view all reports.
 *
 * This filter allows you to grant permission to view all reports to users
 * who are not necessarily administrators. This example grants access to users
 * with the 'editor' role.
 */
add_filter( 'tincanny_view_all_reports_permission', 'my_custom_tincanny_view_all_reports_permission', 10, 1 );

function my_custom_tincanny_view_all_reports_permission( $user_is_admin ) {
    // If the user is already an admin, keep the current permission.
    if ( $user_is_admin ) {
        return true;
    }

    // Check if the current user has the 'editor' role.
    if ( current_user_can( 'editor' ) ) {
        return true;
    }

    // Otherwise, return the default permission (which is likely false if not an admin).
    return false;
}
?>

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/shortcode-tincanny/shortcode-tincanny.php:1025
src/shortcode-tincanny/shortcode-tincanny.php:1104
src/shortcode-tincanny/shortcode-tincanny.php:1132
src/reporting/learndash/courses-users-report/build-report-data.php:174
src/reporting/learndash/courses-users-report/build-report-data.php:972

public static function course_report_page() {

		$user_can_view_all_reports = apply_filters( 'tincanny_view_all_reports_permission', uotc_is_current_user_admin() );

		$is_group_leader = learndash_is_group_leader_user( get_current_user_id() );

		if ( $user_can_view_all_reports ) {
			$group_ids = array();
		} elseif ( $is_group_leader ) {
			$group_ids = learndash_get_administrators_group_ids( get_current_user_id() );
		} else {
			echo esc_html( __( 'This report is only accessible by group leaders and administrators.', 'uncanny-learndash-reporting' ) );

			return;
		}

		if ( empty( $group_ids ) && ! $user_can_view_all_reports ) {
			echo esc_html( __( 'Group Leader has no groups assigned.', 'uncanny-learndash-reporting' ) );

			return;
		} else {
			$groups = get_posts(
				array(
					'numberposts' => 99999, // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_numberposts
					'include'     => $group_ids,
					'post_type'   => 'groups',
					'orderby'     => 'title',
					'order'       => 'ASC',
				)
			);
			if ( $groups && ! $user_can_view_all_reports && ! $is_group_leader ) {

				$gl_ids = array();
				foreach ( $groups as $__group ) {
					$gl__users              = learndash_get_groups_administrators( $__group->ID );
					$gl_ids[ $__group->ID ] = array();
					foreach ( $gl__users as $rr ) {
						$gl_ids[ $__group->ID ][] = $rr->ID;
					}
				}

				foreach ( $groups as $key => $__groups ) {
					if ( ! in_array( get_current_user_id(), $gl_ids[ $__groups->ID ], true ) ) {
						unset( $groups[ $key ] );
					}
				}
			}
		}

		self::$groups_query = $groups;

		if ( ultc_filter_has_var( 'group_id' ) ) {
			self::$isolated_group = absint( ultc_filter_input( 'group_id' ) );
		}

		$context = 'frontend';

		if ( is_admin() ) {
			$context = 'uncanny-learnDash-reporting' === ultc_get_filter_var( 'page', '' ) ? 'plugin' : 'dashboard';
		}


		// Add CSS classes to main container
		$css_classes   = array();
		$css_classes[] = sprintf( 'uo-reporting--%s', $context );

		include dirname( UO_REPORTING_FILE ) . '/src/reporting/learndash/templates/course-user-wrapper.php';
	}


Scroll to Top