You are here

function poll_form in Drupal 6

Same name and namespace in other branches
  1. 4 modules/poll.module \poll_form()
  2. 5 modules/poll/poll.module \poll_form()
  3. 7 modules/poll/poll.module \poll_form()

Implementation of hook_form().

File

modules/poll/poll.module, line 181
Enables your site to capture votes on different topics in the form of multiple choice questions.

Code

function poll_form(&$node, $form_state) {
  global $user;
  $admin = user_access('administer nodes') || user_access('edit any poll content') || user_access('edit own poll content') && $user->uid == $node->uid;
  $type = node_get_types('type', $node);
  $form = array(
    '#cache' => TRUE,
  );
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => -5,
  );
  if (isset($form_state['choice_count'])) {
    $choice_count = $form_state['choice_count'];
  }
  else {
    $choice_count = max(2, empty($node->choice) ? 2 : count($node->choice));
  }

  // Add a wrapper for the choices and more button.
  $form['choice_wrapper'] = array(
    '#tree' => FALSE,
    '#weight' => -4,
    '#prefix' => '<div class="clear-block" id="poll-choice-wrapper">',
    '#suffix' => '</div>',
  );

  // Container for just the poll choices.
  $form['choice_wrapper']['choice'] = array(
    '#prefix' => '<div id="poll-choices">',
    '#suffix' => '</div>',
    '#theme' => 'poll_choices',
  );

  // Add the current choices to the form.
  for ($delta = 0; $delta < $choice_count; $delta++) {
    $text = isset($node->choice[$delta]['chtext']) ? $node->choice[$delta]['chtext'] : '';
    $votes = isset($node->choice[$delta]['chvotes']) ? $node->choice[$delta]['chvotes'] : 0;
    $form['choice_wrapper']['choice'][$delta] = _poll_choice_form($delta, $text, $votes);
  }

  // We name our button 'poll_more' to avoid conflicts with other modules using
  // AHAH-enabled buttons with the id 'more'.
  $form['choice_wrapper']['poll_more'] = array(
    '#type' => 'submit',
    '#value' => t('More choices'),
    '#description' => t("If the amount of boxes above isn't enough, click here to add more choices."),
    '#weight' => 1,
    '#submit' => array(
      'poll_more_choices_submit',
    ),
    // If no javascript action.
    '#ahah' => array(
      'path' => 'poll/js',
      'wrapper' => 'poll-choices',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );

  // Poll attributes
  $_duration = array(
    0 => t('Unlimited'),
  ) + drupal_map_assoc(array(
    86400,
    172800,
    345600,
    604800,
    1209600,
    2419200,
    4838400,
    9676800,
    31536000,
  ), "format_interval");
  $_active = array(
    0 => t('Closed'),
    1 => t('Active'),
  );
  if ($admin) {
    $form['settings'] = array(
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#title' => t('Poll settings'),
      '#weight' => -3,
    );
    $form['settings']['active'] = array(
      '#type' => 'radios',
      '#title' => t('Poll status'),
      '#default_value' => isset($node->active) ? $node->active : 1,
      '#options' => $_active,
      '#description' => t('When a poll is closed, visitors can no longer vote for it.'),
    );
  }
  $form['settings']['runtime'] = array(
    '#type' => 'select',
    '#title' => t('Poll duration'),
    '#default_value' => isset($node->runtime) ? $node->runtime : 0,
    '#options' => $_duration,
    '#description' => t('After this period, the poll will be closed automatically.'),
  );
  return $form;
}