You are here

function advpoll_voting_ranking_form in Advanced Poll 6

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

File

modes/ranking.inc, line 23
Handle ranking votes, e.g. choice A is preferred over choice B, which in turn is preferred over choice C.

Code

function advpoll_voting_ranking_form(&$form_state, $node, $teaser, $page, $status) {
  static $ranking_form_count = 0;
  $form = array(
    '#id' => 'advpoll_voting_ranking_form-' . $ranking_form_count++,
    '#attributes' => array(
      'class' => 'advpoll-vote',
    ),
    '#node' => $node,
  );

  // Add write-in select box if write-ins are enabled and user has permission.
  $handle_writeins = $node->writeins && user_access('add write-ins');
  $form['ajax'] = array(
    '#type' => 'hidden',
    '#attributes' => array(
      'class' => 'ajax',
    ),
  );
  $form['#attributes']['class'] .= ' drag-and-drop';
  $form['js_order'] = array(
    '#type' => 'hidden',
    '#value' => '',
  );
  if ($node->max_choices) {
    $max_choices = $node->max_choices;
  }
  else {
    $max_choices = count($node->choice) - $node->writein_choices;

    /*if ($handle_writeins) {
        $max_choices++;
      }
      */
  }
  $form['max_choices'] = array(
    '#type' => 'hidden',
    '#value' => $max_choices,
  );

  // TODO: figure out why this is here. shouldn't every poll have choices?
  if (isset($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'),
    );

    // List of poll choices, to be populated.
    $form['choice'] = array(
      '#tree' => TRUE,
    );

    // If previewing check the format against the current users permissions.
    $check = $node->build_mode == NODE_BUILD_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,
        );
      }
    }
    if ($handle_writeins) {
      $form['choice'][$key + 1] = array(
        '#type' => 'select',
        '#title' => t('(write-in)'),
        '#options' => $choices,
        '#attributes' => array(
          'class' => 'advpoll-writeins',
        ),
      );

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

  // Add write-in text field.
  if ($handle_writeins) {
    $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 $ranking_vote_count = 0;
    $form['vote'] = array(
      '#type' => 'submit',
      '#value' => t('Vote'),
      '#id' => 'edit-vote-rank-' . $ranking_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'] = $user->uid ? t('You are not eligible to vote in this poll.') : $login_message;
  }
  $form['#action'] = url('node/' . $node->nid);

  // Set form caching because we could have multiple forms on the page.
  // (from poll.module).
  $form['#cache'] = TRUE;
  return $form;
}