| 243 |
function jrating_rate($args) { |
function jrating_rate($args) { |
| 244 |
$content_type = $args['content_type']; |
$content_type = $args['content_type']; |
| 245 |
$content_id = $args['content_id']; |
$content_id = $args['content_id']; |
| 246 |
|
|
|
$form = array(); |
|
|
|
|
|
$form['#base'] = 'jrating_rate'; |
|
|
if ($content_type == 'node'){ // FIXME: is there an individual page for comments? |
|
|
$form['#action'] = url("node/$content_id"); |
|
|
} |
|
|
|
|
|
$form['#attributes'] = array ('class' => 'rating'); |
|
|
|
|
| 247 |
$user_rating = jrating_get_user_rating($content_type, $content_id); |
$user_rating = jrating_get_user_rating($content_type, $content_id); |
|
$form['#attributes']['title'] = 'Rating: ' . $user_rating['rating']; |
|
| 248 |
|
|
| 249 |
|
$rating_title = t('Rate this'); |
| 250 |
|
|
| 251 |
$rating_options = array( |
$rating_options = array( |
| 252 |
'0' => t('---'), |
'0' => t('---'), |
| 253 |
'20' => t('Bad'), |
'20' => t('Bad'), |
| 256 |
'80' => t('Very good'), |
'80' => t('Very good'), |
| 257 |
'100' => t('Excellent!') |
'100' => t('Excellent!') |
| 258 |
); |
); |
| 259 |
|
|
| 260 |
$form['rating'] = array( |
return array( |
| 261 |
'#id' => 'rating-options-' . $content_type . '-' . $content_id, |
'#base' => 'jrating_rate', |
| 262 |
'#type' => 'select', |
'#attributes' => array( |
| 263 |
'#default_value' => round($user_rating['percent'] / 20) * 20, |
'class' => 'rating', |
| 264 |
'#options' => $rating_options, |
'title' => 'Rating: ' . $user_rating['rating'], |
| 265 |
'#title' => t('Rate this'), |
), |
| 266 |
'#attributes' => array('class' => 'rating-options', 'title' => t('Rate this')) |
|
| 267 |
); |
'#action' => ($content_type == 'node') ? url("node/$content_id") : NULL, // FIXME: is there an individual page for comments? |
| 268 |
|
|
| 269 |
$form['content_type'] = array( |
'content_type' => array( |
| 270 |
'#type' => 'value', |
'#type' => 'value', |
| 271 |
'#value' => $content_type |
'#value' => $content_type |
| 272 |
); |
), |
| 273 |
|
|
| 274 |
|
'content_id' => array( |
| 275 |
|
'#type' => 'value', |
| 276 |
|
'#value' => $content_id |
| 277 |
|
), |
| 278 |
|
|
| 279 |
$form['content_id'] = array( |
'rating' => array( |
| 280 |
'#type' => 'value', |
'#id' => "rating-options-{$content_type}-{$content_id}", |
| 281 |
'#value' => $content_id |
'#type' => 'select', |
| 282 |
); |
'#default_value' => round($user_rating['percent'] / 20) * 20, |
| 283 |
|
'#options' => $rating_options, |
| 284 |
$form['submit'] = array( |
'#title' => $rating_title, |
| 285 |
'#type' => 'submit', |
'#attributes' => array('class' => 'rating-options', 'title' => $rating_title), |
| 286 |
'#value' => t('Submit') |
), |
| 287 |
); |
|
| 288 |
return $form; |
'submit' => array( |
| 289 |
|
'#id' => "rating-submit-{$content_type}-{$content_id}", |
| 290 |
|
'#type' => 'submit', |
| 291 |
|
'#value' => t('Submit') |
| 292 |
|
), |
| 293 |
|
); |
| 294 |
} |
} |
| 295 |
|
|
| 296 |
/** |
/** |
| 325 |
'response' => t('Rating saved') |
'response' => t('Rating saved') |
| 326 |
); |
); |
| 327 |
|
|
| 328 |
if ($form_values['content_type'] == 'node'){ |
// FIXME: use hook |
| 329 |
db_query("REPLACE INTO {history} (uid, nid, timestamp) VALUES (%d, %d, %d)", $user->uid, $content_id, time()); // mark item as read |
if ($form_values['content_type'] == 'node') |
| 330 |
} |
node_tag_new($content_id); |
| 331 |
|
//db_query("REPLACE INTO {history} (uid, nid, timestamp) VALUES (%d, %d, %d)", $user->uid, $content_id, time()); // mark item as read |
| 332 |
} |
} |
| 333 |
} |
} |
| 334 |
else { |
else { |
| 343 |
exit(); |
exit(); |
| 344 |
} |
} |
| 345 |
|
|
| 346 |
if (isset($error)) { |
if (isset($error)) |
| 347 |
drupal_set_message($error); |
drupal_set_message($error); |
|
} |
|
| 348 |
} |
} |
| 349 |
|
|
| 350 |
function jrating_allow_change($content_type, $content_id, $vote, $uid){ |
function jrating_allow_change($content_type, $content_id, $vote, $uid){ |
| 351 |
// doesn't apply to anonymous users |
// doesn't apply to anonymous users |
| 352 |
if (!$uid){ |
if (!$uid) |
| 353 |
return TRUE; |
return TRUE; |
|
} |
|
| 354 |
|
|
| 355 |
// changes not allowed |
// changes not allowed |
| 356 |
if (! variable_get('jrating_allow_changes', 1)){ |
if (! variable_get('jrating_allow_changes', 1)) |
| 357 |
if (db_num_rows(db_query("SELECT * FROM {votingapi_vote} WHERE content_type = '%s' AND content_id = %d AND tag = '%s' AND value_type = '%s' AND uid = %d", $content_type, $content_id, $vote->tag, $vote->value_type, $uid))){ // previous vote |
if (db_num_rows(db_query("SELECT * FROM {votingapi_vote} WHERE content_type = '%s' AND content_id = %d AND tag = '%s' AND value_type = '%s' AND uid = %d", $content_type, $content_id, $vote->tag, $vote->value_type, $uid))) // previous vote |
| 358 |
return FALSE; |
return FALSE; |
| 359 |
} |
|
|
} |
|
| 360 |
return TRUE; |
return TRUE; |
| 361 |
} |
} |
| 362 |
|
|
| 380 |
} |
} |
| 381 |
|
|
| 382 |
// recalculate after anonymous vote (run automatically by votingapi_set_vote and votingapi_unset_vote) |
// recalculate after anonymous vote (run automatically by votingapi_set_vote and votingapi_unset_vote) |
| 383 |
if (variable_get('votingapi_calculation_schedule', 'immediate') != 'cron') { |
if (variable_get('votingapi_calculation_schedule', 'immediate') != 'cron') |
| 384 |
votingapi_recalculate_results($content_type, $content_id); |
votingapi_recalculate_results($content_type, $content_id); |
|
} |
|
| 385 |
} |
} |
| 386 |
} |
} |
| 387 |
|
|
| 390 |
*/ |
*/ |
| 391 |
function jrating_get_user_rating($content_type, $content_id, $uid = NULL) { |
function jrating_get_user_rating($content_type, $content_id, $uid = NULL) { |
| 392 |
global $user; |
global $user; |
| 393 |
if ($uid == NULL) { |
if ($uid == NULL) |
| 394 |
$uid = $user->uid; |
$uid = $user->uid; |
|
} |
|
| 395 |
|
|
| 396 |
if ($uid){ |
if ($uid && ($votes = votingapi_get_user_votes($content_type, $content_id, $uid))) { |
| 397 |
if ($votes = votingapi_get_user_votes($content_type, $content_id, $uid)) { |
foreach ($votes as $vote) { |
| 398 |
foreach ($votes as $vote) { |
if ($vote->value_type == 'percent' && $vote->tag == 'vote') { |
| 399 |
if ($vote->value_type == 'percent' && $vote->tag == 'vote') { |
$rating = $vote; |
| 400 |
$rating = $vote; |
break; |
|
break; |
|
|
} |
|
| 401 |
} |
} |
| 402 |
} |
} |
| 403 |
} |
} |
| 417 |
* Get the mean rating for a node. |
* Get the mean rating for a node. |
| 418 |
*/ |
*/ |
| 419 |
function jrating_get_mean_rating($content_type, $content_id) { // FIXME: memcached |
function jrating_get_mean_rating($content_type, $content_id) { // FIXME: memcached |
| 420 |
if ($rating = votingapi_get_voting_result($content_type, $content_id, 'percent', 'vote', 'average')) { |
if ($rating = votingapi_get_voting_result($content_type, $content_id, 'percent', 'vote', 'average')) |
| 421 |
$count = votingapi_get_voting_result($content_type, $content_id, 'percent', 'vote', 'count'); |
$count = votingapi_get_voting_result($content_type, $content_id, 'percent', 'vote', 'count'); |
|
} |
|
| 422 |
|
|
| 423 |
return array( |
return array( |
| 424 |
'rating' => $rating ? sprintf("%.1f", round($rating->value / 20, 1)) : '0.0', |
'rating' => $rating ? sprintf("%.1f", round($rating->value / 20, 1)) : '0.0', |
| 445 |
|
|
| 446 |
|
|
| 447 |
function jrating_votingapi_views_formatters($details = array()) { |
function jrating_votingapi_views_formatters($details = array()) { |
| 448 |
if ($details['value_type'] == 'percent') { |
if ($details['value_type'] == 'percent') |
| 449 |
return array('jrating_views_value_display_handler' => t('jRating stars')); |
return array('jrating_views_value_display_handler' => t('jRating stars')); |
|
} |
|
| 450 |
} |
} |
| 451 |
|
|
| 452 |
function jrating_views_value_display_handler($op, $filter, $value, &$query) { |
function jrating_views_value_display_handler($op, $filter, $value, &$query) { |