--- 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-16 12:47:43.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) {
@@ -55,7 +58,7 @@
   }
 
   if (strlen($op) == 1 && variable_get('user_list_alpha_listing', true)) {
-    $result = pager_query("SELECT u.uid, u.name FROM {users} u WHERE u.status != 0 AND u.access != 0 AND LOWER(u.name) LIKE '%s%%' ORDER BY u.name ASC", $number, $element, NULL, strtolower($op));
+    $result = pager_query("SELECT * FROM {users} u WHERE u.status != 0 AND u.access != 0 AND LOWER(u.name) LIKE '%s%%' ORDER BY u.name ASC", $number, $element, NULL, strtolower($op));
     if ($include_more_link) {
       $count = db_result(db_query("SELECT COUNT(u.uid) FROM {users} u WHERE u.status != 0 AND u.access != 0 AND LOWER(u.name) LIKE '%s%%'"));
     }
@@ -64,15 +67,24 @@
   }
   else if ($op == 'content' && _node_names('base', $content_type) && (in_array($content_type, variable_get('user_list_content_types', array('blog'))))) {
     $header = t('Users who have a %content', array('%content' => _node_names('name', $content_type)));
-    $result = pager_query("SELECT u.uid, u.name FROM {users} u INNER JOIN {node} n ON n.uid = u.uid AND n.type = '%s' WHERE u.status != 0 AND u.access != 0 ORDER BY u.name ASC", $number, $element, NULL, $content_type);
+    $result = pager_query("SELECT u.* FROM {users} u INNER JOIN {node} n ON n.uid = u.uid AND n.type = '%s' WHERE u.status != 0 AND u.access != 0 ORDER BY u.name ASC", $number, $element, NULL, $content_type);
     if ($include_more_link) {
       $count = db_result(db_query("SELECT COUNT(u.uid) FROM {users} u INNER JOIN {node} n ON n.uid = u.uid AND n.type = '%s' WHERE u.status != 0 AND u.access != 0"));
     }
     $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.* 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);
+    $result = pager_query('SELECT * FROM {users} WHERE status != 0 AND access != 0 ORDER BY created DESC', $number, $element);
     if ($include_more_link) {
       $count = db_result(db_query("SELECT COUNT(u.uid) FROM {users} u WHERE u.status != 0 AND u.access != 0"));
     }
@@ -80,7 +92,7 @@
   }
   else if ($op == 'other' && variable_get('user_list_alpha_listing', true)) {
     $header = t('Users Not in A-Z');
-    $result = pager_query("SELECT u.uid, u.name FROM {users} u WHERE (LOWER(u.name) < 'a' OR LOWER(u.name) > 'z') AND u.status != 0 AND u.access != 0 ORDER BY u.name ASC", $number, $element);
+    $result = pager_query("SELECT * FROM {users} u WHERE (LOWER(u.name) < 'a' OR LOWER(u.name) > 'z') AND u.status != 0 AND u.access != 0 ORDER BY u.name ASC", $number, $element);
     if ($include_more_link) {
       $count = db_result(db_query("SELECT COUNT(u.uid) FROM {users} u WHERE (LOWER(u.name) < 'a' OR LOWER(u.name) > 'z') AND u.status != 0 AND u.access != 0"));
     }
@@ -88,13 +100,16 @@
   }
   else {
     $header = t('All Users');
-    $result = pager_query('SELECT u.uid, u.name FROM {users} u WHERE status != 0 AND access != 0 ORDER BY u.name ASC', $number, $element);
+    $result = pager_query('SELECT * FROM {users} u WHERE u.status != 0 AND u.access != 0 ORDER BY u.name ASC', $number, $element);
     if ($include_more_link) {
       $count = db_result(db_query("SELECT COUNT(u.uid) FROM {users} u WHERE u.status != 0 AND u.access != 0"));
     }
     $empty_msg = t('There are currenly no users.');
   }
