You are here

function advpoll_voting_ranking_form_validate in Advanced Poll 5

Same name and namespace in other branches
  1. 6.3 modes/ranking.inc \advpoll_voting_ranking_form_validate()
  2. 6 modes/ranking.inc \advpoll_voting_ranking_form_validate()
  3. 6.2 modes/ranking.inc \advpoll_voting_ranking_form_validate()

Implementation of the vote validation hook for the runoff module.

This checks if the submitted values are within range, if they are not empty, and if they are not repeated.

@returns boolean false on invalid forms, true otherwise.

File

modes/ranking.inc, line 659

Code

function advpoll_voting_ranking_form_validate($form_id, $form_values) {
  $node = node_load($form_values['nid']);
  $ajax = $form_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.
  $writein_option = FALSE;
  $writein_text = $form_values['writein_key'] ? $form_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);
  }

  // Array used to check which values are set.
  $set_values = array();
  $num_choices = 0;

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

    // Set a flag for additional checks.
    $writein_option = TRUE;
  }
  foreach ($node->choice as $key => $choice) {

    // Count the number of choices that are ranked.
    if ($form_values['choice'][$key]) {
      $num_choices++;
    }
    $int_value = intval($form_values['choice'][$key]);

    // Mark this value as seen
    $set_values[$int_value]++;

    // Check range
    if ($int_value > count($node->choice) || $int_value < 0) {

      // TODO: clean up this error message
      $message = "Illegal rank for choice {$key}: {$int_value} (min: 1, max: " . count($node->choice) . ')';
      _advpoll_form_set_error('choice][' . $key, $message, $ajax);
    }
  }

  // Write-ins are enabled, user has permission, and the write-in box is checked.
  if ($writein_option) {
    $int_value = intval($form_values['choice'][$form_values['writein_key']]);

    // Mark this value as seen
    $set_values[$int_value]++;

    // Check range
    if ($int_value > count($node->choice) || $int_value < 0) {

      // TODO: clean up this error message
      $message = "Illegal rank for the write-in choice: {$int_value} (min: 1, max: " . count($node->choice) . ')';
      _advpoll_form_set_error('choice][' . $form_values['writein_key'], $message, $ajax);
    }
  }

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

  // 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);
  }

  // Check that multiple choices are not set to the same value.
  foreach ($set_values as $val => $count) {
    if ($val != 0 && $count > 1) {
      $message = t('Multiple choices given the rank of %value.', array(
        '%value' => $val,
      ));
      _advpoll_form_set_error('choice', $message, $ajax);
    }
  }
}