Drupal Creating Votes in Voting API Through Code

為{幸葍}努か 提交于 2019-12-06 07:13:49

问题


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.


回答1:


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);
  }
}



回答2:


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;
    }


来源:https://stackoverflow.com/questions/5124627/drupal-creating-votes-in-voting-api-through-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!