You are here

function advpoll_choice_form in Advanced Poll 7.3

Same name and namespace in other branches
  1. 7 advpoll.module \advpoll_choice_form()
  2. 7.2 advpoll.module \advpoll_choice_form()

Voting form for advanced poll.

Native ajax functionality is being used to generate write-in field as poll settings and form state dictate.

3 string references to 'advpoll_choice_form'
advpoll_cancel_vote_submit in ./advpoll.module
Submit function for cancelling a vote.
advpoll_form_submit in ./advpoll.module
Submit handler for voting.
advpoll_node_view in ./advpoll.module
Implements hook_node_view().

File

./advpoll.module, line 589

Code

function advpoll_choice_form($form, &$form_state, $values) {
  $data = advpoll_get_data($values);
  $count = count($data->choices);
  $options = array();
  $form['#id'] = 'advpoll-form-' . $values->nid;
  for ($i = 0; $i < $count; $i++) {
    if (!$data->choices[$i]['write_in']) {
      $options[$data->choices[$i]['choice_id']] = strip_tags($data->choices[$i]['choice']);
    }
  }
  $input_type = 'radios';
  if ($data->max_choices > 1) {
    $input_type = 'checkboxes';
  }
  if ($data->write_in && $data->mode != 'unlimited') {
    $options['write-in'] = t('Other (Write-in)');
    $form['choice_' . $count] = array(
      '#type' => $input_type,
      '#title' => '',
      '#options' => $options,
      '#ajax' => array(
        'callback' => 'advpoll_writein_callback',
        'wrapper' => 'advpoll-form-' . $values->nid,
        'effect' => 'fade',
      ),
    );
    if (isset($form_state['values'])) {
      foreach ($form_state['values'] as $key => $item) {
        if ($key == 'choice_' . $count) {
          break;
        }
      }
      $selected = FALSE;
      if ($input_type == 'radios') {
        if ($item === 'write-in') {
          $selected = TRUE;
        }
      }
      else {
        if ($item['write-in']) {
          $selected = TRUE;
        }
      }
      if ($selected) {
        $form['write_in'] = array(
          '#type' => 'textfield',
          '#element_validate' => array(
            'advpoll_writein_validate',
          ),
          '#prefix' => '<div class="advpoll-write-in">',
          '#suffix' => '</div>',
          '#size' => '30',
        );
      }
    }
  }
  else {
    $form['choice_' . $count] = array(
      '#type' => $input_type,
      '#title' => '',
      '#options' => $options,
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#ajax' => array(
      'callback' => 'advpoll_form_submit',
      'wrapper' => 'advpoll-form-' . $values->nid,
      'name' => 'submit1',
    ),
    '#id' => 'edit-submit-advpoll-' . $values->nid,
    '#value' => t('Vote'),
  );
  return $form;
}