You are here

function signup_sign_up_user in Signup 6

Same name and namespace in other branches
  1. 5.2 signup.module \signup_sign_up_user()
  2. 5 signup.module \signup_sign_up_user()
  3. 6.2 signup.module \signup_sign_up_user()
  4. 7 signup.module \signup_sign_up_user()

Signs up a user to a node.

Caution: This function does not perform any access checking.

NOTE: other modules can call this function. To do so, $signup_form must be as follows:

$signup_form['nid'] : nid of the node to which the user will be signed up $signup_form['uid'] : uid of the user to sign up $signup_form['signup_anon_mail'] : Optional. An email address of an anonymous user to sign up. Only include this if the user is not already registered with the site. $signup_form['uid'] will automatically be set to 0 if this element is passed in. NOTE: It's highly recommended to call the signup_validate_anon_email function in the external module's validation cycle or perform that function's validation logic prior to passing in this element! $signup_form['signup_form_data'] : an array of key/value pairs -- key is the data category, value is the user input

Parameters

$notify_user: When set to TRUE, confirmation messages are displayed via drupal_set_message() and confirmation/forwarding emails are sent if enabled for the current node.

$reset_node_load: When set to TRUE, node_load() is called with the optional reset argument as TRUE. This can sometimes be helpful when signups have been opened with signup_open_signup() since that function can cause static caching of the node object, thus leading to the $node->signup_status check here failing when it should not.

Return value

The signup id (SID) if the user was successfully signed up, FALSE if the user is already signed up or signups are not allowed on the given node.

2 calls to signup_sign_up_user()
signup_form_submit in includes/signup_form.inc
Submit handler for the user signup form.
signup_user in ./signup.module
Implementation of hook_user().

File

./signup.module, line 1215
The Signup module (http://drupal.org/project/signup) manages replies to nodes. In particular, it's good for event management. Signup supports sending reminder emails and automatically closing signups for nodes with a start time, via the Event…

Code

function signup_sign_up_user($signup_form, $notify_user = TRUE, $reset_node_load = FALSE) {
  if ($reset_node_load) {
    $node = node_load($signup_form['nid'], NULL, TRUE);
  }
  else {
    $node = node_load($signup_form['nid']);
  }

  // Since this is an API call, we need to validate that there are no
  // duplicate signups being generated, even though through the usual
  // UI, there's no way to reach this function if it's a duplicate.
  // How to find duplicates is different for anonymous and
  // authenticated signups.
  $signup_anon_mail = '';
  if (!empty($signup_form['signup_anon_mail'])) {
    $signup_anon_mail = $signup_form['signup_anon_mail'];

    // Ensure the uid is 0 for anonymous signups, even if it's not duplicate.
    $signup_form['uid'] = 0;

    // Now, see if this email is already signed-up.
    if (db_result(db_query("SELECT COUNT(*) FROM {signup_log} WHERE anon_mail = '%s' AND nid = %d", $signup_anon_mail, $node->nid))) {
      drupal_set_message(t('Anonymous user %email is already signed up for %title', array(
        '%email' => $signup_anon_mail,
        '%title' => $node->title,
      ), 'error'));
      return FALSE;
    }
  }
  else {

    // This query does the JOIN on {users} so we can avoid a full
    // user_load() just so theme('username') can have the data it
    // needs for the error message we might print out.
    $result = db_query("SELECT sl.uid, u.name FROM {signup_log} sl INNER JOIN {users} u ON sl.uid = u.uid WHERE sl.uid = %d AND sl.nid = %d", $signup_form['uid'], $signup_form['nid']);
    $account = db_fetch_object($result);
    if (!empty($account)) {
      drupal_set_message(t('User !user is already signed up for %title', array(
        '!user' => theme('username', $account),
        '%title' => $node->title,
      )), 'error');
      return FALSE;
    }
  }

  // If we made it this far, we're going to need the full user object.
  $account = user_load(array(
    'uid' => $signup_form['uid'],
  ));
  if ($node->signup_status) {

    // Grab the current time once, since we need it in a few places.
    $current_time = time();

    // Allow other modules to inject data into the user's signup data.
    $extra = module_invoke_all('signup_sign_up', $node, $account);
    $signup_info = array();
    if (!empty($signup_form['signup_form_data'])) {
      $signup_info = $signup_form['signup_form_data'];
    }
    if (!empty($extra)) {
      $signup_info = array_merge($signup_info, $extra);
    }

    // Figure out if confirmation or reminder emails will be sent and
    // inform the user.
    $confirmation_email = $node->signup_send_confirmation ? '  ' . t('A confirmation email will be sent shortly containing further information about this %node_type.', array(
      '%node_type' => node_get_types('name', $node->type),
    )) : '';
    $reminder_email = $node->signup_send_reminder ? '  ' . t('A reminder email will be sent !number !days before the %node_type.', array(
      '!number' => $node->signup_reminder_days_before,
      '!days' => format_plural($node->signup_reminder_days_before, 'day', 'days'),
      '%node_type' => node_get_types('name', $node->type),
    )) : '';

    // Construct the appropriate $signup object representing this signup.
    $signup = new stdClass();
    $signup->nid = $node->nid;

    // Grab the values from the $account object we care about
    foreach (array(
      'uid',
      'name',
      'mail',
    ) as $field) {
      $signup->{$field} = $account->{$field};
    }

    // Other special signup values.
    $signup->form_data = $signup_info;
    $signup->signup_time = $current_time;

    // By default, signups should count towards the limit.
    $signup->count_towards_limit = 1;
    if (!empty($signup_anon_mail)) {
      $signup->anon_mail = $signup_anon_mail;
    }

    // Invoke hook_signup_data_alter() to let other modules change this.
    drupal_alter('signup_data', $signup, $signup_form);

    // Insert the signup into the {signup_log} table.
    signup_save_signup($signup);

    // See if we should generate any notifications from this signup.
    if ($notify_user) {

      // Confirmation e-mail to the user who signed up.
      if ($node->signup_send_confirmation) {
        signup_send_confirmation_mail($signup, $node);
      }

      // Notification email to an administrator or coordinator.
      signup_send_forwarding_mail($signup, $node);

      // Message to the screen for the user who signed up.
      // First, allow other modules to alter the messages.
      $messages = array();
      $messages['initial'] = 'Signup to !title confirmed.';
      $messages['confirmation_email'] = $confirmation_email;
      $messages['reminder_email'] = $reminder_email;
      drupal_alter('signup_confirm_messages', $messages, $signup, $node);
      drupal_set_message(t($messages['initial'], array(
        '!title' => l($node->title, "node/{$node->nid}"),
      )) . $messages['confirmation_email'] . $messages['reminder_email']);
    }
    $node->signup_total++;
    if (!empty($signup->count_towards_limit)) {
      $node->signup_effective_total += $signup->count_towards_limit;
      if ($node->signup_close_signup_limit) {
        _signup_check_limit($node, 'total');
      }
    }
    return $signup->sid;
  }
  else {
    drupal_access_denied();
  }
}