You are here

API.txt in Voting API 6

Same filename and directory in other branches
  1. 8.3 API.txt
  2. 6.2 API.txt
  3. 7.3 API.txt
  4. 7.2 API.txt
What's this, now?
=================
VotingAPI provides a flexible, easy-to-use framework for rating, voting, moderation, and consensus-gathering modules in Drupal. It allows module developers to focus on their ideas (say, providing a 'rate this thread' widget at the bottom of each forum post) without worrying about the grunt work of storing votes, preventing ballot-stuffing, calculating the results, and so on.

VotingAPI handles three key things for module developers:

1) CRUD
Create/Retrieve/Update/Delete operations for voting data. The simplest modules only need to call two functions -- votingapi_set_vote() and votingapi_select_results() -- to use the API. Others can use finer-grain functions for complete control.

2) Calculation
Every time a user casts a vote, VotingAPI calculates the results and caches them for you. You can use the default calculations (like average, total, etc) or implement your own custom tallying functions.

3) Display
VotingAPI integrates with Views.module, allowing you to slice and dice your site's content based on user consensus. While some custom modules may need to implement thier own Views integration to provide customized integration, the vast majority can use the built-in system without any additional work.


How Data Is Stored
==================
VotingAPI manages a raw 'pool' of vote data -- it doesn't keep track of any content directly. Instead, it lets modules store each vote with a 'content type' and 'content id', so that the same APIs can be used to rate nodes, comments, users, aggregator items, or even other votes (in a Slashdot-esque meta-moderation system). It can also be used by modules that need to store and calculate custom data like polling results -- using a custom content_type ensures that other modules won't trample on your module's voting data.

For each discrete vote, the API stores the following information:

content_type -- This *usually* corresponds to a type of Drupal content, like 'node' or 'comment' or 'user'.
content_id   -- The key ID of the content being rated.
value        -- This is the actual value of the vote that was cast by the user.
value_type   -- This determines how vote results are totaled. VotingAPI supports three value types by default: 'percent' votes are averaged, 'points' votes are summed up, and 'option' votes get a count of votes cast for each specific option.  Modules can use other value_types, but must implement their own calculation functions to generate vote results -- more on that later.
tag          -- Modules can use different tags to store votes on specific aspects of a piece of content, like 'accuracy' and 'timeliness' of a news story. If you don't need to vote on multiple criteria, you should use the default value of 'vote'. If you use multiple tags, it is STRONGLY recommended that you provide an average or 'overall' value filed under the default 'vote' tag. This gives other modules that display voting data a single value to key on for sorting, etc.
uid          -- The user ID of the person who voted.
timestamp    -- The time the vote was cast.
vote_source  -- A unique identifier used to distinguish votes cast by anonymous users. By default, this is the IP address of the remote machine.

Whenever a vote is cast, VotingAPI gathers up all the votes for the content_type/content_id combination, and creates a collection of cached 'result' records. Each voting result recorded stores the following information:

content_type -- Just what you'd expect from the individual vote objects.
content_id   -- Ditto.
value_type   -- Ditto.
tag          -- Ditto.
function     -- The aggregate function that's been calculated -- for example, 'average', 'sum', and so on.
value        -- The value of the aggregate function.
timestamp    -- The time the results were calculated.


How it works (high-level overview)
==================================
  An example: SimpleVote
    Load the vote results
    Display the vote results
    Cast a vote
      (invisible steps: recalculation, and votingapi $ops)
    And so on, ad infitum


Using the API (The Easy Way)
============================
  High-level functions
    votingapi_set_vote()
    votingapi_select_results()

Using the API (With Full Control)
=================================
  Core functions (for full control)
    votingapi_add_vote()
    votingapi_change_vote()
    votingapi_delete_vote()
    votingapi_delete_votes()
    votingapi_recalculate_results()
    votingapi_get_user_votes()
    votingapi_get_content_votes()


VotingAPI hooks (Bending the API to your will)
================================================
  hook_votingapi_insert($vote)
  hook_votingapi_update($vote, $new_value)
  hook_votingapi_delete($vote)

  hook_votingapi_calculate(&$results, $votes, $content_type, $content_id)
  hook_votingapi_results($results, $votes, $content_type, $content_id)

Upgrading from VotingAPI 1.x
============================
Version 2.0 of VotingAPI offers several notable changes. Modules MUST be updated to work with VotingAPI 2.0, but changes for most modules should be minimal. Among other things, version 2.0 offers automatic support for anonymous votes -- something that required custom vote handling in version 1.x.

1) Functions accept objects and arrays of objects instead of long parameter lists

2) Retrieval functions consolidated

3) Lesser-used hooks eliminated

File

