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