Skip to:
Content
Pages
Categories
Search
Top
Bottom

Solved: Group Stats Problem


  • commium
    Participant

    @commium

    Hi,

    I have here a code to show in group section -> stats page the stats of the group website with an Stats embedded tool.

    But i have a problem, the php work perfectly in group page and i would like to show it on the all users groups on their dashboard like in a widget, i tried with only using the “function display()) but $setting and $setting2 have no value, i don’t know why.

    Someone can maybe help me ?

    Thanks.

    function stats() {
    	
    if ( bp_is_active( 'groups' ) ) :
     
    class Entreprise_Statistiques extends BP_Group_Extension {
        /**
         * Your __construct() method will contain configuration options for 
         * your extension, and will pass them to parent::init()
         */
        function __construct() {
            $args = array(
                'slug' => 'entreprise_statistiques',
                'name' => 'Statistiques',
            );
            parent::init( $args );
        }
     
        /**
         * display() contains the markup that will be displayed on the main 
         * plugin tab
         */
    
    	
    	
        function display( $group_id = NULL ) {
    		$setting = groups_get_groupmeta( $group_id, 'entreprise_statistiques_setting' );
    		$setting2 = groups_get_groupmeta( $group_id, 'entreprise_statistiques_setting2' );
            $group_id = bp_get_group_id();
    		?>
    			
    		<script>
    		StatHatEmbed=new function(){function d(){var a=document.getElementsByTagName("script");return a[a.length-1]}function e(a,b){var d=document.createElement("script"),c="//www.stathat.com/embed/"+a+"/"+b.s1;b.dev&&(c="//localhost:8081/embed/"+a+"/"+b.s1);b.s2&&(c+="/"+b.s2);b.s3&&(c+="/"+b.s3);c+="?w="+b.w+"&h="+b.h+"&tf="+b.tf;b.style&&(c+="&style="+b.style);b.dev&&(c+="&dev=1");b.title&&(c+="&title="+b.title);d.src=c;d.type="text/javascript";document.getElementsByTagName("head")[0].appendChild(d)}function f(a){return[a.s1,
    		a.s2,a.s3,a.w,a.h,a.tf,a.style].join("_")}this.render_graph=function(a){DIV_ID="statd_embed_graph_"+f(a);d().insertAdjacentHTML("AfterEnd","<div id='"+DIV_ID+"' style='display:none'></div>");e("graph",a)};this.render_histogram=function(a){DIV_ID="statd_embed_histogram_"+f(a);d().insertAdjacentHTML("AfterEnd","<div id='"+DIV_ID+"' style='display:none'></div>");e("histogram",a)};this.render_data=function(a){DIV_ID="statd_embed_data_"+f(a);d().insertAdjacentHTML("AfterEnd","<div id='"+DIV_ID+"' style='display:none'></div>");
    		e("data",a)};this.render_table=function(a){DIV_ID="statd_embed_table_"+f(a);d().insertAdjacentHTML("AfterEnd","<div id='"+DIV_ID+"' style='display:none'></div>");e("table",a)};this.render_text=function(a){DIV_ID=["statd_embed_text",a.s1,a.u].join("_");d().insertAdjacentHTML("AfterEnd","<div id='"+DIV_ID+"' style='display:none'></div>");e("text",a)};this.render=function(a){a.tf||(a.tf="week_compare");a.kind||(a.kind="graph");switch(a.kind){case "graph":this.render_graph(a);break;case "histogram":this.render_histogram(a);
    		break;default:this.render_graph(a)}}};
    
    		</script>
    		<div id="stats">
    		<script>StatHatEmbed.render({s1: '<?php echo($setting); ?>', w: 760, h: 235, tf:'month_compare', style:'fill', title:'<?php echo($setting2); ?>'});</script>
    		</div>
    
    		<?php
    		
    		if ($setting == NULL) {
    			echo ("Aucun site internet détecté, merci de contacter l'équipe COMMIUM.");
    		}
        }
     
        /**
         * Création
         */
        function settings_screen( $group_id = NULL ) {
            $setting = groups_get_groupmeta( $group_id, 'entreprise_statistiques_setting' );
    		$setting2 = groups_get_groupmeta( $group_id, 'entreprise_statistiques_setting2' );
     
            ?>
    				<input type="text" name="entreprise_statistiques_setting2" id="entreprise_statistiques_setting2" placeholder="Nom d'entreprise"><?php echo esc_attr( $setting2 ) ?></input>
    				<input type="text" name="entreprise_statistiques_setting" id="entreprise_statistiques_setting"placeholder="Code suivi"><?php echo esc_attr( $setting ) ?></input>
            <?php
    			$settingInfo = $_POST['entreprise_statistiques_setting'];
    			$settingInfo2 = $_POST['entreprise_statistiques_setting2'];
        }
    	function settings_screen_save( $group_id = NULL ) {
    		$setting = '';
    		$setting2 = '';
    
    		if ( isset( $_POST['entreprise_statistiques_setting']) && isset($_POST['entreprise_statistiques_setting2'] ) ) {
    			$setting = $_POST['entreprise_statistiques_setting'];
    			$setting2 = $_POST['entreprise_statistiques_setting2'];
    		}
    
    		groups_update_groupmeta( $group_id, 'entreprise_statistiques_setting', $setting );
    		groups_update_groupmeta( $group_id, 'entreprise_statistiques_setting2', $setting2 );
    		}
    	}
    bp_register_group_extension( 'Entreprise_Statistiques' );
     
    endif; // if ( bp_is_active( 'groups' ) )
    }
    echo stats();
