• Hello.

    I was wondering if i could get some help. I want to create a function to count all the unread documents in a group.

    I already did that for members, but now i’m struggling with this one. This is my approach:

    function bp_docs_get_unread_count_group($user_id = false, $group_id) {
    	if ( false === $user_id ) {
    		$user_id = bp_loggedin_user_id();
    	}
    
    	$unread_post_count = 0;
    	if (bp_group_hierarchy_has_subgroups()) {
    		// recursively call bp_docs_get_unread_count_group for every child
    	} else {
    		// for every member in the group call bp_docs_get_unread_count and add all the counts
    	}
    
    	if( $unread_post_count == 0) {
    		$unread_post_count = '';
    	}
    
    	return $unread_post_count;
    
    }

    Can anyone help me?

    https://www.ads-software.com/plugins/bp-group-hierarchy/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author David Dean

    (@ddean)

    Hi laurahreniucu –

    You can use this method call in place of bp_group_hierarchy_has_subgroups() to get a list of the immediate children:

    BP_Groups_Hierarchy::has_children( $group_id )

    – David

    Thread Starter laurahreniucu

    (@laurahreniucu)

    Thank you David Dean for the response.

    I’m actually struggling with the recursive function. I can’t seem to make it work.

    This is my code so far (the ‘else’ is just a dummy for now):

    function bp_docs_get_unread_count_group($user_id = false, $group_id) {
    	if ( false === $user_id ) {
    		$user_id = bp_loggedin_user_id();
    	}
    
    	$unread_post_count = 0;
    	$children = BP_Groups_Hierarchy::has_children( $group_id );
    
    	if($children.length > 0) {
    		foreach($children as $child) {
    			$unread_post_count = $unread_post_count + bp_docs_get_unread_count_group($user_id,$group_id);
    		}
    	} else {
    		$unread_post_count = 1;
    	}
    
    	if( $unread_post_count == 0) {
    		$unread_post_count = '';
    	}
    
    	return $unread_post_count;
    
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Recursive function’ is closed to new replies.