--- Desktop/user_list/user_list.module	2006-08-21 12:21:19.000000000 -0500
+++ /var/www/localhost/htdocs/drupal/modules/user_list/user_list.module	2006-10-15 16:53:56.000000000 -0500
@@ -26,6 +26,7 @@
  *    'A' - 'Z' => the $paginated $number of users whose username begins with that letter (case doesn't matter)
  *    'other' => the $paginated $number of users whose username begins with something other than 'A' - 'Z'
  *    'content' => the $paginated $number of users who have created at least one node of content-type $content_type
+ *    'roles' => the $paginated $number of users who are in $role_type
  *  $number:
  *    the number of users to return
  *  $paginated:
@@ -36,8 +37,10 @@
  *    If this has a value, then the default empty_msg for this $op will be overridden.
  *  $content_type:
  *    if $op is 'content', then this will be the content type to check, such as 'blog'. otherwise ignored.
+ *  $role_type:
+ *    if $op is 'role', then this will be the user role. otherwise ignored
  */
-function _user_list($op = 'all', $number = NULL, $paginated = true, $include_more_link = NULL, $default_header = NULL, $default_empty_msg = NULL, $content_type = NULL, $embed_menu = NULL, $from_block = false) {
+function _user_list($op = 'all', $number = NULL, $paginated = true, $include_more_link = NULL, $default_header = NULL, $default_empty_msg = NULL, $content_type = NULL,$role_type = NULL, $embed_menu = NULL, $from_block = false) {
   static $element = 0;
   $element += 1;
   if (!$number) {
@@ -70,6 +73,15 @@
     }
     $empty_msg = t('There are currently no users with a %content.', array('%content' => _node_names('name', $content_type)));
   }
+  else if ($op == 'roles' && _user_list_get_roles('base',$role_type) && (in_array($role_type, variable_get('user_list_role_types',0)))) {
+    $role = _user_list_get_roles('get',$role_type);
+    $header = t('Users who have a %role role', array('%role' => $role->name));
+    $result = pager_query("SELECT u.uid, u.name FROM {users} u INNER JOIN {users_roles} r ON r.uid = u.uid AND r.rid = %d WHERE u.status != 0 AND u.access != 0 ORDER BY u.name ASC", $number, $element, NULL, $role->rid);
+    if ($include_more_link) {
+      $count = db_result(db_query("SELECT COUNT(u.uid) FROM {users} u INNER JOIN {users_roles} r ON r.uid = u.uid AND r.rid = %d WHERE u.status != 0 AND u.access != 0",$role->rid));
+    }
+    $empty_msg = t('There are currently no users with a %role role.', array('%role' => $role->name));
+  }
   else if ($op == 'newest' && variable_get('user_list_newest', true)) {
     $header = t('Newest Users');
     $result = pager_query('SELECT uid, name FROM {users} WHERE status != 0 AND access != 0 ORDER BY created DESC', $number, $element);
@@ -108,6 +120,8 @@
 function _user_list_page() {
   if (arg(1) == 'content') {
     return _user_list(arg(1), NULL, true, false, NULL, NULL, arg(2));
+  } else if (arg(1) == 'roles') {
+    return _user_list(arg(1), NULL, true, false, NULL, NULL, NULL, arg(2));
   }
   return _user_list(arg(1));
 }
@@ -176,6 +190,18 @@
             );
           }
         }
+        foreach (variable_get('user_list_role_types',0) as $type) {
+          if (_user_list_get_roles('base',$type)) {   
+            $items[] = array(
+              'path' => 'userlist/roles/' . $type,
+              'title' => _user_list_get_roles('name',$type),
+              'access' => user_access('access user lists'),
+              'callback' => '_user_list_page',
+              'type' => MENU_LOCAL_TASK,
+              'weight' => 4,
+            );
+          }
+        }
       }
     }
   }
@@ -232,7 +258,16 @@
     '#multiple' => TRUE,
     '#description' => t('Select the node types you wish to allow user lists for. For instance, you may have a list of users who have written at least one blog entry at %blog, or who have contributed at least one image at %image.', array('%blog' => l('userlist/content/blog', 'userlist/content/blog'), '%image' => l('userlist/content/image', 'userlist/content/image'))),
   );
