You are here

function commons_groups_first_contribution in Drupal Commons 7.3

When a user first creates content within a group, grant her the contributor role within that group.

3 calls to commons_groups_first_contribution()
commons_groups_node_insert in modules/commons/commons_groups/commons_groups.module
Implements hook_node_insert().
commons_groups_node_update in modules/commons/commons_groups/commons_groups.module
Implements hook_node_update().
commons_wikis_node_update in modules/commons/commons_wikis/commons_wikis.module
Implements hook_node_update().

File

modules/commons/commons_groups/commons_groups.module, line 926

Code

function commons_groups_first_contribution($account, $node) {

  // Find the groups that this piece of content belongs to.
  $groups = og_get_entity_groups('node', $node);

  // @todo: Make it work also with user-groups.
  if (!empty($groups['node'])) {
    $node_groups = array_values($groups['node']);

    // Find the groups that the node author belongs to.
    $account_groups = og_get_groups_by_user($account, 'node');
    if (!$account_groups) {
      $account_groups = array();
    }

    // For groups where this user is not already a member, add her to the group.
    // Anonymous users should never be added to a group automatically
    if ($account->uid == 0) {
      return;
    }
    $new_groups = array_diff($node_groups, $account_groups);
    if (!empty($new_groups)) {
      foreach ($new_groups as $new_group_nid) {
        og_group('node', $new_group_nid, array(
          'entity' => $account->uid,
        ));
      }
    }
  }
}