--- /c/projects/drupal/cvs-47/contributions/modules/minutes/minutes.module	2006-11-06 15:01:11.109375000 -0500
+++ modules/minutes/minutes.module	2006-11-06 20:53:25.921875000 -0500
@@ -15,7 +15,7 @@ function minutes_help($section) {
         case 'admin/help#minutes':
             return t('The minutes module allows users to attach minutes to an event. The minutes inherit the taxonomy and view restrictions of the event. The module keeps track of who attended the event.');
         case 'admin/modules#description':
-            return t('Allows the attachments of minutes to events');
+            return t('Allows the attachments of minutes to events. <em>Note: Requires the Event module</em>.');
         case 'node/add#minutes':
             return t('Minutes for an event. You cannot add minutes through this interface; you must add them through the particular event');
     }
@@ -81,6 +81,13 @@ function minutes_type_is_enabled($type) 
     return variable_get('minutes_enabled_for_type_' . $type, false);
 }
 
+/**
+ * Determines if the specified filter for list of potential attendees is enabled
+ * for the given event type.
+ */
+function minutes_attendees_filter_is_enabled($type, $filter) {
+  return in_array($filter, variable_get('minutes_attendees_filters_for_type_'. $type, array()));
+}
 
 /**
  * Implementation of hook_link().
@@ -173,12 +180,6 @@ function minutes_admin_settings() {
         '#title' => t('Display a list of users as checkboxes, and track attendance for users'),
         '#default_value' => variable_get('minutes_track_users', 1),
     );
-    $form['minutes_user_list']['minutes_which_users_method'] = array(
-        '#type' => 'select',
-        '#title' => t('Which method should I use to determine which users could attend an event'),
-        '#options' => module_invoke_all('minutes_restriction_list'),
-        '#default_value' => variable_get('minutes_which_users_method', 'excluded_users'),
-    );
     
     $form[] = array(
         '#type' => 'submit',
@@ -190,7 +191,6 @@ function minutes_admin_settings() {
 
 function minutes_admin_settings_submit($form_id, $values) {
     variable_set('minutes_track_users', $values['minutes_track_users']);
-    variable_set('minutes_which_users_method', $values['minutes_which_users_method']);
 
     $display_string = $values['minutes_user_display_string'];
     variable_set('minutes_user_display_string', $display_string);
@@ -233,16 +233,35 @@ function minutes_form_alter($form_id, &$
     switch($form_id) {
         // Node settings form
         case $type . '_node_settings':
-            if (variable_get('event_nodeapi_' . $type, 'never') != 'never') {
-                $form['workflow']['minutes_enabled_for_type_' . $type] = array(
-                    '#type' => 'checkbox',
-                    '#title' => t('Allow event to have minutes'),
-                    '#default_value' => minutes_type_is_enabled($type),
-                    '#description' => t('If this option is checked, users will be able to attach minutes to this type of event'),
-                );
-            }
-            break;
+          if (variable_get('event_nodeapi_' . $type, 'never') != 'never') {
+            $form['workflow']['minutes'] = array(
+              '#type' => 'fieldset',
+              '#title' => t('Minutes settings'),
+            );
+          
+            $form['workflow']['minutes']['minutes_enabled_for_type_' . $type] = array(
+              '#type' => 'checkbox',
+              '#title' => t('Allow event to have minutes'),
+              '#default_value' => minutes_type_is_enabled($type),
+              '#description' => t('If this option is checked, users will be able to attach minutes to this type of event'),
+            );
             
+            // Find the available filters for the list of potential attendees
+            foreach (module_implements('minutes_attendees') as $module) {
+              $options[$module] = module_invoke($module, 'minutes_attendees', 'title');
+            }
+            if (count($options) > 0) {
+              $form['workflow']['minutes']['minutes_attendees_filters_for_type_'. $type] = array(
+                '#type' => 'checkboxes',
+                '#title' => t('Filters for the list of potential attendees in minutes'),
+                '#options' => $options,
+                '#default_value' => variable_get('minutes_attendees_filters_for_type_'. $type, array()),
+                '#description' => t('Select the filters that will restrict the list of potential attendees when editing the minutes.'),
+              );
+            }
+          }
+          break;
+          
     }
 }
 
@@ -255,7 +274,7 @@ function minutes_form(&$node, &$param) {
     // this module _must_ be accessed in such a way that there is an
     // event passed to it.  Right now, we will verify this using 'arg'.
     // Is there a better way to do this?
-    if ((arg(0) == 'node') && (arg(1) == 'add') && (arg(2) == 'minutes')) {
+    if ((arg(0) == 'node') && (arg(1) == 'add') && (arg(2) == 'minutes') && is_numeric(arg(3))) {
         $node->event_id = arg(3); // arg == [ node add minutes event_id ]
     }
 
@@ -567,37 +582,65 @@ function _minutes_sort_users($users) {
     return $users;
 }
 
+/**
+ * Return a list of potential attendees for the given event. 
+ *
+ * The list can be filtered by modules that implement hook_minutes_attendees().
+ * The arguments of this hook are:
+ *
+ *   $op: What kind of action to perform. Possible values are:
+ *
+ *     'title': Return a user-friendly title for the filter.
+ *
+ *     'filter': Return an array with appropriate data for the filter to be applied.
+ *               The format of this array goes like this:
+ *
+ *       'join' => array of SQL JOIN expressions. Should join the users table.
+ *
+ *       'condition' => array of SQL WHERE conditions. Will be imploded with ANDs.
+ *
+ *       'args' => array of values to be passed to db_query(). Useful if some
+ *                 WHERE conditions uses typical placeholders such as %d and %s.
+ *
+ *   $event: If $op == 'filter', this contains the event's data, otherwise this is null.
+ */
 function _minutes_valid_users($event) {
-    $which_method = variable_get('minutes_which_users_method', 'excluded_users');
-
-    $uids = module_invoke($which_method, 'minutes_valid_uids', $event);
-
-    $users = _minutes_translate_uids_to_display($uids);
-    asort($users);
-
-    // at some point, we need to figure out how to make this be sorted
-    // as in _minutes_sort_users -- right now, that function renumbers
-    // numerical array associations, which breaks everything
-    return $users;    
-}
-
-function excluded_users_minutes_restriction_list() {
-    return array('excluded_users' => 'Excluded Users');
+  // Collect filters from modules which implement hook_minutes_attendees() to
+  // restrict the list of potential attendees
+  $join = array();
+  $condition = array();
+  $args = array();
+  foreach (module_implements('minutes_attendees') as $module) {
+    if (minutes_attendees_filter_is_enabled($event->type, $module)) {
+      $filters = module_invoke($module, 'minutes_attendees', 'filter', $event);
+      if (count($filters['join']) > 0) {
+        $join[] = implode(' ', $filters['join']);
+      }
+      if (count($filters['condition']) > 0) {
+        $condition[] = implode(' AND ', $filters['condition']);
+      }
+      if (count($filters['args']) > 0) {
+        $args = array_merge($args, $filters['args']);
+      }
+    }
+  }
+
+  // Build and run the query for potential attendees
+  $join_str = implode(' ', $join);
+  if (count($condition)) {
+    $condition_str = ' AND '. implode(' AND ', $condition);
+  }
+  else {
+    $condition_str = '';
+  }
+  $result = db_query("SELECT u.uid FROM {users} AS u $join_str WHERE u.status = 1 $condition_str ORDER BY u.name ASC", $args);
+  
+  // Fetch potential attendees (users)
+  $uids = array();
+  while ($object = db_fetch_object($result)) {
+    $uids[] = $object->uid;
+  }
+  return _minutes_translate_uids_to_display($uids);
 }
 
-function excluded_users_minutes_valid_uids($event) {
-    $excluded_users = array(0);
-    if (module_exist('excluded_users')) {
-        $excluded_users = module_invoke('excluded_users', 'get_excluded_uids');
-    }
-    
-    $uids_query = db_query("SELECT uid FROM {users} WHERE uid not in (" . implode(",",$excluded_users) . ") and status = 1");
-    
-    $uids = array();
-    while($uids_res = db_fetch_array($uids_query)) {
-        $uids[] = $uids_res['uid'];
-    }
-    
-    return $uids;
-}
 
