1

I am trying to use the Voting API module to add a vote through a menu item. Meaning that when someone visits a menu item [the/menu/item] a vote is added to the database.

There is already the Voting API functions that does that. I don't understand what the votingapi_set_votes function in the Voting API module does.

I have read the website documentation however I don't understand what it does and how can I use it. And couldn't find an example of usage either.

I have tried the follwing to add a vote:

  $items['the/menu/path/1'] = array
  (
  'title' => 'Some title',
  'page callback' => 'my_page',
  'access callback' => TRUE
);

return $items;
}

function my_page(){

  $votes = array();
  $votes[] = array(
    'content_type' => 'news',
    'content_id' => 1025,
    'value_type' => 'points',
    'value' => '1',
  );

  $criteria = array(
    'content_type' => 'news',
    'content_id' => 1025,
    'function' => 'sum',
    'value_type' => 'points',
  );


  if (TRUE) {
    if (module_exists('votingapi')) {
      // die("test");
      votingapi_set_votes($votes);
      die();
      $result = (votingapi_select_results($criteria));
      print 'result is: '.$result[0][value];
      $result = '';
    }
  }
  return "true";
}

But it gives me an SQL error like so:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'content_type' in 'where clause': SELECT votingapi_vote.* FROM {votingapi_vote} votingapi_vote WHERE (content_type = :db_condition_placeholder_0) AND (content_id = :db_condition_placeholder_1) AND (value_type = :db_condition_placeholder_2) AND (uid = :db_condition_placeholder_3) ; Array ( [:db_condition_placeholder_0] => news [:db_condition_placeholder_1] => 1025 [:db_condition_placeholder_2] => points [:db_condition_placeholder_3] => 130 ) in votingapi_votingapi_storage_select_votes() (line 225 of C:\Users\tarek.ismail\public\egyptinnovate\sites\all\modules\contrib\votingapi\votingapi.module).

  • Welcome to Drupal Answers :) The summary for that function, Cast a vote on a particular piece of content., is as clear it can get - that's what the function does, it casts a vote on a particular piece of content. it's hard to imagine how it could be explained any more clearly than it already is, there isn't really any room for ambiguity. The docs itself page also has links to 2 example uses. For this question to be re-opened, i.e. for it to be useful to future visitors, please edit it to provide details about exactly which part(s) of that sentence you don't understand. Many thanks – Clive Apr 3 '16 at 10:04
  • Thank you Clive for the welcoming. What does is it mean to cast a vote in drupal. – user30102967 Apr 3 '16 at 11:47
  • I have updated the question please check it Clive. – user30102967 Apr 3 '16 at 11:57
  • Please review my edited version of your question, to ensure there is nothing I corrected you don't agree with. Also, please ADD the SQL error you get (at the end of your question). And also add a link to that "website documentation" you mentioned (I "think" I know which docu you mean, but we do not want to guess about questions ...) . Just trying to get you going in "asking questions" around here, OK? Extra hint: have a look at the Answers module (the 7.x-4.x version). By digging in its code you may get a better understanding of using the Voting API. – Pierre.Vriens Apr 3 '16 at 12:45
  • I agree with the editing of the question. The SQL error is at the end of the Code Block. and that's the link of the documentation (drupalcontrib.org/api/drupal/… ). I'll check the answers module. – user30102967 Apr 3 '16 at 12:55
0

Here is a working example:

function YourModule_menu(){
  $items = array();
  $items['the/menu/path/1'] = array
    (
    'title' => 'Voting button',
    'page callback' => '_YourModule_voting',
    'access callback' => TRUE
    'type' => MENU_CALLBACK,

    );
  return $items;
}

function _YourModule_voting(){
  $nid=arg(3);
  $votes = array(
    'entity_type' => 'node',
    'entity_id' => $nid,
    'value_type' => "points",
    'value' => 1,
    'tag' => "like",
    );

  $criteria = NULL;

  if (module_exists('votingapi')) {
      votingapi_set_votes($votes);
  }     

  return "true";
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.