'vote.getVote', '#callback' => 'voting_service_get_vote', '#args' => array( array( '#name' => 'content_type', '#type' => 'string', '#description' => t('The type of content which you are voting for.')), array( '#name' => 'content_id', '#type' => 'int', '#description' => t('The ID of the content which you are voting for.')), array( '#name' => 'tag', '#type' => 'string', '#description' => t('The category of the vote within the content type.'), '#optional' => TRUE) ), '#return' => 'array', '#help' => t('Returns a vote.')), array( '#method' => 'vote.getUserVote', '#callback' => 'voting_service_get_user_vote', '#args' => array( array( '#name' => 'content_type', '#type' => 'string', '#description' => t('The type of content which you are voting for.')), array( '#name' => 'content_id', '#type' => 'int', '#description' => t('The ID of the content which you are voting for.')), array( '#name' => 'tag', '#type' => 'string', '#description' => t('The category of the vote within the content type.'), '#optional' => TRUE) ), '#return' => 'array', '#help' => t('Returns a vote.')), // vote.set array( '#method' => 'vote.setVote', '#callback' => 'voting_service_set_vote', '#args' => array( array( '#name' => 'content_type', '#type' => 'string', '#description' => t('The type of content which you are voting for.')), array( '#name' => 'content_id', '#type' => 'int', '#description' => t('The ID of the content which you are voting for.')), array( '#name' => 'vote_value', '#type' => 'int', '#description' => t('The value of the vote.')), array( '#name' => 'tag', '#type' => 'string', '#description' => t('The category of the vote within the content type.'), '#optional' => TRUE) ), '#return' => 'array', '#help' => t('Submit a new vote.')), // vote.delete array( '#method' => 'vote.deleteVote', '#callback' => 'voting_service_delete_vote', '#args' => array( array( '#name' => 'content_type', '#type' => 'string', '#description' => t('The type of content which you are deleting.')), array( '#name' => 'content_id', '#type' => 'int', '#description' => t('The ID of the content which you are deleting.')), array( '#name' => 'tag', '#type' => 'string', '#description' => t('The category of the vote within the content type.'), '#optional' => TRUE) ), '#return' => 'array', '#help' => t('Delete a vote.')), ); } function voting_service_get_user_vote($content_type, $content_id, $tag = "vote") { global $user; if( $user->uid ) { $vote = votingapi_get_vote($content_type, $content_id, 'percent', $tag, $user->uid); } if($vote) { return $vote; } else { return array('type'=> $content_type, 'tag' => $tag, 'value' => 0); } } /** * Returns a specified node. */ function voting_service_get_vote($content_type, $content_id, $tag = "vote") { $vote = votingapi_get_voting_result($content_type, $content_id, 'percent', $tag, 'average'); if($vote) { return $vote; } else { return array('type'=> $content_type, 'tag' => $tag, 'value' => 0); } } function voting_service_set_vote($content_type, $content_id, $vote_value, $tag = "vote") { global $user; // Prep variables for anonymous vs. registered voting if ($user->uid) { $uid = $user->uid; } else { $hostname = $_SERVER['REMOTE_ADDR']; if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $hostname .= '-'. $_SERVER['HTTP_X_FORWARDED_FOR']; } } $anon_interval = 86400; // sanity-check the incoming values. if ($vote_value > 100) { $vote_value = 100; } // If the vote value is 0, blindly nuke any votes for this content by the user. if ($vote_value == 0) { if ($uid) { // If the user is logged in, we'll delete votes from that uid. $sql = "DELETE FROM {votingapi_vote} WHERE content_type='%s' AND content_id=%d AND value_type='percent' AND uid=%d AND tag='%s'"; db_query($sql, $content_type, $content_id, $uid, $tag); return; } else { // Otherwise, we'll delete votes from the same IP address within the anonymous interval. $sql = "DELETE FROM {votingapi_vote} WHERE content_type='%s' AND content_id=%d AND value_type='percent' AND uid=%d AND tag='%s' AND hostname='%s'"; $sql .= $anon_interval != -1 ? " AND timestamp > %d" : ''; db_query($sql, $content_type, $content_id, $uid, $tag, $hostname, time() - $anon_interval); return; } } // If the vote ISN'T zero, check for existing votes in the database. else { if ($uid) { // If the user is logged in, we'll look for votes from that uid. $sql = "SELECT vote_id FROM {votingapi_vote} WHERE content_type='%s' AND content_id=%d AND value_type='percent' AND uid=%d AND tag = '%s'"; $result = db_query($sql, $content_type, $content_id, $uid, $tag); } else { // Otherwise, we'll look for votes from the same IP address within the anonymous interval. $sql = "SELECT vote_id FROM {votingapi_vote} WHERE content_type='%s' AND content_id=%d AND value_type='percent' AND uid=%d AND tag = '%s' AND hostname='%s'"; $sql .= $anon_interval != -1 ? " AND timestamp > %d" : ''; $result = db_query($sql, $content_type, $content_id, $uid, $tag, $hostname, time() - $anon_interval); } } // If the old vote exists, either delete it (if the new one is zero) // or change it. If it doesn't exist and the vote is non-zero, cast // it and recalculate. if ($old_vote = db_fetch_object($result)) { votingapi_change_vote($old_vote, $vote_value); } elseif ($vote_value != 0) { votingapi_add_vote($content_type, $content_id, $vote_value, 'percent', $tag, $uid); } votingapi_recalculate_results($content_type, $content_id, TRUE); return votingapi_get_voting_result($content_type, $content_id, 'percent', $tag, 'average'); } function voting_service_delete_vote($content_type, $content_id, $tag = "vote") { global $user; $vote = votingapi_get_vote($content_type, $content_id, 'percent', $tag, $user->uid); if($vote) { votingapi_delete_vote($vote); votingapi_recalculate_results($content_type, $content_id, TRUE); } return votingapi_get_voting_result($content_type, $content_id, 'percent', $tag, 'average'); }