views:

174

answers:

3
$list_of_groups = array("FACULTY","STAFF");

foreach ($list_of_groups as $i => $group) {
  $user_in_group = $adldap->user_ingroup($username,$group);
    print "<h2>Group: ".$group." user in group? ".$user_in_group."</h2>"; // if 1, means yes
}

Need to print run the appropriate function based on what returns true.

There are user's that are members of both FACULTY and STAFF groups, so I want to check for those users and display the appropriate content for them.

So if the user is both faculty and staff, then display this, if they are only of staff, display that, same for faculty, might not make sense, but I will write out some code "in theory" that will help you understand what I am trying to do

switch(Get group membership of user) {
 case "FACULTY":
 print "Faculty group member";
 break;

 case "STAFF":
 print "Staff group member";
 break;

 case "FACULTY and STAFF":
 print "Member of both faculty and staff";
 break;
}

I am unsure on how it will check if they are members of both groups and run that thru the case statement to display the appropriate message.

The foreach look currently runs thru every group the user belongs to, prints out the ones from the $list_of_groups and the number 1 to the right of it, signifying they belong to it. The problem I have is trying to use that information to run thru the case statement, I am unsure of how to go about that.

This is what it prints out for the user currently passed thru the foreach loop:

Group: FACULTY user in group? 1 Group: STAFF user in group? 1

Any help is appreciated.

EDIT: I apologize, I did not explain well enough. I need to run thru all the groups they belong to, if they belong to both Faculty and Staff, print "Belongs to Faculty and Staff", if only faculty print out "Faculty", if only staff print out "Staff", if only student, print out "Student", this is for displaying certain information based on what group the user belongs to. So the code would have to check all groups, I guess the list_of_groups is not needed, as they will find the group membership via $result=$adldap->user_groups("$username);.

A: 

if you can to collect a number of summery of this user group (like staff =1 and faculty = 2 ) , and present a number that summarize the user groups value collection , and display it ,

somthing like this :

switch($collect) {
 case "0":
 print "no group member";
 break;

 case "1":
 print "Staff group member";
 break;

 case "2":
 print "FACULTY group member";
 break;

 case "3":
 print "Member of both faculty and staff";
 break;
}
Haim Evgi
A: 

Also possible:

$list_of_groups   = array("FACULTY", "STAFF");
$group_membership = array();

// get group memberships and store them in a result array
foreach ($list_of_groups as $i => $group) {
  $user_in_group = $adldap->user_ingroup($username, $group);
  $group_membership[$group] = $user_in_group ? 1 : 0;
  print "<h2>Group: $group - user in group? $user_in_group</h2>";
}

// print different outputs depending on group memberships
if (array_sum($group_membership) == count($group_membership)) {
  print "Member of all groups in the list";
}
else {
  foreach ($list_of_groups as $i => $group) {
    if ($group_membership[$group] == 1) {
      print "$group group member";
    }
  }  
}
Tomalak
A: 
function getGroupMembership($username) {
    $list_of_groups = array("FACULTY","STAFF");
    $groups = array();

    foreach ($list_of_groups as $i => $group) {
        $user_in_group = $adldap->user_ingroup($username,$group);
        if ($user_in_group)
            $groups[] = $group;
    }
    return implode(' and ', $groups);
}

This will return a string as you requested for your switch. (Assuming user_ingroup returns 0 or false if the user is not in the group.)

Addendum based on the edit:

I'm assuming the array returned is of the form ("Group1","Group2","etc") in no order. Swap it into a hash with true/false as values.

function convertToHash($groups) {
    $hash = array();
    $foreach($groups as $group) {
        $hash[$group] = true;
    }
    return $hash;
}

Now use normal conditionals to check.

if (isset($hash['Student']) and count($hash) == 1) {
   // just a student
}
else if (isset($hash['Student']) and isset($hash['Staff']) and count($hash) == 2) {
   // student and staff: whatever this means
}
else {
   // my method broke
}
Fletcher Moore