You are here

function advpoll_form_submit in Advanced Poll 7.2

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

Submit handler for voting

1 string reference to 'advpoll_form_submit'
advpoll_choice_form in ./advpoll.module

File

./advpoll.module, line 620

Code

function advpoll_form_submit($form, &$form_state) {
  $data = advpoll_get_form_data($form_state);
  $count = count($data->choices);
  $votes = $form[$count]['#value'];
  $nid = $form_state['build_info']['args'][0]->nid;
  $writein = '';
  $message = advpoll_form_submit_check($data, $nid);
  if ($message) {
    $form['message'] = array(
      '#type' => 'markup',
      '#prefix' => '<div id="message">',
      '#suffix' => '</div>',
      '#markup' => $message,
    );
    return $form;
  }

  // check to see if a write-in exists and was filled in.
  if ($data->write_in) {
    if (isset($form_state['values']['write_in'])) {
      $writein = trim($form_state['values']['write_in']);

      // Sanitize and check to see if there's a valid write in afterward.
      // There is no reason for a user to be allowed to use html tags.
      $writein = filter_xss($writein);
      $writein = check_plain($writein);
      if ($writein) {
        $data->choices[] = advpoll_process_writein($nid, $writein, $data);
      }
      else {
        $form['message'] = array(
          '#type' => 'markup',
          '#prefix' => '<div id="message">',
          '#suffix' => '</div>',
          '#markup' => t('Please type in a valid write-in choice or select a different option.'),
        );
        return $form;
      }
    }
  }

  // data structure returned from form is based upon whether it was a radios
  // or checkbox element
  if ($data->max_choices > 1) {
    if ($writein) {
      unset($votes['write-in']);
      $votes[$writein] = $writein;
    }
    $selected = advpoll_checkbox_selected($data->choices, $votes);
  }
  else {
    if ($writein) {
      $votes = $writein;
    }
    $selected = advpoll_radio_selected($data->choices, $votes);
  }
  if (count($selected) > 0 && count($selected) <= $data->max_choices) {
    foreach ($selected as $item) {
      $vote = array();
      $vote['type'] = 'advpoll';
      $vote['tag'] = $item;
      $vote['nid'] = $nid;
      $vote['value'] = 1;
      $vote['mode'] = $data->mode;
      $vote['duration'] = $data->cookie_duration;
      advpoll_add_votes($vote);
    }
    $element['#markup'] = advpoll_display_results($nid, $data);
    return $element;
  }
  else {
    $form['message'] = array(
      '#type' => 'markup',
      '#prefix' => '<div id="message">',
      '#suffix' => '</div>',
      '#markup' => t('Select up to @quantity @votes.', array(
        '@quantity' => $data->max_choices,
        '@votes' => format_plural($data->max_choices, 'vote', 'votes'),
      )),
    );
    return $form;
  }
}