You are here

function advpoll_ranking_submit in Advanced Poll 7

Same name and namespace in other branches
  1. 7.3 advpoll_ranking/advpoll_ranking.module \advpoll_ranking_submit()
  2. 7.2 advpoll_ranking/advpoll_ranking.module \advpoll_ranking_submit()
1 string reference to 'advpoll_ranking_submit'
advpoll_ranking_choice_form in advpoll_ranking/advpoll_ranking.module

File

advpoll_ranking/advpoll_ranking.module, line 312

Code

function advpoll_ranking_submit($form, &$form_state) {
  $data = advpoll_get_form_data($form_state);
  $count = count($data->choices);
  $nid = $form_state['build_info']['args'][0]->nid;
  $votes = array();

  // even though content type is advpoll, we'll track type as ranking for the purpose of
  // managing votes in the voting API table
  $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']);
      $writein_weight = $form_state['values']['write_in_weight'];

      // Sanitize and check to see if there's a valid write in afterward.
      // Note - no reason for a normal user to add markup so strip it.
      $writein = filter_xss($writein);
      $writein = check_plain($writein);

      // Check for conditions under which checking for write in is appropriate
      if ($writein_weight == 'no-js' || $writein_weight > 0) {
        if ($writein) {
          $writein_choice = advpoll_process_writein($nid, $writein, $data);
          $data->choices[] = $writein_choice;
          if ($writein_weight == 'no-js') {
            $writein_weight = 1;
          }
          $votes[] = array(
            'rank' => $writein_weight,
            'id' => $writein_choice['choice_id'],
          );
        }
        else {
          if ($writein_weight != 'no-js') {

            // assumes the user placed it in selection table but did not fill in the
            // form element.
            $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;
          }
        }
      }
    }
  }

  // If the poll only allows one vote but there is a vote, that is the write in value. No need to process.
  // Otherwise, we need to process it.
  if ($data->max_choices > 1 || !$votes) {
    $votes = advpoll_ranking_process_results($form_state['values']['choice'], $data->choices, $votes);
  }
  if (count($votes) > 0 && count($votes) <= $data->max_choices) {
    advpoll_ranking_process_votes($data, $nid, $votes);
    if ($data->behavior == 'borda') {
      $element['#markup'] = advpoll_display_borda_results($nid, $data);
    }
    else {
      $element['#markup'] = advpoll_display_runoff_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;
  }
}