Index: modules/userplus/userplus.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/userplus/userplus.module,v
retrieving revision 1.2
diff -u -r1.2 userplus.module
--- modules/userplus/userplus.module	14 Jun 2006 19:32:03 -0000	1.2
+++ modules/userplus/userplus.module	17 Aug 2006 01:57:42 -0000
@@ -1,5 +1,5 @@
 <?php
-// $Id: userplus.module,v 1.2 2006/06/14 19:32:03 marcp Exp $
+// $Id: userplus.module,v 1.1.2.1 2006/06/14 19:27:32 marcp Exp $
 
 /**
  * @file
@@ -53,6 +53,14 @@
 		                 'callback' => 'userplus_admin_userperms',
 		                 'type' => MENU_LOCAL_TASK, 
 		                 'weight' => 0);
+    if (module_exist('og')) {
+  		$items[] = array('path' => 'admin/user/userplus/usergroups',
+  		                 'title' => t('assign user groups'),
+  		                 'access' => $admin_access,
+  		                 'callback' => 'userplus_admin_usergroups',
+  		                 'type' => MENU_LOCAL_TASK, 
+  		                 'weight' => 1);
+    }
       
 		$items[] = array('path' => 'admin/user/userplus/deletemultiple',
 		                 'title' => t('delete multiple users'),
@@ -351,6 +359,147 @@
   return $output;
 }
 
+// Assumes $edit['user'][uid][nid]...
+function userplus_admin_usergroups_submit($form_id, $edit) {
+	// Figure out all the users who are on this page so we can bulk
+	// delete their old user/group associations before inserting the
+	// new ones...
+	$users = $edit['user'];
+	$uids = array_keys($users);
+
+	// Clear old user/group associations for these users...
+	if (count($uids) > 0) {
+	  $saved_og_uids = array();
+	  foreach ($edit['user'] as $uid => $groups) {
+	    foreach ($groups as $nid => $state) {
+        if ($state == 0) {
+          $result = db_query('DELETE 
+                              FROM {og_uid}  
+                              WHERE nid = %d 
+                              AND uid = %d',
+                              $nid, $uid
+                              );
+        }
+      }  
+    }
+  }  
+	
+	foreach ($users as $uid => $values) {
+		// Insert the new user/group associations for this user...
+		foreach ($values as $group => $checked) {
+			if ($checked == 1) {
+      	$result = db_query('SELECT * 
+                            FROM {og_uid} ug 
+                            WHERE ug.uid = %d
+                            AND ug.nid = %d',
+                            $uid, $group
+                            );
+      	$record = db_fetch_object($result);
+        if (!$record) {
+  				db_query('INSERT INTO {og_uid} 
+                    (uid, nid, is_active, created, changed) 
+                    VALUES (%d, %d, %d, %d, %d)', 
+                    $uid, $group, 1, time(), time()
+                    );
+        }
+			}
+		}
+	}
+
+	drupal_set_message(t('The changes have been saved.'));
+	
+	// Clear the cached pages and menus:
+	cache_clear_all();
+	menu_rebuild();
+	
+	drupal_goto($_GET['q'], drupal_get_destination());
+}
+
+function userplus_admin_usergroups() {
+	// Compile user/group array:
+	$result = pager_query('SELECT u.uid, u.name 
+                         FROM {users} u 
+                         WHERE u.uid > 0 
+                         ORDER BY u.name', 
+                         variable_get('userplus_max_show_user_groups', 25)
+                         );
+	$users = array();
+	while ($user = db_fetch_object($result)) {
+		$users[$user->uid] = $user->name;
+	}
+	
+	$result = db_query('SELECT ug.uid, ug.nid, ug.is_active 
+                      FROM {og_uid} ug 
+                      WHERE ug.nid > 0 
+                      ORDER BY ug.uid'
+                      );
+
+	$users_groups = array();
+	while ($user_group = db_fetch_object($result)) {
+		$users_groups[$user_group->uid][] = $user_group->nid;
+	}
+	
+	$result = db_query('SELECT ug.nid, n.title 
+                      FROM {og} ug
+                      INNER JOIN {node} n
+                      ON ug.nid = n.nid 
+                      ORDER BY n.title');
+	$group_names = array();
+	while ($group = db_fetch_object($result)) {
+		$group_names[$group->nid] = $group->title;
+	}
+
+	$form['user'] = array('#tree' => true);
+	
+	$weight = 1;
+	foreach ($users as $uid => $user_name) {
+		foreach ($group_names as $nid => $goup_name) {
+			$checked = is_array($users_groups[$uid]) ? in_array($nid, $users_groups[$uid]) : FALSE;
+			$form['user'][$uid][$nid] = array('#type' => 'checkbox', '#title' => $group_name, '#default_value' => $checked, '#weight' => $weight++);
+		}
+	}
+	
+	// Stick the role names in the form so we can use them in the theme function...
+	$form['groups'] = array('#value' => $group_names);
+	// Stick the user names in the form so we can use them in the theme function...
+  $form['usernames'] = array('#value' => $users);
+  
+  $form['submit'] = array('#type' => 'submit', '#value' => t('Save groups'));
+	
+	drupal_set_title('user groups');
+  return drupal_get_form('userplus_admin_usergroups', $form);
+}
+
+function theme_userplus_admin_usergroups($edit) {
+	$rows = array();
+	
+	// Render group overview:
+	$header = array_merge(array(t('User')), $edit['groups']['#value']);
+
+	foreach (element_children($edit['user']) as $uid) {
+		unset($row);
+		$row[] = array('data' => l($edit['usernames']['#value'][$uid], "user/$uid/edit"), 'class' => 'username');
+		foreach (element_children($edit['user'][$uid]) as $nid) {
+		  unset($edit['user'][$uid][$nid]['#title']);
+			$row[] = form_render($edit['user'][$uid][$nid]);
+		}
+		
+		$rows[] = $row;
+	}
+	
+	$output = theme('table', $header, $rows, array('id' => 'users-groups'));
+	$output .= '<br \>';
+  $output .= form_render($edit['submit']);
+	$output .= theme('pager', NULL, variable_get('userplus_max_show_user_groups', 25));
+  
+  // Don't forget the dreaded form_id -- http://drupal.org/node/38926 -- or
+  // the values won't be there when you get to your _submit handler...
+  $output .= form_render($edit['form_id']); 
+  
+  return $output;
+}
+
+
 function userplus_delete_users() {
   $weight = 0;
 	$form['user'] = array('#tree' => true);
@@ -433,6 +582,12 @@
   $form['assign-user-roles'] = array('#type' => 'fieldset', '#collapsible' => true, '#title' => t('"assign user roles" settings'));
   $form['assign-user-roles']['userplus_max_show_user_roles'] = array('#type' => 'textfield', '#title' => t('Number of users to show per page'), '#default_value' => variable_get('userplus_max_show_user_roles', '25'), '#size' => 4, '#maxlength' => 6, '#description' => t('The number of rows that will appear, per page, when using the "assign user roles" page.'));
 
+  if (module_exist('og')) {
+  	// "assign user group" settings
+    $form['assign-user-groups'] = array('#type' => 'fieldset', '#collapsible' => true, '#title' => t('"assign user groups" settings'));
+    $form['assign-user-groups']['userplus_max_show_user_groups'] = array('#type' => 'textfield', '#title' => t('Number of users to show per page'), '#default_value' => variable_get('userplus_max_show_user_groups', '25'), '#size' => 4, '#maxlength' => 6, '#description' => t('The number of rows that will appear, per page, when using the "assign user groups" page.'));
+  }
+
   // "delete multiple users" settings
   $form['delete-multiple-users'] = array('#type' => 'fieldset', '#collapsible' => true, '#title' => t('"delete multiple users" settings'));
   $form['delete-multiple-users']['userplus_max_delete_users'] = array('#type' => 'textfield', '#title' => t('Maximum number of users that can be deleted at once'), '#default_value' => variable_get('userplus_max_delete_users', '25'), '#size' => 4, '#maxlength' => 6, '#description' => t('The number of rows that will appear on the "delete multiple users" page.'));
