You are here

function advpoll_form_submit in Advanced Poll 7.3

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

Submit handler for voting.

1 string reference to 'advpoll_form_submit'
advpoll_choice_form in ./advpoll.module
Voting form for advanced poll.

File

./advpoll.module, line 690

Code

function advpoll_form_submit($form, &$form_state) {

  // Validate the form.
  drupal_validate_form('advpoll_choice_form', $form, $form_state);

  // If there are errors, return the form to display the error messages.
  if (form_get_errors()) {
    $form_state['rebuild'] = TRUE;
    return $form;
  }
  $data = advpoll_get_form_data($form_state);
  $count = count($data->choices);
  $votes = $form['choice_' . $count]['#value'];
  $node = $form_state['build_info']['args'][0];
  $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) {
        $processed_writein = advpoll_process_writein($nid, $writein, $data);
        $writein_id = $processed_writein['choice_id'];
        $data->choices[] = $processed_writein;
      }
      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_id] = $writein_id;
    }
    $selected = advpoll_checkbox_selected($data->choices, $votes);
  }
  else {
    if ($writein) {
      $votes = $writein_id;
    }
    $selected = advpoll_radio_selected($data->choices, $votes);
  }
  if (count($selected) > 0 && count($selected) <= $data->max_choices) {
    foreach ($selected as $item) {
      $vote = array();
      $vote['type'] = 'node';
      $vote['nodetype'] = $node->type;
      $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;
  }
}