You are here

function votingapiactivity_votingapi_insert in Activity 6

Same name and namespace in other branches
  1. 5.4 contrib/votingapiactivity/votingapiactivity.module \votingapiactivity_votingapi_insert()
  2. 5.2 contrib/votingapiactivity.module \votingapiactivity_votingapi_insert()
  3. 5.3 contrib/votingapiactivity/votingapiactivity.module \votingapiactivity_votingapi_insert()

Implementation of hook_votingapi_insert()

File

contrib/votingapiactivity/votingapiactivity.module, line 90

Code

function votingapiactivity_votingapi_insert($votes) {
  foreach ($votes as $vote) {

    // Check if both type and operation are
    // enabled for activity. If not then stop here
    if (!in_array($vote['tag'], variable_get('votingapiactivity_token_types', array(
      $vote['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 ($vote['uid'] == 0 || !($user = user_load(array(
      'uid' => $vote['uid'],
    )))) {
      return FALSE;
    }

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

    // User hide activity permission check
    if (user_access('hide activity', $user)) {
      return FALSE;
    }

    // Only handle votes on nodes or comments for now
    if ($vote['content_type'] == 'node') {
      $content = node_load($vote['content_id']);
    }
    else {
      if ($vote['content_type'] == 'comment') {
        $content = _comment_load($vote['content_id']);
        $content->type = t('comment');
        $content->title = $content->subject;
      }
      else {
        return FALSE;
      }
    }
    $data = array(
      'content-id' => $vote['content_id'],
      'content-nid' => $content->nid,
      'content-type' => $content->type,
      'content-title' => $content->title,
    );
    if ($vote['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);
  }
}