Drupal Creating Votes in Voting API Through Code

2.5k views Asked by At

I have a custom module I'm writing, part of what I want it to do is create a vote associated with a node, I'm trying to figure out how to call the voting API from my module. I loookd in the documentation but it's a little sparse.

2

There are 2 answers

1
Matt V. On BEST ANSWER

Here is an example from a module I wrote a while ago.

while ($data = db_fetch_object($result)) {
  $node = node_load($data->nid);
  $node_terms = taxonomy_node_get_terms($node);
  $vote['value'] = 0;
  $vote['value_type'] = 'points';
  foreach ($node_terms as $term) {
    $vote['value'] = $vote['value'] + $users_tags[$term->name];
  }
  $vote['content_id'] = $node->nid;
  if (isset($vote['content_id'])) {
    votingapi_set_votes($vote);
  }
}
0
NickOpris On

Just another example of using this:

function _ept_set_vote($nid, $status, $uid = NULL) {
  global $user;

  $vote = array(
    array(
      'entity_type' => 'node',
      'value' => 1,
      'entity_id' => $nid,
      'uid' => (!$uid) ? $user->uid : $uid,
      'tag' => $status
    )
  );

  votingapi_set_votes($vote, array());
}

I call it like this:

switch($task_status){
      case('start'):
        _ept_set_vote($nid, "Start");
        break;
      case('completed'):
        _ept_set_vote($nid, "Completed");
        break;
    }