You are here

function advpoll_voting_binary_form in Advanced Poll 6.2

Same name and namespace in other branches
  1. 5 modes/binary.inc \advpoll_voting_binary_form()
  2. 6.3 modes/binary.inc \advpoll_voting_binary_form()
  3. 6 modes/binary.inc \advpoll_voting_binary_form()

Implementation of the view_voting hook for the poll module.

This creates a list of choices to allow the user to vote on choices.

File

modes/binary.inc, line 25
Handle binary (true/false) votes.

Code

function advpoll_voting_binary_form(&$form_state, $node, $teaser, $page, $status) {
  static $binary_form_count = 0;
  $form = array(
    '#id' => 'advpoll-voting-binary-form-' . $binary_form_count++,
    '#attributes' => array(
      'class' => 'advpoll-vote',
    ),
  );
  $form['ajax'] = array(
    '#type' => 'hidden',
    '#attributes' => array(
      'class' => 'ajax',
    ),
  );
  $form['#node'] = $node;
  if ($node->choice) {
    $list = array();

    // If previewing check the format against the current users permissions.
    $check = $node->build_mode == NODE_BUILD_PREVIEW;
    foreach ($node->choice as $i => $choice) {

      // Don't show blank choices or write-in votes if the setting is disabled.
      if ($choice['label'] && ($node->show_writeins || !$choice['writein'])) {
        $list[$i] = _advpoll_choice_markup($choice['label'], $node->format, $check) . ($choice['writein'] ? ' ' . t('(write-in)') : '');
      }
    }

    // Add write-in checkbox/radio if write-ins are enabled and user has permission.
    if ($node->writeins && user_access('add write-ins')) {
      $list[$i + 1] = t('(write-in)');
      $form['writein_key'] = array(
        '#type' => 'value',
        '#value' => $i + 1,
      );
    }
    $form['choice'] = array(
      '#options' => $list,
      '#tree' => TRUE,
    );
    $max_choices = $node->build_mode == NODE_BUILD_PREVIEW ? $node->settings['max_choices'] : $node->max_choices;
    if ($max_choices == 1) {

      // Plurality voting
      $form['choice']['#type'] = 'radios';
      $form['choice']['#default_value'] = -1;
    }
    else {

      // Approval voting
      $form['choice']['#type'] = 'checkboxes';
    }
  }

  // Add write-in text field if write-ins are enabled and user has permission.
  if ($node->writeins && user_access('add write-ins')) {
    $form['writein_choice'] = array(
      '#type' => 'textfield',
      '#title' => t('Write-in vote'),
      '#size' => 25,
    );
  }
  $form['nid'] = array(
    '#type' => 'hidden',
    '#value' => $node->nid,
    '#attributes' => array(
      'class' => 'edit-nid',
    ),
  );

  // Hide vote button if user can't vote and instead display appropriate message.
  if ($node->build_mode != NODE_BUILD_PREVIEW && advpoll_eligible($node) && $status == 'open') {
    static $binary_vote_count = 0;
    $form['vote'] = array(
      '#type' => 'submit',
      '#value' => t('Vote'),
      '#id' => 'edit-vote-binary-' . $binary_vote_count++,
    );
  }
  elseif ($node->build_mode == NODE_BUILD_PREVIEW) {

    // Display nothing.
  }
  elseif ($status == 'pending') {
    $form['message']['#value'] = t('This poll opens @time.', array(
      '@time' => format_date($node->start_date),
    ));
  }
  else {
    global $user;
    $login_message = t('<a href="@login">Login</a> to vote in this poll.', array(
      '@login' => url('user/login', array(
        'query' => drupal_get_destination(),
      )),
    ));
    $form['message']['#value'] = isset($user->uid) ? t('You are not eligible to vote in this poll.') : $login_message;
  }
  $form['#action'] = url('node/' . $node->nid);
  return $form;
}