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
17 * @version $Id: KARMA.php 1470 2010-11-29 22:10:16Z ftruscot $
21 // id of item about which this object contains information
24 // indicates if the karma vote info has already been intialized from the DB
27 // amount of positive/negative votes
31 function KARMA($itemid, $initpos = 0, $initneg = 0, $initread = 0) {
33 $this->itemid = intval($itemid);
35 // have we read the karma info yet?
36 $this->inforead = intval($initread);
38 // number of positive and negative votes
39 $this->karmapos = intval($initpos);
40 $this->karmaneg = intval($initneg);
43 function getNbPosVotes() {
44 if (!$this->inforead) $this->readFromDatabase();
45 return $this->karmapos;
47 function getNbNegVotes() {
48 if (!$this->inforead) $this->readFromDatabase();
49 return $this->karmaneg;
51 function getNbOfVotes() {
52 if (!$this->inforead) $this->readFromDatabase();
53 return ($this->karmapos + $this->karmaneg);
55 function getTotalScore() {
56 if (!$this->inforead) $this->readFromDatabase();
57 return ($this->karmapos - $this->karmaneg);
60 function setNbPosVotes($val) {
61 $this->karmapos = intval($val);
63 function setNbNegVotes($val) {
64 $this->karmaneg = intval($val);
68 // adds a positive vote
69 function votePositive() {
70 $newKarma = $this->getNbPosVotes() + 1;
71 $this->setNbPosVotes($newKarma);
72 $this->writeToDatabase();
76 // adds a negative vote
77 function voteNegative() {
78 $newKarma = $this->getNbNegVotes() + 1;
79 $this->setNbNegVotes($newKarma);
80 $this->writeToDatabase();
86 // these methods shouldn't be called directly
87 function readFromDatabase() {
88 $query = 'SELECT ikarmapos, ikarmaneg FROM '.sql_table('item').' WHERE inumber=' . $this->itemid;
89 $res = sql_query($query);
90 $obj = sql_fetch_object($res);
92 $this->karmapos = $obj->ikarmapos;
93 $this->karmaneg = $obj->ikarmaneg;
98 function writeToDatabase() {
99 $query = 'UPDATE '.sql_table('item').' SET ikarmapos=' . $this->karmapos . ', ikarmaneg='.$this->karmaneg.' WHERE inumber=' . $this->itemid;
103 // checks if a vote is still allowed for an IP
104 function isVoteAllowed($ip) {
105 $query = 'SELECT * FROM '.sql_table('karma')." WHERE itemid=$this->itemid and ip='".sql_real_escape_string($ip)."'";
106 $res = sql_query($query);
107 return (sql_num_rows($res) == 0);
110 // save IP in database so no multiple votes are possible
112 $query = 'INSERT INTO '.sql_table('karma').' (itemid, ip) VALUES ('.$this->itemid.",'".sql_real_escape_string(serverVar('REMOTE_ADDR'))."')";