Projet

Général

Profil

Paste
Télécharger (9,02 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / votingapi / API.txt @ 7942932f

1

    
2
What's this, now?
3
=================
4
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.
5

    
6
VotingAPI handles three key things for module developers:
7

    
8
1) CRUD
9
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.
10

    
11
2) Calculation
12
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.
13

    
14
3) Display
15
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 displays, the vast majority can use the built-in system without any additional work.
16

    
17
How Data Is Stored
18
==================
19
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 entity_type ensures that other modules won't trample on your module's voting data.
20

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

    
23
entity_type  -- This *usually* corresponds to a type of Drupal content, like 'node' or 'comment' or 'user'.
24
entity_id    -- The key ID of the content being rated.
25
value        -- This is the actual value of the vote that was cast by the user.
26
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.
27
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.
28
uid          -- The user ID of the person who voted.
29
timestamp    -- The time the vote was cast.
30
vote_source  -- A unique identifier used to distinguish votes cast by anonymous users. By default, this is the IP address of the remote machine.
31

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

    
34
entity_type  -- Just what you'd expect from the individual vote objects.
35
entity_id    -- Ditto.
36
value_type   -- Ditto.
37
tag          -- Ditto.
38
function     -- The aggregate function that's been calculated -- for example, 'average', 'sum', and so on.
39
value        -- The value of the aggregate function.
40
timestamp    -- The time the results were calculated.
41

    
42

    
43
Upgrading from VotingAPI 1.x
44
============================
45
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.
46

    
47
1) Functions accept objects and arrays of objects instead of long parameter lists
48
VotingAPI 1.x used relatively complex parameter lists in its most commonly used functions. In version 2.x, VotingAPI vote-casting functions accept a single vote object or array of vote objects, while vote and result retrieval functions accept a keyed array describing the filter criteria to be used.
49

    
50
2) hook_votingapi_update() Removed
51
This function allowed modules to intervene whenever a user changed their vote. The processing overhead that it imposed on most operations, however, was severe. No modules in contrib implemented the hook, and it has been eliminated. hook_votingapi_insert() and hook_votingapi_delete() are still available.
52

    
53
3) Retrieval functions consolidated
54
In VotingAPI 1.x, votes for a content object were retrieved using a dizzying array of functions, including the ugly buy often-used internal function, _votingapi_get_raw_votes(). In version 2.x, the following functions are provided:
55

    
56
* votingapi_select_votes();
57
* votingapi_select_results();
58
* votingapi_select_single_vote_value()
59
* votingapi_select_single_result_value();
60

    
61
4) Custom result calculations must do their own SQL
62
In version 1.x, VotingAPI loaded all votes for a given content object in order to calculate the average vote using PHP. Modules calculating their own results (median, etc.) were handed the stack of vote objects and given an opportunity to do more using hook_votingapi_calculate(). This was fine for simple cases, but consumed monstrous amounts of RAM whenever a single piece of content accumulated large numbers of votes.
63

    
64
In version 2.x, VotingAPI modules may implement hook_votingapi_results_alter() instead. They receive the same information about the content object, and the stack of results to modify, but are responsible for using their own SQL to generate their results. Fortunately, most modules implementing custom results required complex calculations more efficiently done in SQL anyways.
65

    
66
5) Views integration hooks have changed
67
VotingAPI now supports any valid base table when exposing its data to Views. Modules that cast votes on non-node content can implement hook_votingapi_views_entity_types() to let VotingAPI know what base tables should get the relationships.
68

    
69
Because Views' internal data structures have changed, and the VotingAPI integration now supports additional base tables, the data handed off to hook_votingapi_views_formatters() has changed. See the reference at the bottom of this document for an example implementation.
70

    
71
In the future, modules that need to offer highly customized ways of presenting VotingAPI data in a view are advised to use hook_views_data_alter() to simply add a new custom field to the VotingAPI table definition. That will give full control over display and filtering, but will take advantage of the flexible, base-table-agnostic join handlers VotingAPI provides.
72

    
73

    
74
An example custom calculation
75
=============================
76
The following function adds a standard_deviation result to the calculated result data.
77
Note that in previous versions of VotingAPI, this function received in-memory copies of
78
each and every cast vote to avoid the need for custom SQL. This turned out to be very,
79
very, very inefficient -- the slowdown of possibly running multiple aggregate queries is
80
far outweighed by the memory savings of each module handling its own queries. After all,
81
MySQL calculates standard deviation far faster than you can in PHP.
82

    
83
function mymodule_votingapi_results_alter(&$results, $entity_type, $entity_id) {
84
  // We're using a MySQLism (STDDEV isn't ANSI SQL), but it's OK because this is
85
  // an example. And no one would ever base real code on sample code. Ever. Never.
86
  $sql  = "SELECT v.tag, STDDEV(v.value) as standard_deviation ";
87
  $sql .= "FROM {votingapi_vote} v ";
88
  $sql .= "WHERE v.entity_type = '%s' AND v.entity_id = %d AND v.value_type = 'percent' ";
89
  $sql .= "GROUP BY v.tag";
90

    
91
  $results = db_query($sql, $entity_type, $entity_id);
92

    
93
  // VotingAPI wants the data in the following format:
94
  // $cache[$tag][$value_type][$math_function] = $value;
95
  while ($result = db_fetch_array($results)) {
96
    $cache[$result['tag']]['percent']['standard_deviation'] = $result['standard_deviation'];
97
  }
98
}
99

    
100
An example of a custom Views value formatter
101
============================================
102
VotingAPI's Views integration can present vote results as simple numbers, but most users will want to see something a bit snazzier. By implementing hook_votingapi_views_formatters(), you can expose a custom formatter for any given VotingAPI view field. For the View field that's passed in, your hook should return an array of key => value pairs, where the key is a the name of a callback function that will format the values from the database.
103

    
104
function mymodule_votingapi_views_formatters($field) {
105
  if ($field->field == 'value') {
106
    return array('mymodule_funky_formatter' => t('MyModule value formatter'));
107
  }
108
  if ($field->field == 'tag') {
109
    return array('mymodule_funky_tags' => t('MyModule tag formatter'));
110
  }
111
}