| 50 |
'#title' => t('Display a rating widget for anonymous users'), |
'#title' => t('Display a rating widget for anonymous users'), |
| 51 |
'#default_value' => variable_get('jrating_display_anonymous', 1), |
'#default_value' => variable_get('jrating_display_anonymous', 1), |
| 52 |
//'#options' => array ('1' => t('Yes'), '0' => t('No')), |
//'#options' => array ('1' => t('Yes'), '0' => t('No')), |
| 53 |
'#description' => t('Should a rating widget be displayed when users aren\'t logged in?') |
'#description' => t('Should a rating widget be displayed when users aren\'t logged in? (ignored if anonymous users are allowed to rate items)') |
| 54 |
); |
); |
| 55 |
|
|
| 56 |
$form['jrating_display_form_teaser'] = array( |
$form['jrating_display_form_teaser'] = array( |
| 168 |
$node = node_load($comment->nid); |
$node = node_load($comment->nid); |
| 169 |
if (variable_get('jrating_nodeapi_comments_' . $node->type, 0)){ |
if (variable_get('jrating_nodeapi_comments_' . $node->type, 0)){ |
| 170 |
jrating_add_files(); |
jrating_add_files(); |
|
|
|
| 171 |
$comment->comment .= jrating_display_rating_form('comment', $comment->cid); |
$comment->comment .= jrating_display_rating_form('comment', $comment->cid); |
| 172 |
} |
} |
| 173 |
} |
} |
| 176 |
} |
} |
| 177 |
|
|
| 178 |
function jrating_display_rating_form($content_type, $content_id, $teaser = FALSE){ |
function jrating_display_rating_form($content_type, $content_id, $teaser = FALSE){ |
| 179 |
if ((!$teaser || variable_get('jrating_display_form_teaser', 0)) && jrating_display_form_to_user($content_type, $content_id)){ |
if ((!$teaser || variable_get('jrating_display_form_teaser', 0)) && jrating_allow_form_display($content_type, $content_id)){ |
| 180 |
$rating_form = drupal_get_form( |
$rating_form = drupal_get_form( |
| 181 |
'rating_form_' . $content_type . '_' . $content_id, |
'rating_form_' . $content_type . '_' . $content_id, |
| 182 |
array( |
array( |
| 193 |
return theme('jrating_rating_item', $content_type, $content_id, $mean_rating, $rating_form); |
return theme('jrating_rating_item', $content_type, $content_id, $mean_rating, $rating_form); |
| 194 |
} |
} |
| 195 |
|
|
| 196 |
function jrating_display_form_to_user($content_type, $content_id){ |
function jrating_allow_form_display($content_type, $content_id){ |
| 197 |
global $user; |
global $user; |
|
// anonymous and 'display rating widget to anonymous' set |
|
|
if (!$user->uid && variable_get('jrating_display_anonymous', 1)) { |
|
|
return TRUE; |
|
|
} |
|
| 198 |
|
|
| 199 |
// signed in and allowed to rate this item |
if ($user->uid){ |
| 200 |
if (jrating_allow_user_rating($content_type, $content_id)){ |
// signed-in and not owner of this node |
| 201 |
|
return (user_access('rate content') && jrating_allow_user_rating($content_type, $content_id)); |
| 202 |
|
} |
| 203 |
|
else{ |
| 204 |
|
// anonymous and allowed to rate, or 'display rating widget to anonymous' allowed |
| 205 |
|
return (user_access('rate content') || variable_get('jrating_display_anonymous', 1)); |
| 206 |
|
} |
| 207 |
|
} |
| 208 |
|
|
| 209 |
|
function jrating_allow_user_rating($content_type, $content_id){ |
| 210 |
|
// signed-in users can always vote on comments |
| 211 |
|
if ($content_type != 'node'){ |
| 212 |
return TRUE; |
return TRUE; |
| 213 |
} |
} |
| 214 |
|
// either users can rate their own nodes, or the current user doesn't own this node |
| 215 |
|
if (variable_get('jrating_allow_rate_own_nodes', 1) || !jrating_node_owner($content_type, $content_id)){ |
| 216 |
|
return TRUE; |
| 217 |
|
} |
| 218 |
return FALSE; |
return FALSE; |
| 219 |
} |
} |
| 220 |
|
|
| 221 |
function jrating_allow_user_rating($content_type, $content_id){ |
function jrating_node_owner($content_type, $content_id){ |
| 222 |
global $user; |
global $user; |
| 223 |
if (user_access('rate content')) { |
$node = node_load($content_id); |
| 224 |
if ($user->uid && $content_type == 'node' && !variable_get('jrating_allow_rate_own_nodes', 1)){ |
return ($node->uid == $user->uid); |
|
$node = node_load($content_id); |
|
|
if ($node->uid == $user->uid){ |
|
|
return FALSE; |
|
|
} |
|
|
} |
|
|
return TRUE; |
|
|
} |
|
|
return FALSE; |
|
| 225 |
} |
} |
| 226 |
|
|
| 227 |
/** |
/** |
| 301 |
$content_type = $form_values['content_type']; |
$content_type = $form_values['content_type']; |
| 302 |
$content_id = $form_values['content_id']; |
$content_id = $form_values['content_id']; |
| 303 |
|
|
| 304 |
if (jrating_allow_user_rating($content_type, $content_id)) { |
// allowed to rate this content |
| 305 |
|
// check whether anonymous or signed-in and not node owner |
| 306 |
|
if (user_access('rate content') && (!$user->uid || jrating_allow_user_rating($content_type, $content_id))) { |
| 307 |
$vote->value = max(0, min(100, $form_values['rating'])); |
$vote->value = max(0, min(100, $form_values['rating'])); |
| 308 |
$vote->value_type = 'percent'; |
$vote->value_type = 'percent'; |
| 309 |
$vote->tag = 'vote'; |
$vote->tag = 'vote'; |
| 310 |
|
|
| 311 |
if (!$user->uid || jrating_allow_change($content_type, $content_id, $vote, $user->uid)){ |
// reject if signed-in and not allowed to change votes |
| 312 |
|
if (!jrating_allow_change($content_type, $content_id, $vote, $user->uid)){ |
| 313 |
|
$response = array('error' => t('You have already rated this item')); |
| 314 |
|
} |
| 315 |
|
else{ |
| 316 |
jrating_set_vote($content_type, $content_id, $vote); |
jrating_set_vote($content_type, $content_id, $vote); |
| 317 |
|
|
| 318 |
$rating = jrating_get_mean_rating($content_type, $content_id); |
$rating = jrating_get_mean_rating($content_type, $content_id); |
| 319 |
|
|
| 320 |
$response = array( |
$response = array( |
| 321 |
'mean' => $rating['rating'], |
'mean' => $rating['rating'], |
| 322 |
'num_votes' => format_plural($rating['num_votes'], '1 vote', '@count votes'), |
'num_votes' => format_plural($rating['num_votes'], '1 vote', '@count votes'), |
| 323 |
'response' => t('Rating saved') |
'response' => t('Rating saved') |
| 324 |
); |
); |
| 325 |
|
|
| 326 |
if ($form_values['content_type'] == 'node'){ |
if ($form_values['content_type'] == 'node'){ |
| 327 |
db_query("REPLACE INTO {history} (uid, nid, timestamp) VALUES (%d, %d, %d)", $user->uid, $content_id, time()); // mark item as read |
db_query("REPLACE INTO {history} (uid, nid, timestamp) VALUES (%d, %d, %d)", $user->uid, $content_id, time()); // mark item as read |
| 328 |
} |
} |
| 329 |
} |
} |
|
else { |
|
|
$response = array('error' => t('You have already rated this item')); |
|
|
} |
|
| 330 |
} |
} |
| 331 |
else { |
else { |
| 332 |
$response = array('error' => $user->uid ? |
$response = array('error' => $user->uid ? |
| 346 |
} |
} |
| 347 |
|
|
| 348 |
function jrating_allow_change($content_type, $content_id, $vote, $uid){ |
function jrating_allow_change($content_type, $content_id, $vote, $uid){ |
| 349 |
if (! variable_get('jrating_allow_changes', 1)){ // changes not allowed |
// doesn't apply to anonymous users |
| 350 |
|
if (!$uid){ |
| 351 |
|
return TRUE; |
| 352 |
|
} |
| 353 |
|
|
| 354 |
|
// changes not allowed |
| 355 |
|
if (! variable_get('jrating_allow_changes', 1)){ |
| 356 |
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 |
| 357 |
return FALSE; |
return FALSE; |
| 358 |
} |
} |
| 368 |
if ($user->uid) { |
if ($user->uid) { |
| 369 |
$vote->value ? votingapi_set_vote($content_type, $content_id, $vote) : votingapi_unset_vote($content_type, $content_id); |
$vote->value ? votingapi_set_vote($content_type, $content_id, $vote) : votingapi_unset_vote($content_type, $content_id); |
| 370 |
} |
} |
| 371 |
else { // anonymous |
// anonymous |
| 372 |
|
else { |
| 373 |
$hostname = $_SERVER['HTTP_X_FORWARDED_FOR'] ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']; |
$hostname = $_SERVER['HTTP_X_FORWARDED_FOR'] ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']; |
| 374 |
|
|
| 375 |
if ($old_vote = db_fetch_object(db_query("SELECT vote_id FROM {votingapi_vote} WHERE content_type = '%s' AND content_id = %d AND value_type = '%s' AND tag = 'vote' AND uid = 0 AND hostname = '%s' AND timestamp > %d", $content_type, $content_id, $vote->value_type, $hostname, time() - (60 * 60 * 24)))){ // existing vote from the same IP address within the past day |
if ($old_vote = db_fetch_object(db_query("SELECT vote_id FROM {votingapi_vote} WHERE content_type = '%s' AND content_id = %d AND value_type = '%s' AND tag = 'vote' AND uid = 0 AND hostname = '%s' AND timestamp > %d", $content_type, $content_id, $vote->value_type, $hostname, time() - (60 * 60 * 24)))){ // existing vote from the same IP address within the past day: overwrite |
| 376 |
$vote->value ? votingapi_change_vote($old_vote, $vote->value) : votingapi_delete_vote($old_vote); |
$vote->value ? votingapi_change_vote($old_vote, $vote->value) : votingapi_delete_vote($old_vote); |
| 377 |
} |
} |
| 378 |
else { |
else { |