You are here

function rate_save_vote in Rate 6.2

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

Save a vote to the database.

Parameters

object $widget:

string $content_type:

int $content_id:

int $value:

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 478
Rate module

Code

function rate_save_vote($widget, $content_type, $content_id, $value, $ahah = 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.
  static $saved = FALSE;
  if ($saved) {
    return;
  }
  $saved = TRUE;

  // Determine if the user may vote.
  $node = $content_type == 'node' ? node_load($content_id) : NULL;
  $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:
        drupal_set_message(t('You must login before you can vote.'));

        // 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', array(
            'query' => $query,
            'absolute' => TRUE,
          ));
          module_invoke_all('exit') & exit;
        }
        else {
          drupal_goto('user', http_build_query($query, NULL, '&'));
        }
        break;
      case RATE_NOPERM_REDIRECT_WITHOUT_MESSAGE:
        $query = array(
          'destination' => $_GET['q'],
        );
        if ($ahah) {
          print url('user', array(
            'query' => $query,
            'absolute' => TRUE,
          ));
          module_invoke_all('exit') & exit;
        }
        else {
          drupal_goto('user', http_build_query($query, NULL, '&'));
        }
        break;
      default:
        return;
    }
  }
  $votes = array(
    'content_type' => $content_type,
    'content_id' => $content_id,
    'value_type' => $widget->value_type,
    'value' => $value,
    'tag' => $widget->tag,
  );

  // Call hook_rate_vote_alter().
  $redirect = FALSE;
  $save = TRUE;
  $context = array(
    'redirect' => &$redirect,
    'save' => &$save,
    'widget' => $widget,
  );
  drupal_alter('rate_vote', $votes, $context);
  if ($save) {
    votingapi_set_votes($votes);
  }
  if ($redirect) {
    preg_match('/^(.+?)(\\?(.*?))?(\\#(.*))?$/', $redirect, $parts);
    $path = $parts[1];
    $query = isset($parts[3]) ? $parts[3] : '';
    $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);
    }
  }
}