Révision 7942932f
Ajouté par Assos Assos il y a plus de 9 ans
drupal7/sites/all/modules/votingapi/API.txt | ||
---|---|---|
97 | 97 |
} |
98 | 98 |
} |
99 | 99 |
|
100 |
An example of advanced Views integration |
|
101 |
======================================== |
|
102 |
VotingAPI provides Views Relationship connections for votes cast on nodes and comments. If your module casts other types of votes that should be made available via Views, it needs to implement hook_votingapi_relationships(). |
|
103 |
|
|
104 |
|
|
105 |
function mymodule_votingapi_relationships() { |
|
106 |
$relationships[] = array( |
|
107 |
// 'description' is used to construct the field description in the Views UI. |
|
108 |
'description' => t('nodes'), |
|
109 |
// 'entity_type' contain the value that your module stores in the voting |
|
110 |
// api 'entity_type' column. 'node', 'comment', etc. |
|
111 |
'entity_type' => 'node', |
|
112 |
// 'base_table' contain the name of the Views base table that stores the |
|
113 |
// data your votes apply to. |
|
114 |
'base_table' => 'node', |
|
115 |
// 'entity_id_column' contains the name of the views field that represents |
|
116 |
// your base_table's primary key. This column will be joined against the |
|
117 |
// voting api 'entity_id' column. |
|
118 |
'entity_id_column' => 'nid', |
|
119 |
// VotingAPI constructs pseudo-tables so that multiple relationships can |
|
120 |
// point to the same base table (normal and translation-based votes nodes |
|
121 |
// for example. These two columns allow you to override the names of the |
|
122 |
// pseudo-tables. You probably don't need to change this part unless you're |
|
123 |
// nedjo. |
|
124 |
'pseudo_vote' => 'votingapi_vote', |
|
125 |
'pseudo_cache' => 'votingapi_cache', |
|
126 |
); |
|
127 |
return $relationships; |
|
128 |
} |
|
129 |
|
|
130 | 100 |
An example of a custom Views value formatter |
131 | 101 |
============================================ |
132 | 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. |
drupal7/sites/all/modules/votingapi/views/votingapi.views.inc | ||
---|---|---|
275 | 275 |
|
276 | 276 |
//Get all entity types in the system and register as relationship. |
277 | 277 |
$entity_types = entity_get_info(); |
278 |
$default_relationships = array(); |
|
278 | 279 |
foreach($entity_types as $key => $entity_type) { |
279 |
$default_relationships[] = array( |
|
280 |
'description' => $entity_type['label'], |
|
281 |
'entity_type' => $key, |
|
282 |
'base_table' => $entity_type['base table'], |
|
283 |
'entity_id_column' => $entity_type['entity keys']['id'], |
|
284 |
'pseudo_vote' => 'votingapi_vote', // for legacy compatability w/RC1. |
|
285 |
'pseudo_cache' => 'votingapi_cache', // for legacy compatability w/RC1. |
|
286 |
); |
|
280 |
if (isset($entity_type['base table'])) { |
|
281 |
$default_relationships[] = array( |
|
282 |
'description' => $entity_type['label'], |
|
283 |
'entity_type' => $key, |
|
284 |
'base_table' => $entity_type['base table'], |
|
285 |
'entity_id_column' => $entity_type['entity keys']['id'], |
|
286 |
'pseudo_vote' => 'votingapi_vote', // for legacy compatability w/RC1. |
|
287 |
'pseudo_cache' => 'votingapi_cache', // for legacy compatability w/RC1. |
|
288 |
); |
|
289 |
} |
|
287 | 290 |
} |
288 | 291 |
|
289 | 292 |
foreach ($default_relationships as $data) { |
drupal7/sites/all/modules/votingapi/views/votingapi_views_handler_field_value.inc | ||
---|---|---|
35 | 35 |
} |
36 | 36 |
} |
37 | 37 |
|
38 |
function element_type($none_supported = FALSE, $default_empty = FALSE, $inline = FALSE) { |
|
39 |
return 'div'; |
|
40 |
} |
|
41 |
|
|
42 | 38 |
/** |
43 | 39 |
* Called to determine what to tell the clicksorter. |
44 | 40 |
*/ |
drupal7/sites/all/modules/votingapi/votingapi.admin.inc | ||
---|---|---|
83 | 83 |
'types' => $form_state['values']['node_types'], |
84 | 84 |
'kill' => $form_state['values']['kill_votes'], |
85 | 85 |
); |
86 |
require_once(drupal_get_path('module', 'votingapi') . '/votingapi.drush.inc');
|
|
86 |
require_once(drupal_get_path('module', 'votingapi') . '/votingapi.devel.inc');
|
|
87 | 87 |
votingapi_generate_votes('node', $form_state['values']['vote_type'], $options); |
88 | 88 |
} |
drupal7/sites/all/modules/votingapi/votingapi.api.php | ||
---|---|---|
93 | 93 |
); |
94 | 94 |
} |
95 | 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 | 96 |
/** |
139 | 97 |
* Returns callback functions and descriptions to format a VotingAPI Views field. |
140 | 98 |
* |
... | ... | |
189 | 147 |
/** |
190 | 148 |
* Select invidual votes from the database |
191 | 149 |
* |
192 |
/** |
|
193 |
* Select individual votes from the database. |
|
194 |
* |
|
195 | 150 |
* @param $criteria |
196 | 151 |
* A keyed array used to build the select query. Keys can contain |
197 | 152 |
* a single value or an array of values to be matched. |
... | ... | |
229 | 184 |
return $votes; |
230 | 185 |
} |
231 | 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 |
|
|
232 | 209 |
/** |
233 | 210 |
* TODO |
234 | 211 |
* |
drupal7/sites/all/modules/votingapi/votingapi.devel.inc | ||
---|---|---|
1 |
<?php |
|
2 |
|
|
3 |
/** |
|
4 |
* @file |
|
5 |
* Generate fake votingapi votes for development testing. |
|
6 |
*/ |
|
7 |
|
|
8 |
/** |
|
9 |
* Utility function to generate votes. |
|
10 |
*/ |
|
11 |
function votingapi_generate_votes($entity_type = 'node', $vote_type = 'percent', $options = array()) { |
|
12 |
module_load_include('inc', 'devel_generate'); |
|
13 |
$options += array( |
|
14 |
'age' => 36000, |
|
15 |
'types' => array(), |
|
16 |
'kill' => FALSE, |
|
17 |
); |
|
18 |
|
|
19 |
if (!empty($options['kill_votes'])) { |
|
20 |
db_truncate('votingapi_vote'); |
|
21 |
db_truncate('votingapi_cache'); |
|
22 |
} |
|
23 |
|
|
24 |
if (($schema = drupal_get_schema($entity_type)) && ($entity_id_column = array_shift($schema['primary key']))) { |
|
25 |
// oh look we found a schema yay |
|
26 |
} |
|
27 |
else { |
|
28 |
$entity_type = 'node'; |
|
29 |
$entity_id_column = 'nid'; |
|
30 |
} |
|
31 |
$uids = devel_get_users(); |
|
32 |
|
|
33 |
$query = db_select($entity_type, 'e')->fields('e', array($entity_id_column)); |
|
34 |
if ($entity_type == 'node' && !empty($options['types'])) { |
|
35 |
$query->condition('e.type', $options['types'], 'IN'); |
|
36 |
} |
|
37 |
$results = $query->execute()->fetchAll(PDO::FETCH_ASSOC); |
|
38 |
|
|
39 |
foreach ($results as $entity) { |
|
40 |
_votingapi_cast_votes($entity_type, $entity[$entity_id_column], $options['age'], $uids, $vote_type); |
|
41 |
} |
|
42 |
} |
|
43 |
|
|
44 |
/** |
|
45 |
* Utility function to generate votes on a node by a set of users. |
|
46 |
*/ |
|
47 |
function _votingapi_cast_votes($etype, $eid, $timestamp = 0, $uids = array(), $style = 'percent') { |
|
48 |
$votes = array(); |
|
49 |
static $tags; |
|
50 |
if (!isset($tags)) { |
|
51 |
$tags = explode(' ', devel_create_greeking(30)); |
|
52 |
} |
|
53 |
foreach ($uids as $uid) { |
|
54 |
switch ($style) { |
|
55 |
case 'percent': |
|
56 |
$votes[] = array( |
|
57 |
'uid' => $uid, |
|
58 |
'entity_type' => $etype, |
|
59 |
'entity_id' => $eid, |
|
60 |
'value_type' => 'percent', |
|
61 |
'timestamp' => REQUEST_TIME - mt_rand(0, $timestamp), |
|
62 |
'value' => mt_rand(1, 5) * 20, |
|
63 |
'tag' => $tags[mt_rand(0, 20)], |
|
64 |
); |
|
65 |
break; |
|
66 |
case 'points': |
|
67 |
if (rand(0, 3)) { |
|
68 |
$votes[] = array( |
|
69 |
'uid' => $uid, |
|
70 |
'entity_type' => $etype, |
|
71 |
'entity_id' => $eid, |
|
72 |
'value_type' => 'points', |
|
73 |
'timestamp' => REQUEST_TIME - mt_rand(0, $timestamp), |
|
74 |
'value' => rand(0, 1) ? 1 : -1, |
|
75 |
); |
|
76 |
} |
|
77 |
break; |
|
78 |
} |
|
79 |
} |
|
80 |
votingapi_set_votes($votes, array()); |
|
81 |
} |
|
82 |
|
drupal7/sites/all/modules/votingapi/votingapi.drush.inc | ||
---|---|---|
2 | 2 |
|
3 | 3 |
/** |
4 | 4 |
* @file |
5 |
* Generate votingapi votes, recalculate results for existing votes, or flush
|
|
6 |
* VotingAPI data entirely. |
|
5 |
* Drush commands to generate votingapi votes, recalculate results
|
|
6 |
* for existing votes, or flush VotingAPI data entirely.
|
|
7 | 7 |
*/ |
8 | 8 |
|
9 | 9 |
/** |
... | ... | |
57 | 57 |
return $items; |
58 | 58 |
} |
59 | 59 |
|
60 |
|
|
61 | 60 |
/** |
62 | 61 |
* Command callback. Generate a number of votes. |
63 | 62 |
*/ |
... | ... | |
68 | 67 |
'types' => drush_get_option('types'), |
69 | 68 |
); |
70 | 69 |
|
70 |
require_once(drupal_get_path('module', 'votingapi') . '/votingapi.devel.inc'); |
|
71 | 71 |
votingapi_generate_votes($entity_type, $vote_type, $options); |
72 | 72 |
|
73 | 73 |
drush_log(t('Generating @vtype votes for @etype entities.', array('@vtype' => $vote_type, '@etype' => $entity_type)), 'success'); |
74 | 74 |
} |
75 | 75 |
|
76 |
/** |
|
77 |
* Utility function to generate votes. |
|
78 |
*/ |
|
79 |
function votingapi_generate_votes($entity_type = 'node', $vote_type = 'percent', $options = array()) { |
|
80 |
module_load_include('inc', 'devel_generate'); |
|
81 |
$options += array( |
|
82 |
'age' => 36000, |
|
83 |
'types' => array(), |
|
84 |
'kill' => FALSE, |
|
85 |
); |
|
86 |
|
|
87 |
if (!empty($options['kill_votes'])) { |
|
88 |
db_truncate('votingapi_vote'); |
|
89 |
db_truncate('votingapi_cache'); |
|
90 |
} |
|
91 |
|
|
92 |
if (($schema = drupal_get_schema($entity_type)) && ($entity_id_column = array_shift($schema['primary key']))) { |
|
93 |
// oh look we found a schema yay |
|
94 |
} |
|
95 |
else { |
|
96 |
$entity_type = 'node'; |
|
97 |
$entity_id_column = 'nid'; |
|
98 |
} |
|
99 |
$uids = devel_get_users(); |
|
100 |
|
|
101 |
$query = db_select($entity_type, 'e')->fields('e', array($entity_id_column)); |
|
102 |
if ($entity_type == 'node' && !empty($options['types'])) { |
|
103 |
$query->condition('e.type', $options['types'], 'IN'); |
|
104 |
} |
|
105 |
$results = $query->execute()->fetchAll(PDO::FETCH_ASSOC); |
|
106 |
|
|
107 |
foreach ($results as $entity) { |
|
108 |
_votingapi_cast_votes($entity_type, $entity[$entity_id_column], $options['age'], $uids, $vote_type); |
|
109 |
} |
|
110 |
} |
|
111 |
|
|
112 |
/** |
|
113 |
* Utility function to generate votes on a node by a set of users. |
|
114 |
*/ |
|
115 |
function _votingapi_cast_votes($etype, $eid, $timestamp = 0, $uids = array(), $style = 'percent') { |
|
116 |
$votes = array(); |
|
117 |
static $tags; |
|
118 |
if (!isset($tags)) { |
|
119 |
$tags = explode(' ', devel_create_greeking(30)); |
|
120 |
} |
|
121 |
foreach ($uids as $uid) { |
|
122 |
switch ($style) { |
|
123 |
case 'percent': |
|
124 |
$votes[] = array( |
|
125 |
'uid' => $uid, |
|
126 |
'entity_type' => $etype, |
|
127 |
'entity_id' => $eid, |
|
128 |
'value_type' => 'percent', |
|
129 |
'timestamp' => REQUEST_TIME - mt_rand(0, $timestamp), |
|
130 |
'value' => mt_rand(1, 5) * 20, |
|
131 |
'tag' => $tags[mt_rand(0, 30)], |
|
132 |
); |
|
133 |
break; |
|
134 |
case 'points': |
|
135 |
if (rand(0, 3)) { |
|
136 |
$votes[] = array( |
|
137 |
'uid' => $uid, |
|
138 |
'entity_type' => $etype, |
|
139 |
'entity_id' => $eid, |
|
140 |
'value_type' => 'points', |
|
141 |
'timestamp' => REQUEST_TIME - mt_rand(0, $timestamp), |
|
142 |
'value' => rand(0, 1) ? 1 : -1, |
|
143 |
); |
|
144 |
} |
|
145 |
break; |
|
146 |
} |
|
147 |
} |
|
148 |
votingapi_set_votes($votes, array()); |
|
149 |
} |
|
150 |
|
|
151 | 76 |
/** |
152 | 77 |
* Command callback. Recalculate voting results. |
153 | 78 |
*/ |
... | ... | |
173 | 98 |
drush_log($message, 'success'); |
174 | 99 |
} |
175 | 100 |
|
176 |
|
|
177 | 101 |
/** |
178 | 102 |
* Command callback. Flush votes and results. |
179 | 103 |
*/ |
drupal7/sites/all/modules/votingapi/votingapi.info | ||
---|---|---|
10 | 10 |
files[] = views/votingapi_views_handler_sort_nullable.inc |
11 | 11 |
files[] = views/votingapi_views_handler_relationship.inc |
12 | 12 |
|
13 |
; Information added by drupal.org packaging script on 2013-03-22
|
|
14 |
version = "7.x-2.11"
|
|
13 |
; Information added by Drupal.org packaging script on 2014-08-14
|
|
14 |
version = "7.x-2.12"
|
|
15 | 15 |
core = "7.x" |
16 | 16 |
project = "votingapi" |
17 |
datestamp = "1363989617"
|
|
17 |
datestamp = "1407995929"
|
|
18 | 18 |
|
drupal7/sites/all/modules/votingapi/votingapi.module | ||
---|---|---|
131 | 131 |
$votes = array($votes); |
132 | 132 |
} |
133 | 133 |
|
134 |
// Allow other modules to modify or unset/remove votes. |
|
135 |
// module_invoke_all does not allow passing variables by reference |
|
136 |
// http://api.drupal.org/api/drupal/includes%21module.inc/function/module_invoke_all/7#comment-35778 |
|
137 |
drupal_alter(array('votingapi_preset_votes'), $votes); |
|
134 | 138 |
// Handle clearing out old votes if they exist. |
135 | 139 |
if (!isset($criteria)) { |
136 | 140 |
// If the calling function didn't explicitly set criteria for vote deletion, |
Formats disponibles : Unified diff
Weekly update of contrib modules