| 1 |
<?php
|
| 2 |
|
| 3 |
/**
|
| 4 |
* $Id$
|
| 5 |
*
|
| 6 |
* @file
|
| 7 |
* A medium-level demonstration of VotingAPI.
|
| 8 |
* Original simplevote module with:
|
| 9 |
* 1) AJAX handling (if javascript is not enabled, uses regular links)
|
| 10 |
* 2) Multi-criteria voting
|
| 11 |
* 3) Option to choose voting mode (either all modes, mode-specific)
|
| 12 |
* settings->mediumvote
|
| 13 |
* 4) Node-specific voting options
|
| 14 |
*
|
| 15 |
* Development funded by addictedtotravel.com
|
| 16 |
*/
|
| 17 |
|
| 18 |
// ability to show voting on nodes & comments
|
| 19 |
define ('MEDIUMVOTE_DENY', 0);
|
| 20 |
define ('MEDIUMVOTE_ALLOW', 1);
|
| 21 |
define ('MEDIUMVOTE_NODE', 2);
|
| 22 |
|
| 23 |
// ability to vote on nodes and comments
|
| 24 |
define ('VOTINGAPI_VALUE_NODE_CONTENT_TYPE', 'node');
|
| 25 |
define ('VOTINGAPI_VALUE_COMMENT_CONTENT_TYPE', 'comment');
|
| 26 |
|
| 27 |
// define the tags used for multi-criteria voting
|
| 28 |
define ('VOTINGAPI_COMMENT_TAG_TAG1', '1');
|
| 29 |
define ('VOTINGAPI_COMMENT_TAG_TAG2', '2');
|
| 30 |
|
| 31 |
// number of allowed voting criteria
|
| 32 |
define ('NUM_VOTING_CRITERIA', 5);
|
| 33 |
|
| 34 |
function mediumvote_menu($may_cache) {
|
| 35 |
drupal_set_html_head(theme('stylesheet_import', theme('mediumvote_css_path')));
|
| 36 |
|
| 37 |
$items = array();
|
| 38 |
|
| 39 |
if ($may_cache) {
|
| 40 |
$items[] = array(
|
| 41 |
'path' => 'vote',
|
| 42 |
'title' => t('vote on content'),
|
| 43 |
'callback' => 'mediumvote_vote',
|
| 44 |
'access' => TRUE,
|
| 45 |
'type' => MENU_CALLBACK
|
| 46 |
);
|
| 47 |
$items[] = array(
|
| 48 |
'path' => 'ajax_mediumvote',
|
| 49 |
'title' => t('vote on content (ajax)'),
|
| 50 |
'callback' => 'ajax_mediumvote_handler',
|
| 51 |
'access' => TRUE,
|
| 52 |
'type' => MENU_CALLBACK
|
| 53 |
);
|
| 54 |
$items[] = array(
|
| 55 |
'path' => 'admin/settings/mediumvote',
|
| 56 |
'title' => t('mediumvote'),
|
| 57 |
'callback' => 'mediumvote_configure'
|
| 58 |
);
|
| 59 |
|
| 60 |
}
|
| 61 |
|
| 62 |
return $items;
|
| 63 |
}
|
| 64 |
|
| 65 |
function mediumvote_help($section) {
|
| 66 |
switch ($section) {
|
| 67 |
case 'admin/modules#description':
|
| 68 |
return t('Adds a vote widget to every node and comment.');
|
| 69 |
break;
|
| 70 |
case 'admin/settings/mediumvote':
|
| 71 |
return t('<p>Configure mediumvote:</p>');
|
| 72 |
break;
|
| 73 |
}
|
| 74 |
}
|
| 75 |
|
| 76 |
/**
|
| 77 |
* Return an array of voting modes.
|
| 78 |
* Three standard types defined by votingapi:
|
| 79 |
* VOTINGAPI_VALUE_TYPE_PERCENT -- 'Value' is a number from 0-100. The API will cache an average for all votes.
|
| 80 |
* VOTINGAPI_VALUE_TYPE_TOKEN -- 'Value' is a positive or negative int. The API will cache the sum of all votes.
|
| 81 |
* VOTINGAPI_VALUE_TYPE_KEY -- 'Value' is a foreign key. The API will cache a vote-count for each discrete value.
|
| 82 |
*/
|
| 83 |
function _mediumvote_get_modes_array() {
|
| 84 |
return array(
|
| 85 |
VOTINGAPI_VALUE_TYPE_PERCENT => t('Percentage (0-100)'),
|
| 86 |
VOTINGAPI_VALUE_TYPE_TOKEN => t('Score (positive or negative)')
|
| 87 |
|
| 88 |
// Not yet implemented:
|
| 89 |
//VOTINGAPI_VALUE_TYPE_KEY => t('Foreign Key')
|
| 90 |
);
|
| 91 |
}
|
| 92 |
|
| 93 |
/**
|
| 94 |
* Return voting mode for a node or comment
|
| 95 |
*
|
| 96 |
* @param $id
|
| 97 |
* Element (node, comment) id.
|
| 98 |
* @param $type
|
| 99 |
* node or comment
|
| 100 |
* @return
|
| 101 |
* Voting mode for element
|
| 102 |
*/
|
| 103 |
function _mediumvote_get_mode($id, $type) {
|
| 104 |
$allow_override = variable_get('allow_override_'.$type, FALSE);
|
| 105 |
if ($allow_override) {
|
| 106 |
if ($type == VOTINGAPI_VALUE_NODE_CONTENT_TYPE) {
|
| 107 |
$node = node_load(array('nid' => $id));
|
| 108 |
$mode = variable_get($type.'_mode_'.$node->type, VOTINGAPI_VALUE_TYPE_PERCENT);
|
| 109 |
}
|
| 110 |
elseif ($type == VOTINGAPI_VALUE_COMMENT_CONTENT_TYPE) {
|
| 111 |
$comment_result = _comment_load($id);
|
| 112 |
$comment_nid = $comment_result->nid;
|
| 113 |
$node = node_load(array('nid' => $comment_nid));
|
| 114 |
$mode = variable_get($type.'_mode_'.$node->type, VOTINGAPI_VALUE_TYPE_PERCENT);
|
| 115 |
}
|
| 116 |
else {
|
| 117 |
$mode = variable_get($type.'_mode_all', VOTINGAPI_VALUE_TYPE_PERCENT);
|
| 118 |
}
|
| 119 |
}
|
| 120 |
else {
|
| 121 |
$mode = variable_get($type.'_mode_all', VOTINGAPI_VALUE_TYPE_PERCENT);
|
| 122 |
}
|
| 123 |
|
| 124 |
return $mode;
|
| 125 |
}
|
| 126 |
|
| 127 |
/**
|
| 128 |
* Return voting tag for a node or comment
|
| 129 |
*
|
| 130 |
* @param $index
|
| 131 |
* Tag number
|
| 132 |
* @param $id
|
| 133 |
* Element (node, comment) id.
|
| 134 |
* @param $type
|
| 135 |
* node or comment
|
| 136 |
* @param $node
|
| 137 |
* Either node, or node comment belongs to
|
| 138 |
* @return
|
| 139 |
* Voting tag for element at index
|
| 140 |
*/
|
| 141 |
function _mediumvote_get_tag($index, $type, &$node) {
|
| 142 |
$allow_override = variable_get('allow_override_'.$type, FALSE);
|
| 143 |
if ($allow_override) {
|
| 144 |
if ($type == VOTINGAPI_VALUE_NODE_CONTENT_TYPE) {
|
| 145 |
$tag = variable_get($type.'_tag_'.$node->type.'_'.$index, '');
|
| 146 |
}
|
| 147 |
elseif ($type == VOTINGAPI_VALUE_COMMENT_CONTENT_TYPE) {
|
| 148 |
$tag = variable_get($type.'_tag_'.$node->type.'_'.$index, '');
|
| 149 |
}
|
| 150 |
else {
|
| 151 |
$tag = variable_get($type.'_tag_'.$index, '');
|
| 152 |
}
|
| 153 |
}
|
| 154 |
else {
|
| 155 |
$tag = variable_get($type.'_tag_'.$index, '');
|
| 156 |
}
|
| 157 |
|
| 158 |
return $tag;
|
| 159 |
}
|
| 160 |
|
| 161 |
/**
|
| 162 |
* Return an array of allow modes.
|
| 163 |
*/
|
| 164 |
function _mediumvote_get_allow($type, $node_type) {
|
| 165 |
$allow = array(
|
| 166 |
MEDIUMVOTE_ALLOW => t('Allow').' '.($type?$type.' ':'').t('voting for'.' '.$node_type),
|
| 167 |
MEDIUMVOTE_DENY => t('Deny').' '.($type?$type.' ':'').t('voting for'.' '.$node_type),
|
| 168 |
);
|
| 169 |
|
| 170 |
return $allow;
|
| 171 |
}
|
| 172 |
|
| 173 |
function mediumvote_configure_settings() {
|
| 174 |
}
|
| 175 |
|
| 176 |
|
| 177 |
/**
|
| 178 |
* mediumvote settings page.
|
| 179 |
*/
|
| 180 |
function mediumvote_configure() {
|
| 181 |
|
| 182 |
// default settings
|
| 183 |
$form['default_settings'] = array(
|
| 184 |
'#type' => 'fieldset',
|
| 185 |
'#title' => t('Default settings'),
|
| 186 |
);
|
| 187 |
|
| 188 |
// nodes defaults
|
| 189 |
$form['default_settings']['node'] = array(
|
| 190 |
'#type' => 'fieldset',
|
| 191 |
'#title' => t('Nodes'),
|
| 192 |
);
|
| 193 |
|
| 194 |
$form['default_settings']['node']['allow_node_all'] = array(
|
| 195 |
'#type' => 'radios',
|
| 196 |
'#title' => t('Allow/Deny'),
|
| 197 |
'#default_value' => variable_get('allow_node_all', MEDIUMVOTE_ALLOW),
|
| 198 |
'#options' => _mediumvote_get_allow('', t('all nodes'))
|
| 199 |
);
|
| 200 |
|
| 201 |
$form['default_settings']['node']['node_mode_all'] = array(
|
| 202 |
'#type' => 'radios',
|
| 203 |
'#title' => t('Default Voting Mode'),
|
| 204 |
'#default_value' => variable_get('node_mode_all', VOTINGAPI_VALUE_TYPE_PERCENT),
|
| 205 |
'#options' => _mediumvote_get_modes_array(),
|
| 206 |
'#description' => t('The default voting mode: Percentages or Raw Scores.'),
|
| 207 |
);
|
| 208 |
|
| 209 |
$form['default_settings']['node']['criteria'] = array(
|
| 210 |
'#type' => 'fieldset',
|
| 211 |
'#title' => t('Voting Criteria'),
|
| 212 |
);
|
| 213 |
|
| 214 |
for ($tag_count = 1; $tag_count <= NUM_VOTING_CRITERIA; $tag_count++) {
|
| 215 |
$form['default_settings']['node']['criteria']['node_tag_'.$tag_count] = array(
|
| 216 |
'#type' => 'textfield',
|
| 217 |
'#title' => t('Tag').' '.$tag_count,
|
| 218 |
'#default_value' => variable_get('node_tag_'.$tag_count, ''),
|
| 219 |
'#size' => 25,
|
| 220 |
'#maxlength' => 25
|
| 221 |
);
|
| 222 |
}
|
| 223 |
|
| 224 |
$form['default_settings']['node']['allow_override_node'] = array(
|
| 225 |
'#type' => 'checkbox',
|
| 226 |
'#title' => t('Allow node-specific override'),
|
| 227 |
'#return_value' => 1,
|
| 228 |
'#default_value' => variable_get('allow_override_node', FALSE),
|
| 229 |
'#description' => t('If checked, node-specific settings below will override default values.')
|
| 230 |
);
|
| 231 |
|
| 232 |
// comments defaults
|
| 233 |
$form['default_settings']['comment'] = array(
|
| 234 |
'#type' => 'fieldset',
|
| 235 |
'#title' => t('Comments'),
|
| 236 |
);
|
| 237 |
|
| 238 |
$form['default_settings']['comment']['allow_comment_all'] = array(
|
| 239 |
'#type' => 'radios',
|
| 240 |
'#title' => t('Allow/Deny'),
|
| 241 |
'#default_value' => variable_get('allow_comment_all', MEDIUMVOTE_ALLOW),
|
| 242 |
'#options' => _mediumvote_get_allow('', t('all comments'))
|
| 243 |
);
|
| 244 |
|
| 245 |
$form['default_settings']['comment']['comment_mode_all'] = array(
|
| 246 |
'#type' => 'radios',
|
| 247 |
'#title' => t('Default Voting Mode'),
|
| 248 |
'#default_value' => variable_get('comment_mode_all', VOTINGAPI_VALUE_TYPE_PERCENT),
|
| 249 |
'#options' => _mediumvote_get_modes_array(),
|
| 250 |
'#description' => t('The default voting mode: Percentages or Raw Scores.'),
|
| 251 |
);
|
| 252 |
|
| 253 |
for ($tag_count = 1; $tag_count <= NUM_VOTING_CRITERIA; $tag_count++) {
|
| 254 |
$form['default_settings']['comment']['criteria']['comment_tag_'.$tag_count] = array(
|
| 255 |
'#type' => 'textfield',
|
| 256 |
'#title' => t('Tag').' '.$tag_count,
|
| 257 |
'#default_value' => variable_get('comment_tag_'.$tag_count, ''),
|
| 258 |
'#size' => 25,
|
| 259 |
'#maxlength' => 25
|
| 260 |
);
|
| 261 |
}
|
| 262 |
|
| 263 |
$form['default_settings']['comment']['allow_override_comment'] = array(
|
| 264 |
'#type' => 'checkbox',
|
| 265 |
'#title' => t('Allow node-specific override for comments'),
|
| 266 |
'#return_value' => 1,
|
| 267 |
'#default_value' => variable_get('allow_override_comment', FALSE),
|
| 268 |
'#description' => t('If checked, node-specific settings below will override default values.')
|
| 269 |
);
|
| 270 |
|
| 271 |
// if override of node or comments is allowed
|
| 272 |
// show node-specific settings
|
| 273 |
if (variable_get('allow_override_node', FALSE) OR variable_get('allow_override_comment', FALSE)) {
|
| 274 |
|
| 275 |
// node-specific settings
|
| 276 |
$form['allow_votes'] = array(
|
| 277 |
'#type' => 'fieldset',
|
| 278 |
'#title' => t('Node-specific settings'),
|
| 279 |
);
|
| 280 |
|
| 281 |
foreach(node_get_types() as $type => $name) {
|
| 282 |
|
| 283 |
$form['allow_votes'][$type] = array(
|
| 284 |
'#type' => 'fieldset',
|
| 285 |
'#title' => $name,
|
| 286 |
'#collapsible' => TRUE,
|
| 287 |
'#collapsed' => TRUE,
|
| 288 |
);
|
| 289 |
|
| 290 |
if (variable_get('allow_override_node', FALSE)) {
|
| 291 |
// node
|
| 292 |
$form['allow_votes'][$type]['node'] = array(
|
| 293 |
'#type' => 'fieldset',
|
| 294 |
'#title' => t('Node'),
|
| 295 |
);
|
| 296 |
|
| 297 |
$form['allow_votes'][$type]['node']['allow_node_'.$type] = array(
|
| 298 |
'#type' => 'radios',
|
| 299 |
'#title' => t('Allow/Deny'),
|
| 300 |
'#default_value' => variable_get('allow_node_'.$type, MEDIUMVOTE_ALLOW),
|
| 301 |
'#options' => _mediumvote_get_allow(t('node'), $name)
|
| 302 |
);
|
| 303 |
|
| 304 |
$form['allow_votes'][$type]['node']['node_mode_'.$type] = array(
|
| 305 |
'#type' => 'radios',
|
| 306 |
'#title' => t('Voting Mode'),
|
| 307 |
'#default_value' => variable_get('node_mode_'.$type, VOTINGAPI_VALUE_TYPE_PERCENT),
|
| 308 |
'#options' => _mediumvote_get_modes_array(),
|
| 309 |
'#description' => t('The default voting mode: Percentages or Raw Scores.'),
|
| 310 |
);
|
| 311 |
|
| 312 |
for ($tag_count = 1; $tag_count <= NUM_VOTING_CRITERIA; $tag_count++) {
|
| 313 |
$form['allow_votes'][$type]['node']['criteria']['node_tag_'.$type.'_'.$tag_count] = array(
|
| 314 |
'#type' => 'textfield',
|
| 315 |
'#title' => t('Tag').' '.$tag_count,
|
| 316 |
'#default_value' => variable_get('node_tag_'.$type.'_'.$tag_count, ''),
|
| 317 |
'#size' => 25,
|
| 318 |
'#maxlength' => 25
|
| 319 |
);
|
| 320 |
}
|
| 321 |
}
|
| 322 |
else {
|
| 323 |
// clear variables
|
| 324 |
variable_set('allow_node_'.$type, MEDIUMVOTE_DENY);
|
| 325 |
variable_set('node_mode_'.$type, VOTINGAPI_VALUE_TYPE_PERCENT);
|
| 326 |
for ($tag_count = 1; $tag_count <= NUM_VOTING_CRITERIA; $tag_count++) {
|
| 327 |
variable_set('node_tag_'.$type.'_'.$tag_count, '');
|
| 328 |
}
|
| 329 |
|
| 330 |
} // end if node override
|
| 331 |
|
| 332 |
if (variable_get('allow_override_comment', FALSE)) {
|
| 333 |
// comments
|
| 334 |
$form['allow_votes'][$type]['comment'] = array(
|
| 335 |
'#type' => 'fieldset',
|
| 336 |
'#title' => t('Comments'),
|
| 337 |
);
|
| 338 |
|
| 339 |
$form['allow_votes'][$type]['comment']['allow_comment_'.$type] = array(
|
| 340 |
'#type' => 'radios',
|
| 341 |
'#title' => t('Allow/Deny'),
|
| 342 |
'#default_value' => variable_get('allow_comment_'.$type, MEDIUMVOTE_ALLOW),
|
| 343 |
'#options' => _mediumvote_get_allow(t('comment'), $name)
|
| 344 |
);
|
| 345 |
|
| 346 |
$form['allow_votes'][$type]['comment']['comment_mode_'.$type] = array(
|
| 347 |
'#type' => 'radios',
|
| 348 |
'#title' => t('Voting Mode'),
|
| 349 |
'#default_value' => variable_get('comment_mode_'.$type, VOTINGAPI_VALUE_TYPE_PERCENT),
|
| 350 |
'#options' => _mediumvote_get_modes_array(),
|
| 351 |
'#description' => t('The default voting mode: Percentages or Raw Scores.'),
|
| 352 |
);
|
| 353 |
|
| 354 |
for ($tag_count = 1; $tag_count <= NUM_VOTING_CRITERIA; $tag_count++) {
|
| 355 |
$form['allow_votes'][$type]['comment']['criteria']['comment_tag_'.$type.'_'.$tag_count] = array(
|
| 356 |
'#type' => 'textfield',
|
| 357 |
'#title' => t('Tag').' '.$tag_count,
|
| 358 |
'#default_value' => variable_get('comment_tag_'.$type.'_'.$tag_count, ''),
|
| 359 |
'#size' => 25,
|
| 360 |
'#maxlength' => 25
|
| 361 |
);
|
| 362 |
}
|
| 363 |
}
|
| 364 |
else {
|
| 365 |
// clear variables
|
| 366 |
variable_set('allow_comment_'.$type, MEDIUMVOTE_DENY);
|
| 367 |
variable_set('comment_mode_'.$type, VOTINGAPI_VALUE_TYPE_PERCENT);
|
| 368 |
for ($tag_count = 1; $tag_count <= NUM_VOTING_CRITERIA; $tag_count++) {
|
| 369 |
variable_set('comment_tag_'.$type.'_'.$tag_count, '');
|
| 370 |
}
|
| 371 |
} // end if comment override
|
| 372 |
|
| 373 |
} // end foreach node type
|
| 374 |
|
| 375 |
}
|
| 376 |
else {
|
| 377 |
// clear variables
|
| 378 |
foreach(node_get_types() as $type => $name) {
|
| 379 |
variable_set('allow_node_'.$type, MEDIUMVOTE_DENY);
|
| 380 |
variable_set('node_mode_'.$type, VOTINGAPI_VALUE_TYPE_PERCENT);
|
| 381 |
variable_set('allow_comment_'.$type, MEDIUMVOTE_DENY);
|
| 382 |
variable_set('comment_mode_'.$type, VOTINGAPI_VALUE_TYPE_PERCENT);
|
| 383 |
for ($tag_count = 1; $tag_count <= NUM_VOTING_CRITERIA; $tag_count++) {
|
| 384 |
variable_set('node_tag_'.$type.'_'.$tag_count, '');
|
| 385 |
variable_set('comment_tag_'.$type.'_'.$tag_count, '');
|
| 386 |
}
|
| 387 |
}
|
| 388 |
} // end if override
|
| 389 |
|
| 390 |
return system_settings_form('mediumvote_configure_settings', $form);
|
| 391 |
|
| 392 |
}
|
| 393 |
|
| 394 |
function mediumvote_vote($type, $tag = VOTINGAPI_VALUE_DEFAULT_TAG, $cid, $value) {
|
| 395 |
|
| 396 |
$mode = _mediumvote_get_mode($cid, $type);
|
| 397 |
|
| 398 |
if ($mode == VOTINGAPI_VALUE_TYPE_PERCENT) {
|
| 399 |
// sanity-check the incoming values.
|
| 400 |
if (is_numeric($cid) && is_numeric($value)) {
|
| 401 |
if ($value > 100) {
|
| 402 |
$value = 100;
|
| 403 |
}
|
| 404 |
}
|
| 405 |
}
|
| 406 |
|
| 407 |
$vote->value = $value;
|
| 408 |
$vote->value_type = $mode;
|
| 409 |
$vote->tag = $tag;
|
| 410 |
|
| 411 |
votingapi_set_vote($type, $cid, $vote);
|
| 412 |
|
| 413 |
drupal_goto(drupal_get_destination());
|
| 414 |
}
|
| 415 |
|
| 416 |
/**
|
| 417 |
* An ajax voting get handler
|
| 418 |
*
|
| 419 |
* Params passed by url:
|
| 420 |
* @param $content_type
|
| 421 |
* A string identifying the type of content whose votes are being retrieved. Node, comment, aggregator item, etc.
|
| 422 |
* @param $tag
|
| 423 |
* Tag defining vote criteria.
|
| 424 |
* @param $id
|
| 425 |
* Element (node, comment) id.
|
| 426 |
* @param $score
|
| 427 |
* Score of the vote cast.
|
| 428 |
* @return
|
| 429 |
* String containing an Updated score (via print)
|
| 430 |
*/
|
| 431 |
function ajax_mediumvote_handler() {
|
| 432 |
|
| 433 |
// get vote paramaters from get
|
| 434 |
$content_type = arg(1);
|
| 435 |
$tag = arg(2);
|
| 436 |
$id = arg(3);
|
| 437 |
$score = arg(4);
|
| 438 |
|
| 439 |
$mode = _mediumvote_get_mode($id, $content_type);
|
| 440 |
|
| 441 |
if ($mode == VOTINGAPI_VALUE_TYPE_PERCENT) {
|
| 442 |
$function = 'average';
|
| 443 |
}
|
| 444 |
else {
|
| 445 |
$function = 'sum';
|
| 446 |
}
|
| 447 |
|
| 448 |
// add the vote
|
| 449 |
$vote->value = $score;
|
| 450 |
$vote->value_type = $mode;
|
| 451 |
$vote->tag = $tag;
|
| 452 |
|
| 453 |
votingapi_set_vote($content_type, $id, $vote);
|
| 454 |
|
| 455 |
// get updated results
|
| 456 |
$vote = votingapi_get_voting_result($content_type, $id, $mode, $tag, $function);
|
| 457 |
|
| 458 |
// send the results back
|
| 459 |
print $tag.' (' . $vote->value . ')';
|
| 460 |
|
| 461 |
exit();
|
| 462 |
}
|
| 463 |
|
| 464 |
|
| 465 |
function theme_mediumvote_widget($cid, $type, $tag = VOTINGAPI_VALUE_DEFAULT_TAG) {
|
| 466 |
|
| 467 |
global $user;
|
| 468 |
|
| 469 |
$mode = _mediumvote_get_mode($cid, $type);
|
| 470 |
|
| 471 |
// if user is logged in, get votes already cast
|
| 472 |
$user_vote = '';
|
| 473 |
if ($user->uid != 0) {
|
| 474 |
$user_vote = votingapi_get_vote($type, $cid, $mode, $tag, $user->uid);
|
| 475 |
}
|
| 476 |
|
| 477 |
if ($mode == VOTINGAPI_VALUE_TYPE_PERCENT) {
|
| 478 |
// Get the current vote. It should come in as a percentage,
|
| 479 |
$vote = votingapi_get_voting_result($type, $cid, $mode, $tag, 'average');
|
| 480 |
|
| 481 |
if ($vote) {
|
| 482 |
$stars = round($vote->value);
|
| 483 |
}
|
| 484 |
else {
|
| 485 |
$stars = 0;
|
| 486 |
}
|
| 487 |
|
| 488 |
$output = '<div class="mediumvote_widget">';
|
| 489 |
switch ($type)
|
| 490 |
{
|
| 491 |
case VOTINGAPI_VALUE_NODE_CONTENT_TYPE:
|
| 492 |
$output .= '<span class="score" id="score_' . $tag . '_' . $cid .'">' . $tag . ' (' . $stars . ')';
|
| 493 |
break;
|
| 494 |
case VOTINGAPI_VALUE_COMMENT_CONTENT_TYPE:
|
| 495 |
default:
|
| 496 |
$output .= '<span class="score" id="score_' . $tag . '_' . $cid .'">' . $tag . ' (' . $stars . ')';
|
| 497 |
break;
|
| 498 |
}
|
| 499 |
|
| 500 |
for ($i = 20; $i <= 100; $i += 20) {
|
| 501 |
$output .= theme('mediumvote_icon', $type, $cid, $i, $stars >= $i, $tag, $mode, $user_vote);
|
| 502 |
}
|
| 503 |
|
| 504 |
$output .= '</span>';
|
| 505 |
|
| 506 |
$output .= '</div>';
|
| 507 |
}
|
| 508 |
else {
|
| 509 |
// Get the current vote. It should come in as a score
|
| 510 |
$vote = votingapi_get_voting_result($type, $cid, $mode, $tag, 'sum');
|
| 511 |
|
| 512 |
if ($vote) {
|
| 513 |
$score = $vote->value;
|
| 514 |
}
|
| 515 |
else {
|
| 516 |
$score = 0;
|
| 517 |
}
|
| 518 |
|
| 519 |
$output = '<div class="mediumvote_widget">';
|
| 520 |
switch ($type)
|
| 521 |
{
|
| 522 |
case VOTINGAPI_VALUE_NODE_CONTENT_TYPE:
|
| 523 |
$output .= '<span class="score" id="score_' . $tag . '_' . $cid .'">' . $tag . ' (' . $score . ')';
|
| 524 |
break;
|
| 525 |
case VOTINGAPI_VALUE_COMMENT_CONTENT_TYPE:
|
| 526 |
default:
|
| 527 |
$output .= '<span class="score" id="score_' . $tag . '_' . $cid .'">' . $tag . ' (' . $score . ')';
|
| 528 |
break;
|
| 529 |
}
|
| 530 |
|
| 531 |
// add positive vote
|
| 532 |
$output .= theme('mediumvote_icon', $type, $cid, 1, FALSE, $tag, $mode, $user_vote);
|
| 533 |
// add negative vote
|
| 534 |
$output .= theme('mediumvote_icon', $type, $cid, -1, FALSE, $tag, $mode, $user_vote);
|
| 535 |
|
| 536 |
$output .= '</span>';
|
| 537 |
|
| 538 |
$output .= '</div>';
|
| 539 |
}
|
| 540 |
|
| 541 |
return $output;
|
| 542 |
}
|
| 543 |
|
| 544 |
function theme_mediumvote_icon($type, $cid, $value, $filled, $tag = VOTINGAPI_VALUE_DEFAULT_TAG, $mode = VOTINGAPI_VALUE_TYPE_PERCENT, $user_vote = '') {
|
| 545 |
global $user;
|
| 546 |
|
| 547 |
$url = 'vote/' . $type . '/' . $tag . '/' . $cid . '/' . $value;
|
| 548 |
$ajax_url = 'ajax_mediumvote/' . $type . '/' . $tag . '/' . $cid . '/' . $value;
|
| 549 |
|
| 550 |
if ($mode == VOTINGAPI_VALUE_TYPE_PERCENT) {
|
| 551 |
|
| 552 |
if ($filled) {
|
| 553 |
$class = 'vote-on';
|
| 554 |
}
|
| 555 |
else {
|
| 556 |
$class = 'vote-off';
|
| 557 |
}
|
| 558 |
|
| 559 |
// If the user isn't logged in, or has voted already, show the vote but don't let them change it.
|
| 560 |
// We do this by writing out a span with the same classes, rather than an <A> tag.
|
| 561 |
if ($user->uid == 0 OR $user_vote) {
|
| 562 |
return '<span class="vote"></span>';
|
| 563 |
}
|
| 564 |
else {
|
| 565 |
$attributes = array(
|
| 566 |
'class' => $class,
|
| 567 |
'title' => 'Rate'
|
| 568 |
);
|
| 569 |
$link = '';
|
| 570 |
}
|
| 571 |
|
| 572 |
}
|
| 573 |
else {
|
| 574 |
|
| 575 |
// If the user isn't logged in, or has voted already, show the vote but don't let them change it.
|
| 576 |
// We do this by writing out a span with the same classes, rather than an <A> tag.
|
| 577 |
if ($user->uid == 0 OR $user_vote) {
|
| 578 |
return '<span class="vote"></span>';
|
| 579 |
}
|
| 580 |
else {
|
| 581 |
|
| 582 |
// add either positive or negative vote link
|
| 583 |
if ($value == 1) {
|
| 584 |
$attributes = array(
|
| 585 |
'class' => 'link_pos',
|
| 586 |
'title' => 'Like'
|
| 587 |
);
|
| 588 |
$class = 'vote_pos';
|
| 589 |
$link = '[+]';
|
| 590 |
}
|
| 591 |
else {
|
| 592 |
$attributes = array(
|
| 593 |
'class' => 'link_neg',
|
| 594 |
'title' => 'Dislike'
|
| 595 |
);
|
| 596 |
$class = 'vote_neg';
|
| 597 |
$link = '[-]';
|
| 598 |
}
|
| 599 |
}
|
| 600 |
}
|
| 601 |
|
| 602 |
return '<span class = "' . $class . '" id = "' . $tag . '_' . $cid . '_' . $value . '" ' .
|
| 603 |
'title = "' . $ajax_url . '">' .
|
| 604 |
l($link, $url, $attributes, drupal_get_destination(), NULL, FALSE, TRUE) .
|
| 605 |
'</span>';
|
| 606 |
}
|
| 607 |
|
| 608 |
function theme_mediumvote_css_path() {
|
| 609 |
// depending on installation this path may require a preceding /
|
| 610 |
// return '/' . drupal_get_path('module', 'mediumvote') . '/theme/mediumvote.css';
|
| 611 |
|
| 612 |
return drupal_get_path('module', 'mediumvote') . '/theme/mediumvote.css';
|
| 613 |
}
|
| 614 |
|
| 615 |
function mediumvote_nodeapi(&$node, $op, $teaser, $page) {
|
| 616 |
|
| 617 |
$path = drupal_get_path('module', 'mediumvote');
|
| 618 |
drupal_add_js($path . '/theme/ajax_mediumvote.js');
|
| 619 |
|
| 620 |
$allow_all = variable_get('allow_node_all', FALSE);
|
| 621 |
$allow_override = variable_get('allow_override_node', FALSE);
|
| 622 |
$allow_node_type = variable_get('allow_node_'.$node->type, FALSE);
|
| 623 |
|
| 624 |
if ($allow_all OR ($allow_override AND $allow_node_type)) {
|
| 625 |
switch ($op) {
|
| 626 |
case 'view':
|
| 627 |
if ($teaser == FALSE) {
|
| 628 |
for ($tag_count = 1; $tag_count <= NUM_VOTING_CRITERIA; $tag_count++) {
|
| 629 |
$tag = _mediumvote_get_tag($tag_count, VOTINGAPI_VALUE_NODE_CONTENT_TYPE, $node);
|
| 630 |
if ($tag) {
|
| 631 |
$node->body = theme('mediumvote_widget', $node->nid, VOTINGAPI_VALUE_NODE_CONTENT_TYPE, $tag) . $node->body;
|
| 632 |
}
|
| 633 |
}
|
| 634 |
}
|
| 635 |
break;
|
| 636 |
}
|
| 637 |
}
|
| 638 |
}
|
| 639 |
|
| 640 |
function mediumvote_comment(&$comment, $op) {
|
| 641 |
|
| 642 |
$path = drupal_get_path('module', 'mediumvote');
|
| 643 |
drupal_add_js($path . '/theme/ajax_mediumvote.js');
|
| 644 |
|
| 645 |
// get comment's node to see if comment voting
|
| 646 |
// is allowed for node type
|
| 647 |
$node = node_load(array('nid' => $comment->nid));
|
| 648 |
|
| 649 |
$allow_all = variable_get('allow_comment_all', FALSE);
|
| 650 |
$allow_override = variable_get('allow_override_comment', FALSE);
|
| 651 |
$allow_node_type = variable_get('allow_comment_'.$node->type, FALSE);
|
| 652 |
|
| 653 |
if ($allow_all OR ($allow_override AND $allow_node_type)) {
|
| 654 |
switch ($op) {
|
| 655 |
case 'view':
|
| 656 |
|
| 657 |
for ($tag_count = 1; $tag_count <= NUM_VOTING_CRITERIA; $tag_count++) {
|
| 658 |
$tag = _mediumvote_get_tag($tag_count, VOTINGAPI_VALUE_COMMENT_CONTENT_TYPE, $node);
|
| 659 |
if ($tag) {
|
| 660 |
$comment->comment = theme('mediumvote_widget', $node->nid, VOTINGAPI_VALUE_COMMENT_CONTENT_TYPE, $tag) . $comment->comment;
|
| 661 |
}
|
| 662 |
}
|
| 663 |
|
| 664 |
break;
|
| 665 |
}
|
| 666 |
} // end if voting allowed
|
| 667 |
}
|