00001 <?php 00002 /* 00003 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) 00004 * Copyright (C) 2002-2007 The Nucleus Group 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * (see nucleus/documentation/index.html#license for more info) 00011 */ 00020 class KARMA { 00021 00022 // id of item about which this object contains information 00023 var $itemid; 00024 00025 // indicates if the karma vote info has already been intialized from the DB 00026 var $inforead; 00027 00028 // amount of positive/negative votes 00029 var $karmapos; 00030 var $karmaneg; 00031 00032 function KARMA($itemid, $initpos = 0, $initneg = 0, $initread = 0) { 00033 // itemid 00034 $this->itemid = intval($itemid); 00035 00036 // have we read the karma info yet? 00037 $this->inforead = intval($initread); 00038 00039 // number of positive and negative votes 00040 $this->karmapos = intval($initpos); 00041 $this->karmaneg = intval($initneg); 00042 } 00043 00044 function getNbPosVotes() { 00045 if (!$this->inforead) $this->readFromDatabase(); 00046 return $this->karmapos; 00047 } 00048 function getNbNegVotes() { 00049 if (!$this->inforead) $this->readFromDatabase(); 00050 return $this->karmaneg; 00051 } 00052 function getNbOfVotes() { 00053 if (!$this->inforead) $this->readFromDatabase(); 00054 return ($this->karmapos + $this->karmaneg); 00055 } 00056 function getTotalScore() { 00057 if (!$this->inforead) $this->readFromDatabase(); 00058 return ($this->karmapos - $this->karmaneg); 00059 } 00060 00061 function setNbPosVotes($val) { 00062 $this->karmapos = intval($val); 00063 } 00064 function setNbNegVotes($val) { 00065 $this->karmaneg = intval($val); 00066 } 00067 00068 00069 // adds a positive vote 00070 function votePositive() { 00071 $newKarma = $this->getNbPosVotes() + 1; 00072 $this->setNbPosVotes($newKarma); 00073 $this->writeToDatabase(); 00074 $this->saveIP(); 00075 } 00076 00077 // adds a negative vote 00078 function voteNegative() { 00079 $newKarma = $this->getNbNegVotes() + 1; 00080 $this->setNbNegVotes($newKarma); 00081 $this->writeToDatabase(); 00082 $this->saveIP(); 00083 } 00084 00085 00086 00087 // these methods shouldn't be called directly 00088 function readFromDatabase() { 00089 $query = 'SELECT ikarmapos, ikarmaneg FROM '.sql_table('item').' WHERE inumber=' . $this->itemid; 00090 $res = sql_query($query); 00091 $obj = mysql_fetch_object($res); 00092 00093 $this->karmapos = $obj->ikarmapos; 00094 $this->karmaneg = $obj->ikarmaneg; 00095 $this->inforead = 1; 00096 } 00097 00098 00099 function writeToDatabase() { 00100 $query = 'UPDATE '.sql_table('item').' SET ikarmapos=' . $this->karmapos . ', ikarmaneg='.$this->karmaneg.' WHERE inumber=' . $this->itemid; 00101 sql_query($query); 00102 } 00103 00104 // checks if a vote is still allowed for an IP 00105 function isVoteAllowed($ip) { 00106 $query = 'SELECT * FROM '.sql_table('karma')." WHERE itemid=$this->itemid and ip='".addslashes($ip)."'"; 00107 $res = sql_query($query); 00108 return (mysql_num_rows($res) == 0); 00109 } 00110 00111 // save IP in database so no multiple votes are possible 00112 function saveIP() { 00113 $query = 'INSERT INTO '.sql_table('karma').' (itemid, ip) VALUES ('.$this->itemid.",'".addslashes(serverVar('REMOTE_ADDR'))."')"; 00114 sql_query($query); 00115 } 00116 } 00117 00118 ?>