root / drupal7 / sites / all / modules / votingapi / votingapi.api.php @ ba09eb79
1 |
<?php
|
---|---|
2 |
|
3 |
/**
|
4 |
* @file
|
5 |
* Provides hook documentation for the VotingAPI module.
|
6 |
*/
|
7 |
|
8 |
|
9 |
/**
|
10 |
* Adds to or changes the calculated vote results for a piece of content.
|
11 |
*
|
12 |
* VotingAPI calculates a number of common aggregate functions automatically,
|
13 |
* including the average vote and total number of votes cast. Results are grouped
|
14 |
* by 'tag', 'value_type', and then 'function' in the following format:
|
15 |
*
|
16 |
* $results[$tag][$value_type][$aggregate_function] = $value;
|
17 |
*
|
18 |
* If no custom tag is being used for votes, the catch-all "vote" tag should be
|
19 |
* used. In cases where custom tags are used to vote on different aspects of a
|
20 |
* piece of content, a catch-all "vote" value should still be calculated for use
|
21 |
* on summary screens, etc.
|
22 |
*
|
23 |
* @param $vote_results
|
24 |
* An alterable array of aggregate vote results.
|
25 |
* @param $content_type
|
26 |
* A string identifying the type of content being rated. Node, comment,
|
27 |
* aggregator item, etc.
|
28 |
* @param $content_id
|
29 |
* The key ID of the content being rated.
|
30 |
*
|
31 |
* @see votingapi_recalculate_results()
|
32 |
*/
|
33 |
function hook_votingapi_results_alter(&$vote_results, $content_type, $content_id) { |
34 |
// We're using a MySQLism (STDDEV isn't ANSI SQL), but it's OK because this is
|
35 |
// an example. And no one would ever base real code on sample code. Ever. Never.
|
36 |
|
37 |
$sql = "SELECT v.tag, STDDEV(v.value) as standard_deviation "; |
38 |
$sql .= "FROM {votingapi_vote} v "; |
39 |
$sql .= "WHERE v.content_type = '%s' AND v.content_id = %d AND v.value_type = 'percent' "; |
40 |
$sql .= "GROUP BY v.tag"; |
41 |
|
42 |
$aggregates = db_query($sql, $content_type, $content_id); |
43 |
|
44 |
// VotingAPI wants the data in the following format:
|
45 |
// $vote_results[$tag][$value_type][$aggregate_function] = $value;
|
46 |
|
47 |
while ($aggregate = db_fetch_array($aggregates)) { |
48 |
$vote_results[$result['tag']]['percent']['standard_deviation'] = $result['standard_deviation']; |
49 |
} |
50 |
} |
51 |
|
52 |
|
53 |
/**
|
54 |
* Adds to or alters metadata describing Voting tags, value_types, and functions.
|
55 |
*
|
56 |
* If your module uses custom tags or value_types, or calculates custom aggregate
|
57 |
* functions, please implement this hook so other modules can properly interperet
|
58 |
* and display your data.
|
59 |
*
|
60 |
* Three major bins of data are stored: tags, value_types, and aggregate result
|
61 |
* functions. Each entry in these bins is keyed by the value stored in the actual
|
62 |
* VotingAPI tables, and contains an array with (minimally) 'name' and
|
63 |
* 'description' keys. Modules can add extra keys to their entries if desired.
|
64 |
*
|
65 |
* @param $data
|
66 |
* An alterable array of aggregate vote results.
|
67 |
*
|
68 |
* @see votingapi_metadata()
|
69 |
*/
|
70 |
function hook_votingapi_metadata_alter(&$data) { |
71 |
// Document several custom tags for rating restaurants and meals.
|
72 |
$data['tags']['bread'] = array( |
73 |
'name' => t('Bread'), |
74 |
'description' => t('The quality of the food at a restaurant.'), |
75 |
'module' => 'mymodule', // This is optional; we can add it for our own purposes. |
76 |
); |
77 |
$data['tags']['circuses'] = array( |
78 |
'name' => t('Circuses'), |
79 |
'description' => t('The quality of the presentation and atmosphere at a restaurant.'), |
80 |
'module' => 'mymodule', |
81 |
); |
82 |
|
83 |
// Document two custom aggregate function.
|
84 |
$data['functions']['standard_deviation'] = array( |
85 |
'name' => t('Standard deviation'), |
86 |
'description' => t('The standard deviation of all votes cast on a given piece of content. Use this to find controversial content.'), |
87 |
'module' => 'mymodule', |
88 |
); |
89 |
$data['functions']['median'] = array( |
90 |
'name' => t('Median vote'), |
91 |
'description' => t('The median vote value cast on a given piece of content. More accurate than a pure average when there are a few outlying votes.'), |
92 |
'module' => 'mymodule', |
93 |
); |
94 |
} |
95 |
|
96 |
/**
|
97 |
* Returns callback functions and descriptions to format a VotingAPI Views field.
|
98 |
*
|
99 |
* Loads all votes for a given piece of content, then calculates and caches the
|
100 |
* aggregate vote results. This is only intended for modules that have assumed
|
101 |
* responsibility for the full voting cycle: the votingapi_set_vote() function
|
102 |
* recalculates automatically.
|
103 |
*
|
104 |
* @param $field
|
105 |
* A Views field object. This can be used to expose formatters only for tags,
|
106 |
* vote values, aggregate functions, etc.
|
107 |
* @return
|
108 |
* An array of key-value pairs, in which each key is a callback function and
|
109 |
* each value is a human-readable description of the formatter.
|
110 |
*
|
111 |
* @see votingapi_set_votes()
|
112 |
*/
|
113 |
function hook_votingapi_views_formatters($field) { |
114 |
if ($field->field == 'value') { |
115 |
return array('mymodule_funky_formatter' => t('MyModule value formatter')); |
116 |
} |
117 |
if ($field->field == 'tag') { |
118 |
return array('mymodule_funky_tags' => t('MyModule tag formatter')); |
119 |
} |
120 |
} |
121 |
|
122 |
/**
|
123 |
* Save a vote in the database.
|
124 |
*
|
125 |
* @param $vote
|
126 |
* See votingapi_add_votes() for the structure of this array, with the
|
127 |
* defaults loaded from votingapi_prep_vote().
|
128 |
*/
|
129 |
function hook_votingapi_storage_add_vote(&$vote) { |
130 |
_mongodb_votingapi_prepare_vote($criteria);
|
131 |
mongodb_collection('votingapi_vote')->insert($vote); |
132 |
} |
133 |
|
134 |
/**
|
135 |
* Delete votes from the database.
|
136 |
*
|
137 |
* @param $votes
|
138 |
* An array of votes to delete. Minimally, each vote must have the 'vote_id'
|
139 |
* key set.
|
140 |
* @param $vids
|
141 |
* A list of the 'vote_id' values from $voes.
|
142 |
*/
|
143 |
function hook_votingapi_storage_delete_votes($votes, $vids) { |
144 |
mongodb_collection('votingapi_vote')->delete(array('vote_id' => array('$in' => array_map('intval', $vids)))); |
145 |
} |
146 |
|
147 |
/**
|
148 |
* Select invidual votes from the database
|
149 |
*
|
150 |
* @param $criteria
|
151 |
* A keyed array used to build the select query. Keys can contain
|
152 |
* a single value or an array of values to be matched.
|
153 |
* $criteria['vote_id'] (If this is set, all other keys are skipped)
|
154 |
* $criteria['entity_id']
|
155 |
* $criteria['entity_type']
|
156 |
* $criteria['value_type']
|
157 |
* $criteria['tag']
|
158 |
* $criteria['uid']
|
159 |
* $criteria['vote_source']
|
160 |
* $criteria['timestamp'] If this is set, records with timestamps
|
161 |
* GREATER THAN the set value will be selected. Defaults to
|
162 |
* REQUEST_TIME - variable_get('votingapi_anonymous_window', 3600); if
|
163 |
* the anonymous window is above zero.
|
164 |
* @param $limit
|
165 |
* An integer specifying the maximum number of votes to return. 0 means
|
166 |
* unlimited and is the default.
|
167 |
* @return
|
168 |
* An array of votes matching the criteria.
|
169 |
*/
|
170 |
function hook_votingapi_storage_select_votes($criteria, $limit) { |
171 |
_mongodb_votingapi_prepare_vote($criteria);
|
172 |
$find = array(); |
173 |
foreach ($criteria as $key => $value) { |
174 |
$find[$key] = is_array($value) ? array('$in' => $value) : $value; |
175 |
} |
176 |
$cursor = mongodb_collection('votingapi_vote')->find($find); |
177 |
if (!empty($limit)) { |
178 |
$cursor->limit($limit); |
179 |
} |
180 |
$votes = array(); |
181 |
foreach ($cursor as $vote) { |
182 |
$votes[] = $vote; |
183 |
} |
184 |
return $votes; |
185 |
} |
186 |
|
187 |
/**
|
188 |
* Allows to act on votes before being inserted.
|
189 |
*
|
190 |
* @param $votes
|
191 |
* An array of votes, each with the following structure:
|
192 |
* $vote['entity_type'] (Optional, defaults to 'node')
|
193 |
* $vote['entity_id'] (Required)
|
194 |
* $vote['value_type'] (Optional, defaults to 'percent')
|
195 |
* $vote['value'] (Required)
|
196 |
* $vote['tag'] (Optional, defaults to 'vote')
|
197 |
* $vote['uid'] (Optional, defaults to current user)
|
198 |
* $vote['vote_source'] (Optional, defaults to current IP)
|
199 |
* $vote['timestamp'] (Optional, defaults to REQUEST_TIME)
|
200 |
*/
|
201 |
function hook_votingapi_preset_votes(&$votes) { |
202 |
foreach ($votes as $vote) { |
203 |
if ($vote['tag'] == 'recommend') { |
204 |
// Do something if the 'recommend' vote is being inserted.
|
205 |
} |
206 |
} |
207 |
} |
208 |
|
209 |
/**
|
210 |
* TODO
|
211 |
*
|
212 |
*/
|
213 |
function hook_votingapi_storage_standard_results($entity_id, $entity) { |
214 |
// TODO
|
215 |
} |