You are here

function signup_sign_up_user in Signup 6.2

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 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 associative array of extra data, keyed by signup form pane id. The value is itself an array in the same format as a $form_values array, that is, form_element => value.

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.

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.

1 call to signup_sign_up_user()
signup_form_submit in includes/signup_form.inc
Submit handler for the user signup form.

File

./signup.module, line 1200
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) {
  $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_total = 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);

    // Altering modules may set $signup->block_signup = TRUE to prevent signup.
    if ($signup->block_signup == TRUE) {
      return FALSE;
    }

    // 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) {

      // Invoke hook_signup_form_data_display_alter() to let other modules
      // (in particular pane modules) alter the data for output.
      // This may involve internal data being removed.
      $form_data = $signup->form_data;
      drupal_alter('signup_form_data_display', $form_data, $node->nid, $signup->sid, $signup->uid, 'mail');
      $signup->form_data = $form_data;

      // 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.
      drupal_set_message(t('Signup to !title confirmed.', array(
        '!title' => l($node->title, "node/{$node->nid}"),
      )) . $confirmation_email . $reminder_email);
    }
    $node->signup_total++;
    if (!empty($signup->count_towards_total)) {
      $node->signup_effective_total += $signup->count_towards_total;
      if ($node->signup_close_signup_limit) {
        _signup_check_limit($node, 'total');
      }
    }
    return $signup->sid;
  }
  else {
    drupal_access_denied();
  }
}