X-Git-Url: http://git.osdn.net/view?p=nucleus-jp%2Fnucleus-next.git;a=blobdiff_plain;f=nucleus%2Flibs%2FKARMA.php;h=5471c0428f6844eb6319b4c3a88f44069d8ef557;hp=1c7dcb46c2f9e793a0167c3e3893b6bee50bd5b3;hb=c90b0980cfa3e79cd4bc7eed551a64a5e2b02a5c;hpb=53a745a8808c5e5302bf5e2c702b383b190ad672 diff --git a/nucleus/libs/KARMA.php b/nucleus/libs/KARMA.php index 1c7dcb4..5471c04 100644 --- a/nucleus/libs/KARMA.php +++ b/nucleus/libs/KARMA.php @@ -1,3 +1,4 @@ +<<<<<<< HEAD +======= +itemid = intval($itemid); + + // have we read the karma info yet? + $this->inforead = intval($initread); + + // number of positive and negative votes + $this->karmapos = intval($initpos); + $this->karmaneg = intval($initneg); + } + + function getNbPosVotes() { + if (!$this->inforead) $this->readFromDatabase(); + return $this->karmapos; + } + function getNbNegVotes() { + if (!$this->inforead) $this->readFromDatabase(); + return $this->karmaneg; + } + function getNbOfVotes() { + if (!$this->inforead) $this->readFromDatabase(); + return ($this->karmapos + $this->karmaneg); + } + function getTotalScore() { + if (!$this->inforead) $this->readFromDatabase(); + return ($this->karmapos - $this->karmaneg); + } + + function setNbPosVotes($val) { + $this->karmapos = intval($val); + } + function setNbNegVotes($val) { + $this->karmaneg = intval($val); + } + + + // adds a positive vote + function votePositive() { + $newKarma = $this->getNbPosVotes() + 1; + $this->setNbPosVotes($newKarma); + $this->writeToDatabase(); + $this->saveIP(); + } + + // adds a negative vote + function voteNegative() { + $newKarma = $this->getNbNegVotes() + 1; + $this->setNbNegVotes($newKarma); + $this->writeToDatabase(); + $this->saveIP(); + } + + + + // these methods shouldn't be called directly + function readFromDatabase() { + $query = 'SELECT ikarmapos, ikarmaneg FROM '.sql_table('item').' WHERE inumber=' . $this->itemid; + $res = DB::getRow($query); + + $this->karmapos = $res['ikarmapos']; + $this->karmaneg = $res['ikarmaneg']; + $this->inforead = 1; + } + + + function writeToDatabase() { + $query = 'UPDATE '.sql_table('item').' SET ikarmapos=' . $this->karmapos . ', ikarmaneg='.$this->karmaneg.' WHERE inumber=' . $this->itemid; + DB::execute($query); + } + + // checks if a vote is still allowed for an IP + function isVoteAllowed($ip) { + $query = 'SELECT * FROM '.sql_table('karma')." WHERE itemid={$this->itemid} and ip=". DB::quoteValue($ip); + $res = DB::getResult($query); + return ($res->rowCount() == 0); + } + + // save IP in database so no multiple votes are possible + function saveIP() { + $query = 'INSERT INTO ' . sql_table('karma') .' (itemid, ip) VALUES (' . $this->itemid . ','. DB::quoteValue(serverVar('REMOTE_ADDR')) .')'; + DB::execute($query); + } +} + +?> +>>>>>>> skinnable-master