Viewing 6 replies - 1 through 6 (of 6 total)

  • shanebp
    Moderator

    @shanebp

    The BP_Group_Extension is for use within a specific group. And the group_id is ‘known’ within that context.

    To show data specific to a member’s groups, anywhere on the site, use: groups_get_user_groups to get all the group ids for that member.
    For example: $member_group_ids = groups_get_user_groups( bp_loggedin_user_id() );
    So $member_group_ids will be an array of group ids that you can use in a custom function to gather and display group data specific to the current user.

    For more info on that function and other useful group functions, see:
    buddypress\bp-groups\bp-groups-functions.php


    commium
    Participant

    @commium

    Thanks @shanebp for informations, i tried with $member_group_ids, it’s take the good user group id but it’s didn’t take the $setting and $setting2 variable, they are empty but in group page they aren’t.. i don’t know why.


    commium
    Participant

    @commium

    i used this code on an other page (dashboard) to display the stats functionnality but it didn’t worked..

        function display2( $member_group_ids = NULL ) {
    		$setting = groups_get_groupmeta( $member_group_ids, 'entreprise_statistiques_setting' );
    		$setting2 = groups_get_groupmeta( $member_group_ids, 'entreprise_statistiques_setting2' );
            $member_group_ids = groups_get_user_groups(bp_loggedin_user_id());
    		?>
    		<div id="stats">
    		<script>StatHatEmbed.render({s1: '<?php echo($setting); ?>', w: 760, h: 235, tf:'month_compare', style:'fill', title:'<?php echo($setting2); ?>'});</script>
    		</div>
    
    		<?php
    		
    		if ($setting == NULL) {
    			echo ("Aucun site internet détecté, merci de contacter l'équipe COMMIUM.");
    		}
        }
    echo display2();

    shanebp
    Moderator

    @shanebp

    As I said, the Group Extension only works on group pages. Stop trying to use that code on other pages.

    In your last example, you are calling groups_get_user_groups( bp_loggedin_user_id() ); AFTER you try to use it to gather group meta.
    As I said, groups_get_user_groups() will return an array.
    But you are trying to use the return value in groups_get_groupmeta which only takes a single group id.

    It seems like you are trying to copy and paste various bits of code.
    You should probably hire a developer.

    This is my guess at what you are trying to do.

    function display2() {
    
    	$member_group_ids = groups_get_user_groups(bp_loggedin_user_id());
    	
    	$setting = groups_get_groupmeta( $member_group_ids[0], 'entreprise_statistiques_setting' );
    	$setting2 = groups_get_groupmeta( $member_group_ids[0], 'entreprise_statistiques_setting2' );
    
    	?>
    	<div id="stats">
    	<script>StatHatEmbed.render({s1: '<?php echo($setting); ?>', w: 760, h: 235, tf:'month_compare', style:'fill', title:'<?php echo($setting2); ?>'});</script>
    	</div>
    
    	<?php
    	
    	if ($setting == NULL) {
    		echo ("Aucun site internet détecté, merci de contacter l'équipe COMMIUM.");
    	
    }

    commium
    Participant

    @commium

    Ok i understand now ! problem solved thanks 😉


    commium
    Participant

    @commium

    God daaaaamn it work !!!!!!

    function dashboardStats($member_group_ids = NULL) {
    
    	$member_group_ids = groups_get_user_groups(get_current_user_id());
    	
    	foreach( $member_group_ids["groups"] as $id ) { 
      	$group = groups_get_group( array( 'group_id' => $id) );	
      	echo '<pre>';		
      	var_dump( $id );	
      	echo '</pre>';
    	}
    
    	$setting = groups_get_groupmeta( $id, 'entreprise_statistiques_setting' );
    	$setting2 = groups_get_groupmeta( $id, 'entreprise_statistiques_setting2' );
    	
    
    	?>
    
    	<div id="stats">
    		<script>StatHatEmbed.render({s1: '<?php echo($setting); ?>', w: 760, h: 235, tf:'month_compare', style:'fill', title:'<?php echo($setting2); ?>'});</script>
    	</div>
    
    	<?php
    			if ($setting == NULL) {
    			echo ("Aucun site internet détecté, merci de contacter l'équipe COMMIUM.");
    		}
    
    	if (isset($setting))
    	{
    		echo ("test");
    		} else {
    			echo ("notest");
    		}
    		var_dump($setting);
    		var_dump($setting2);
    	}

    I added new function, you touched me by saying “You should probably hire a developer”, so i searched 😀
    Thanks again my friend for your help ! it’s soooo peaceful to solve a problem..

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
Skip to toolbar