API.txt
View source
  1. What's this, now?
  2. =================
  3. VotingAPI provides a flexible, easy-to-use framework for rating, voting, moderation, and consensus-gathering modules in Drupal. It allows module developers to focus on their ideas (say, providing a 'rate this thread' widget at the bottom of each forum post) without worrying about the grunt work of storing votes, preventing ballot-stuffing, calculating the results, and so on.
  4. VotingAPI handles three key things for module developers:
  5. 1) CRUD
  6. Create/Retrieve/Update/Delete operations for voting data. The simplest modules only need to call two functions -- votingapi_set_vote() and votingapi_select_results() -- to use the API. Others can use finer-grain functions for complete control.
  7. 2) Calculation
  8. Every time a user casts a vote, VotingAPI calculates the results and caches them for you. You can use the default calculations (like average, total, etc) or implement your own custom tallying functions.
  9. 3) Display
  10. VotingAPI integrates with Views.module, allowing you to slice and dice your site's content based on user consensus. While some custom modules may need to implement thier own Views integration to provide customized integration, the vast majority can use the built-in system without any additional work.
  11. How Data Is Stored
  12. ==================
  13. VotingAPI manages a raw 'pool' of vote data -- it doesn't keep track of any content directly. Instead, it lets modules store each vote with a 'content type' and 'content id', so that the same APIs can be used to rate nodes, comments, users, aggregator items, or even other votes (in a Slashdot-esque meta-moderation system). It can also be used by modules that need to store and calculate custom data like polling results -- using a custom content_type ensures that other modules won't trample on your module's voting data.
  14. For each discrete vote, the API stores the following information:
  15. content_type -- This *usually* corresponds to a type of Drupal content, like 'node' or 'comment' or 'user'.
  16. content_id -- The key ID of the content being rated.
  17. value -- This is the actual value of the vote that was cast by the user.
  18. value_type -- This determines how vote results are totaled. VotingAPI supports three value types by default: 'percent' votes are averaged, 'points' votes are summed up, and 'option' votes get a count of votes cast for each specific option. Modules can use other value_types, but must implement their own calculation functions to generate vote results -- more on that later.
  19. tag -- Modules can use different tags to store votes on specific aspects of a piece of content, like 'accuracy' and 'timeliness' of a news story. If you don't need to vote on multiple criteria, you should use the default value of 'vote'. If you use multiple tags, it is STRONGLY recommended that you provide an average or 'overall' value filed under the default 'vote' tag. This gives other modules that display voting data a single value to key on for sorting, etc.
  20. uid -- The user ID of the person who voted.
  21. timestamp -- The time the vote was cast.
  22. vote_source -- A unique identifier used to distinguish votes cast by anonymous users. By default, this is the IP address of the remote machine.
  23. Whenever a vote is cast, VotingAPI gathers up all the votes for the content_type/content_id combination, and creates a collection of cached 'result' records. Each voting result recorded stores the following information:
  24. content_type -- Just what you'd expect from the individual vote objects.
  25. content_id -- Ditto.
  26. value_type -- Ditto.
  27. tag -- Ditto.
  28. function -- The aggregate function that's been calculated -- for example, 'average', 'sum', and so on.
  29. value -- The value of the aggregate function.
  30. timestamp -- The time the results were calculated.
  31. How it works (high-level overview)
  32. ==================================
  33. An example: SimpleVote
  34. Load the vote results
  35. Display the vote results
  36. Cast a vote
  37. (invisible steps: recalculation, and votingapi $ops)
  38. And so on, ad infitum
  39. Using the API (The Easy Way)
  40. ============================
  41. High-level functions
  42. votingapi_set_vote()
  43. votingapi_select_results()
  44. Using the API (With Full Control)
  45. =================================
  46. Core functions (for full control)
  47. votingapi_add_vote()
  48. votingapi_change_vote()
  49. votingapi_delete_vote()
  50. votingapi_delete_votes()
  51. votingapi_recalculate_results()
  52. votingapi_get_user_votes()
  53. votingapi_get_content_votes()
  54. VotingAPI hooks (Bending the API to your will)
  55. ================================================
  56. hook_votingapi_insert($vote)
  57. hook_votingapi_update($vote, $new_value)
  58. hook_votingapi_delete($vote)
  59. hook_votingapi_calculate(&$results, $votes, $content_type, $content_id)
  60. hook_votingapi_results($results, $votes, $content_type, $content_id)
  61. Upgrading from VotingAPI 1.x
  62. ============================
  63. Version 2.0 of VotingAPI offers several notable changes. Modules MUST be updated to work with VotingAPI 2.0, but changes for most modules should be minimal. Among other things, version 2.0 offers automatic support for anonymous votes -- something that required custom vote handling in version 1.x.
  64. 1) Functions accept objects and arrays of objects instead of long parameter lists
  65. 2) Retrieval functions consolidated
  66. 3) Lesser-used hooks eliminated