karma.php
changeset 1 87ba86c9d5a6
parent 0 6904847e956b
equal deleted inserted replaced
0:6904847e956b 1:87ba86c9d5a6
     1 <?php
     1 <?php
     2 /**!info**
     2 /**!info**
     3 {
     3 {
     4   "Plugin Name"  : "Karma",
     4   "Plugin Name"  : "Karma",
     5   "Plugin URI"   : "http://example.com/",
     5   "Plugin URI"   : "http://enanocms.org/plugin/karma",
     6   "Description"  : "Karma is a plugin that enables in the user page a voting system, to evaluate the popularity of each member.",
     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",
     7   "Author"       : "Adriano Pereira",
     8   "Version"      : "1.0",
     8   "Version"      : "1.0",
     9   "Author URI"   : "http://enanocms.org/"
     9   "Author URI"   : "http://enanocms.org/"
    10 }
    10 }
    19   
    19   
    20   if($session->user_logged_in)
    20   if($session->user_logged_in)
    21   {  
    21   {  
    22 
    22 
    23   // If the user votes, get the vote
    23   // If the user votes, get the vote
    24   $vote = @$_GET['vote'];
    24   $vote = !empty($_GET['vote']) && in_array($_GET['vote'], array('Yes', 'No'))
       
    25             ? $_GET['vote']
       
    26             : null;
    25   
    27   
    26   // Get the user_id from the user that is voting
    28   // Get the user_id from the user that is voting
    27   $user_voting_id = $session->user_id;
    29   $user_voting_id = $session->user_id;
    28   
    30   
    29   // Find the page_id that is the username of the current user page and gets the user_id from database
    31   // 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;
    32   $username = str_replace('_', ' ', dirtify_page_id($paths->page_id));
    31   
    33   
    32   $q = $db->sql_query('SELECT user_id FROM '. table_prefix. "users WHERE username = '$username'");
    34   $q = $db->sql_query('SELECT user_id FROM '. table_prefix. "users WHERE username = '$username'");
    33   if ( !$q )
    35   if ( !$q )
    34     $db->_die();
    36     $db->_die();
    35   $voted = $db->fetchrow();
    37   $voted = $db->fetchrow();
    36   $user_voted_id = $voted['user_id'];
    38   $user_voted_id = $voted['user_id'];
    37   
    39   
    38   // Retrieves from database the total votes, yes votes, no votes and the karma from user
    40   // 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'");
    41   $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'");
    40   if ( !$q )
    42   if ( !$q )
    41     $db->_die();
    43     $db->_die();
    42   $karma_info = $db->fetchrow();
    44   $karma_info = $db->fetchrow();
    43   $total_votes = $karma_info['karma_total_votes'];
    45   $total_votes = $karma_info['karma_total_votes'];
    44   $yes_votes = $karma_info['karma_yes_votes'];
    46   $yes_votes = $karma_info['karma_yes_votes'];
    48   // Search in the database if the user has already voted in this user
    50   // 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'");
    51   $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 )
    52   if ( !$q )
    51     $db->_die();
    53     $db->_die();
    52   $num_votes = $db->numrows();
    54   $num_votes = $db->numrows();
       
    55   $db->free_result();
    53   
    56   
    54   // If the user that votes and the user voted id is equal or the user has already voted, displays the commom page
    57   // 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   
    58   
    57   if ($num_votes == 0 && empty($vote)) goto vote;
    59   // If we're on our own user page, block voting
       
    60   $same_user = $user_voting_id === $user_voted_id;
    58   
    61   
    59   if ($num_votes != 0) goto commom_page_title;
    62   // If we have not yet voted on this user, allow that to take place below
    60 
    63   $can_vote = $num_votes == 0 && !$same_user && $session->user_level >= USER_LEVEL_MEMBER;
    61   // Know if the vote is yes or no and do the respective action in database
    64   
    62   if ($vote == 'Yes')
    65   echo "<th colspan='4'>$username's karma</th>";
       
    66   
       
    67   $did_vote = false;
       
    68   if ( $can_vote )
    63   {
    69   {
    64 	$karma = $karma + 1;
    70     // Know if the vote is yes or no and do the respective action in database
    65 	$total_votes = $total_votes + 1;
    71     $increment_col = !empty($vote) && $vote == 'Yes' ? 'karma_yes_votes' : 'karma_no_votes';
    66 	$yes_votes = $yes_votes + 1;
    72     if ( !empty($vote) )
    67 	$q = $db->sql_query('INSERT INTO '. table_prefix."karma (user_voting_id, user_voted_id) VALUES ('$user_voting_id', '$user_voted_id')");
    73     {
    68 	if ( !$q )
    74       $q = $db->sql_query('INSERT INTO '. table_prefix."karma (user_voting_id, user_voted_id) VALUES ('$user_voting_id', '$user_voted_id')");
    69       $db->_die();
    75       if ( !$q )
    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'");
    76         $db->_die();
    71     if ( !$q )
    77       $q = $db->sql_query('UPDATE '. table_prefix."users_extra SET $increment_col = $increment_col + 1");
    72       $db->_die();  
    78         if ( !$q )
    73   }  
    79           $db->_die();
    74   elseif ($vote == 'No')
    80         
    75   {
    81       if ( $vote == 'Yes' )
    76 	$karma = $karma - 1;
    82         $yes_votes++;
    77 	$total_votes = $total_votes + 1;
    83       else
    78 	$no_votes = $no_votes + 1;
    84         $no_votes++;
    79 	$q = $db->sql_query('INSERT INTO '. table_prefix."karma (user_voting_id, user_voted_id) VALUES ('$user_voting_id', '$user_voted_id')");
    85         
    80 	if ( !$q )
    86       // recalculate
    81       $db->_die();
    87       $karma = $yes_votes - $no_votes;
    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'");
    88       $total_votes = $yes_votes + $no_votes;
    83 	if ( !$q )
    89       
    84       $db->_die();	
    90       $did_vote = true;
       
    91     }
       
    92     else
       
    93     {
       
    94       // Label to commom page title
       
    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     }
    85   }
   106   }
    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   
   107   
   106   // Label to commom page content and page content
   108   // Label to commom page content and page content
   107   commom_page:
       
   108   
   109   
   109   if ($karma < 0)
   110   if ($karma < 0)
   110   {
   111   {
   111 	$karma_color = '#FA1205';
   112 	  $karma_color = '#FA1205';
   112   }
   113   }
   113   elseif ($karma > 0)
   114   elseif ($karma > 0)
   114   { 
   115   { 
   115    $karma_color = '#00CA00';
   116     $karma_color = '#00CA00';
   116   }
   117   }
   117   else
   118   else
   118    {
   119   {
   119    $karma_color = '#000000';
   120     $karma_color = '#000000';
   120   }
   121   }
   121 ?>
   122 ?>
       
   123   <?php if ( $did_vote ): ?>
       
   124     <tr>
       
   125       <td colspan="4" class="row3">
       
   126         <div class="info-box-mini">Thanks for voting for this user's karma.</div>
       
   127       </td>
       
   128     </tr>
       
   129   <?php endif; ?>
       
   130     
   122 	<tr>
   131 	<tr>
       
   132 	
   123 		<td colspan="2" class="row1">
   133 		<td colspan="2" class="row1">
   124 			Your Karma is: <font color="<?php echo $karma_color;?>"><?php echo $karma;?><br/></font>
   134       Your Karma is: <span style="color: <?php echo $karma_color; ?>;"><?php echo $karma;?><br/></font>
   125 		</td>
   135 		</td>
   126 		
   136 		
   127 		<td colspan="2" class="row2">
   137 		<td colspan="2" class="row2">
   128 			'Yes' votes: <?php echo $yes_votes;?><br/>
   138 			'Yes' votes: <?php echo $yes_votes;?><br/>
   129 			'No' votes: <?php echo $no_votes;?><br/>
   139 			'No' votes: <?php echo $no_votes;?><br/>
   140   user_voting_id int(12),
   150   user_voting_id int(12),
   141   user_voted_id int(12),  
   151   user_voted_id int(12),  
   142   PRIMARY KEY ( vote_id )
   152   PRIMARY KEY ( vote_id )
   143  ) ENGINE=`MyISAM` CHARSET=`UTF8` COLLATE=`utf8_bin`;
   153  ) ENGINE=`MyISAM` CHARSET=`UTF8` COLLATE=`utf8_bin`;
   144  
   154  
   145 ALTER TABLE {{TABLE_PREFIX}}users_extra ADD COLUMN karma int(12) DEFAULT 0;
   155 ALTER TABLE {{TABLE_PREFIX}}users_extra ADD COLUMN karma_yes_votes int(12) NOT NULL DEFAULT 0;
   146 ALTER TABLE {{TABLE_PREFIX}}users_extra ADD COLUMN karma_total_votes int(12) DEFAULT 0;
   156 ALTER TABLE {{TABLE_PREFIX}}users_extra ADD COLUMN karma_no_votes int(12) NOT NULL 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 
   157 
   150 **!*/
   158 **!*/
   151 
   159 
   152 /**!uninstall **
   160 /**!uninstall **
   153 DROP TABLE {{TABLE_PREFIX}}karma;
   161 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;
   162 ALTER TABLE {{TABLE_PREFIX}}users_extra DROP karma_yes_votes;
   157 ALTER TABLE {{TABLE_PREFIX}}users_extra DROP karma_no_votes;
   163 ALTER TABLE {{TABLE_PREFIX}}users_extra DROP karma_no_votes;
   158 **!*/
   164 **!*/
   159 
   165 
   160 
   166