function pm_get_assignment_options in Drupal PM (Project Management) 7
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 team, 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
If $organization_nid is provided but $project_nid is omitted, then the logic is as above, just for all projects of the given organization.
3 calls to pm_get_assignment_options()
- pmproject_form in pmproject/
pmproject.module - Implements hook_form().
- pmtask_form in pmtask/
pmtask.module - Implements hook_form().
- pmticket_form in pmticket/
pmticket.module - Implements hook_form().
File
- ./
pm.module, line 896 - Main module file for the Project Management module.
Code
function pm_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[0] = t('- unassigned -');
if (module_exists('pmteam')) {
$options[$teams] = array();
}
if (module_exists('pmperson')) {
$options[$people] = array();
}
$add_org_people = TRUE;
if ($organization_nid) {
$add_org_people = FALSE;
$manager = array();
$projects = array();
if ($project_nid) {
$projects[] = $project_nid;
}
else {
$query = db_select('node', 'n');
$query
->join('pmproject', 'pro', 'n.vid = pro.vid');
$query
->fields('n', array(
'nid',
))
->condition('n.status', 1)
->condition('n.type', 'pmproject')
->condition('pro.organization_nid', $organization_nid)
->addTag('node_access');
$result = $query
->execute();
foreach ($result as $project) {
$projects[] = $project->nid;
}
$add_org_people = TRUE;
}
foreach ($projects as $pid) {
$project = node_load($pid);
if ($project->manager_nid) {
$manager[] = node_load($project->manager_nid);
}
if ($project->assigned_nid) {
$node = node_load($project->assigned_nid);
if ($node->type == 'pmperson') {
if (module_exists('pmperson')) {
$options[$people][$node->nid] = check_plain($node->title);
}
}
else {
// ($node->type == 'pmteam')
if (module_exists('pmteam')) {
$options[$teams][$node->nid] = check_plain($node->title);
foreach ($node->members_array as $nid => $name) {
// do not add deactivated persons
if (!array_key_exists($nid, $node->members_deactivated_array)) {
$options[$people][$nid] = check_plain($name);
}
}
}
}
}
else {
$add_org_people = TRUE;
}
}
}
if ($add_org_people) {
if (module_exists('pmperson')) {
$query = db_select('node', 'n');
$query
->join('pmperson', 'per', 'n.vid = per.vid');
$query
->fields('n', array(
'title',
))
->fields('per', array(
'nid',
))
->addTag('node_access')
->orderBy('title', 'ASC');
if (isset($organization_nid) && $organization_nid != 0) {
$query
->condition('per.organization_nid', $organization_nid);
}
$result = $query
->execute();
foreach ($result as $person) {
$options[$people][$person->nid] = check_plain($person->title);
if (empty($options[$people][$person->nid])) {
$options[$people][$person->nid] = t('Person !nid', array(
'!nid' => $person->nid,
));
}
}
}
}
else {
if (isset($manager) && module_exists('pmperson')) {
foreach ($manager as $manager_node) {
if (!array_key_exists($manager_node->nid, $options[$people])) {
$options[$people][$manager_node->nid] = check_plain($manager_node->title);
}
}
}
}
if (module_exists('pmteam')) {
$query = db_select('node', 'n');
$query
->join('pmteam', 'team', 'n.vid = team.vid');
$query
->fields('n', array(
'nid',
'vid',
'title',
))
->condition('n.type', 'pmteam')
->addTag('node_access')
->groupBy('team.nid')
->orderBy('title', 'ASC');
// Do not add teams, which we have already added
if (!empty($options[$teams])) {
$query
->condition('team.nid', $options[$teams], 'NOT IN');
}
// Only add teams that have at least the same members as persons we added so far
if (!empty($organization_nid) && !empty($options[$people])) {
// Add all persons that should be in the team
$query
->condition('team.mnid', array_keys($options[$people]), 'IN');
// Add a count, how many of the given persons should be in the other teams
$count = count($options[$people]);
$query
->having('count(team.nid) = :count', array(
':count' => $count,
));
}
$result = $query
->execute();
foreach ($result as $team) {
$options[$teams][$team->nid] = check_plain($team->title);
}
}
if (isset($options[$people]) && array_key_exists(0, $options[$people])) {
unset($options[$people][0]);
}
if (isset($options[$teams]) && array_key_exists(0, $options[$teams])) {
unset($options[$teams][0]);
}
if (isset($options[$people]) && !sizeof($options[$people])) {
unset($options[$people]);
}
if (isset($options[$teams]) && !sizeof($options[$teams])) {
unset($options[$teams]);
}
// SORT OPTIONS
if (!empty($options[$people]) && is_array($options[$people])) {
asort($options[$people], SORT_LOCALE_STRING);
}
if (!empty($options[$teams]) && is_array($options[$teams])) {
asort($options[$teams], SORT_LOCALE_STRING);
}
return $options;
}