You are here

function photos_image_vote in Album Photos 7.3

Same name and namespace in other branches
  1. 6.2 inc/photos.image.inc \photos_image_vote()

Photos image vote. photos/image/%/vote

1 string reference to 'photos_image_vote'
photos_menu in ./photos.module
Implements hook_menu().

File

inc/photos.image.inc, line 170
Handles image page view(s) and content.

Code

function photos_image_vote($fid) {
  global $user;
  if (user_access('allowed to vote')) {
    switch (arg(4)) {
      case 'up':
        $value = 1;
        break;
      case 'down':
        $value = -1;
        break;
      default:
        exit;
    }
    $vote = array(
      'entity_type' => 'image',
      'entity_id' => $fid,
      'tag' => 'vote',
      'value_type' => 'points',
      'uid' => $user->uid,
      'value' => $value,
    );
    $t = votingapi_set_votes($vote);
    if ($t['image'][$fid][0]['value']) {
      $x['sum'] = $t['image'][$fid][2]['value'];
      $x['average'] = $t['image'][$fid][1]['value'];
      $x['count'] = $t['image'][$fid][0]['value'];
    }
  }
  if (!isset($_GET['destination'])) {
    drupal_add_http_header('Content-Type:', 'text/plain;');
    if ($x['count']) {
      foreach ($x as $key => $val) {
        $array[] = '"' . $key . '":' . $val;
      }
      echo '{' . implode(',', $array) . '}';
    }
    else {
      echo '{"count": 0}';
    }
  }
  else {
    if ($x['count']) {
      drupal_set_message(t('Vote success.'));
    }
    else {
      drupal_set_message(t('Vote failure.'));
    }
    return drupal_goto();
  }
}