root / htmltest / sites / all / modules / votingapi / votingapi.api.php @ a5572547
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 |
/**
|
98 |
* Return metadata used to build Views relationships on voting data.
|
99 |
*
|
100 |
* VotingAPI can store votes on any entity in the Drupal database: its content_type
|
101 |
* and content_id columns can be used to store "node"/1, "comment"/2, and so
|
102 |
* on. This hook is used to tell VotingAPI what Views base table the content_type
|
103 |
* field corresponds to, and what field in that base table contains the value in
|
104 |
* votingapi's content_id table.
|
105 |
*
|
106 |
* @return
|
107 |
* An array of records containing 'description', 'content_type', 'base_table',
|
108 |
* and 'content_id_column' entries.
|
109 |
*/
|
110 |
function hook_votingapi_relationships() { |
111 |
$relationships[] = array( |
112 |
// 'description' is used to construct the field description in the Views UI.
|
113 |
'description' => t('users'), |
114 |
|
115 |
// 'content_type' contain the value that your module stores in the voting
|
116 |
// api 'content_type' column. 'node', 'comment', etc.
|
117 |
'content_type' => 'user', |
118 |
|
119 |
// 'base_table' contain the name of the Views base table that stores the
|
120 |
// data your votes apply to.
|
121 |
'base_table' => 'user', |
122 |
|
123 |
// 'content_id_column' contains the name of the views field that represents
|
124 |
// your base_table's primary key. This column will be joined against the
|
125 |
// voting api 'content_id' column.
|
126 |
'content_id_column' => 'uid', |
127 |
|
128 |
// VotingAPI constructs pseudo-tables so that multiple relationships can
|
129 |
// point to the same base table (normal and translation-based votes nodes
|
130 |
// for example. These two columns allow you to override the names of the
|
131 |
// pseudo-tables. You probably don't need to change this part unless you're
|
132 |
// nedjo.
|
133 |
'pseudo_vote' => 'votingapi_vote_special', |
134 |
'pseudo_cache' => 'votingapi_cache_special', |
135 |
); |
136 |
} |
137 |
|
138 |
/**
|
139 |
* Returns callback functions and descriptions to format a VotingAPI Views field.
|
140 |
*
|
141 |
* Loads all votes for a given piece of content, then calculates and caches the
|
142 |
* aggregate vote results. This is only intended for modules that have assumed
|
143 |
* responsibility for the full voting cycle: the votingapi_set_vote() function
|
144 |
* recalculates automatically.
|
145 |
*
|
146 |
* @param $field
|
147 |
* A Views field object. This can be used to expose formatters only for tags,
|
148 |
* vote values, aggregate functions, etc.
|
149 |
* @return
|
150 |
* An array of key-value pairs, in which each key is a callback function and
|
151 |
* each value is a human-readable description of the formatter.
|
152 |
*
|
153 |
* @see votingapi_set_votes()
|
154 |
*/
|
155 |
function hook_votingapi_views_formatters($field) { |
156 |
if ($field->field == 'value') { |
157 |
return array('mymodule_funky_formatter' => t('MyModule value formatter')); |
158 |
} |
159 |
if ($field->field == 'tag') { |
160 |
return array('mymodule_funky_tags' => t('MyModule tag formatter')); |
161 |
} |
162 |
} |
163 |
|
164 |
/**
|
165 |
* Save a vote in the database.
|
166 |
*
|
167 |
* @param $vote
|
168 |
* See votingapi_add_votes() for the structure of this array, with the
|
169 |
* defaults loaded from votingapi_prep_vote().
|
170 |
*/
|
171 |
function hook_votingapi_storage_add_vote(&$vote) { |
172 |
_mongodb_votingapi_prepare_vote($criteria);
|
173 |
mongodb_collection('votingapi_vote')->insert($vote); |
174 |
} |
175 |
|
176 |
/**
|
177 |
* Delete votes from the database.
|
178 |
*
|
179 |
* @param $votes
|
180 |
* An array of votes to delete. Minimally, each vote must have the 'vote_id'
|
181 |
* key set.
|
182 |
* @param $vids
|
183 |
* A list of the 'vote_id' values from $voes.
|
184 |
*/
|
185 |
function hook_votingapi_storage_delete_votes($votes, $vids) { |
186 |
mongodb_collection('votingapi_vote')->delete(array('vote_id' => array('$in' => array_map('intval', $vids)))); |
187 |
} |
188 |
|
189 |
/**
|
190 |
* Select invidual votes from the database
|
191 |
*
|
192 |
/**
|
193 |
* Select individual votes from the database.
|
194 |
*
|
195 |
* @param $criteria
|
196 |
* A keyed array used to build the select query. Keys can contain
|
197 |
* a single value or an array of values to be matched.
|
198 |
* $criteria['vote_id'] (If this is set, all other keys are skipped)
|
199 |
* $criteria['entity_id']
|
200 |
* $criteria['entity_type']
|
201 |
* $criteria['value_type']
|
202 |
* $criteria['tag']
|
203 |
* $criteria['uid']
|
204 |
* $criteria['vote_source']
|
205 |
* $criteria['timestamp'] If this is set, records with timestamps
|
206 |
* GREATER THAN the set value will be selected. Defaults to
|
207 |
* REQUEST_TIME - variable_get('votingapi_anonymous_window', 3600); if
|
208 |
* the anonymous window is above zero.
|
209 |
* @param $limit
|
210 |
* An integer specifying the maximum number of votes to return. 0 means
|
211 |
* unlimited and is the default.
|
212 |
* @return
|
213 |
* An array of votes matching the criteria.
|
214 |
*/
|
215 |
function hook_votingapi_storage_select_votes($criteria, $limit) { |
216 |
_mongodb_votingapi_prepare_vote($criteria);
|
217 |
$find = array(); |
218 |
foreach ($criteria as $key => $value) { |
219 |
$find[$key] = is_array($value) ? array('$in' => $value) : $value; |
220 |
} |
221 |
$cursor = mongodb_collection('votingapi_vote')->find($find); |
222 |
if (!empty($limit)) { |
223 |
$cursor->limit($limit); |
224 |
} |
225 |
$votes = array(); |
226 |
foreach ($cursor as $vote) { |
227 |
$votes[] = $vote; |
228 |
} |
229 |
return $votes; |
230 |
} |
231 |
|
232 |
/**
|
233 |
* TODO
|
234 |
*
|
235 |
*/
|
236 |
function hook_votingapi_storage_standard_results($entity_id, $entity) { |
237 |
// TODO
|
238 |
} |