-  while ($account = db_fetch_object($result)) {
+  while ($account = db_fetch_array($result)) {
+    if(variable_get('user_list_profiles',NULL)) {
+      $account['profiles'] = _user_list_profiles('get',$account['uid']);
+    }
     $users[] = $account;
   }
   if ($include_more_link) {
@@ -108,6 +123,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 +193,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 +261,25 @@
     '#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)))),
+  );
+  $options = _user_list_profiles();
+  $form['user_list_types']['user_list_profiles'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Show Profile fields in User Lists'),
+    '#default_value' => variable_get('user_list_profiles',0),
+    '#options' => $options,
+    '#multiple' => TRUE,
+    '#description' => t('Select the profile fields to show in the userlist. NOTE: profile_field must be set to be visible in Listings'),
+  );
+  
   $form['user_list_menu_settings'] = array(
     '#type' => 'fieldset',
     '#title' => t('Menu Settings'),
@@ -271,6 +318,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 +334,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 +402,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 +459,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';
@@ -407,7 +481,13 @@
 function theme_user_list_menu($items) {
   theme('add_style', drupal_get_path('module', 'user_list') .'/user_list.css');
   foreach ($items as $item) {
-    $output .= '  <li class="leaf' . ($item['active'] ? ' active' : '') . '">' . l(t($item['title']), $item['path']) . "</li>\n";
+    if(substr($item['path'],0,15) == 'userlist/roles/') {
+      $output .= '  <li class="leaf roles' . ($item['active'] ? ' active' : '') . '">' . l(t($item['title']), $item['path']) . "</li>\n";
+    } else if(substr($item['path'],0,17) == 'userlist/content/') {
+      $output .= '  <li class="leaf content' . ($item['active'] ? ' active' : '') . '">' . l(t($item['title']), $item['path']) . "</li>\n";
+    } else {
+      $output .= '  <li class="leaf' . ($item['active'] ? ' active' : '') . '">' . l(t($item['title']), $item['path']) . "</li>\n";
+    }
   }
   return "<div class=\"menu user_list_menu\">\n<ul>\n" . $output . '</ul></div>';
 }
@@ -424,7 +504,48 @@
     $output .= $empty_msg;
   }
   else {
-    $output .= theme('user_list', $users);
+    if(variable_get('user_list_profiles',NULL)) {
+      $headerRow = array('','','');
+      foreach($users as $user) {
+        $rows[] = array(
+          'data' => array(
+            array('data' => l($user['name'], "user/".$user['uid']), 'colspan' => 3),
+          )
+        );
+        foreach($user['profiles'] as $profile) {
+          //must refine to handle data types. 
+          //Thinking about adhering to profile module permissions and using module functions
+          //using Cache might be smart too
+          if($profile['type'] == 'date') {
+            $profile_date = unserialize($profile['value']);
+            $format = substr(variable_get('date_format_short', 'm/d/Y - H:i'), 0, 5);
+            // Note: Avoid PHP's date() because it does not handle dates before
+            // 1970 on Windows. This would make the date field useless for e.g.
+            // birthdays.
+            $replace = array('d' => sprintf('%02d', $profile_date['day']),
+                             'j' => $profile_date['day'],
+                             'm' => sprintf('%02d', $profile_date['month']),
+                             'M' => map_month($profile_date['month']),
+                             'Y' => $profile_date['year'],
+                             'H:i' => null,
+                             'g:ia' => null);
+            $value = strtr($format, $replace);
+          } else {
+            $value = $profile['value'];
+          }
+          $rows[] = array(
+            'data' => array(
+              array('data' => t($profile['title'].':')),
+              
+                array('data' => t($value), 'colspan' => 2),
+            )
+          );
+        }
+      }
+      $output .= theme('table', $headerRow, $rows, array("id"=>"user_list_table"));
+    } else {
+      $output .= theme('user_list', $users);
+    }
   }
   $output .= "  </div>\n  ";
   if ($include_more) {
@@ -436,3 +557,88 @@
   $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;
+  }
+}
+
+/**
+ *  returns an array|object|boolean of profiles
+ *  $op:
+ *    'get' => return an array of profile fields or profile fields for one user
+ *    'base' => return whether a profile field exists
+ *  $profile_name:
+ *    check profile_name exists in profile_fields
+ *  $user_id:
+ *    if user is null gets all profile fields or if user is specified returns profile_values in an object
+ */
+function _user_list_profiles($op = 'get', $user_id = NULL, $profile_name = NULL) {
+  if($user_id != NULL) {
+    $result = db_query('SELECT f.name, f.title, f.type, v.value FROM {profile_fields} f INNER JOIN {profile_values} v ON f.fid = v.fid WHERE v.uid = %d AND f.visibility = %d ORDER BY f.category, f.weight', $user_id, 3);
+  } else if($profile_name != NULL) {
+    $result = db_query("SELECT f.fid FROM {profile_fields} f WHERE f.name = '%s' AND f.visibility = %d ORDER BY f.category, f.weight", $profile_name, 3);
+  } else {
+    $result = db_query("SELECT f.name, f.title FROM {profile_fields} f WHERE f.visibility = %d ORDER BY f.category, f.weight",3);
+  }
+
+  if($result) {
+    if($op == 'base') {
+      return TRUE;
+    } else if($op == 'get') {
+      $profiles = array();
+      
+      if($user_id == NULL) {
+        while ($profile = db_fetch_object($result)) {
+          $profiles[$profile->name] = $profile->title;
+        }
+      } else {
+        $test_profiles = variable_get('user_list_profiles',NULL);
+        while ($profile = db_fetch_array($result)) {
+          if(in_array($profile['name'],$test_profiles) && $profile['value'] != '') {
+            $profiles[] = $profile;
+          }
+        }
+      }
+      return $profiles;
+    }
+  } else {
+    return FALSE;
+  }
+}
+
