4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
5 * Copyright (C) 2002-2012 The Nucleus Group
\r
7 * This program is free software; you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation; either version 2
\r
10 * of the License, or (at your option) any later version.
\r
11 * (see nucleus/documentation/index.html#license for more info)
\r
14 * Class representing the karma votes for a certain item
\r
16 * @license http://nucleuscms.org/license.txt GNU General Public License
\r
17 * @copyright Copyright (C) 2002-2012 The Nucleus Group
\r
18 * @version $Id: KARMA.php 1470 2010-11-29 22:10:16Z ftruscot $
\r
22 // id of item about which this object contains information
\r
25 // indicates if the karma vote info has already been intialized from the DB
\r
28 // amount of positive/negative votes
\r
32 function KARMA($itemid, $initpos = 0, $initneg = 0, $initread = 0) {
\r
34 $this->itemid = intval($itemid);
\r
36 // have we read the karma info yet?
\r
37 $this->inforead = intval($initread);
\r
39 // number of positive and negative votes
\r
40 $this->karmapos = intval($initpos);
\r
41 $this->karmaneg = intval($initneg);
\r
44 function getNbPosVotes() {
\r
45 if (!$this->inforead) $this->readFromDatabase();
\r
46 return $this->karmapos;
\r
48 function getNbNegVotes() {
\r
49 if (!$this->inforead) $this->readFromDatabase();
\r
50 return $this->karmaneg;
\r
52 function getNbOfVotes() {
\r
53 if (!$this->inforead) $this->readFromDatabase();
\r
54 return ($this->karmapos + $this->karmaneg);
\r
56 function getTotalScore() {
\r
57 if (!$this->inforead) $this->readFromDatabase();
\r
58 return ($this->karmapos - $this->karmaneg);
\r
61 function setNbPosVotes($val) {
\r
62 $this->karmapos = intval($val);
\r
64 function setNbNegVotes($val) {
\r
65 $this->karmaneg = intval($val);
\r
69 // adds a positive vote
\r
70 function votePositive() {
\r
71 $newKarma = $this->getNbPosVotes() + 1;
\r
72 $this->setNbPosVotes($newKarma);
\r
73 $this->writeToDatabase();
\r
77 // adds a negative vote
\r
78 function voteNegative() {
\r
79 $newKarma = $this->getNbNegVotes() + 1;
\r
80 $this->setNbNegVotes($newKarma);
\r
81 $this->writeToDatabase();
\r
87 // these methods shouldn't be called directly
\r
88 function readFromDatabase() {
\r
89 $query = 'SELECT ikarmapos, ikarmaneg FROM '.sql_table('item').' WHERE inumber=' . $this->itemid;
\r
90 $res = DB::getRow($query);
\r
92 $this->karmapos = $res['ikarmapos'];
\r
93 $this->karmaneg = $res['ikarmaneg'];
\r
94 $this->inforead = 1;
\r
98 function writeToDatabase() {
\r
99 $query = 'UPDATE '.sql_table('item').' SET ikarmapos=' . $this->karmapos . ', ikarmaneg='.$this->karmaneg.' WHERE inumber=' . $this->itemid;
\r
100 DB::execute($query);
\r
103 // checks if a vote is still allowed for an IP
\r
104 function isVoteAllowed($ip) {
\r
105 $query = 'SELECT * FROM '.sql_table('karma')." WHERE itemid={$this->itemid} and ip=". DB::quoteValue($ip);
\r
106 $res = DB::getResult($query);
\r
107 return ($res->rowCount() == 0);
\r
110 // save IP in database so no multiple votes are possible
\r
111 function saveIP() {
\r
112 $query = 'INSERT INTO ' . sql_table('karma') .' (itemid, ip) VALUES (' . $this->itemid . ','. DB::quoteValue(serverVar('REMOTE_ADDR')) .')';
\r
113 DB::execute($query);
\r
121 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
122 * Copyright (C) 2002-2009 The Nucleus Group
124 * This program is free software; you can redistribute it and/or
125 * modify it under the terms of the GNU General Public License
126 * as published by the Free Software Foundation; either version 2
127 * of the License, or (at your option) any later version.
128 * (see nucleus/documentation/index.html#license for more info)
131 * Class representing the karma votes for a certain item
133 * @license http://nucleuscms.org/license.txt GNU General Public License
134 * @copyright Copyright (C) 2002-2009 The Nucleus Group
135 * @version $Id: KARMA.php 1812 2012-05-01 14:59:07Z sakamocchi $
139 // id of item about which this object contains information
142 // indicates if the karma vote info has already been intialized from the DB
145 // amount of positive/negative votes
149 function KARMA($itemid, $initpos = 0, $initneg = 0, $initread = 0) {
151 $this->itemid = intval($itemid);
153 // have we read the karma info yet?
154 $this->inforead = intval($initread);
156 // number of positive and negative votes
157 $this->karmapos = intval($initpos);
158 $this->karmaneg = intval($initneg);
161 function getNbPosVotes() {
162 if (!$this->inforead) $this->readFromDatabase();
163 return $this->karmapos;
165 function getNbNegVotes() {
166 if (!$this->inforead) $this->readFromDatabase();
167 return $this->karmaneg;
169 function getNbOfVotes() {
170 if (!$this->inforead) $this->readFromDatabase();
171 return ($this->karmapos + $this->karmaneg);
173 function getTotalScore() {
174 if (!$this->inforead) $this->readFromDatabase();
175 return ($this->karmapos - $this->karmaneg);
178 function setNbPosVotes($val) {
179 $this->karmapos = intval($val);
181 function setNbNegVotes($val) {
182 $this->karmaneg = intval($val);
186 // adds a positive vote
187 function votePositive() {
188 $newKarma = $this->getNbPosVotes() + 1;
189 $this->setNbPosVotes($newKarma);
190 $this->writeToDatabase();
194 // adds a negative vote
195 function voteNegative() {
196 $newKarma = $this->getNbNegVotes() + 1;
197 $this->setNbNegVotes($newKarma);
198 $this->writeToDatabase();
204 // these methods shouldn't be called directly
205 function readFromDatabase() {
206 $query = 'SELECT ikarmapos, ikarmaneg FROM '.sql_table('item').' WHERE inumber=' . $this->itemid;
207 $res = DB::getRow($query);
209 $this->karmapos = $res['ikarmapos'];
210 $this->karmaneg = $res['ikarmaneg'];
215 function writeToDatabase() {
216 $query = 'UPDATE '.sql_table('item').' SET ikarmapos=' . $this->karmapos . ', ikarmaneg='.$this->karmaneg.' WHERE inumber=' . $this->itemid;
220 // checks if a vote is still allowed for an IP
221 function isVoteAllowed($ip) {
222 $query = 'SELECT * FROM '.sql_table('karma')." WHERE itemid={$this->itemid} and ip=". DB::quoteValue($ip);
223 $res = DB::getResult($query);
224 return ($res->rowCount() == 0);
227 // save IP in database so no multiple votes are possible
229 $query = 'INSERT INTO ' . sql_table('karma') .' (itemid, ip) VALUES (' . $this->itemid . ','. DB::quoteValue(serverVar('REMOTE_ADDR')) .')';
235 >>>>>>> skinnable-master