-
+  $options = _user_list_get_roles();
+  $form['user_list_types']['user_list_role_types'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Role User Lists'),
+    '#default_value' => variable_get('user_list_role_types',0),
+    '#options' => $options,
+    '#multiple' => TRUE,
+    '#description' => t('Select the roles you wish to allow user lists for. For instance, you may have a list of users who are of the %role role at %role_dest. NOTE: Spaces in role names are converted to underscore, but does not effect the role table.', array('%role' => current($options), '%role_dest' => l('userlist/roles/'.current($options), 'userlist/roles/'.current($options)))),
+  );
+  
   $form['user_list_menu_settings'] = array(
     '#type' => 'fieldset',
     '#title' => t('Menu Settings'),
@@ -271,6 +306,11 @@
           $blocks[$type] = array('info' => t('User List: Users with %types', array('%type' => _node_names('name', $type))));
         }
       }
+      foreach (variable_get('user_list_role_types',0) as $type) {
+        if (_user_list_get_roles('base',$type)) {
+          $blocks[$type] = array('info' => t('User List: Users with %role', array('%role' => _user_list_get_roles('name',$type))));
+        }
+      }
       return $blocks;
     case 'configure':
       if ($delta == 'all') {
@@ -282,6 +322,9 @@
       else if (in_array($delta, variable_get('user_list_content_types', array('blog')))) {
         $default_header = t('Users who have a %content', array('%content' => _node_names('name', $delta)));
       }
+      else if (in_array($delta, variable_get('user_list_role_types',0))) {
+        $default_header = t('Users who are %role role', array('%role' => _user_list_get_roles('name',$delta)));
+      }
       $form['header'] = array(
         '#type' => 'textfield',
         '#title' => t('Header'),
@@ -347,6 +390,13 @@
           $block['subject'] = variable_get('user_list_block_header_' . $delta, t('Users who have a %content', array('%content' => _node_names('name', $delta))));
           $block['content'] = _user_list('content', $number, false, $include_more, '', NULL, $delta, $embed_menu, true);
         }
+        else if (in_array($delta, variable_get('user_list_role_types',0))) {
+          if ($include_more) {
+            $include_more = l(variable_get('user_list_block_more_msg_' . $delta, t('View More')), 'userlist/roles/' . $delta);
+          }
+          $block['subject'] = variable_get('user_list_block_header_' . $delta, t('Users who are %role role', array('%role' => _user_list_get_roles('name', $delta))));
+          $block['content'] = _user_list('roles', $number, false, $include_more, '', NULL, $delta, $embed_menu, true);
+        }
       }
       return $block;
   }
@@ -397,6 +447,18 @@
       }
     }
   }
+  foreach (variable_get('user_list_role_types',0) as $type) {
+    if (_user_list_get_roles('base',$type)) {
+      $items[$type] = array(
+        'path' => 'userlist/roles/' . $type,
+        'title' => _user_list_get_roles('name',$type),
+      );
+      if ($args[1] == 'roles' && $args[2] == $type) {
+        $active = $type;
+        $items[$type]['active'] = true;
+      }
+    }
+  }
   if (!$active) {
     $items['all']['active'] = true;
     $active = 'all';
@@ -436,3 +498,42 @@
   $output .= "</div>\n";
   return $output;
 }
+/**
+ *  returns an array|object|boolean of role table
+ *  $op:
+ *    'get' => return a $role object for a $role_type or an array of roles from role table
+ *    'base' => return whether a role exists in role table
+ *    'name' => return just the name of the role specified in $role_type
+ *  $role_type:
+ *    if role_type is null gets all roles, if role_type is specified grabs information for only one role from role table
+ */
+function _user_list_get_roles($op = 'get', $role_type = NULL) {
+  if($role_type != NULL) {
+    $role_type = str_replace('_',' ',$role_type);
+    $result = db_query("SELECT r.rid, r.name FROM {role} r WHERE r.name = '%s'",$role_type);
+  } else {
+    $result = db_query("SELECT r.rid, r.name FROM {role} r");
+  }
+  if($result) {
+    if($op == 'base') {
+      return TRUE;
+    } else if($op == 'get') {
+      $roles = array();
+      if($role_type == NULL) {
+        while ($role = db_fetch_object($result)) {
+          if($role->rid > 2) {
+            $roles[str_replace(' ','_',$role->name)] = $role->name;
+          }
+        }
+      } else {
+        $roles = db_fetch_object($result);
+      }
+      return $roles;
+    } else if($op == 'name') {
+      $roles = db_fetch_object($result);
+      return $roles->name;
+    }
+  } else {
+    return FALSE;
+  }
+}
