Update of /cvsroot/php-blog/serendipity/plugins/serendipity_event_karma
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7252/plugins/serendipity_event_karma
Added Files:
serendipity_event_karma.php
Log Message:
* Added Karma event plugin.
* New event hook 'css'
* New variable 'add_footer' for use within entry-handling event plugins
* Enhanced plugin API to allow passing of second array of data
--- NEW FILE: serendipity_event_karma.php ---
<?php # $Id: serendipity_event_karma.php,v 1.1 2004/05/26 12:07:00 garvinhicking Exp $
/* Contributed by Matthias Lange (http://blog.dinnri.de/ml/) */
switch ($serendipity['lang']) {
case 'de':
@define('PLUGIN_KARMA_NAME', 'Karma');
@define('PLUGIN_KARMA_BLAHBLAH', 'Abstimmung über die Eintrags');
@define('PLUGIN_KARMA_VOTETEXT', 'Karma dieses Eintrags: ');
@define('PLUGIN_KARMA_RATE', 'Eintrag bewerten: %s');
@define('PLUGIN_KARMA_VOTEPOINT_1', 'Sehr gut!');
@define('PLUGIN_KARMA_VOTEPOINT_2', 'Gut');
@define('PLUGIN_KARMA_VOTEPOINT_3', 'Neutral');
@define('PLUGIN_KARMA_VOTEPOINT_4', 'Uninteressant');
@define('PLUGIN_KARMA_VOTEPOINT_5', 'Übel');
@define('PLUGIN_KARMA_VOTED', 'Punktzahl "%s" gespeichert.');
@define('PLUGIN_KARMA_INVALID', 'Bewertung ungültig oder manipuliert!');
@define('PLUGIN_KARMA_ALREADYVOTED', 'Bewertung bereits gespeichert.');
@define('PLUGIN_KARMA_NOCOOKIE', 'Der Browser muss Cookies unterstützen um eine Abstimmung zu ermöglichen.');
@define('PLUGIN_KARMA_CLOSED', 'Stimm ab für Artikel, die jünger als %s Tage sind!');
@define('PLUGIN_KARMA_ENTRYTIME', 'Abstimmungszeitraum nach Veröffentlichung eines Artikels');
@define('PLUGIN_KARMA_VOTINGTIME', 'Abstimmungszeitraum');
@define('PLUGIN_KARMA_ENTRYTIME_BLAHBLAH', 'Wie lang (in Minuten) kann nach der Veröffentlichung eines Artikels ein uneingeschränkter Abstimmungszeitraum durchgeführt werden? Standard sind 1440 Minuten (ein Tag).');
@define('PLUGIN_KARMA_VOTINGTIME_BLAHBLAH', 'Wartezeit (in Minuten), die nach einer Stimmabgabe vergehen muss bevor eine nächste Stimme angenommen wird. Diese Einschränkung gilt erst nach Überschreitung des obigen uneingeschränkten Abstimmungszeitraumes. Standard: 5 Minuten.');
@define('PLUGIN_KARMA_TIMEOUT', 'Wahlschutz: Ein anderer Besucher hat gerade abgestimmt. Bitte %s Minuten warten.');
@define('PLUGIN_KARMA_CURRENT', 'Derzeitiges Karma: %2$s, %3$s Stimme(n)');
@define('PLUGIN_KARMA_EXTENDEDONLY', 'Nur erweiterte Artikel');
@define('PLUGIN_KARMA_EXTENDEDONLY_BLAHBLAH', 'Karmavoting nur im erweiterten Artikel einblenden');
@define('PLUGIN_KARMA_MAXKARMA', 'Maximaler Abstimmungszeitraum');
@define('PLUGIN_KARMA_MAXKARMA_BLAHBLAH', 'Abstimmungen nur erlauben bis der Artikel X Tage alt ist (Standard: 7)');
break;
case 'en':
default:
@define('PLUGIN_KARMA_NAME', 'Karma');
@define('PLUGIN_KARMA_BLAHBLAH', 'Let people vote the quality of your entries');
@define('PLUGIN_KARMA_VOTETEXT', 'Karma for this article: ');
@define('PLUGIN_KARMA_RATE', 'Rate this article: %s');
@define('PLUGIN_KARMA_VOTEPOINT_1', 'Very good!');
@define('PLUGIN_KARMA_VOTEPOINT_2', 'Good');
@define('PLUGIN_KARMA_VOTEPOINT_3', 'Neutral');
@define('PLUGIN_KARMA_VOTEPOINT_4', 'Not interesting');
@define('PLUGIN_KARMA_VOTEPOINT_5', 'Bad');
@define('PLUGIN_KARMA_VOTED', 'Your rating "%s" was stored.');
@define('PLUGIN_KARMA_INVALID', 'Your voting was invalid.');
@define('PLUGIN_KARMA_ALREADYVOTED', 'Your rating was already stored.');
@define('PLUGIN_KARMA_NOCOOKIE', 'Your browser must support cookies to be able to vote.');
@define('PLUGIN_KARMA_CLOSED', 'Vote for articles fresher than %s days!');
@define('PLUGIN_KARMA_ENTRYTIME', 'Voting time after publishing');
@define('PLUGIN_KARMA_VOTINGTIME', 'Voting time');
@define('PLUGIN_KARMA_ENTRYTIME_BLAHBLAH', 'How long (in minutes) after your article has been published an unrestricted voting is allowed? Default: 1440 (one day)');
@define('PLUGIN_KARMA_VOTINGTIME_BLAHBLAH', 'Amount of time (in minutes) that needs to be passed from one vote to the other. Is only applied after the time above is expired. Default: 5');
@define('PLUGIN_KARMA_TIMEOUT', 'Flood protection: Another visitor has just recently voted. Please wait %s minutes.');
@define('PLUGIN_KARMA_CURRENT', 'Current karma: %2$s, %3$s vote(s)');
@define('PLUGIN_KARMA_EXTENDEDONLY', 'Only extended article');
@define('PLUGIN_KARMA_EXTENDEDONLY_BLAHBLAH', 'Only show karmavoting on extended article view');
@define('PLUGIN_KARMA_MAXKARMA', 'Karmavoting period');
@define('PLUGIN_KARMA_MAXKARMA_BLAHBLAH', 'Only allow karmavoting until the article is X days old (Default: 7)');
break;
}
class serendipity_event_karma extends serendipity_event
{
var $karmaVote = '';
var $karmaId = '';
var $karmaTimeOut = '';
var $karmaVoting = '';
function introspect(&$propbag)
{
global $serendipity;
$propbag->add('name', PLUGIN_KARMA_NAME);
$propbag->add('description', PLUGIN_KARMA_BLAHBLAH);
$propbag->add('event_hooks', array('frontend_configure' => true, 'entry_display' => true, 'css' => true));
$propbag->add('configuration', array('max_entrytime', 'max_votetime', 'extended_only', 'max_karmatime'));
}
function introspect_config_item($name, &$propbag)
{
switch($name) {
case 'max_entrytime':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_KARMA_ENTRYTIME);
$propbag->add('description', PLUGIN_KARMA_ENTRYTIME_BLAHBLAH);
break;
case 'max_votetime':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_KARMA_VOTINGTIME);
$propbag->add('description', PLUGIN_KARMA_VOTINGTIME_BLAHBLAH);
break;
case 'max_karmatime':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_KARMA_MAXKARMA);
$propbag->add('description', PLUGIN_KARMA_MAXKARMA_BLAHBLAH);
break;
case 'extended_only':
$propbag->add('type', 'boolean');
$propbag->add('name', PLUGIN_KARMA_EXTENDEDONLY);
$propbag->add('description', PLUGIN_KARMA_EXTENDEDONLY_BLAHBLAH);
break;
default:
return false;
}
return true;
}
function register_dependencies($remove = false, $authorid = '0')
{
global $serendipity;
if ($this->get_config('version') != '1.0') {
$q = "CREATE TABLE {$serendipity['dbPrefix']}karma (
entryid int(11) default null,
points int(4) default null,
votes int(4) default null,
lastvote int(10) {UNSIGNED} NULL
)";
$sql = serendipity_db_schema_import($q);
$q = "CREATE INDEX kfetch ON {$serendipity['dbPrefix']}karma (entryid, lastvote);";
$sql = serendipity_db_schema_import($q);
$q = "CREATE INDEX kentryid ON {$serendipity['dbPrefix']}karma (entryid);";
$sql = serendipity_db_schema_import($q);
$this->set_config('version', '1.0');
}
}
function generate_content(&$title)
{
$title = PLUGIN_KARMA_NAME;
}
function event_hook($event, &$bag, &$eventData, $addData = null) {
global $serendipity;
$hooks = &$bag->get('event_hooks');
if (isset($hooks[$event])) {
switch($event) {
case 'frontend_configure':
if (!isset($serendipity['COOKIE']['karmaVote'])) {
serendipity_setCookie('karmaVote', serialize(array()));
}
if (!isset($serendipity['GET']['karmaId']) || !isset($serendipity['GET']['karmaVote'])) {
return;
}
$this->karmaId = (int)$serendipity['GET']['karmaId'];
$this->karmaVoting = (int)$serendipity['GET']['karmaVote'];
if (!isset($serendipity['COOKIE']['karmaVote'])) {
$this->karmaVote = 'nocookie';
return;
}
$karma = unserialize($serendipity['COOKIE']['karmaVote']);
if (!is_array($karma) || !is_numeric($this->karmaVoting) || !is_numeric($this->karmaId) || $this->karmaVoting > 2 || $this->karmaVoting < -2) {
$this->karmaVote = 'invalid1';
return;
}
if (!empty($karma[$this->karmaId])) {
$this->karmaVote = 'alreadyvoted';
return ;
}
// Voting takes place here.
$q = 'SELECT *
FROM ' . $serendipity['dbPrefix'] . 'entries AS e
LEFT OUTER JOIN ' . $serendipity['dbPrefix'] . 'karma AS k
ON e.id = k.entryid
WHERE e.id = ' . serendipity_db_escape_string($this->karmaId) . ' LIMIT 1';
$row = serendipity_db_query($q, true);
if (!isset($row) || !is_array($row)) {
$this->karmaVote = 'invalid2';
return;
}
if ($row['votes'] > 0) {
// Votes for this entry already exist. Do some checking.
$max_entrytime = $this->get_config('max_entrytime', 1440) * 60;
$max_votetime = $this->get_config('max_votetime', 5) * 60;
$max_karmatime = $this->get_config('max_karmatime', 7) * 24 * 60 * 60;
$now = time();
if ($row['timestamp'] < ($now - $max_karmatime)) {
$this->karmaVote = 'timeout2';
return;
}
if (($row['timestamp'] > ($now - $max_entrytime)) || ($row['lastvote'] + $max_votetime < $now)) {
// Update votes
$q = sprintf(
"UPDATE {$serendipity['dbPrefix']}karma
SET points = %s,
votes = %s,
lastvote = %s
WHERE entryid = %s",
$row['points'] + $this->karmaVoting,
$row['votes'] + 1,
$now,
$this->karmaId
);
serendipity_db_query($q);
} else {
$this->karmaVote = 'timeout';
$this->karmaTimeOut = abs(round(($now - ($row['lastvote'] + $max_votetime)) / 60, 1));
return;
}
} else {
// No Votes. Just insert it.
$q = sprintf(
"INSERT INTO {$serendipity['dbPrefix']}karma
(entryid, points, votes, lastvote)
VALUES (%s, %s, %s, %s)",
$this->karmaId,
$this->karmaVoting,
1,
time()
);
$sql = serendipity_db_query($q);
}
$karma[$this->karmaId] = $this->karmaVoting;
$this->karmaVote = 'voted';
serendipity_setCookie('karmaVote', serialize($karma));
return true;
break;
case 'css':
?>
.serendipity_karmaVoting {
margin-left: auto;
margin-right: 0px;
text-align: right;
font-size: 7pt;
display: block;
margin-top: 5px;
margin-bottom: 0px;
}
.serendipity_karmaVoting a {
font-size: 7pt;
text-decoration: none;
}
.serendipity_karmaVoting a:hover {
color: green;
}
.serendipity_karmaError {
color: orange;
}
.serendipity_karmaSuccess {
color: green;
}
<?php
return true;
break;
case 'entry_display':
switch($this->karmaVote) {
case 'nocookie':
// Users with no cookies won't be able to vote.
$msg = '<div class="serendipity_karmaVoting serendipity_karmaError"><a id="karma_vote' . $this->karmaId . '"></a>' . PLUGIN_KARMA_NOCOOKIE . '</div>';
case 'timeout2':
if (!isset($msg)) {
$msg = '<div class="serendipity_karmaVoting serendipity_karmaError"><a id="karma_vote' . $this->karmaId . '"></a>' . PLUGIN_KARMA_CLOSED . '</div>';
}
case 'timeout':
if (!isset($msg)) {
$msg = '<div class="serendipity_karmaVoting serendipity_karmaError"><a id="karma_vote' . $this->karmaId . '"></a>' . sprintf(PLUGIN_KARMA_TIMEOUT, $this->karmaTimeOut) . '</div>';
}
case 'alreadyvoted':
if (!isset($msg)) {
$msg = '<div class="serendipity_karmaVoting serendipity_karmaError"><a id="karma_vote' . $this->karmaId . '"></a>' . PLUGIN_KARMA_ALREADYVOTED . '</div>';
}
case 'invalid1':
case 'invalid2':
case 'invalid':
if (!isset($msg)) {
$msg = '<div class="serendipity_karmaVoting serendipity_karmaError"><a id="karma_vote' . $this->karmaId . '"></a>' . PLUGIN_KARMA_INVALID . '</div>';
}
case 'voted':
if (!isset($msg)) {
$msg = '<div class="serendipity_karmaVoting serendipity_karmaSuccess"><a id="karma_vote' . $this->karmaId . '"></a>' . sprintf(PLUGIN_KARMA_VOTED, $this->karmaVoting) . '</div>';
}
/* OUTPUT MESSAGE */
if ($addData['extended']) {
$eventData[0]['extended'] .= $msg;
} else {
$elements = count($eventData);
// Find the right container to store our message in.
for ($i = 0; $i < $elements; $i++) {
if ($eventData[$i]['id'] == $this->karmaId) {
$eventData[$i]['add_footer'] .= $msg;
}
}
}
break;
default:
$karmatime = $this->get_config('max_karmatime', 7);
$max_karmatime = $karmatime * 24 * 60 * 60;
$now = time();
$url = $_SERVER['REQUEST_URI'] . (stristr($_SERVER['REQUEST_URI'], '?') ? '&' : '?');
$karma = (isset($serendipity['COOKIE']['karmaVote']) ? unserialize($serendipity['COOKIE']['karmaVote']) : array());
$link_1 = '<a class="serendipity_karmaVoting_link1" href="' . $url . 'serendipity[karmaVote]=2&serendipity[karmaId]=%1$s#karma_vote%1$s" title="' . sprintf(PLUGIN_KARMA_RATE, PLUGIN_KARMA_VOTEPOINT_1) . '">++</a>';
$link_2 = '<a class="serendipity_karmaVoting_link2" href="' . $url . 'serendipity[karmaVote]=1&serendipity[karmaId]=%1$s#karma_vote%1$s" title="' . sprintf(PLUGIN_KARMA_RATE, PLUGIN_KARMA_VOTEPOINT_2) . '">+</a>';
$link_3 = '<a class="serendipity_karmaVoting_link3" href="' . $url . 'serendipity[karmaVote]=0&serendipity[karmaId]=%1$s#karma_vote%1$s" title="' . sprintf(PLUGIN_KARMA_RATE, PLUGIN_KARMA_VOTEPOINT_3) . '">0</a>';
$link_4 = '<a class="serendipity_karmaVoting_link4" href="' . $url . 'serendipity[karmaVote]=-1&serendipity[karmaId]=%1$s#karma_vote%1$s" title="' . sprintf(PLUGIN_KARMA_RATE, PLUGIN_KARMA_VOTEPOINT_4) . '">-</a>';
$link_5 = '<a class="serendipity_karmaVoting_link5" href="' . $url . 'serendipity[karmaVote]=-2&serendipity[karmaId]=%1$s#karma_vote%1$s" title="' . sprintf(PLUGIN_KARMA_RATE, PLUGIN_KARMA_VOTEPOINT_5) . '">--</a>';
$karma_voting = '
<br /><div class="serendipity_karmaVoting"><a id="karma_vote%1$s"></a>
' . PLUGIN_KARMA_VOTETEXT . ' ' . $link_1 . ' | ' . $link_2 . ' | ' . $link_3 . ' | ' . $link_4 . ' | ' . $link_5 . '<br />
' . PLUGIN_KARMA_CURRENT . '
</div>';
$karma_current = '
<br /><div class="serendipity_karmaVoting"><a id="karma_vote%1$s"></a>
<div class="serendipity_karmaSuccess">' . PLUGIN_KARMA_VOTED . '</div>
' . PLUGIN_KARMA_CURRENT . '
</div>';
$karma_timeout = '
<br /><div class="serendipity_karmaVoting"><a id="karma_vote%1$s"></a>
<div>' . sprintf(PLUGIN_KARMA_CLOSED, $karmatime) . '</div>
' . PLUGIN_KARMA_CURRENT . '
</div>';
if ($addData['extended']) {
$entryid = $eventData[0]['id'];
if ($eventData[0]['timestamp'] < ($now - $max_karmatime)) {
return;
}
$q = 'SELECT *
FROM ' . $serendipity['dbPrefix'] . 'karma AS k
WHERE k.entryid = ' . serendipity_db_escape_string($entryid) . ' LIMIT 1';
$row = serendipity_db_query($q, true);
if (empty($row['votes'])) {
$row['votes'] = 0;
}
if (empty($row['points'])) {
$row['points'] = 0;
}
if (isset($karma[$entryid])) {
$eventData[0]['extended'] .= sprintf($karma_current, $karma[$entryid], $row['points'], $row['votes']);
} elseif ($eventData[0]['timestamp'] < ($now - $max_karmatime)) {
$eventData[0]['extended'] .= sprintf($karma_timeout, $entryid, $row['points'], $row['votes']);
} else {
$eventData[0]['extended'] .= sprintf($karma_voting, $entryid, $row['points'], $row['votes']);
}
} elseif (!$this->get_config('extended_only', false)) {
$elements = count($eventData);
// Get all existing entry IDs
$entries = array();
for ($i = 0; $i < $elements; $i++) {
$entries[] = $eventData[$i]['id'];
}
// Fetch votes for all entry IDs. Store them in an array for later usage.
$q = 'SELECT *
FROM ' . $serendipity['dbPrefix'] . 'karma AS k
WHERE k.entryid IN (' . implode(', ', $entries) . ')';
$sql = serendipity_db_query($q);
$rows = array();
if ($sql && is_array($sql)) {
foreach($sql AS $idx => $row) {
$rows[$row['entryid']] = array('votes' => $row['votes'], 'points' => $row['points']);
}
}
// Walk entry array and insert karma voting line.
for ($i = 0; $i < $elements; $i++) {
$entryid = $eventData[$i]['id'];
$votes = (!empty($rows[$entryid]['votes']) ? $rows[$entryid]['votes'] : 0);
$points = (!empty($rows[$entryid]['points']) ? $rows[$entryid]['points'] : 0);
if (isset($karma[$entryid])) {
$eventData[$i]['add_footer'] .= sprintf($karma_current, $karma[$entryid], $points, $votes);
} elseif ($eventData[$i]['timestamp'] < ($now - $max_karmatime)) {
$eventData[$i]['add_footer'] .= sprintf($karma_timeout, $entryid, $points, $votes);
} else {
$eventData[$i]['add_footer'] .= sprintf($karma_voting, $entryid, $points, $votes);
}
}
}
}
return true;
break;
default:
return false;
}
} else {
return false;
}
}
}
/* vim: set sts=4 ts=4 expandtab : */
?>
|