Index: trunk/xpressme_integration_kit/wp-content/plugins/xpressme/include/user_sync_xoops.php
===================================================================
--- trunk/xpressme_integration_kit/wp-content/plugins/xpressme/include/user_sync_xoops.php	(revision 417)
+++ trunk/xpressme_integration_kit/wp-content/plugins/xpressme/include/user_sync_xoops.php	(revision 534)
@@ -26,5 +26,4 @@
 */
 function get_xoops_group_role($uid=0){
-	$ans = array();
 	if ($uid == 0){
 		return '';
@@ -32,41 +31,124 @@
 	
 	global $xoops_db;
+	
 	$db_groups_users_link = get_xoops_prefix() . 'groups_users_link';
 	$db_group_role = get_wp_prefix() . 'group_role';
 	$db_groups = get_xoops_prefix() . 'groups';
-	
-	$sql  = "SELECT * ";
-	$sql .= "FROM $db_groups_users_link ";
-	$sql .= "LEFT JOIN $db_group_role ON $db_groups_users_link.groupid = $db_group_role.groupid ";
-	$sql .= "LEFT JOIN $db_groups ON $db_groups_users_link.groupid = $db_groups.groupid ";
-	$sql .= "WHERE  uid = $uid";
-	$user_groups = $xoops_db->get_results($sql);
-	$ans['capabillities'] = '';
-	$ans['allway_update'] = 0;
-	
-	foreach($user_groups as $user_group){
-		if ($user_group->group_type == 'Admin'){
-			$ans['capabillities'] = 'administrator';
-			if ($user_group->groupid =1){
-				//It always rewrites it as WordPress adninistrator for an initial admin group of XOOPS.	
-				$ans['allway_update'] = 1;
-			} else {
-				// admin groups other than initial admin group of XOOPS 
-				// It group rewrites group_type of the data base in Admin.
-				$ans['allway_update'] = $user_group->login_all; 
-			}
-			break;
-		}
-		$before_level = get_role_level($ans['capabillities']);
+
+	$blog_id_list = array();
+	
+	if (function_exists('is_multisite') && is_multisite()){
+		$blog_id_sql = "SELECT blog_id FROM $db_group_role GROUP BY blog_id";
+		$blog_id_list = $xoops_db->get_results($blog_id_sql);
+	} else {
+		$std = new stdClass();
+		$std->blog_id = 1;
+		$blog_id_list[] = $std;
+	}
+	
+	$ans_array = array();
+	foreach($blog_id_list as $blog_id){
+		$ans = array();
+		$sql  = "SELECT * ";
+		$sql .= "FROM $db_groups_users_link ";
+		$sql .= "LEFT JOIN $db_group_role ON $db_groups_users_link.groupid = $db_group_role.groupid ";
+		$sql .= "LEFT JOIN $db_groups ON $db_groups_users_link.groupid = $db_groups.groupid ";
+		$sql .= "WHERE  uid = $uid AND $db_group_role.blog_id = $blog_id->blog_id";
+		$user_groups = $xoops_db->get_results($sql);
 		
-		$now_level = get_role_level($user_group->role);
-		if ($now_level > $before_level){
-			$ans['capabillities'] = $user_group->role;
-			$ans['allway_update'] = $user_group->login_all;
-		}
-	}
-	return $ans;
-}
-	
+		//default value set
+		$ans['blog_id'] = $blog_id->blog_id;
+		$ans['capabillities'] = '';
+		$ans['allway_update'] = 0;
+		
+		//get maximum role
+		foreach($user_groups as $user_group){
+			if ($user_group->group_type == 'Admin'){
+				$ans['capabillities'] = 'administrator';
+				if ($user_group->groupid =1){
+					//It always rewrites it as WordPress adninistrator for an initial admin group of XOOPS.	
+					$ans['allway_update'] = 1;
+				} else {
+					// admin groups other than initial admin group of XOOPS 
+					// It group rewrites group_type of the data base in Admin.
+					$ans['allway_update'] = $user_group->login_all; 
+				}
+				break;
+			}
+			$before_level = get_role_level($ans['capabillities']);
+			
+			$now_level = get_role_level($user_group->role);
+			if ($now_level > $before_level){
+				$ans['capabillities'] = $user_group->role;
+				$ans['allway_update'] = $user_group->login_all;
+			}
+		}
+		$ans_array[] = $ans;
+	}
+	return $ans_array;
+}
+
+// for Multi Blog group_role delete
+function blog_group_role_delete($blog_id,$drop = false) {
+	global $xoops_db;
+	
+	$db_group_role = get_wp_prefix() . 'group_role';
+	if ($drop){
+		$delsql = "DELETE FROM $db_group_role WHERE blog_id = $blog_id";
+		$xoops_db->query($delsql);
+	}
+}
+
+// for Multi Blog group_role add
+function blog_group_role_add($blog_id,$uid = 0) {
+	global $xoops_db,$xpress_config;
+	$xpress_config->GroupeRoleCheck($blog_id);
+}
+
+// check user has groupe role
+function has_group_role($uid = 0) {
+	if (empty($uid)) return false;
+	$user_roles = get_xoops_group_role($uid);
+	foreach ($user_roles as $user_role){
+		if (!empty($user_role['capabillities']))
+			return true;
+	}
+	return false;
+}
+
+/*
+ * WP User ID exists is checked.. 
+*/
+function set_user_role($uid=0,$new_user = false){
+	$roles = get_xoops_group_role($uid);
+	
+	foreach($roles as $role){
+		$b_id = $role['blog_id'];
+		$capabillities_name = $role['capabillities'];
+		$allway_update = $role['allway_update'];
+		if ($allway_update || $new_user ){
+			$sycc_user = new WP_User($uid);
+			if (function_exists('is_multisite') && is_multisite()){
+				$sycc_user->for_blog($b_id);	// for Multi blog
+			}
+			// check user role is admin
+			$is_blog_admin = false;
+			$user_roles = $sycc_user->roles;
+			foreach ($user_roles as $user_role){
+				if (strcmp($user_role , 'administrator') == 0){
+					$is_blog_admin = true;
+					break;
+				}
+			}
+			if (!$is_blog_admin){	// admin not change role
+				$sycc_user->set_role($capabillities_name);
+			}
+		}
+//		$message .= '...UPDATE ' . $xoops_user->uname . '(' . $capabillities_name . ')';
+//		$message .= '...INSERT ' . $user_login_name . '(' . $capabillities_name . ')';
+	}
+
+}
+
 /*
  * Get User ID of WordPress from the login name. 
@@ -118,11 +200,10 @@
 
 	foreach($xoops_users as $xoops_user){
-		$role = get_xoops_group_role($xoops_user->uid);
-		$capabillities_name = $role['capabillities'];
-		$allway_update = $role['allway_update'];
 		
 		$wp_user_id = get_wp_user_id($xoops_user->uname) ;
-		
-		if (empty($capabillities_name)){
+
+
+		$has_role = has_group_role($xoops_user->uid);
+		if (!$has_role){
 			if ($sync_uid != 0){
 				if ($wp_user_id) {
@@ -148,4 +229,5 @@
 		
 		if ($wp_user_id){
+			$add_user =  true;
 			$wu_sql  = 	"UPDATE $db_xpress_users ";
 			$wu_sql .= 	'SET ';
@@ -159,12 +241,7 @@
 
 			$xoops_db->query($wu_sql);
-			if ($allway_update){
-				$sycc_user = new WP_User($wp_user_id);
-				$sycc_user->set_role($capabillities_name);
-				$message .= '...UPDATE ' . $xoops_user->uname . '(' . $capabillities_name . ')';
-
-			} else {
-				$message .= '...UPDATE ' . $xoops_user->uname . '(Level Not Change)';
-			}
+			
+			$message .= set_user_role($wp_user_id,$add_user);
+
 			if (!check_user_meta_prefix($wp_user_id)){
 				repair_user_meta_prefix();
@@ -173,4 +250,5 @@
 			$is_update = true;
 		}else{
+			$add_user =  false;
 			if (is_used_wp_user_id($xoops_user->uid) ) { // WP User ID has already been used. 
 				$wu_sql  =	"INSERT INTO $db_xpress_users ";
@@ -192,7 +270,5 @@
 				$wp_user_id = $xoops_user->uid;
 			}
-			$sycc_user = new WP_User($wp_user_id);
-			$sycc_user->set_role($capabillities_name);						
-			$message .= '...INSERT ' . $user_login_name . '(' . $capabillities_name . ')';
+			$message .= set_user_role($wp_user_id,$add_user);
 		}
 		
