You are here

function advpoll_voting_ranking_form in Advanced Poll 5

Same name and namespace in other branches
  1. 6.3 modes/ranking.inc \advpoll_voting_ranking_form()
  2. 6 modes/ranking.inc \advpoll_voting_ranking_form()
  3. 6.2 modes/ranking.inc \advpoll_voting_ranking_form()

File

modes/ranking.inc, line 18

Code

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

    // Generate the list of possible rankings
    $choices[0] = '--';
    for ($i = 1; $i <= $num_choices; $i++) {
      if ($i == 1) {
        $val = t('1st');
      }
      elseif ($i == 2) {
        $val = t('2nd');
      }
      elseif ($i == 3) {
        $val = t('3rd');
      }
      else {
        $val = t($i . 'th');
      }
      $choices[$i] = $val;
    }

    // Fix to work around limitations in the current translation system. By
    // listing the strings here they are made immediately available for
    // translating. Listing up to 15 here, as it should be enough for most
    // users. If more are needed they are made available for translating in
    // Drupal when a poll with more than 15 choices has been created.
    // TODO: Find a better solution for this.
    array(
      t('4th'),
      t('5th'),
      t('6th'),
      t('7th'),
      t('8th'),
      t('9th'),
      t('10th'),
      t('11th'),
      t('12th'),
      t('13th'),
      t('14th'),
      t('15th'),
    );
    $form['choice'] = array(
      '#tree' => TRUE,
      // XXX: Workaround for FormAPI bug in PHP 4, see http://drupal.org/node/86657.
      '#type' => 'checkboxes',
      '#prefix' => '<div class="vote-choices">',
      '#suffix' => '</div>',
    );

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

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

    // Add write-in select box if write-ins are enabled and user has permission.
    if ($node->writeins && user_access('add write-ins')) {
      $form['choice'][$key + 1] = array(
        '#type' => 'select',
        '#title' => t('(write-in)'),
        '#options' => $choices,
      );

      // Key index of the write-in option.
      $form['writein_key'] = array(
        '#type' => 'value',
        '#value' => $key + 1,
      );
    }
  }

  // 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(
      '#prefix' => '<div class="writein-choice">',
      '#suffix' => '</div>',
      '#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->in_preview && advpoll_eligible($node) && $status == 'open') {
    static $ranking_vote_count = 0;
    $form['vote'] = array(
      '#type' => 'submit',
      '#value' => t('Vote'),
      '#attributes' => array(
        'id' => 'edit-vote-rank-' . $ranking_vote_count++,
      ),
    );
  }
  elseif ($node->in_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', drupal_get_destination()),
    ));
    $form['message']['#value'] = $user->uid ? t('You are not eligible to vote in this poll.') : $login_message;
  }
  $form['#action'] = url('node/' . $node->nid);
  return $form;
}