You are here

function og_subscribe_user in Organic groups 6

Same name and namespace in other branches
  1. 5.8 og.module \og_subscribe_user()
  2. 5 og.module \og_subscribe_user()
  3. 5.2 og.module \og_subscribe_user()
  4. 5.3 og.module \og_subscribe_user()
  5. 5.7 og.module \og_subscribe_user()
  6. 6.2 og.module \og_subscribe_user()

Create a new membership for a given user to given group. Edits to membership should go through og_save_subscription(). No access control since this is an API function.

Return value

string 'approval', 'subscribed' or 'rejected' depending on the group's configuration.

3 calls to og_subscribe_user()
og_confirm_subscribe_submit in ./og.module
Confirm og membership submit handler
og_user in ./og.module
og_xmlrpc_subscribe_user in includes/og.xmlrpc.inc

File

./og.module, line 1022

Code

function og_subscribe_user($gid, $account, $request = NULL) {

  // moderated groups must approve all members (selective=1)
  $node = node_load($gid);
  switch ($node->og_selective) {
    case OG_MODERATED:
      $admins = array();
      og_save_subscription($gid, $account->uid, array(
        'is_active' => 0,
      ));
      $sql = og_list_users_sql(1, 1, NULL);
      $res = db_query($sql, $node->nid);
      $admins = array();
      while ($row = db_fetch_object($res)) {
        $admins[] = $row->uid;
      }
      if (!empty($admins)) {

        // Prepend user's request text with standard cruft. Should be a
        // variable but that's a bit annoying.
        if ($request) {
          $request = t("\n\nPersonal message from @name:\n------------------\n\n@request", array(
            '@name' => $account->name,
            '@request' => $request,
          ));
        }
        $variables = array(
          '@group' => $node->title,
          '@username' => $account->name,
          '!approve_url' => url("og/approve/{$node->nid}/{$account->uid}", array(
            'absolute' => TRUE,
          )),
          '!group_url' => url("og/users/{$node->nid}", array(
            'absolute' => TRUE,
          )),
          '@request' => $request,
        );
        $message = array(
          'subject' => _og_mail_text('og_request_user_subject', $variables),
          'body' => _og_mail_text('og_request_user_body', $variables) . $request,
        );

        // Send notifications to each admin; Sending an array of recipients
        // implies that this is a bulk message.
        module_invoke_all('og', 'user request', $gid, $admins, $message);
      }
      $return_value = array(
        'type' => 'approval',
        'message' => t('Membership request to the %group group awaits approval by an administrator.', array(
          '%group' => $node->title,
        )),
      );
      break;
    case OG_OPEN:
      og_save_subscription($gid, $account->uid, array(
        'is_active' => 1,
      ));
      $return_value = array(
        'type' => 'subscribed',
        'message' => t('You are now a member of the %group.', array(
          '%group' => $node->title,
        )),
      );
      break;
    case OG_CLOSED:
    case OG_INVITE_ONLY:

      // admins can add members to these groups, but others can't.
      if (og_is_group_admin($node)) {
        og_save_subscription($gid, $account->uid, array(
          'is_active' => 1,
        ));
      }
      else {
        $return_value = array(
          'type' => 'rejected',
          'message' => t('Membership request to the %group group was rejected, only group administrators can add users to this group.', array(
            '%group' => $node->title,
          )),
        );
      }
  }
  return $return_value;
}