OSDN Git Service

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