# HG changeset patch # User Dan Fuhry # Date 1280545413 14400 # Node ID 87ba86c9d5a690b9fcb58ed176253df0f7ea641a # Parent 6904847e956b94d4092b8181c320aeb69102905c Fixed and cleaned up diff -r 6904847e956b -r 87ba86c9d5a6 karma.php --- a/karma.php Fri Jul 30 23:03:02 2010 -0400 +++ b/karma.php Fri Jul 30 23:03:33 2010 -0400 @@ -2,7 +2,7 @@ /**!info** { "Plugin Name" : "Karma", - "Plugin URI" : "http://example.com/", + "Plugin URI" : "http://enanocms.org/plugin/karma", "Description" : "Karma is a plugin that enables in the user page a voting system, to evaluate the popularity of each member.", "Author" : "Adriano Pereira", "Version" : "1.0", @@ -21,13 +21,15 @@ { // If the user votes, get the vote - $vote = @$_GET['vote']; + $vote = !empty($_GET['vote']) && in_array($_GET['vote'], array('Yes', 'No')) + ? $_GET['vote'] + : null; // Get the user_id from the user that is voting $user_voting_id = $session->user_id; // Find the page_id that is the username of the current user page and gets the user_id from database - $username = $paths->page_id; + $username = str_replace('_', ' ', dirtify_page_id($paths->page_id)); $q = $db->sql_query('SELECT user_id FROM '. table_prefix. "users WHERE username = '$username'"); if ( !$q ) @@ -36,7 +38,7 @@ $user_voted_id = $voted['user_id']; // Retrieves from database the total votes, yes votes, no votes and the karma from user - $q = $db->sql_query('SELECT karma, karma_total_votes, karma_yes_votes, karma_no_votes FROM '. table_prefix."users_extra WHERE user_id = '$user_voted_id'"); + $q = $db->sql_query('SELECT karma_yes_votes, karma_no_votes, (karma_yes_votes + karma_no_votes) AS karma_total_votes, (karma_yes_votes - karma_no_votes) AS karma FROM '. table_prefix."users_extra WHERE user_id = '$user_voted_id'"); if ( !$q ) $db->_die(); $karma_info = $db->fetchrow(); @@ -50,78 +52,86 @@ if ( !$q ) $db->_die(); $num_votes = $db->numrows(); + $db->free_result(); // If the user that votes and the user voted id is equal or the user has already voted, displays the commom page - if ($user_voting_id == $user_voted_id) goto commom_page_title; - if ($num_votes == 0 && empty($vote)) goto vote; + // If we're on our own user page, block voting + $same_user = $user_voting_id === $user_voted_id; - if ($num_votes != 0) goto commom_page_title; - - // Know if the vote is yes or no and do the respective action in database - if ($vote == 'Yes') - { - $karma = $karma + 1; - $total_votes = $total_votes + 1; - $yes_votes = $yes_votes + 1; - $q = $db->sql_query('INSERT INTO '. table_prefix."karma (user_voting_id, user_voted_id) VALUES ('$user_voting_id', '$user_voted_id')"); - if ( !$q ) - $db->_die(); - $q = $db->sql_query('UPDATE '. table_prefix."users_extra SET karma = '$karma', karma_total_votes = '$total_votes', karma_yes_votes = '$yes_votes' WHERE user_id = '$user_voted_id'"); - if ( !$q ) - $db->_die(); - } - elseif ($vote == 'No') + // If we have not yet voted on this user, allow that to take place below + $can_vote = $num_votes == 0 && !$same_user && $session->user_level >= USER_LEVEL_MEMBER; + + echo "$username's karma"; + + $did_vote = false; + if ( $can_vote ) { - $karma = $karma - 1; - $total_votes = $total_votes + 1; - $no_votes = $no_votes + 1; - $q = $db->sql_query('INSERT INTO '. table_prefix."karma (user_voting_id, user_voted_id) VALUES ('$user_voting_id', '$user_voted_id')"); - if ( !$q ) - $db->_die(); - $q = $db->sql_query('UPDATE '. table_prefix."users_extra SET karma = '$karma', karma_total_votes = '$total_votes', karma_no_votes = '$no_votes' WHERE user_id = '$user_voted_id'"); - if ( !$q ) - $db->_die(); + // Know if the vote is yes or no and do the respective action in database + $increment_col = !empty($vote) && $vote == 'Yes' ? 'karma_yes_votes' : 'karma_no_votes'; + if ( !empty($vote) ) + { + $q = $db->sql_query('INSERT INTO '. table_prefix."karma (user_voting_id, user_voted_id) VALUES ('$user_voting_id', '$user_voted_id')"); + if ( !$q ) + $db->_die(); + $q = $db->sql_query('UPDATE '. table_prefix."users_extra SET $increment_col = $increment_col + 1"); + if ( !$q ) + $db->_die(); + + if ( $vote == 'Yes' ) + $yes_votes++; + else + $no_votes++; + + // recalculate + $karma = $yes_votes - $no_votes; + $total_votes = $yes_votes + $no_votes; + + $did_vote = true; + } + else + { + // Label to commom page title + echo << + + Do you like me?
+
+ + +
+ +EOF; + } } - else commom_page_title; - - // Label to commom page title - commom_page_title: -?> - - ". $username."'s karma"; - echo << - - Do you like me?
-
- - -
- -EOF; // Label to commom page content and page content - commom_page: if ($karma < 0) { - $karma_color = '#FA1205'; + $karma_color = '#FA1205'; } elseif ($karma > 0) { - $karma_color = '#00CA00'; + $karma_color = '#00CA00'; } else - { - $karma_color = '#000000'; + { + $karma_color = '#000000'; } ?> + + + +
Thanks for voting for this user's karma.
+ + + + + - Your Karma is:
+ Your Karma is:
@@ -142,17 +152,13 @@ PRIMARY KEY ( vote_id ) ) ENGINE=`MyISAM` CHARSET=`UTF8` COLLATE=`utf8_bin`; -ALTER TABLE {{TABLE_PREFIX}}users_extra ADD COLUMN karma int(12) DEFAULT 0; -ALTER TABLE {{TABLE_PREFIX}}users_extra ADD COLUMN karma_total_votes int(12) DEFAULT 0; -ALTER TABLE {{TABLE_PREFIX}}users_extra ADD COLUMN karma_yes_votes int(12) DEFAULT 0; -ALTER TABLE {{TABLE_PREFIX}}users_extra ADD COLUMN karma_no_votes int(12) DEFAULT 0; +ALTER TABLE {{TABLE_PREFIX}}users_extra ADD COLUMN karma_yes_votes int(12) NOT NULL DEFAULT 0; +ALTER TABLE {{TABLE_PREFIX}}users_extra ADD COLUMN karma_no_votes int(12) NOT NULL DEFAULT 0; **!*/ /**!uninstall ** DROP TABLE {{TABLE_PREFIX}}karma; -ALTER TABLE {{TABLE_PREFIX}}users_extra DROP karma; -ALTER TABLE {{TABLE_PREFIX}}users_extra DROP karma_total_votes; ALTER TABLE {{TABLE_PREFIX}}users_extra DROP karma_yes_votes; ALTER TABLE {{TABLE_PREFIX}}users_extra DROP karma_no_votes; **!*/