Index: storm.module
===================================================================
--- storm.module	(revision 675)
+++ storm.module	(working copy)
@@ -793,3 +793,126 @@
     }
   }
 }
+
+/**
+ * Get a list of people and teams for select boxes
+ *
+ * Params:
+ *  $organization_nid
+ *    Leave blank to get a list of all teams and persons, otherwise also provide
+ *  $project_nid
+ *    to get a limited list of teams and persons following the following logic:
+ *    - If the project is assigned to a person, only that person is listed as an option
+ *    - If the project is assigned to a teram, all team members are listed as options
+ *    - If the project is neither assigned to a person nor a team, all people that are
+ *      assigned to the given origanization are listed as options
+ *    - In addition, if the project is assigned to a manager, that person is also listed
+ *    - Finally, look into all existing teams and list those teams that exclusively
+ *      contain members that are already selected
+ *
+ */
+function storm_get_assignment_options($organization_nid = 0, $project_nid = 0) {
+  $teams = t('Teams:');
+  $people = t('People:');
+  $options = array();
+  if (!$organization_nid) {
+    $options['all'] = t('- no filter -');
+    $options['mine'] = t('- mine -');
+  }
+  $options['none'] = t('- unassigned -');
+  if (module_exists('stormteam')) {
+    $options[$teams] = array();
+  }
+  if (module_exists('stormperson')) {
+    $options[$people] = array();
+  }
+  $add_org_people = TRUE;
+  if ($organization_nid) {
+    $add_org_people = FALSE;
+    $project = node_load($project_nid);
+    if ($project->manager_nid) {
+      $manager = node_load($project->manager_nid);
+    }
+    if ($project->assigned_nid) {
+      $node = node_load($project->assigned_nid);
+      if ($node->type == 'stormperson') {
+        if (module_exists('stormperson')) {
+          $options[$people][$node->nid] = $node->title;
+        }
+      }
+      else { // ($node->type == 'stormteam')
+        if (module_exists('stormteam')) {
+          $options[$teams][$node->nid] = $node->title;
+          foreach ($node->members_array as $nid => $name) {
+            $options[$people][$nid] = $name;
+          }
+        }
+      }
+    }
+    else {
+      $add_org_people = TRUE;
+    }
+  }
+  if ($add_org_people) {
+    if (module_exists('stormperson')) {
+      $where = isset($organization_nid) ? 'WHERE spe.organization_nid = %d' : '';
+      $sql = "SELECT  spe.nid,
+                      spe.fullname
+                  FROM {node} n
+                  INNER JOIN {stormperson} spe
+                    ON n.vid = spe.vid
+                  $where
+                  ORDER BY spe.fullname ASC";
+      $sql = stormperson_access_sql($sql);
+      $sql = db_rewrite_sql($sql);
+      $result = db_query($sql, array($organization_nid));
+      while ($person = db_fetch_object($result)) {
+        $options[$people][$person->nid] = $person->fullname;
+      }
+    }
+  }
+  else {
+    if (isset($manager) && module_exists('stormperson')) {
+      if (!array_key_exists($manager->nid, $options[$people])) {
+        $options[$people][$manager->nid] = $manager->title;
+      }
+    }
+  }
+  if (module_exists('stormteam')) {
+    $sql = "SELECT  n.nid,
+                    n.title,
+                    ste.members
+                FROM {node} n
+                INNER JOIN {stormteam} ste
+                  ON n.vid = ste.vid
+                WHERE n.type = 'stormteam'
+                ORDER BY n.title ASC";
+    $sql = stormteam_access_sql($sql);
+    $sql = db_rewrite_sql($sql);
+    $result = db_query($sql);
+    while ($team = db_fetch_object($result)) {
+      if (!array_key_exists($team->nid, $options[$teams])) {
+        $add_team = TRUE;
+        if (isset($organization_nid)) {
+          //Check if all team members are relevant to current org or project
+          foreach (unserialize($team->members) as $nid => $name) {
+            if (!array_key_exists($nid, $options[$people])) {
+              $add_team = FALSE;
+              break;
+            }
+          }
+        }
+        if ($add_team) {
+          $options[$teams][$team->nid] = $team->title;
+        }
+      }
+    }
+  }
+  if (!sizeof($options[$people])) {
+    unset($options[$people]);
+  }
+  if (!sizeof($options[$teams])) {
+    unset($options[$teams]);
+  }
+  return $options;
+}
Index: stormtask/stormtask.module
===================================================================
--- stormtask/stormtask.module	(revision 683)
+++ stormtask/stormtask.module	(working copy)
@@ -655,34 +655,8 @@
     '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'group5') : -16,
   );
   
