'voting/flash', 'title' => t('voting'), 'callback' => 'voting_flash', 'access' => user_access('access content'), 'type' => MENU_CALLBACK); return $items; } /** * Implementation of hook_settings() */ function voting_settings() { if (!function_exists('votingapi_set_vote')) { drupal_set_message('The voting module depends on the voting API module. Please install the voting API module before attempting to use this module.', 'error'); } // General options $form['general'] = array( '#type' => 'fieldset', '#title' => t('General options'), '#collapsible' => true, '#collapsed' => false, ); $form['general']['voting_location'] = array( '#type' => 'radios', '#title' => t('Voting controls on nodes'), '#default_value' => variable_get('voting_location', 1), '#options' => array(t('Display above the node'), t('Display below the node'), t('Do not display')), '#description' => t('Position of the voting control. Select "Do not display" if you are calling the voting module function directly from your theme and/or module code.'), ); $form['general']['voting_location'] = array( '#type' => 'radios', '#title' => t('Show voting in teaser'), '#default_value' => variable_get('voting_show_in_teaser', 0), '#options' => array(1 => t('yes'), 0 => t('no')), ); $form['general']['voting_ip_timeout'] = array( '#type' => 'textfield', '#title' => t("Voting IP timeout (in seconds)"), '#default_value' => variable_get('voting_ip_timeout', 3600), '#size' => 8, '#maxlength' => 8, '#description' => t('Controls if and how often users with the same IP address can vote for the same thing. Set it to 0 to disable this feature.'), ); // Colors $form['colors'] = array( '#type' => 'fieldset', '#title' => t('Colors'), '#collapsible' => true, '#collapsed' => true, ); $form['colors']['voting_bgcolor'] = array( '#type' => 'textfield', '#title' => t('Background color for voting control'), '#default_value' => variable_get('voting_bgcolor', '0xffffff'), '#size' => 8, '#maxlength' => 8, ); $form['colors']['voting_on_fill'] = array( '#type' => 'textfield', '#title' => t("Star 'on' fill color"), '#default_value' => variable_get('voting_on_fill', '0x990000'), '#size' => 8, '#maxlength' => 8, ); $form['colors']['voting_on_border'] = array( '#type' => 'textfield', '#title' => t("Star 'on' border color"), '#default_value' => variable_get('voting_on_border', '0x330000'), '#size' => 8, '#maxlength' => 8, ); $form['colors']['voting_off_fill'] = array( '#type' => 'textfield', '#title' => t("Star 'off' fill color"), '#default_value' => variable_get('voting_off_fill', '0xcccccc'), '#size' => 8, '#maxlength' => 8, ); $form['colors']['voting_off_border'] = array( '#type' => 'textfield', '#title' => t("Star 'off' border color"), '#default_value' => variable_get('voting_off_border', '0x333333'), '#size' => 8, '#maxlength' => 8, ); $form['colors']['voting_txt_color'] = array( '#type' => 'textfield', '#title' => t("Text color"), '#default_value' => variable_get('voting_txt_color', '0x000000'), '#size' => 8, '#maxlength' => 8, ); // Text strings $form['text'] = array( '#type' => 'fieldset', '#title' => t('Text Strings'), '#collapsible' => true, '#collapsed' => true, ); $form['text']['voting_txt_vote1'] = array( '#type' => 'textfield', '#title' => t('Star 1'), '#default_value' => variable_get('voting_txt_vote1', 'Awful'), '#size' => 20, '#maxlength' => 20, ); $form['text']['voting_txt_vote2'] = array( '#type' => 'textfield', '#title' => t('Star 2'), '#default_value' => variable_get('voting_txt_vote2', 'Poor'), '#size' => 20, '#maxlength' => 20, ); $form['text']['voting_txt_vote3'] = array( '#type' => 'textfield', '#title' => t('Star 3'), '#default_value' => variable_get('voting_txt_vote3', 'Average'), '#size' => 20, '#maxlength' => 20, ); $form['text']['voting_txt_vote4'] = array( '#type' => 'textfield', '#title' => t('Star 4'), '#default_value' => variable_get('voting_txt_vote4', 'Good'), '#size' => 20, '#maxlength' => 20, ); $form['text']['voting_txt_vote5'] = array( '#type' => 'textfield', '#title' => t('Star 5'), '#default_value' => variable_get('voting_txt_vote5', 'Excellent'), '#size' => 20, '#maxlength' => 20, ); $form['text']['voting_txt_before_vote'] = array( '#type' => 'textfield', '#title' => t('Message shown BEFORE a user votes'), '#default_value' => variable_get('voting_txt_before_vote', 'Rate This:'), '#size' => 20, '#maxlength' => 20, ); $form['text']['voting_txt_after_vote'] = array( '#type' => 'textfield', '#title' => t('Message shown AFTER a user votes'), '#default_value' => variable_get('voting_txt_after_vote', 'My Vote:'), '#size' => 20, '#maxlength' => 20, ); $form['text']['voting_txt_login'] = array( '#type' => 'textfield', '#title' => t('No permission to vote message'), '#default_value' => variable_get('voting_txt_login', 'Please login or register to vote.'), '#size' => 40, '#maxlength' => 40, ); return $form; } /** * Return a vote object (containing the user's vote, average vote, and number of votes) */ function voting_get_vote($content_type, $content_id) { global $user; // my vote if ($_COOKIE["vote_{$content_type}_{$content_id}"]) { // get user's vote from a cookie on their computer $voting_id = $_COOKIE["vote_{$content_type}_{$content_id}"]; $result = db_query("SELECT * FROM {votes} WHERE voting_id=%d", $voting_id); $vote = db_fetch_object($result); } elseif ($user->uid) { // get the user's vote by using their user_id $result = db_query("SELECT * FROM {votes} WHERE content_type='%s' AND content_id=%d AND uid=%d", $content_type, $content_id, $user->uid); $vote = db_fetch_object($result); } else { // get the user's vote from their IP address (only within one hour, or whatever the voting_ip_timeout variable is set to) $result = db_query("SELECT * FROM {votes} WHERE content_type='%s' AND content_id=%d AND hostname='%s' AND timestamp > %d", $content_type, $content_id, $_SERVER['REMOTE_ADDR'], time() - variable_get("voting_ip_timeout", 60 * 60)); $vote = db_fetch_object($result); } // average vote $result = db_query("SELECT SUM(vote)/COUNT(vote) AS avg_vote FROM {votes} WHERE content_type='%s' AND content_id=%d", $content_type, $content_id); $vote->avg_vote = $result ? db_result($result, "avg_vote") : 0; $vote->avg_vote = round($vote->avg_vote / 0.5) * 0.5; // rounds to nearest half $vote->avg_vote = number_format($vote->avg_vote, 1); // fixes to 1 decimal place // number of votes $result = db_query("SELECT COUNT(vote) FROM {votes} WHERE content_type='%s' AND content_id=%d", $content_type, $content_id); $vote->num_votes = $result ? db_result($result, 0) : 0; return $vote; } function voting_convert_to_percent($vote) { // round to nearest integer $vote = round($vote); // set limits $vote = ($vote < 1) ? 1 : $vote; $vote = ($vote > 5) ? 5 : $vote; // convert to percentage switch($vote) { case 5: return 90; case 4: return 70; case 3: return 50; case 2: return 30; case 1: return 10; default: return 0; } } function voting_convert_from_percent($vote) { // round to nearest ten $vote = 10 * round($vote / 10); // set limits $vote = ($vote < 0) ? 0 : $vote; $vote = ($vote > 100) ? 100 : $vote; // convert to scale from 1.0 to 5.0 switch($vote) { case 100: case 90: return 5.0; case 80: return 4.5; case 70: return 4.0; case 60: return 3.5; case 50: return 3.0; case 40: return 2.5; case 30: return 2.0; case 20: return 1.5; case 10: return 1.0; default: return 0; } } /** * This function is used with a Macromedia Flash based voting control to send and receive data in * one step. First, Flash is passed the content_type and content_id from the page where it is used. * Then, Flash calls /voting/flash and sends the content_type and content_id using POST. A vote is * not passed, so this function will not insert a vote, but it will return the current voting information. * If a user votes (or changes their vote), this function is called again from Flash, this time with * a vote. */ function voting_flash() { global $user; $content_type = $_POST['content_type']; $content_id = $_POST['content_id']; $user_vote = $_POST['vote']; if ($user_vote) { // if a new vote is sent... $vote->value = voting_convert_to_percent($user_vote); $vote->value_type = 'percent'; $vote->tag = 'vote'; votingapi_set_vote($content_type, $content_id, $vote); } else { $vote = votingapi_get_user_votes($content_type, $content_id); $user_vote = voting_convert_from_percent($vote[0]->value); } // get average and count $average = votingapi_get_voting_result($content_type, $content_id, 'percent', 'vote', 'average'); $avg_vote = voting_convert_from_percent($average->value); $count = votingapi_get_voting_result($content_type, $content_id, 'percent', 'vote', 'count'); // return the current vote info to flash $user_vote = ($user_vote) ? $user_vote : 0; $avg_vote = ($avg_vote) ? $avg_vote : 0; $num_votes = ($count->value) ? $count->value : 0; echo "vote=$user_vote&avg_vote=$avg_vote&num_votes=$num_votes"; } /** * Call this from a node to display a voting control */ function voting_control_node(&$node, $teaser = false, $page = false) { // called by node-based modules (or the nodeapi's view function in this file) if (user_access('vote on content') || user_access('show average without voting')) { if (($node->voting) && ($page)) { return theme('voting_control_flash', 'node', $node->nid); } } } /** * Call this from anywhere to display a voting control */ function voting_control_generic($content_type, $content_id, $attributes = array()) { // called by non-node types of content if (user_access('vote on content') || user_access('show average without voting')) { return theme('voting_control_flash', $content_type, $content_id, $attributes); } } /** * Display a voting control in Flash */ function theme_voting_control_flash($content_type, $content_id, $attributes = array()) { global $user; // optional user_access() dependent variables if (user_access('vote on content') && user_access('show average without voting')) { $mode = 'show_avg_first'; } elseif (user_access('show average without voting')) { $mode = 'show_avg_only'; } // optional variables set from administer/settings/voting $star_on_fill = variable_get('voting_on_fill', '0x990000'); $star_on_border = variable_get('voting_on_border', '0x330000'); $star_off_fill = variable_get('voting_off_fill', '0xcccccc'); $star_off_border = variable_get('voting_off_border', '0x333333'); $txt_color = variable_get('voting_txt_color', '0x000000'); $txt_vote1 = variable_get('voting_txt_vote1', 'Awful'); $txt_vote2 = variable_get('voting_txt_vote2', 'Poor'); $txt_vote3 = variable_get('voting_txt_vote3', 'Average'); $txt_vote4 = variable_get('voting_txt_vote4', 'Good'); $txt_vote5 = variable_get('voting_txt_vote5', 'Excellent'); $txt_login = urlencode(variable_get('voting_txt_login', 'Please login or register to vote.')); // optional: certain variables can be overridden on individual voting controls // ex: $output .= voting_control_generic('pagevote', $node->nid, array('prompt' => 'Do you like it?', 'confirmation' => 'Vote saved!', 'bgcolor' => '0xffffff')); $txt_before_vote = urlencode(($attributes['prompt']) ? $attributes['prompt'] : variable_get('voting_txt_before_vote', 'Rate This:')); $txt_after_vote = urlencode(($attributes['confirmation']) ? $attributes['confirmation'] : variable_get('voting_txt_after_vote', 'My Vote:')); $bgcolor = ($attributes['bgcolor']) ? $attributes['bgcolor'] : variable_get('voting_bgcolor', '0xffffff'); // make optional variables into a really long querystring $optional_vars = "&mode=$mode&bgcolor=$bgcolor&star_on_fill=$star_on_fill&star_on_border=$star_on_border"; $optional_vars .= "&star_off_fill=$star_off_fill&star_off_border=$star_off_border&txt_color=$txt_color"; $optional_vars .= "&txt_vote1=$txt_vote1&txt_vote2=$txt_vote2&txt_vote3=$txt_vote3&txt_vote4=$txt_vote4&txt_vote5=$txt_vote5"; $optional_vars .= "&txt_login=$txt_login&txt_before_vote=$txt_before_vote&txt_after_vote=$txt_after_vote"; // create the Flash filename, including the required variables and optional querystring $filename = base_path() . drupal_get_path('module', 'voting') . "/voting.swf?content_type=$content_type&content_id=$content_id&base_url=" . base_path() . $optional_vars; // create the HTML to put the Flash object on the page // // NOTE: The object is nested in a table for CSS styling. It does // not seem to work correctly in Firefox when a DIV is used instead. $output .= "
| \n"; $output .= '\n"; $output .= " |
You can let people vote on your post, or things in your post, just by adding a carefully formatted tag in your post.
The tag should look like this:
[voting|type=artwork|prompt=Rate my artwork:|confirmation=Thanks for voting!]
The prompt and confirmation are optional.