OSDN Git Service

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