| 1 |
<?php
|
| 2 |
// $Id: votingapi_views.inc,v 1.11.2.6 2007/03/30 15:10:59 eaton Exp $
|
| 3 |
|
| 4 |
function votingapi_views_tables() {
|
| 5 |
$vote_tags = _votingapi_views_values($field = 'tag', $table = 'vote');
|
| 6 |
$vote_types = _votingapi_views_values($field = 'value_type', $table = 'vote');
|
| 7 |
|
| 8 |
$vote_tags = array_unique(array_merge($vote_tags, array('vote')));
|
| 9 |
$vote_types = array_unique(array_merge($vote_types, array('percent')));
|
| 10 |
|
| 11 |
foreach ($vote_tags as $vote_tag) {
|
| 12 |
foreach ($vote_types as $vote_type) {
|
| 13 |
$table = array(
|
| 14 |
"name" => "votingapi_vote",
|
| 15 |
"provider" => "votingapi",
|
| 16 |
"join" => array(
|
| 17 |
"left" => array(
|
| 18 |
"table" => "node",
|
| 19 |
"field" => "nid"
|
| 20 |
),
|
| 21 |
"right" => array(
|
| 22 |
"field" => "content_id"
|
| 23 |
),
|
| 24 |
"extra" => array(
|
| 25 |
'content_type' => 'node',
|
| 26 |
'value_type' => $vote_type,
|
| 27 |
'tag' => $vote_tag,
|
| 28 |
),
|
| 29 |
),
|
| 30 |
"fields" => array(
|
| 31 |
"value" => array(
|
| 32 |
'name' => t("VotingAPI !type !tag value", array('!tag' => $vote_tag, '!type' => $vote_type)),
|
| 33 |
'handler' => votingapi_get_formatters(array('value_type' => $vote_type, 'tag' => $vote_tag)),
|
| 34 |
'value_type' => $vote_type,
|
| 35 |
'tag' => $vote_tag,
|
| 36 |
'sortable' => TRUE,
|
| 37 |
),
|
| 38 |
"timestamp" => array(
|
| 39 |
'name' => t("VotingAPI !type !tag timestamp", array('!tag' => $vote_tag, '!type' => $vote_type)),
|
| 40 |
'sortable' => TRUE,
|
| 41 |
'handler' =>
|
| 42 |
array(
|
| 43 |
"views_handler_field_date_small" => t('As Short Date'),
|
| 44 |
"views_handler_field_date" => t('As Medium Date'),
|
| 45 |
"views_handler_field_date_large" => t('As Long Date'),
|
| 46 |
"views_handler_field_since" => t('As Time Ago')
|
| 47 |
),
|
| 48 |
'sortable' => TRUE,
|
| 49 |
'help' => "Display the time the vote was cast.",
|
| 50 |
),
|
| 51 |
"uid" => array(
|
| 52 |
'name' => t("VotingAPI !type !tag user", array('!tag' => $vote_tag, '!type' => $vote_type)),
|
| 53 |
'handler' => 'votingapi_views_handler_field_username',
|
| 54 |
'sortable' => TRUE,
|
| 55 |
),
|
| 56 |
),
|
| 57 |
"sorts" => array(
|
| 58 |
"value" => array('name' => t("VotingAPI !type !tag vote", array('!tag' => $vote_tag, '!type' => $vote_type))),
|
| 59 |
"uid" => array('name' => t("VotingAPI !type !tag voter", array('!tag' => $vote_tag, '!type' => $vote_type))),
|
| 60 |
"timestamp" => array('name' => t("VotingAPI !type !tag timestamp", array('!tag' => $vote_tag, '!type' => $vote_type))),
|
| 61 |
),
|
| 62 |
'filters' => array(
|
| 63 |
'value' => array(
|
| 64 |
'name' => t("VotingAPI !type !tag value", array('!tag' => $vote_tag, '!type' => $vote_type)),
|
| 65 |
'operator' => 'views_handler_operator_gtlt',
|
| 66 |
'handler' => 'votingapi_handler_filter_value',
|
| 67 |
'value-type' => 'string',
|
| 68 |
'help' => t('Filter nodes by values of the individual votes users cast for them.'),
|
| 69 |
),
|
| 70 |
'timestamp' => array(
|
| 71 |
'name' => t("VotingAPI !type !tag timestamp", array('!tag' => $vote_tag, '!type' => $vote_type)),
|
| 72 |
'operator' => 'views_handler_operator_gtlt',
|
| 73 |
'value' => views_handler_filter_date_value_form(),
|
| 74 |
'handler' => 'views_handler_filter_timestamp',
|
| 75 |
'option' => 'string',
|
| 76 |
'help' => t('Filter nodes by the date they were voted on.')
|
| 77 |
.' '. views_t_strings('filter date'),
|
| 78 |
),
|
| 79 |
'uid' => array(
|
| 80 |
'name' => t("VotingAPI !type !tag user", array('!tag' => $vote_tag, '!type' => $vote_type)),
|
| 81 |
'operator' => array('IS NOT NULL' => t('Has been voted on by'), 'IS NULL' => t('Has not been voted on by')),
|
| 82 |
'list' => array('***CURRENT_USER***' => t('Currently Logged In User'), '***ANY_USER***' => t('Any user')),
|
| 83 |
'list-type' => 'select',
|
| 84 |
'handler' => 'votingapi_handler_filter_uid_voted',
|
| 85 |
'help' => t('Filter nodes by whether the currently logged in user has voted.'),
|
| 86 |
'content_type' => 'node',
|
| 87 |
'value_type' => $vote_type,
|
| 88 |
'tag' => $vote_tag,
|
| 89 |
)
|
| 90 |
),
|
| 91 |
);
|
| 92 |
$tables['votingapi_vote_'. $vote_tag .'_'. $vote_type] = $table;
|
| 93 |
|
| 94 |
$table = array(
|
| 95 |
"name" => "votingapi_vote",
|
| 96 |
"provider" => "votingapi",
|
| 97 |
"join" => array(
|
| 98 |
"left" => array(
|
| 99 |
"table" => "node",
|
| 100 |
"field" => "nid"
|
| 101 |
),
|
| 102 |
"right" => array(
|
| 103 |
"field" => "content_id"
|
| 104 |
),
|
| 105 |
"extra" => array(
|
| 106 |
'content_type' => 'node',
|
| 107 |
'value_type' => $vote_type,
|
| 108 |
'tag' => $vote_tag,
|
| 109 |
'uid' => '***CURRENT_USER***'
|
| 110 |
),
|
| 111 |
),
|
| 112 |
"fields" => array(
|
| 113 |
"value" => array(
|
| 114 |
'name' => t("VotingAPI !type !tag value (current user only)", array('!tag' => $vote_tag, '!type' => $vote_type)),
|
| 115 |
'field' => 'value',
|
| 116 |
'handler' => votingapi_get_formatters(array('value_type' => $vote_type, 'tag' => $vote_tag)),
|
| 117 |
'value_type' => $vote_type,
|
| 118 |
'tag' => $tag,
|
| 119 |
'sortable' => TRUE,
|
| 120 |
),
|
| 121 |
|
| 122 |
"timestamp" => array(
|
| 123 |
'name' => t("VotingAPI !type !tag timestamp (current user only)", array('!tag' => $vote_tag, '!type' => $vote_type)),
|
| 124 |
'sortable' => TRUE,
|
| 125 |
'handler' =>
|
| 126 |
array(
|
| 127 |
"views_handler_field_date_small" => t('As Short Date'),
|
| 128 |
"views_handler_field_date" => t('As Medium Date'),
|
| 129 |
"views_handler_field_date_large" => t('As Long Date'),
|
| 130 |
"views_handler_field_since" => t('As Time Ago')
|
| 131 |
),
|
| 132 |
'sortable' => TRUE,
|
| 133 |
'help' => "Display the time the vote was cast.",
|
| 134 |
),
|
| 135 |
),
|
| 136 |
"sorts" => array(
|
| 137 |
"value" => array('name' => t("VotingAPI !type !tag vote", array('!tag' => $vote_tag, '!type' => $vote_type))),
|
| 138 |
"timestamp" => array('name' => t("VotingAPI !type !tag timestamp", array('!tag' => $vote_tag, '!type' => $vote_type))),
|
| 139 |
),
|
| 140 |
'filters' => array(
|
| 141 |
'value' => array(
|
| 142 |
'name' => t("VotingAPI !type !tag value (current user only)", array('!tag' => $vote_tag, '!type' => $vote_type)),
|
| 143 |
'operator' => 'views_handler_operator_gtlt',
|
| 144 |
'handler' => 'votingapi_handler_filter_value',
|
| 145 |
'value-type' => 'string',
|
| 146 |
'help' => t("Filter nodes by values of the current user's vote for them."),
|
| 147 |
),
|
| 148 |
'timestamp' => array(
|
| 149 |
'name' => t("VotingAPI !type !tag timestamp (current user only)", array('!tag' => $vote_tag, '!type' => $vote_type)),
|
| 150 |
'operator' => 'views_handler_operator_gtlt',
|
| 151 |
'value' => views_handler_filter_date_value_form(),
|
| 152 |
'handler' => 'views_handler_filter_timestamp',
|
| 153 |
'option' => 'string',
|
| 154 |
'help' => t('Filter nodes by the date they were voted on by the current user.')
|
| 155 |
.' '. views_t_strings('filter date'),
|
| 156 |
),
|
| 157 |
),
|
| 158 |
);
|
| 159 |
$tables['votingapi_vote_'. $vote_tag .'_'. $vote_type . '_current_user'] = $table;
|
| 160 |
}
|
| 161 |
}
|
| 162 |
|
| 163 |
$cache_tags = _votingapi_views_values($field = 'tag', $table = 'cache');
|
| 164 |
$cache_types = _votingapi_views_values($field = 'value_type', $table = 'cache');
|
| 165 |
$cache_functions = _votingapi_views_values($field = 'function', $table = 'cache');
|
| 166 |
|
| 167 |
$cache_tags = array_unique(array_merge($vote_tags, array('vote')));
|
| 168 |
$cache_types = array_unique(array_merge($cache_types, array('percent')));
|
| 169 |
$cache_functions = array_unique(array_merge($cache_functions, array('average', 'count')));
|
| 170 |
|
| 171 |
foreach ($cache_functions as $cache_function) {
|
| 172 |
foreach ($cache_tags as $cache_tag) {
|
| 173 |
foreach ($cache_types as $cache_type) {
|
| 174 |
$table = array(
|
| 175 |
"name" => "votingapi_cache",
|
| 176 |
"provider" => "votingapi",
|
| 177 |
"join" => array(
|
| 178 |
"left" => array(
|
| 179 |
"table" => "node",
|
| 180 |
"field" => "nid"
|
| 181 |
),
|
| 182 |
"right" => array(
|
| 183 |
"field" => "content_id"
|
| 184 |
),
|
| 185 |
"extra" => array(
|
| 186 |
'content_type' => 'node',
|
| 187 |
'value_type' => $cache_type,
|
| 188 |
'tag' => $cache_tag,
|
| 189 |
'function' => $cache_function,
|
| 190 |
),
|
| 191 |
),
|
| 192 |
"fields" => array(
|
| 193 |
"value" => array(
|
| 194 |
'name' => t("VotingAPI !type !tag result (!function)", array('!tag' => $cache_tag, '!type' => $cache_type, '!function' => $cache_function)),
|
| 195 |
'handler' => votingapi_get_formatters(array('value_type' => $cache_type, 'tag' => $cache_tag, 'function' => $cache_function)),
|
| 196 |
'value_type' => $cache_type,
|
| 197 |
'tag' => $cache_tag,
|
| 198 |
'function' => $cache_function,
|
| 199 |
'sortable' => TRUE,
|
| 200 |
),
|
| 201 |
),
|
| 202 |
"sorts" => array(
|
| 203 |
"value" => array('name' => t("VotingAPI !type !tag result (!function)", array('!tag' => $cache_tag, '!type' => $cache_type, '!function' => $cache_function))),
|
| 204 |
),
|
| 205 |
'filters' => array(
|
| 206 |
'value' => array(
|
| 207 |
'name' => t("VotingAPI !type !tag result (!function)", array('!tag' => $cache_tag, '!type' => $cache_type, '!function' => $cache_function)),
|
| 208 |
'operator' => 'views_handler_operator_gtlt',
|
| 209 |
'handler' => 'votingapi_handler_filter_value',
|
| 210 |
'value-type' => 'string',
|
| 211 |
'help' => t('Filter nodes by the aggregate results of votes cast.'),
|
| 212 |
),
|
| 213 |
),
|
| 214 |
);
|
| 215 |
$tables[implode('_', array('votingapi_cache', $cache_tag, $cache_type, $cache_function))] = $table;
|
| 216 |
}
|
| 217 |
}
|
| 218 |
}
|
| 219 |
|
| 220 |
return $tables;
|
| 221 |
}
|
| 222 |
|
| 223 |
function votingapi_views_handler_field_username($fieldinfo, $fielddata, $value, $data) {
|
| 224 |
if ($value === NULL) {
|
| 225 |
return '';
|
| 226 |
}
|
| 227 |
else {
|
| 228 |
$user = user_load(array('uid' => $value));
|
| 229 |
return theme('username', $user);
|
| 230 |
}
|
| 231 |
}
|
| 232 |
|
| 233 |
function votingapi_handler_filter_uid_voted($op, $filter, $filterinfo, &$query) {
|
| 234 |
$table_data = _views_get_tables();
|
| 235 |
$joininfo['left'] = array(
|
| 236 |
'table' => 'node',
|
| 237 |
'field' => 'nid'
|
| 238 |
);
|
| 239 |
$joininfo['right'] = array(
|
| 240 |
'field' => 'content_id'
|
| 241 |
);
|
| 242 |
$joininfo['extra'] = array(
|
| 243 |
'value_type' => $filterinfo['value_type'],
|
| 244 |
'content_type' => $filterinfo['content_type'],
|
| 245 |
'tag' => $filterinfo['tag'],
|
| 246 |
);
|
| 247 |
|
| 248 |
if ($filter['value'] != '***ANY_USER***') {
|
| 249 |
$joininfo['extra']['uid'] = $filter['value'];
|
| 250 |
}
|
| 251 |
|
| 252 |
$tblnum = $query->add_table('votingapi_vote', false, 1, $joininfo);
|
| 253 |
$table = $query->get_table_name('votingapi_vote', $tblnum);
|
| 254 |
|
| 255 |
$query->add_where("$table.value %s", $filter['operator']);
|
| 256 |
}
|
| 257 |
|
| 258 |
function votingapi_views_qhandler_currentuser_vote($field, $fieldinfo, &$query) {
|
| 259 |
$table = $fieldinfo['table'];
|
| 260 |
$query->add_where("$table.uid = ***CURRENT_USER***");
|
| 261 |
}
|
| 262 |
|
| 263 |
function votingapi_handler_filter_value($op, $filter, $filterinfo, &$query) {
|
| 264 |
$table = $filterinfo['table'];
|
| 265 |
$column = $filterinfo['field'];
|
| 266 |
$field = "$table.$column";
|
| 267 |
$query->ensure_table($table);
|
| 268 |
$query->add_where("%s %s %d", $field, $filter['operator'], $filter['value']);
|
| 269 |
}
|
| 270 |
|
| 271 |
function votingapi_get_formatters($details = array()) {
|
| 272 |
$formatters = array(
|
| 273 |
'votingapi_views_formatter_raw' => t('Raw value'),
|
| 274 |
'votingapi_views_formatter_cleaned' => t('Cleaned version'),
|
| 275 |
);
|
| 276 |
$formatters = array_merge($formatters, module_invoke_all('votingapi_views_formatters', $details));
|
| 277 |
return $formatters;
|
| 278 |
}
|
| 279 |
|
| 280 |
function votingapi_views_formatter_raw($fieldinfo, $fielddata, $value, $data) {
|
| 281 |
return $value;
|
| 282 |
}
|
| 283 |
|
| 284 |
|
| 285 |
function votingapi_views_formatter_cleaned($fieldinfo, $fielddata, $value, $data) {
|
| 286 |
if ($value === NULL) {
|
| 287 |
return '';
|
| 288 |
}
|
| 289 |
|
| 290 |
switch ($fieldinfo['value_type']) {
|
| 291 |
case 'percent':
|
| 292 |
return round($value, 1) .'%';
|
| 293 |
case 'points':
|
| 294 |
return round($value, 1) .' '. t('points');
|
| 295 |
default:
|
| 296 |
return $value;
|
| 297 |
}
|
| 298 |
}
|
| 299 |
|
| 300 |
function _votingapi_views_values($field = 'tag', $table = 'vote') {
|
| 301 |
static $cached;
|
| 302 |
if (!isset($cached[$table][$field])) {
|
| 303 |
$results = db_query("SELECT DISTINCT %s FROM {votingapi_%s} WHERE content_type = 'node'", $field, $table);
|
| 304 |
while ($result = db_fetch_object($results)) {
|
| 305 |
$cached[$table][$field][$result->$field] = $result->$field;
|
| 306 |
}
|
| 307 |
}
|
| 308 |
return empty($cached[$table][$field]) ? array() : $cached[$table][$field];
|
| 309 |
}
|