Index: modules/voting/voting.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/voting/voting.module,v retrieving revision 1.5 diff -u -r1.5 voting.module --- modules/voting/voting.module 9 Sep 2005 19:01:38 -0000 1.5 +++ modules/voting/voting.module 2 Mar 2006 18:57:19 -0000 @@ -24,13 +24,14 @@ */ function voting_menu($type) { $items = array(); + $items[] = array( 'path' => 'voting/flash', 'title' => t('voting'), 'callback' => 'voting_flash', 'access' => user_access('access content'), 'type' => MENU_CALLBACK); - + return $items; } @@ -39,50 +40,153 @@ */ function voting_settings() { // form group: Colors - $group = form_textfield(t('Background color for voting control'), 'voting_bgcolor', - variable_get('voting_bgcolor', '0xffffff'), 8, 8); - $group .= form_textfield(t("Star 'on' fill color"), 'voting_on_fill', - variable_get('voting_on_fill', '0x990000'), 8, 8); - $group .= form_textfield(t("Star 'on' border color"), 'voting_on_border', - variable_get('voting_on_border', '0x330000'), 8, 8); - $group .= form_textfield(t("Star 'off' fill color"), 'voting_off_fill', - variable_get('voting_off_fill', '0xcccccc'), 8, 8); - $group .= form_textfield(t("Star 'off' border color"), 'voting_off_border', - variable_get('voting_off_border', '0x333333'), 8, 8); - $group .= form_textfield(t("Text color"), 'voting_txt_color', - variable_get('voting_txt_color', '0x000000'), 8, 8); - $output .= form_group(t('Colors (0xffffff for white, 0x000000 for black, etc.)'), $group); - - // form group: Text Strings - $group = form_textfield(t('Star 1'), 'voting_txt_vote1', - variable_get('voting_txt_vote1', 'Awful'), 20, 20); - $group .= form_textfield(t('Star 2'), 'voting_txt_vote2', - variable_get('voting_txt_vote2', 'Poor'), 20, 20); - $group .= form_textfield(t('Star 3'), 'voting_txt_vote3', - variable_get('voting_txt_vote3', 'Average'), 20, 20); - $group .= form_textfield(t('Star 4'), 'voting_txt_vote4', - variable_get('voting_txt_vote4', 'Good'), 20, 20); - $group .= form_textfield(t('Star 5'), 'voting_txt_vote5', - variable_get('voting_txt_vote5', 'Excellent'), 20, 20); - $group .= form_textfield(t('Message shown BEFORE a user votes'), 'voting_txt_before_vote', - variable_get('voting_txt_before_vote', 'Rate This:'), 20, 20); - $group .= form_textfield(t('Message shown AFTER a user votes'), 'voting_txt_after_vote', - variable_get('voting_txt_after_vote', 'My Vote:'), 20, 20); - $group .= form_textfield(t('No permission to vote message'), 'voting_txt_login', - variable_get('voting_txt_login', 'Please login or register to vote.'), 40, 40); - $output .= form_group(t('Text Messages (Leave blank for defaults)'), $group); - - // form group: Other - $group = form_radios(t('Voting controls on nodes'), 'voting_location', - variable_get('voting_location', 1), array(t('Display above the node'), t('Display below the node'), t('Do not display')), - 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.')); - $group .= form_textfield(t("Voting IP timeout (in seconds)"), 'voting_ip_timeout', - variable_get('voting_ip_timeout', 3600), 8, 8, - 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.')); - - $output .= form_group(t('Other'), $group); - - return $output; + $form['colors'] = array( + '#type' => 'fieldset', + '#title' => t('Colors (0xffffff for white, 0x000000 for black, etc.)'), + ); + + $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, + ); + + // form group: Text Strings + $form['text'] = array( + '#type' => 'fieldset', + '#title' => t('Text Messages (Leave blank for defaults)'), + ); + + $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, + ); + + // form group: Other + $form['other'] = array( + '#type' => 'fieldset', + '#title' => t('Other'), + ); + + $form['other']['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['other']['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.'), + ); + + return $form; } /** @@ -90,8 +194,8 @@ */ function voting_get_vote($content_type, $content_id) { global $user; - - // my vote + + // 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); @@ -103,37 +207,37 @@ $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; - + + // 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; } /** - * This function is used with a Macromedia Flash based voting control to send and receive data in + * 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 + * 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 + * 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']; $new_vote = $_POST['vote']; $vote = voting_get_vote($content_type, $content_id); // get average vote, number of votes, and user's current vote (if any) - + if ($new_vote) { // if a new vote is sent... if ($vote->voting_id) { // ...and the user has already voted db_query("UPDATE {votes} SET vote=%d, timestamp=%d, hostname='%s' WHERE voting_id=%d", $new_vote, time(), $_SERVER['REMOTE_ADDR'], $vote->voting_id); @@ -142,13 +246,13 @@ $result = db_query("SELECT MAX(voting_id) FROM {votes}"); $voting_id = $result ? db_result($result, 0) : 0; $voting_id++; - db_query("INSERT INTO {votes} (voting_id, content_type, content_id, vote, uid, timestamp, hostname) VALUES (%d, '%s', %d, %d, %d, %d, '%s')", + db_query("INSERT INTO {votes} (voting_id, content_type, content_id, vote, uid, timestamp, hostname) VALUES (%d, '%s', %d, %d, %d, %d, '%s')", $voting_id, $content_type, $content_id, $new_vote, $user->uid, time(), $_SERVER['REMOTE_ADDR']); setcookie ("vote_{$content_type}_{$content_id}", $voting_id, time()+60*60*24*30); // expire in 30 days } - $vote = voting_get_vote($content_type, $content_id); // get new updated average vote and number of votes + $vote = voting_get_vote($content_type, $content_id); // get new updated average vote and number of votes } - + // return the current vote info to flash $vote->vote = ($vote->vote) ? $vote->vote : 0; $vote->avg_vote = ($vote->avg_vote) ? $vote->avg_vote : 0; @@ -181,18 +285,18 @@ */ function theme_voting_control_flash($content_type, $content_id, $attributes = array()) { global $user; - + // required variables $url = urlencode('voting/flash'); // callback URL that retrieves voting info and process votes - + // 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 + + // 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'); @@ -204,26 +308,26 @@ $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')); + + // 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'); - + $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 = drupal_get_path('module', 'voting') . "/voting.swf?content_type=$content_type&content_id=$content_id&url=$url" . $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. + + // 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&url=$url" . $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.
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.