You are here

function rate_save_vote in Rate 7

Same name and namespace in other branches
  1. 6.2 rate.module \rate_save_vote()

Save a vote to the database.

Parameters

object $widget:

string $content_type:

int $content_id:

int $value:

bool $ahah:

bool $reset:

2 calls to rate_save_vote()
rate_generate_widget in ./rate.module
Generate a widget.
rate_vote_ahah in ./rate.module
AHAH callback for the vote buttons.

File

./rate.module, line 576
Rate module

Code

function rate_save_vote($widget, $content_type, $content_id, $value, $ahah = FALSE, $reset = FALSE) {

  // Prevent votes from saved twice. This does not check for different widgets /
  // content id's, but it's only possible to save a single vote per request for now,
  // unless one uses the $reset flag with a TRUE value
  static $saved = FALSE;
  if ($saved && !$reset) {
    return;
  }
  $saved = TRUE;
  module_load_include('inc', 'rate', 'rate.bots');
  if (rate_bots_is_blocked()) {
    if (variable_get(RATE_VAR_BOT_RETROACTIVE, TRUE)) {
      $ip = ip_address();
      if (!rate_bots_is_local($ip)) {
        $queue = DrupalQueue::get('rate_delete_votes');
        $queue
          ->createQueue();
        $queue
          ->createItem($ip);
      }
    }
    return;
  }

  // Check if we should use the source translation.
  if ($widget->use_source_translation) {
    $content_id = _rate_get_source_translation($content_type, $content_id);
  }

  // Determine if the user may vote.
  $entities = entity_load($content_type, array(
    $content_id,
  ));
  $node = $entities[$content_id];
  $permission_status = _rate_check_permissions($widget, $node);

  // This option should be available, check for legacy.
  isset($widget->noperm_behaviour) or $widget->noperm_behaviour = RATE_NOPERM_REDIRECT_WITH_MESSAGE;
  if ($permission_status != RATE_PERMISSION_OK) {
    switch ($widget->noperm_behaviour) {
      case RATE_NOPERM_REDIRECT_WITH_MESSAGE:

        // Redirects via AHAH have a destination param to prevent redirecting to the AJAX callback.
        $destination = isset($_GET['destination']) ? $_GET['destination'] : $_GET['q'];

        // Strip off protocol domain name to prevent long query strings in URL.
        $destination = preg_replace('/http:\\/\\/[^\\/]+\\//', '', $destination);
        $query = array(
          'destination' => $destination,
        );
        if ($ahah) {
          print url('user/login-to-rate', array(
            'query' => $query,
            'absolute' => TRUE,
          ));
          module_invoke_all('exit') & exit;
        }
        else {
          drupal_goto('user/login-to-rate', array(
            'query' => $query,
          ));
        }
        break;
      case RATE_NOPERM_REDIRECT_WITHOUT_MESSAGE:
        $destination = isset($_GET['destination']) ? $_GET['destination'] : $_GET['q'];
        $query = array(
          'destination' => $destination,
        );
        if ($ahah) {
          print url('user', array(
            'query' => $query,
            'absolute' => TRUE,
          ));
          module_invoke_all('exit') & exit;
        }
        else {
          drupal_goto('user', array(
            'query' => $query,
          ));
        }
        break;
      default:
        return;
    }
  }
  $votes = array(
    'entity_type' => $content_type,
    'entity_id' => $content_id,
    'value_type' => $widget->value_type,
    'value' => $value,
    'tag' => $widget->tag,
  );
  $criteria = NULL;

  // Call hook_rate_vote_alter().
  $redirect = FALSE;
  $save = TRUE;
  $context = array(
    'redirect' => &$redirect,
    'save' => &$save,
    'criteria' => &$criteria,
    'widget' => $widget,
  );
  drupal_alter('rate_vote', $votes, $context);
  if ($save) {
    votingapi_set_votes($votes, $criteria);
  }
  if ($redirect) {
    preg_match('/^(.+?)(\\?(.*?))?(\\#(.*))?$/', $redirect, $parts);
    $path = $parts[1];
    if (isset($parts[3])) {
      parse_str($parts[3], $query);
    }
    else {
      $query = array();
    }
    $fragment = isset($parts[5]) ? $parts[5] : '';
    if ($ahah) {
      print url($path, array(
        'absolute' => TRUE,
        'query' => $query,
        'fragment' => $fragment,
      ));
      module_invoke_all('exit') & exit;
    }
    else {
      drupal_goto($path, $query, $fragment);
    }
  }
}