You are here

function signup_sign_up_user in Signup 5.2

Same name and namespace in other branches
  1. 5 signup.module \signup_sign_up_user()
  2. 6.2 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.

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

1 call to signup_sign_up_user()
signup_form_submit in ./signup.module
Executes the user signup form

File

./signup.module, line 1756
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) {
  $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.
  if (!empty($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_num_rows(db_query("SELECT anon_mail FROM {signup_log} WHERE anon_mail = '%s' AND nid = %d", $signup_form['signup_anon_mail'], $node->nid))) {
      drupal_set_message(t('Anonymous user %email is already signed up for %title', array(
        '%email' => $signup_form['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.
    $query = 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']);
    if (db_num_rows($query)) {
      $user = db_fetch_object($query);
      drupal_set_message(t('User !user is already signed up for %title', array(
        '!user' => theme('username', $user),
        '%title' => $node->title,
      )), 'error');
      return FALSE;
    }
  }

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

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

    // Allow other modules to inject data into the user's signup data.
    $extra = module_invoke_all('signup_sign_up', $node, $user);
    $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);
    }
    $signup_form_data = serialize($signup_info);

    // Figure out if confirmation or reminder emails will be sent and
    // inform the user.
    $confirmation_email = $node->signup_send_confirmation ? '  ' . t('You will receive a confirmation email shortly which contains further information about this %node_type.', array(
      '%node_type' => node_get_types('name', $node->type),
    )) : '';
    $reminder_email = $node->signup_send_reminder ? '  ' . t('You will receive a reminder email !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),
    )) : '';

    // Insert the user into the signup_log.
    db_query("INSERT INTO {signup_log} (uid, nid, anon_mail, signup_time, form_data) VALUES (%d, %d, '%s', %d, '%s')", $signup_form['uid'], $signup_form['nid'], $signup_form['signup_anon_mail'], $curtime, $signup_form_data);
    $from = variable_get('site_mail', ini_get('sendmail_from'));

    // Get the hard-coded tokens provided by the signup module to use
    // for the confirmation and/or forwarding emails.  We need to create
    // an object representing the user's signup to get the right values.
    $signup = $user;
    $signup->form_data = $signup_info;
    if (!empty($signup_form['signup_anon_mail'])) {
      $signup->anon_mail = $signup_form['signup_anon_mail'];
    }
    $signup_tokens = _signup_get_email_tokens($node, $signup);

    // The right email to use (anon vs. auth) is one of the tokens.
    $user_mail = $signup_tokens['%user_mail'];
    $node_type_name = node_get_types('name', $node->type);

    // If a confirmation is to be sent, compose the mail message,
    // replace the tokens with the right values, and send it.
    if ($node->signup_send_confirmation && $user_mail) {
      $subject = t('Signup confirmation for !node_type: !title', array(
        '!node_type' => $node_type_name,
        '!title' => $node->title,
      ));
      $message = strtr($node->signup_confirmation_email, $signup_tokens);
      if (module_exists('token')) {

        // If the token.module is enabled, also handle any tokens it provides.
        $message = token_replace($message, 'node', $node);
      }
      drupal_mail('signup_confirmation_mail', $user_mail, $subject, $message, $from);
    }

    // If a forwarding email is to be sent, compose the mail message,
    // replace the tokens with the right values, and send it.
    if ($node->signup_forwarding_email) {
      $header = array(
        'From' => t('New !node_type Signup', array(
          '!node_type' => $node_type_name,
        )) . "<{$from}>",
      );
      $subject = t('Signup confirmation for !node_type: !title', array(
        '!node_type' => $node_type_name,
        '!title' => $node->title,
      ));
      $message = t('The following information was submitted as a signup for !title', array(
        '!title' => $node->title,
      ));
      if (_signup_get_node_scheduler($node) != 'none') {
        $message .= "\n\r" . t('Date/Time: !time', array(
          '!time' => $signup_tokens['%node_start_time'],
        ));
      }
      $message .= "\n\r\n\r" . t('Username: !name', array(
        '!name' => empty($user->uid) ? variable_get('anonymous', t('Anonymous')) : $user->name,
      ));
      if (!empty($user->uid)) {

        // For authenticated users, just include a link to their profile page.
        $message .= "\n\r" . t('Profile page: !url', array(
          '!url' => url('user/' . $user->uid, NULL, NULL, TRUE),
        ));
      }
      else {

        // For anonymous users, their email is all we've got, so disclose it.
        $message .= "\n\r" . t('E-mail: !email', array(
          '!email' => $user_mail,
        ));
      }
      if (!empty($signup_tokens['%user_signup_info'])) {
        $message .= "\n\r\n\r" . $signup_tokens['%user_signup_info'];
      }
      drupal_mail('signup_forwarding_mail', $node->signup_forwarding_email, $subject, $message, $from, $header);
    }
    drupal_set_message(t('Signup to !title confirmed.', array(
      '!title' => l($node->title, "node/{$node->nid}"),
    )) . $confirmation_email . $reminder_email);
    $node->signup_total++;
    if ($node->signup_close_signup_limit) {
      _signup_check_limit($node, 'total');
    }
  }
  else {
    drupal_access_denied();
  }
}