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