You are here

votingapiactivity.module in Activity 5.4

File

contrib/votingapiactivity/votingapiactivity.module
View source
<?php

/**
 * Activity definition file
 *
 * This defines what hooks activity module should use
 */
function votingapiactivity_activity_info() {
  $tags = array();
  $query = db_query("SELECT DISTINCT tag FROM {votingapi_cache}");
  while ($result = db_fetch_array($query)) {
    $tags[$result['tag']] = $result['tag'];
  }
  return array(
    'ops' => array(
      'vote' => t('Vote'),
    ),
    'types' => $tags,
    'roles' => array(
      'author' => array(
        '#name' => t('Author'),
        '#description' => t('The person who voted'),
        '#default' => t('[author] voted on the [content-type] [content-link]'),
      ),
      'all' => array(
        '#name' => t('All'),
        '#description' => t('The general public.'),
        '#default' => t('[author-all] voted on the [content-type] [content-link]'),
      ),
    ),
  );
}

/** 
 * Implementation of hook_activityapi().
 */
function votingapiactivity_activityapi(&$activity, $op) {
  if ($op == 'load') {
    if ($activity['data']['module'] == 'votingapiactivity' && !node_access('view', node_load($activity['data']['content-nid']))) {
      $activity = array();
    }
  }
}

/**
 * Token module integration. Defines available default tokens.
 */
function votingapiactivity_token_list($type = 'all') {
  if ($type == 'votingapiactivity') {
    $tokens['votingapiactivity'] = array(
      'author-vote' => t('Value of the author\'s vote'),
      'content-type' => t('Content type that was voted on such as "story" or "image" or "comment"'),
      'content-id' => t('Id of the content that was voted upon'),
      'content-nid' => t('Id of the node that was voted upon'),
      'content-cid' => t('Id of the comment that was voted upon'),
      'content-title' => t('Title of the content that was voted upon'),
      'content-link' => t('Link to the content that was voted upon'),
      'content-vote-avg' => t('Average of all votes for this content'),
      'content-vote-count' => t('Count of all votes for this content'),
    );
    return $tokens;
  }
}
function votingapiactivity_token_values($type, $data = NULL, $options = array()) {
  if ($type == 'votingapiactivity' && !empty($data)) {
    $data['content-link'] = l($data['content-title'], 'node/' . $data['content-nid'], array(), NULL, 'comment-' . $data['content-cid']);
    $data['content-link'] = l($data['content-title'], 'node/' . $data['content-nid']);
    $author_vote = db_fetch_object(db_query('SELECT value FROM {votingapi_vote} WHERE uid = %d AND content_id = %d', $data['uid'], $data['content-id']));
    $data['author-vote'] = $author_vote->value;
    $content_votes = db_query('SELECT value, function FROM {votingapi_cache} WHERE content_id = %d', $data['content-id']);
    while ($row = db_fetch_object($content_votes)) {
      $content_vote[$row->function] = $row->value;
    }
    $data['content-vote-avg'] = $content_vote['average'];
    $data['content-vote-count'] = $content_vote['count'];
    $data['content-type'] = $data['content-type'] == t('comment') ? $data['content-type'] : theme('activity_node_type', $data['content-type']);
    $link_fragment = NULL;
    if ($data['content-type'] == t('comment')) {
      $link_fragment = 'comment-' . $data['content_id'];
    }
    $data['content-link'] = l($data['content-title'], 'node/' . $data['content-nid'], array(), NULL, $link_fragment);
    return $data;
  }
}

/**
 * Implementation of hook_votingapi_insert()
 */
function votingapiactivity_votingapi_insert() {
  $args = func_get_args();

  // Check if both type and operation are
  // enabled for activity. If not then stop here
  if (!in_array($args[0]->tag, variable_get('votingapiactivity_token_types', array(
    $args[0]->tag,
  )), TRUE) || !in_array('vote', variable_get('votingapiactivity_op_types', array(
    'vote',
  )), TRUE)) {
    return FALSE;
  }

  // let's not write a record if the voter is
  // anonymous or otherwise not valid
  if ($args[0]->uid == 0 || !($user = user_load(array(
    'uid' => $args[0]->uid,
  )))) {
    return FALSE;
  }

  // Privacy setting check
  if (activity_user_privacy_optout($user)) {
    return FALSE;
  }

  // Only handle votes on nodes or comments for now
  if ($args[0]->content_type == 'node') {
    $content = node_load($args[0]->content_id);
  }
  else {
    if ($args[0]->content_type == 'comment') {
      $content = _comment_load($args[0]->content_id);
      $content->type = t('comment');
      $content->title = $content->subject;
    }
    else {
      return FALSE;
    }
  }
  $data = array(
    'content-id' => $args[0]->content_id,
    'content-nid' => $content->nid,
    'content-type' => $content->type,
    'content-title' => $content->title,
  );
  if ($args[0]->content_type == 'comment') {
    $data['content-cid'] = $content->cid;
  }
  $target_users_roles = array(
    ACTIVITY_ALL => 'all',
    $user->uid => 'author',
  );
  activity_insert($user->uid, 'votingapiactivity', 'vote', 'vote', $data, $target_users_roles);
}

Functions

Namesort descending Description
votingapiactivity_activityapi Implementation of hook_activityapi().
votingapiactivity_activity_info Activity definition file
votingapiactivity_token_list Token module integration. Defines available default tokens.
votingapiactivity_token_values
votingapiactivity_votingapi_insert Implementation of hook_votingapi_insert()