3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2009 The Nucleus Group
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 * (see nucleus/documentation/index.html#license for more info)
13 * Class representing the karma votes for a certain item
15 * @license http://nucleuscms.org/license.txt GNU General Public License
16 * @copyright Copyright (C) 2002-2009 The Nucleus Group
18 * $NucleusJP: KARMA.php,v 1.5 2006/07/17 20:03:44 kimitake Exp $
22 // id of item about which this object contains information
25 // indicates if the karma vote info has already been intialized from the DB
28 // amount of positive/negative votes
32 function KARMA($itemid, $initpos = 0, $initneg = 0, $initread = 0) {
34 $this->itemid = intval($itemid);
36 // have we read the karma info yet?
37 $this->inforead = intval($initread);
39 // number of positive and negative votes
40 $this->karmapos = intval($initpos);
41 $this->karmaneg = intval($initneg);
44 function getNbPosVotes() {
45 if (!$this->inforead) $this->readFromDatabase();
46 return $this->karmapos;
48 function getNbNegVotes() {
49 if (!$this->inforead) $this->readFromDatabase();
50 return $this->karmaneg;
52 function getNbOfVotes() {
53 if (!$this->inforead) $this->readFromDatabase();
54 return ($this->karmapos + $this->karmaneg);
56 function getTotalScore() {
57 if (!$this->inforead) $this->readFromDatabase();
58 return ($this->karmapos - $this->karmaneg);
61 function setNbPosVotes($val) {
62 $this->karmapos = intval($val);
64 function setNbNegVotes($val) {
65 $this->karmaneg = intval($val);
69 // adds a positive vote
70 function votePositive() {
71 $newKarma = $this->getNbPosVotes() + 1;
72 $this->setNbPosVotes($newKarma);
73 $this->writeToDatabase();
77 // adds a negative vote
78 function voteNegative() {
79 $newKarma = $this->getNbNegVotes() + 1;
80 $this->setNbNegVotes($newKarma);
81 $this->writeToDatabase();
87 // these methods shouldn't be called directly
88 function readFromDatabase() {
89 $query = 'SELECT ikarmapos, ikarmaneg FROM '.sql_table('item').' WHERE inumber=' . $this->itemid;
90 $res = sql_query($query);
91 $obj = sql_fetch_object($res);
93 $this->karmapos = $obj->ikarmapos;
94 $this->karmaneg = $obj->ikarmaneg;
99 function writeToDatabase() {
100 $query = 'UPDATE '.sql_table('item').' SET ikarmapos=' . $this->karmapos . ', ikarmaneg='.$this->karmaneg.' WHERE inumber=' . $this->itemid;
104 // checks if a vote is still allowed for an IP
105 function isVoteAllowed($ip) {
106 $query = 'SELECT * FROM '.sql_table('karma')." WHERE itemid=$this->itemid and ip='".addslashes($ip)."'";
107 $res = sql_query($query);
108 return (sql_num_rows($res) == 0);
111 // save IP in database so no multiple votes are possible
113 $query = 'INSERT INTO '.sql_table('karma').' (itemid, ip) VALUES ('.$this->itemid.",'".addslashes(serverVar('REMOTE_ADDR'))."')";