OSDN Git Service

MERGE:リビジョン1828をマージ
[nucleus-jp/nucleus-next.git] / nucleus / libs / KARMA.php
1 <?php\r
2 /*\r
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
4  * Copyright (C) 2002-2012 The Nucleus Group\r
5  *\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
11  */\r
12 /**\r
13  * Class representing the karma votes for a certain item\r
14  *\r
15  * @license http://nucleuscms.org/license.txt GNU General Public License\r
16  * @copyright Copyright (C) 2002-2012 The Nucleus Group\r
17  * @version $Id: KARMA.php 1470 2010-11-29 22:10:16Z ftruscot $\r
18  */\r
19 class Karma\r
20 {\r
21         // id of item about which this object contains information\r
22         var $itemid;\r
23         \r
24         // indicates if the karma vote info has already been intialized from the DB\r
25         var $inforead;\r
26         \r
27         // amount of positive/negative votes\r
28         var $karmapos;\r
29         var $karmaneg;\r
30         \r
31         function KARMA($itemid, $initpos = 0, $initneg = 0, $initread = 0) {\r
32                 // itemid\r
33                 $this->itemid = intval($itemid);\r
34 \r
35                 // have we read the karma info yet?\r
36                 $this->inforead = intval($initread);\r
37 \r
38                 // number of positive and negative votes\r
39                 $this->karmapos = intval($initpos);\r
40                 $this->karmaneg = intval($initneg);\r
41         }\r
42 \r
43         function getNbPosVotes() {\r
44                 if (!$this->inforead) $this->readFromDatabase();\r
45                 return $this->karmapos;\r
46         }\r
47         function getNbNegVotes() {\r
48                 if (!$this->inforead) $this->readFromDatabase();\r
49                 return $this->karmaneg;\r
50         }\r
51         function getNbOfVotes() {\r
52                 if (!$this->inforead) $this->readFromDatabase();\r
53                 return ($this->karmapos + $this->karmaneg);\r
54         }\r
55         function getTotalScore() {\r
56                 if (!$this->inforead) $this->readFromDatabase();\r
57                 return ($this->karmapos - $this->karmaneg);\r
58         }\r
59 \r
60         function setNbPosVotes($val) {\r
61                 $this->karmapos = intval($val);\r
62         }\r
63         function setNbNegVotes($val) {\r
64                 $this->karmaneg = intval($val);\r
65         }\r
66 \r
67 \r
68         // adds a positive vote\r
69         function votePositive() {\r
70                 $newKarma = $this->getNbPosVotes() + 1;\r
71                 $this->setNbPosVotes($newKarma);\r
72                 $this->writeToDatabase();\r
73                 $this->saveIP();\r
74         }\r
75 \r
76         // adds a negative vote\r
77         function voteNegative() {\r
78                 $newKarma = $this->getNbNegVotes() + 1;\r
79                 $this->setNbNegVotes($newKarma);\r
80                 $this->writeToDatabase();\r
81                 $this->saveIP();\r
82         }\r
83 \r
84 \r
85 \r
86         // these methods shouldn't be called directly\r
87         function readFromDatabase() {\r
88                 $query = 'SELECT ikarmapos, ikarmaneg FROM '.sql_table('item').' WHERE inumber=' . $this->itemid;\r
89                 $res = DB::getRow($query);\r
90 \r
91                 $this->karmapos = $res['ikarmapos'];\r
92                 $this->karmaneg = $res['ikarmaneg'];\r
93                 $this->inforead = 1;\r
94         }\r
95 \r
96 \r
97         function writeToDatabase() {\r
98                 $query = 'UPDATE '.sql_table('item').' SET ikarmapos=' . $this->karmapos . ', ikarmaneg='.$this->karmaneg.' WHERE inumber=' . $this->itemid;\r
99                 DB::execute($query);\r
100         }\r
101 \r
102         // checks if a vote is still allowed for an IP\r
103         function isVoteAllowed($ip) {\r
104                 $query = 'SELECT * FROM '.sql_table('karma')." WHERE itemid={$this->itemid} and ip=". DB::quoteValue($ip);\r
105                 $res = DB::getResult($query);\r
106                 return ($res->rowCount() == 0);\r
107         }\r
108 \r
109         // save IP in database so no multiple votes are possible\r
110         function saveIP() {\r
111                 $query = 'INSERT INTO ' . sql_table('karma') .' (itemid, ip) VALUES (' . $this->itemid . ','. DB::quoteValue(serverVar('REMOTE_ADDR')) .')';\r
112                 DB::execute($query);\r
113         }\r
114 }\r
115 \r
116 ?>\r