karma.php
author Adriano Pereira <adrianopt2@gmail.com>
Fri, 30 Jul 2010 23:03:02 -0400
changeset 0 6904847e956b
child 1 87ba86c9d5a6
permissions -rw-r--r--
First revision
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
     1
<?php
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
     2
/**!info**
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
     3
{
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
     4
  "Plugin Name"  : "Karma",
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
     5
  "Plugin URI"   : "http://example.com/",
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
     6
  "Description"  : "Karma is a plugin that enables in the user page a voting system, to evaluate the popularity of each member.",
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
     7
  "Author"       : "Adriano Pereira",
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
     8
  "Version"      : "1.0",
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
     9
  "Author URI"   : "http://enanocms.org/"
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    10
}
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    11
**!*/
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    12
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    13
$plugins->attachHook('userpage_sidebar_left',  'karma();');
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    14
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    15
function karma()
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    16
{
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    17
  // Importing...
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    18
  global $db, $session, $paths;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    19
  
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    20
  if($session->user_logged_in)
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    21
  {  
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    22
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    23
  // If the user votes, get the vote
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    24
  $vote = @$_GET['vote'];
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    25
  
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    26
  // Get the user_id from the user that is voting
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    27
  $user_voting_id = $session->user_id;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    28
  
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    29
  // Find the page_id that is the username of the current user page and gets the user_id from database
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    30
  $username = $paths->page_id;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    31
  
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    32
  $q = $db->sql_query('SELECT user_id FROM '. table_prefix. "users WHERE username = '$username'");
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    33
  if ( !$q )
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    34
    $db->_die();
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    35
  $voted = $db->fetchrow();
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    36
  $user_voted_id = $voted['user_id'];
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    37
  
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    38
  // Retrieves from database the total votes, yes votes, no votes and the karma from user
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    39
  $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'");
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    40
  if ( !$q )
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    41
    $db->_die();
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    42
  $karma_info = $db->fetchrow();
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    43
  $total_votes = $karma_info['karma_total_votes'];
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    44
  $yes_votes = $karma_info['karma_yes_votes'];
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    45
  $no_votes = $karma_info['karma_no_votes'];
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    46
  $karma = $karma_info['karma'];
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    47
  
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    48
  // Search in the database if the user has already voted in this user
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    49
  $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'");
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    50
  if ( !$q )
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    51
    $db->_die();
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    52
  $num_votes = $db->numrows();
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    53
  
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    54
  // If the user that votes and the user voted id is equal or the user has already voted, displays the commom page
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    55
  if ($user_voting_id == $user_voted_id) goto commom_page_title; 
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    56
  
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    57
  if ($num_votes == 0 && empty($vote)) goto vote;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    58
  
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    59
  if ($num_votes != 0) goto commom_page_title;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    60
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    61
  // Know if the vote is yes or no and do the respective action in database
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    62
  if ($vote == 'Yes')
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    63
  {
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    64
	$karma = $karma + 1;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    65
	$total_votes = $total_votes + 1;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    66
	$yes_votes = $yes_votes + 1;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    67
	$q = $db->sql_query('INSERT INTO '. table_prefix."karma (user_voting_id, user_voted_id) VALUES ('$user_voting_id', '$user_voted_id')");
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    68
	if ( !$q )
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    69
      $db->_die();
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    70
	$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'");
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    71
    if ( !$q )
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    72
      $db->_die();  
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    73
  }  
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    74
  elseif ($vote == 'No')
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    75
  {
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    76
	$karma = $karma - 1;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    77
	$total_votes = $total_votes + 1;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    78
	$no_votes = $no_votes + 1;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    79
	$q = $db->sql_query('INSERT INTO '. table_prefix."karma (user_voting_id, user_voted_id) VALUES ('$user_voting_id', '$user_voted_id')");
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    80
	if ( !$q )
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    81
      $db->_die();
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    82
	$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'");
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    83
	if ( !$q )
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    84
      $db->_die();	
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    85
  }
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    86
  else commom_page_title;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    87
  
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    88
  // Label to commom page title
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    89
  commom_page_title:
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    90
?>
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    91
 <th colspan="4"><?php echo $username."'s karma"; goto commom_page;?></th>
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    92
	<?php
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    93
	vote:
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    94
	echo "<th colspan='4'>". $username."'s karma</th>";
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    95
	echo <<<EOF
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    96
		<tr>
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    97
		<td colspan="4" class="row3" style="text-align: center;">
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    98
		<b>Do you like me?</b><br/>
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
    99
		<form action=''>
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   100
			<input type="submit" value="Yes" name="vote" style="background-color: #00CA00; border: 2px solid #000000; width: 40px; color: #FFFFFF; font-size: 14px; text-align:center;">
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   101
			<input type="submit" value="No" name="vote" style="background-color: #FA1205; border: 2px solid #000000; width: 40px; color: #FFFFFF; font-size: 14px; text-align:center;">
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   102
		</form>
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   103
	</tr>
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   104
EOF;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   105
  
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   106
  // Label to commom page content and page content
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   107
  commom_page:
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   108
  
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   109
  if ($karma < 0)
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   110
  {
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   111
	$karma_color = '#FA1205';
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   112
  }
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   113
  elseif ($karma > 0)
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   114
  { 
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   115
   $karma_color = '#00CA00';
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   116
  }
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   117
  else
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   118
   {
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   119
   $karma_color = '#000000';
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   120
  }
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   121
?>
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   122
	<tr>
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   123
		<td colspan="2" class="row1">
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   124
			Your Karma is: <font color="<?php echo $karma_color;?>"><?php echo $karma;?><br/></font>
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   125
		</td>
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   126
		
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   127
		<td colspan="2" class="row2">
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   128
			'Yes' votes: <?php echo $yes_votes;?><br/>
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   129
			'No' votes: <?php echo $no_votes;?><br/>
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   130
			Number of votes: <?php echo $total_votes;?><br/>			
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   131
		</td>
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   132
	</tr>
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   133
<?php
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   134
}
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   135
}
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   136
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   137
/**!install dbms="mysql"; **
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   138
CREATE TABLE {{TABLE_PREFIX}}karma(
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   139
  vote_id int(18) NOT NULL auto_increment,
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   140
  user_voting_id int(12),
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   141
  user_voted_id int(12),  
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   142
  PRIMARY KEY ( vote_id )
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   143
 ) ENGINE=`MyISAM` CHARSET=`UTF8` COLLATE=`utf8_bin`;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   144
 
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   145
ALTER TABLE {{TABLE_PREFIX}}users_extra ADD COLUMN karma int(12) DEFAULT 0;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   146
ALTER TABLE {{TABLE_PREFIX}}users_extra ADD COLUMN karma_total_votes int(12) DEFAULT 0;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   147
ALTER TABLE {{TABLE_PREFIX}}users_extra ADD COLUMN karma_yes_votes int(12) DEFAULT 0;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   148
ALTER TABLE {{TABLE_PREFIX}}users_extra ADD COLUMN karma_no_votes int(12) DEFAULT 0;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   149
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   150
**!*/
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   151
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   152
/**!uninstall **
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   153
DROP TABLE {{TABLE_PREFIX}}karma;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   154
ALTER TABLE {{TABLE_PREFIX}}users_extra DROP karma;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   155
ALTER TABLE {{TABLE_PREFIX}}users_extra DROP karma_total_votes;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   156
ALTER TABLE {{TABLE_PREFIX}}users_extra DROP karma_yes_votes;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   157
ALTER TABLE {{TABLE_PREFIX}}users_extra DROP karma_no_votes;
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   158
**!*/
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   159
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   160
6904847e956b First revision
Adriano Pereira <adrianopt2@gmail.com>
parents:
diff changeset
   161
?>