-  $options = array(0 => '-');
-  
-  if (module_exists('stormperson')) {
-    $s_per = "SELECT n.nid, n.title FROM {node} n INNER JOIN {stormperson} spe ON n.vid=spe.vid WHERE n.type='stormperson' ORDER BY n.title";
-    $s_per = stormperson_access_sql($s_per);
-    $s_per = db_rewrite_sql($s_per);
-    $r_per = db_query($s_per);
-    $people = array();
-    while ($person = db_fetch_object($r_per)) {
-      $people[$person->nid] = $person->title;
-    }
-    
-    $options = $options + array(-1 => '-PEOPLE-') + $people;
-  }
-  
-  if (module_exists('stormteam')) {
-    $s_team = "SELECT n.nid, n.title FROM {node} n INNER JOIN {stormteam} ste ON n.vid=ste.vid WHERE n.type='stormteam' ORDER BY n.title";
-    $s_team = stormteam_access_sql($s_team);
-    $s_team = db_rewrite_sql($s_team);
-    $r_team = db_query($s_team);
-    $teams = array();
-    while ($team = db_fetch_object($r_team)) {
-      $teams[$team->nid] = $team->title;
-    }
-    
-    $options = $options + array(-2 => '-TEAMS-') + $teams;
-  }
-  
+  $options = storm_get_assignment_options($node->organization_nid, $node->project_nid);
+
   $form['group5']['assigned_nid'] = array(
     '#type' => 'select',
     '#title' => t('Assigned to'),
Index: stormticket/stormticket.module
===================================================================
--- stormticket/stormticket.module	(revision 683)
+++ stormticket/stormticket.module	(working copy)
@@ -533,34 +533,8 @@
     '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'group5') : -16,
   );
   
-  $options = array(0 => '-');
+  $options = storm_get_assignment_options($node->organization_nid, $node->project_nid);
   
-  if (module_exists('stormperson')) {
-    $s_per = "SELECT n.nid, n.title FROM {node} n INNER JOIN {stormperson} spe ON n.vid=spe.vid WHERE n.type='stormperson' ORDER BY n.title";
-    $s_per = stormperson_access_sql($s_per);
-    $s_per = db_rewrite_sql($s_per);
-    $r_per = db_query($s_per);
-    $people = array();
-    while ($person = db_fetch_object($r_per)) {
-      $people[$person->nid] = $person->title;
-    }
-    
-    $options = $options + array(-1 => '-PEOPLE-') + $people;
-  }
-  
-  if (module_exists('stormteam')) {
-    $s_team = "SELECT n.nid, n.title FROM {node} n INNER JOIN {stormteam} ste ON n.vid=ste.vid WHERE n.type='stormteam' ORDER BY n.title";
-    $s_team = stormteam_access_sql($s_team);
-    $s_team = db_rewrite_sql($s_team);
-    $r_team = db_query($s_team);
-    $teams = array();
-    while ($team = db_fetch_object($r_team)) {
-      $teams[$team->nid] = $team->title;
-    }
-    
-    $options = $options + array(-2 => '-TEAMS-') + $teams;
-  }
-  
   $form['group5']['assigned_nid'] = array(
     '#type' => 'select',
     '#title' => t('Assigned to'),
