OSDN Git Service

Merge branch 'skinnable-master'
[nucleus-jp/nucleus-next.git] / nucleus / libs / KARMA.php
1 <<<<<<< HEAD
2 <?php\r
3 /*\r
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
5  * Copyright (C) 2002-2012 The Nucleus Group\r
6  *\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
12  */\r
13 /**\r
14  * Class representing the karma votes for a certain item\r
15  *\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
19  */\r
20 class Karma\r
21 {\r
22         // id of item about which this object contains information\r
23         var $itemid;\r
24         \r
25         // indicates if the karma vote info has already been intialized from the DB\r
26         var $inforead;\r
27         \r
28         // amount of positive/negative votes\r
29         var $karmapos;\r
30         var $karmaneg;\r
31         \r
32         function KARMA($itemid, $initpos = 0, $initneg = 0, $initread = 0) {\r
33                 // itemid\r
34                 $this->itemid = intval($itemid);\r
35 \r
36                 // have we read the karma info yet?\r
37                 $this->inforead = intval($initread);\r
38 \r
39                 // number of positive and negative votes\r
40                 $this->karmapos = intval($initpos);\r
41                 $this->karmaneg = intval($initneg);\r
42         }\r
43 \r
44         function getNbPosVotes() {\r
45                 if (!$this->inforead) $this->readFromDatabase();\r
46                 return $this->karmapos;\r
47         }\r
48         function getNbNegVotes() {\r
49                 if (!$this->inforead) $this->readFromDatabase();\r
50                 return $this->karmaneg;\r
51         }\r
52         function getNbOfVotes() {\r
53                 if (!$this->inforead) $this->readFromDatabase();\r
54                 return ($this->karmapos + $this->karmaneg);\r
55         }\r
56         function getTotalScore() {\r
57                 if (!$this->inforead) $this->readFromDatabase();\r
58                 return ($this->karmapos - $this->karmaneg);\r
59         }\r
60 \r
61         function setNbPosVotes($val) {\r
62                 $this->karmapos = intval($val);\r
63         }\r
64         function setNbNegVotes($val) {\r
65                 $this->karmaneg = intval($val);\r
66         }\r
67 \r
68 \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
74                 $this->saveIP();\r
75         }\r
76 \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
82                 $this->saveIP();\r
83         }\r
84 \r
85 \r
86 \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
91 \r
92                 $this->karmapos = $res['ikarmapos'];\r
93                 $this->karmaneg = $res['ikarmaneg'];\r
94                 $this->inforead = 1;\r
95         }\r
96 \r
97 \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
101         }\r
102 \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
108         }\r
109 \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
114         }\r
115 }\r
116 \r
117 ?>\r
118 =======
119 <?php
120 /*
121  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
122  * Copyright (C) 2002-2009 The Nucleus Group
123  *
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)
129  */
130 /**
131  * Class representing the karma votes for a certain item
132  *
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 $
136  */
137 class Karma
138 {
139         // id of item about which this object contains information
140         var $itemid;
141         
142         // indicates if the karma vote info has already been intialized from the DB
143         var $inforead;
144         
145         // amount of positive/negative votes
146         var $karmapos;
147         var $karmaneg;
148         
149         function KARMA($itemid, $initpos = 0, $initneg = 0, $initread = 0) {
150                 // itemid
151                 $this->itemid = intval($itemid);
152
153                 // have we read the karma info yet?
154                 $this->inforead = intval($initread);
155
156                 // number of positive and negative votes
157                 $this->karmapos = intval($initpos);
158                 $this->karmaneg = intval($initneg);
159         }
160
161         function getNbPosVotes() {
162                 if (!$this->inforead) $this->readFromDatabase();
163                 return $this->karmapos;
164         }
165         function getNbNegVotes() {
166                 if (!$this->inforead) $this->readFromDatabase();
167                 return $this->karmaneg;
168         }
169         function getNbOfVotes() {
170                 if (!$this->inforead) $this->readFromDatabase();
171                 return ($this->karmapos + $this->karmaneg);
172         }
173         function getTotalScore() {
174                 if (!$this->inforead) $this->readFromDatabase();
175                 return ($this->karmapos - $this->karmaneg);
176         }
177
178         function setNbPosVotes($val) {
179                 $this->karmapos = intval($val);
180         }
181         function setNbNegVotes($val) {
182                 $this->karmaneg = intval($val);
183         }
184
185
186         // adds a positive vote
187         function votePositive() {
188                 $newKarma = $this->getNbPosVotes() + 1;
189                 $this->setNbPosVotes($newKarma);
190                 $this->writeToDatabase();
191                 $this->saveIP();
192         }
193
194         // adds a negative vote
195         function voteNegative() {
196                 $newKarma = $this->getNbNegVotes() + 1;
197                 $this->setNbNegVotes($newKarma);
198                 $this->writeToDatabase();
199                 $this->saveIP();
200         }
201
202
203
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);
208
209                 $this->karmapos = $res['ikarmapos'];
210                 $this->karmaneg = $res['ikarmaneg'];
211                 $this->inforead = 1;
212         }
213
214
215         function writeToDatabase() {
216                 $query = 'UPDATE '.sql_table('item').' SET ikarmapos=' . $this->karmapos . ', ikarmaneg='.$this->karmaneg.' WHERE inumber=' . $this->itemid;
217                 DB::execute($query);
218         }
219
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);
225         }
226
227         // save IP in database so no multiple votes are possible
228         function saveIP() {
229                 $query = 'INSERT INTO ' . sql_table('karma') .' (itemid, ip) VALUES (' . $this->itemid . ','. DB::quoteValue(serverVar('REMOTE_ADDR')) .')';
230                 DB::execute($query);
231         }
232 }
233
234 ?>
235 >>>>>>> skinnable-master