3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
4 * Copyright (C) 2002-2004 The Nucleus Group
\r
6 * This program is free software; you can redistribute it and/or
\r
7 * modify it under the terms of the GNU General Public License
\r
8 * as published by the Free Software Foundation; either version 2
\r
9 * of the License, or (at your option) any later version.
\r
10 * (see nucleus/documentation/index.html#license for more info)
\r
12 * Class representing the karma votes for a certain item
\r
16 // id of item about which this object contains information
\r
19 // indicates if the karma vote info has already been intialized from the DB
\r
22 // amount of positive/negative votes
\r
26 function KARMA($itemid, $initpos = 0, $initneg = 0, $initread = 0) {
\r
28 $this->itemid = intval($itemid);
\r
30 // have we read the karma info yet?
\r
31 $this->inforead = intval($initread);
\r
33 // number of positive and negative votes
\r
34 $this->karmapos = intval($initpos);
\r
35 $this->karmaneg = intval($initneg);
\r
38 function getNbPosVotes() {
\r
39 if (!$this->inforead) $this->readFromDatabase();
\r
40 return $this->karmapos;
\r
42 function getNbNegVotes() {
\r
43 if (!$this->inforead) $this->readFromDatabase();
\r
44 return $this->karmaneg;
\r
46 function getNbOfVotes() {
\r
47 if (!$this->inforead) $this->readFromDatabase();
\r
48 return ($this->karmapos + $this->karmaneg);
\r
50 function getTotalScore() {
\r
51 if (!$this->inforead) $this->readFromDatabase();
\r
52 return ($this->karmapos - $this->karmaneg);
\r
55 function setNbPosVotes($val) {
\r
56 $this->karmapos = intval($val);
\r
58 function setNbNegVotes($val) {
\r
59 $this->karmaneg = intval($val);
\r
63 // adds a positive vote
\r
64 function votePositive() {
\r
65 $newKarma = $this->getNbPosVotes() + 1;
\r
66 $this->setNbPosVotes($newKarma);
\r
67 $this->writeToDatabase();
\r
71 // adds a negative vote
\r
72 function voteNegative() {
\r
73 $newKarma = $this->getNbNegVotes() + 1;
\r
74 $this->setNbNegVotes($newKarma);
\r
75 $this->writeToDatabase();
\r
81 // these methods shouldn't be called directly
\r
82 function readFromDatabase() {
\r
83 $query = 'SELECT ikarmapos, ikarmaneg FROM '.sql_table('item').' WHERE inumber=' . $this->itemid;
\r
84 $res = sql_query($query);
\r
85 $obj = mysql_fetch_object($res);
\r
87 $this->karmapos = $obj->ikarmapos;
\r
88 $this->karmaneg = $obj->ikarmaneg;
\r
89 $this->inforead = 1;
\r
93 function writeToDatabase() {
\r
94 $query = 'UPDATE '.sql_table('item').' SET ikarmapos=' . $this->karmapos . ', ikarmaneg='.$this->karmaneg.' WHERE inumber=' . $this->itemid;
\r
98 // checks if a vote is still allowed for an IP
\r
99 function isVoteAllowed($ip) {
\r
100 $query = 'SELECT * FROM '.sql_table('karma')." WHERE itemid=$this->itemid and ip='".addslashes($ip)."'";
\r
101 $res = sql_query($query);
\r
102 return (mysql_num_rows($res) == 0);
\r
105 // save IP in database so no multiple votes are possible
\r
106 function saveIP() {
\r
107 $query = 'INSERT INTO '.sql_table('karma').' (itemid, ip) VALUES ('.$this->itemid.",'".addslashes(serverVar('REMOTE_ADDR'))."')";
\r