You are here

function advpoll_voting_binary_form_validate in Advanced Poll 6.2

Same name and namespace in other branches
  1. 5 modes/binary.inc \advpoll_voting_binary_form_validate()
  2. 6.3 modes/binary.inc \advpoll_voting_binary_form_validate()
  3. 6 modes/binary.inc \advpoll_voting_binary_form_validate()

Check if the submitted key exists, just to make sure the form is not bypassed.

@returns boolean true if the form is valid

File

modes/binary.inc, line 224
Handle binary (true/false) votes.

Code

function advpoll_voting_binary_form_validate($form, &$form_state) {
  $node = node_load($form_state['values']['nid']);
  $ajax = $form_state['values']['ajax'];

  // Check if user is eligible to vote
  if (!advpoll_eligible($node)) {
    _advpoll_form_set_error('choice[', t('You are not allowed to vote in this poll.'), $ajax);
  }

  // Check if poll is active
  if (!_advpoll_is_active($node)) {
    _advpoll_form_set_error('choice[', t('This poll is closed.'), $ajax);
  }

  // Whether the write-in option is selected. This is calculated differently for
  // radio buttons and checkboxes.
  $writein_option = FALSE;
  $writein_text = isset($form_state['values']['writein_key']) ? $form_state['values']['writein_choice'] : '';

  // Check if user has already voted
  list($voted, $cancel_vote) = _advpoll_user_voted($node->nid);
  if ($voted) {
    _advpoll_form_set_error('choice[', t('You have already voted in this poll.'), $ajax);

    // Redirect to the current poll node to view the poll result instead of the voting form. This is only
    // initiated for non-Ajax voting.
    drupal_goto('node/' . $node->nid);
  }
  if ($node->max_choices == 1) {

    // Plurality voting
    // Write-ins are enabled, user has permission, and it's the write-in option.
    if ($node->writeins && user_access('add write-ins') && $form_state['values']['choice'] == $form_state['values']['writein_key']) {

      // Set the flag to true for additional checks.
      $writein_option = TRUE;
    }
    elseif (!isset($node->choice[$form_state['values']['choice']])) {

      // Nothing is selected.
      _advpoll_form_set_error('choice[', t('At least one choice must be selected.'), $ajax);
    }
  }
  else {

    // Approval voting.
    $num_choices = 0;
    foreach ($node->choice as $i => $val) {

      // See if the box is checked.
      if ($form_state['values']['choice'][$i]) {
        $num_choices++;
      }
    }

    // Write-ins are enabled, user has permission, and the write-in box is checked.
    if ($node->writeins && user_access('add write-ins') && $form_state['values']['choice'][$form_state['values']['writein_key']]) {

      // Add one to number of choices for check on min/max boxes checked.
      $num_choices++;

      // Set the flag to true for additional checks.
      $writein_option = TRUE;
    }

    // Too many choices ranked.
    if ($node->max_choices != 0 && $num_choices > $node->max_choices) {
      $message = t('%num choices were selected but only %max are allowed.', array(
        '%num' => $num_choices,
        '%max' => $node->max_choices,
      ));
      _advpoll_form_set_error('choice[', $message, $ajax);
    }

    // Not enough choices ranked.
    $min_choices = 1;
    if ($num_choices < $min_choices) {
      _advpoll_form_set_error('choice[', t('At least one choice must be selected.'), $ajax);
    }
  }

  // Do validation specific to writeins.
  _advpoll_writeins_voting_form_validate($node, $writein_option, $writein_text, $ajax);
}