[pLog-CVS] plog_plugins karma.php,1.1,1.2 pluginkarma.class.php,1.1,1.2
Brought to you by:
jondaley
From: <phu...@us...> - 2004-03-14 15:39:47
|
Update of /cvsroot/plog/plog_plugins In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21292 Modified Files: karma.php pluginkarma.class.php Log Message: first basic working version of the karma plugin Index: karma.php =================================================================== RCS file: /cvsroot/plog/plog_plugins/karma.php,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** karma.php 13 Mar 2004 17:57:13 -0000 1.1 --- karma.php 14 Mar 2004 15:30:41 -0000 1.2 *************** *** 22,199 **** include_once( PLOG_CLASS_PATH."class/view/viewarticleview.class.php" ); include_once( PLOG_CLASS_PATH."class/view/defaultview.class.php" ); - define( KARMA_SCORE_MODERATE_DOWN, 1 ); - define( KARMA_SCORE_MODERATE_UP, 2 ); - - /** - * encapsulates the scoring logic - * - * At the moment, 50% more negative votes than positive are needed to make the post - * "vanish" from the main page. At that point, it will only be found via the "show moderated down" - * posts. - */ - class KarmaScoreLogic extends Object - { - function karmaScore( $positiveKarma, $negativeKarma ) - { - if( (1.5 * $positiveKarma) < $negativeKarma ) - return KARMA_SCORE_MODERATE_DOWN; - else - return KARMA_SCORE_MODERATE_UP; - } - } - - /** - * fetches and retrieves karma scores from the database - */ - class KarmaScores extends Model - { - function KarmaScores() - { - $this->Model(); - } - - /** - * records a vote in the database - */ - function recordVote( $articleId, $blogId, $mode ) - { - $prefix = $this->getPrefix(); - - // check if the row exists - $query = "SELECT * FROM {$prefix}karma_scores - WHERE article_id = $articleId - AND blog_id = $blogId"; - $result = $this->_db->Execute( $query ); - if( !$result ) - return false; - - if( $result->RecordCount() == 0 ) - $exists = false; - else - $exists = true; - - if( $mode == 1 ) { - if( !$exists ) { - $query = "INSERT INTO {$prefix}karma_scores - (blog_id, article_id, positive_karma, negative_karma) - VALUES( $blogId, $articleId, 1, 0 )"; - } - else { - $query = "UPDATE {$prefix}karma_scores - SET positive_karma=positive_karma+1 - WHERE blog_id = $blogId AND - article_id = $articleId"; - } - } - else { - if( !$exists ) { - $query = "INSERT INTO {$prefix}karma_scores - (blog_id, article_id, positive_karma, negative_karma) - VALUES( $blogId, $articleId, 0, 1)"; - } - else { - $query = "UPDATE {$prefix}karma_scores - SET negative_karma=negative_karma+1 - WHERE blog_id = $blogId AND - article_id = $articleId"; - } - } - - $result = $this->_db->Execute( $query ); - - return $result; - } - - function getPostKarma( $blogId, $articleId ) - { - $query = "SELECT * FROM ".$this->getPrefix()."karma_scores - WHERE article_id = $articleId AND blog_id = $blogId"; - - $result = $this->_db->Execute( $query ); - - if( !$result ) - return false; - - $row = $result->FetchRow(); - - return $row; - } - - function getModeratedDownArticles( $blogId ) - { - $query = "SELECT * FROM ".$this->getPrefix()."karma_scores - WHERE blog_id = $blogId ORDER BY last_vote DESC"; - - $this->_db->debug=true; - $result = $this->_db->Execute( $query ); - - if( !$result ) - return Array(); - - $modDown = Array(); - $articles = new Articles(); - while( $row = $result->FetchRow()) { - // do this only if the article has been moderated down!! - if( KarmaScoreLogic::karmaScore( $row["positive_karma"], $row["negative_karma"] ) == KARMA_SCORE_MODERATE_DOWN ) { - print("article id = ".$row["article_id"]." is moderated down."); - $modDownPost = $articles->getBlogArticle( $row["article_id"], $row["blog_id"] ); - // if the post is not set to draft, then either it is set to 'published' (already - // in the main page, or 'deleted' (shouldn't be shown anyway!) - if( $modDownPost->getStatus() == DRAFT ) { - $modDownPost->setTopic( "[MOD] ".$modDownPost->getTopic()); - array_push( $modDown, $modDownPost ); - } - } - } - - return $modDown; - } - } - - class KarmaVoters extends Model - { - function KarmaVoters() - { - $this->Model(); - } - - /** - * returns true if the given user ip has alrady tried to vote - */ - function clientHasVoted( $clientIp, $articleId ) - { - $query = "SELECT * FROM ".$this->getPrefix()."karma_voters - WHERE client_ip = '$clientIp' AND - article_id = $articleId"; - $this->_db->debug=true; - $result = $this->_db->Execute( $query ); - - if( !$result ) - return false; - - if( $result->RecordCount() > 0 ) - return true; - else - return false; - } - - /** - * Stores a user vote - */ - function storeClientVote( $clientIp, $articleId ) - { - $query = "INSERT INTO ".$this->getPrefix()."karma_voters - (article_id, client_ip) VALUES - ($articleId, '$clientIp')"; - - $result = $this->_db->Execute( $query ); - - - print("storing user vote ($clientIp) for blog=$blogId and article=$articleId<br/>"); - - return $result; - } - } class RecordVoteAction extends BlogAction --- 22,27 ---- include_once( PLOG_CLASS_PATH."class/view/viewarticleview.class.php" ); include_once( PLOG_CLASS_PATH."class/view/defaultview.class.php" ); + include_once( PLOG_CLASS_PATH."class/karma/karmalib.class.php" ); class RecordVoteAction extends BlogAction Index: pluginkarma.class.php =================================================================== RCS file: /cvsroot/plog/plog_plugins/pluginkarma.class.php,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** pluginkarma.class.php 13 Mar 2004 17:57:13 -0000 1.1 --- pluginkarma.class.php 14 Mar 2004 15:30:49 -0000 1.2 *************** *** 4,11 **** include_once( PLOG_CLASS_PATH."class/plugin/pluginmanager.class.php" ); include_once( PLOG_CLASS_PATH."class/net/requestgenerator.class.php" ); ! define( "PLUGIN_SHOW_CONFIGURATION_PAGE", "configurePage" ); ! define( "PLUGIN_SHOW_MODERATED_DOWN", "viewModeratedDown" ); ! define( "PLUGIN_SAVE_SETTINGS", "saveSettings" ); class PluginKarma extends PluginBase --- 4,13 ---- include_once( PLOG_CLASS_PATH."class/plugin/pluginmanager.class.php" ); include_once( PLOG_CLASS_PATH."class/net/requestgenerator.class.php" ); + include_once( PLOG_CLASS_PATH."class/karma/karmalib.class.php" ); ! define( "PLUGIN_KARMA_SHOW_CONFIGURATION_PAGE", "configurePage" ); ! define( "PLUGIN_KARMA_SHOW_MODERATED_DOWN", "viewModeratedDown" ); ! define( "PLUGIN_KARMA_SAVE_SETTINGS", "saveSettings" ); ! define( "PLUGIN_KARMA_RESET_KARMA", "resetKarma" ); class PluginKarma extends PluginBase *************** *** 15,18 **** --- 17,21 ---- var $negativeKarmaThreshold; var $trackVoters; + var $_result; function PluginKarma() *************** *** 21,25 **** $this->author = "The pLog Team"; ! $this->desc = "A karma plugin"; } --- 24,62 ---- $this->author = "The pLog Team"; ! $this->desc = "<p>This plugin will allow your readers to give a positive or negative scoring to your ! posts. If the article received too many negative votes, it will be removed from the ! main page (it will be technically set as a 'Draft') and it will only be accessible ! via a \"view moderated down posts\"-like link. In order for that post to be shown ! again in the main page, it will have to receive as many positive votes as ! necessary.</p> ! <p>The plugin can keep track of voters so that they do not vote more than once ! per post. It can also be set not to hide posts that have been moderated down if we ! are only interested in the positive and negative scores. Finally, the whole plugin ! can be enabled/disabled at will if needed.</p> ! <p>In order to show the 'karma' score gathered by a post so far, add the following ! bit of code to your template (the <b>post.template</b> template file would be a ! good place:<br/><br/> ! <b>{\$karma->getPositiveKarma($post)}<br/> ! {\$karma->getNegativeKarma($post)}</b> ! </p> ! <p>In order to allow readers to vote, by for instance clicking a link showing the ! positive score and a link showing the negative score, the following bit of code ! would do:<br/><br/><b> ! <a href=\"{\$karma->getKarmaUrl(\$post,1)}\">+{\$karma->getPositiveKarma($post)}</a><br/> ! <a href=\"{\$karma->getKarmaUrl(\$post,2)}\">-{\$karma->getNegativeKarma($post)}</a> ! </b><br/><br/> ! The {\$karma->getKarmaUrl(\$post,X)} method takes a post as a parameter ! and if the value of 'X' is 1, it will return the url for positive votes and if ! 'X' is 2, it will return the url for negative votes. Once readers have voted, they ! will be redirected to the post itself with the updated score. ! </p> ! <p> ! In order to generate the correct link to the page containing all the moderated down posts, ! the following method can be used in for example, the header.template somewhere where ! the column with all the link, calendars, and other is located:<br/><br/><b> ! <a href=\"{\$karma->getKarmaModeratedDownUrl($blog)}\">Moderated Down Posts</a> ! </b><br/><br/> ! </p>"; ! } *************** *** 191,194 **** --- 228,237 ---- $this->updateBlogSettings(); } + if( $this->_pluginOp == PLUGIN_KARMA_RESET_KARMA ) { + $blogId = $this->blogInfo->getId(); + $articleId = $params->getValue( "articleId" ); + $karmas = new KarmaScores(); + $this->_result = $karmas->resetPostKarmaScore( $this->blogInfo->getId(), $articleId ); + } return true; *************** *** 200,204 **** function getConfigurationMessage() { ! if( $this->_pluginOp == PLUGIN_SAVE_SETTINGS ) { $message = "<h2>Karma Plugin</h2> <p> --- 243,247 ---- function getConfigurationMessage() { ! if( $this->_pluginOp == PLUGIN_KARMA_SAVE_SETTINGS ) { $message = "<h2>Karma Plugin</h2> <p> *************** *** 209,216 **** </p>"; } ! elseif( $this->_pluginOp == PLUGIN_SHOW_MODERATED_DOWN ) { ! $message = "Not implemented yet."; } ! elseif( $this->_pluginOp == PLUGIN_SHOW_CONFIGURATION_PAGE ) { $message ="<h2>Karma Plugin Configuration</h2> <form name=\"karmaPluginConfig\" method=\"post\"> --- 252,284 ---- </p>"; } ! elseif( $this->_pluginOp == PLUGIN_KARMA_SHOW_MODERATED_DOWN ) { ! // this option generates a list with the psots that have been moderated ! // down so far in the site, so that we can reset or change ! // their karma scores ! // It will only show their subject and the scores ! $karmas = new KarmaScores(); ! $modDownArticles = $karmas->getModeratedDownArticles( $this->blogInfo->getId()); ! ! $message = "<h2>Karma Plugin Moderated Down Articles</h2>"; ! ! if( count($modDownArticles) == 0 ) { ! $message .= "<p>This blog does not have any moderated down article yet.</p>"; ! } ! else { ! foreach( $modDownArticles as $article ) { ! $postKarma = $karmas->getPostKarma( $this->blogInfo->getId(), $article->getId()); ! $message .= $article->getTopic()." (+".$postKarma["positive_karma"]." / -".$postKarma["negative_karma"].")"; ! $message .= " <a href=\"admin.php?op=updatePluginConfig&pluginId=karma&pluginOp=resetKarma&articleId=".$article->getId()."\">[ Reset Karma ]</a>"; ! $message .= "<br/><br/>"; ! } ! } ! ! $message .= "<br/><br/><a href=\"admin.php?op=updatePluginConfig&pluginId=karma\">Back</a>"; } ! elseif( $this->_pluginOp == PLUGIN_KARMA_RESET_KARMA ) { ! $message = "Article score reset successfully.<br/><br/>"; ! $message .= "<a href=\"admin.php?op=updatePluginConfig&pluginId=karma&pluginOp=viewModeratedDown\">Back</a>"; ! } ! elseif( $this->_pluginOp == PLUGIN_KARMA_SHOW_CONFIGURATION_PAGE ) { $message ="<h2>Karma Plugin Configuration</h2> <form name=\"karmaPluginConfig\" method=\"post\"> |