# HG changeset patch # User Adriano Pereira # Date 1280545382 14400 # Node ID 6904847e956b94d4092b8181c320aeb69102905c First revision diff -r 000000000000 -r 6904847e956b karma.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/karma.php Fri Jul 30 23:03:02 2010 -0400 @@ -0,0 +1,161 @@ +attachHook('userpage_sidebar_left', 'karma();'); + +function karma() +{ + // Importing... + global $db, $session, $paths; + + if($session->user_logged_in) + { + + // If the user votes, get the vote + $vote = @$_GET['vote']; + + // 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; + + $q = $db->sql_query('SELECT user_id FROM '. table_prefix. "users WHERE username = '$username'"); + if ( !$q ) + $db->_die(); + $voted = $db->fetchrow(); + $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'"); + if ( !$q ) + $db->_die(); + $karma_info = $db->fetchrow(); + $total_votes = $karma_info['karma_total_votes']; + $yes_votes = $karma_info['karma_yes_votes']; + $no_votes = $karma_info['karma_no_votes']; + $karma = $karma_info['karma']; + + // Search in the database if the user has already voted in this user + $q = $db->sql_query('SELECT user_voted_id, user_voting_id FROM '. table_prefix."karma WHERE user_voted_id = '$user_voted_id' AND user_voting_id = '$user_voting_id'"); + if ( !$q ) + $db->_die(); + $num_votes = $db->numrows(); + + // 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 ($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') + { + $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(); + } + 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'; + } + elseif ($karma > 0) + { + $karma_color = '#00CA00'; + } + else + { + $karma_color = '#000000'; + } +?> + + + Your Karma is:
+ + + + 'Yes' votes:
+ 'No' votes:
+ Number of votes:
+ + + \ No newline at end of file