OSDN Git Service

preparation for 4.0 trunk: move whole scripts just under trunk directory
[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-2011 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-2011 The Nucleus Group
17  * @version $Id$
18  * $NucleusJP: KARMA.php,v 1.5 2006/07/17 20:03:44 kimitake Exp $
19  */
20 class KARMA {
21
22         // id of item about which this object contains information
23         var $itemid;
24
25         // indicates if the karma vote info has already been intialized from the DB
26         var $inforead;
27
28         // amount of positive/negative votes
29         var $karmapos;
30         var $karmaneg;
31
32         function KARMA($itemid, $initpos = 0, $initneg = 0, $initread = 0) {
33                 // itemid
34                 $this->itemid = intval($itemid);
35
36                 // have we read the karma info yet?
37                 $this->inforead = intval($initread);
38
39                 // number of positive and negative votes
40                 $this->karmapos = intval($initpos);
41                 $this->karmaneg = intval($initneg);
42         }
43
44         function getNbPosVotes() {
45                 if (!$this->inforead) $this->readFromDatabase();
46                 return $this->karmapos;
47         }
48         function getNbNegVotes() {
49                 if (!$this->inforead) $this->readFromDatabase();
50                 return $this->karmaneg;
51         }
52         function getNbOfVotes() {
53                 if (!$this->inforead) $this->readFromDatabase();
54                 return ($this->karmapos + $this->karmaneg);
55         }
56         function getTotalScore() {
57                 if (!$this->inforead) $this->readFromDatabase();
58                 return ($this->karmapos - $this->karmaneg);
59         }
60
61         function setNbPosVotes($val) {
62                 $this->karmapos = intval($val);
63         }
64         function setNbNegVotes($val) {
65                 $this->karmaneg = intval($val);
66         }
67
68
69         // adds a positive vote
70         function votePositive() {
71                 $newKarma = $this->getNbPosVotes() + 1;
72                 $this->setNbPosVotes($newKarma);
73                 $this->writeToDatabase();
74                 $this->saveIP();
75         }
76
77         // adds a negative vote
78         function voteNegative() {
79                 $newKarma = $this->getNbNegVotes() + 1;
80                 $this->setNbNegVotes($newKarma);
81                 $this->writeToDatabase();
82                 $this->saveIP();
83         }
84
85
86
87         // these methods shouldn't be called directly
88         function readFromDatabase() {
89                 $query = 'SELECT ikarmapos, ikarmaneg FROM '.sql_table('item').' WHERE inumber=' . $this->itemid;
90                 $res = sql_query($query);
91                 $obj = sql_fetch_object($res);
92
93                 $this->karmapos = $obj->ikarmapos;
94                 $this->karmaneg = $obj->ikarmaneg;
95                 $this->inforead = 1;
96         }
97
98
99         function writeToDatabase() {
100                 $query = 'UPDATE '.sql_table('item').' SET ikarmapos=' . $this->karmapos . ', ikarmaneg='.$this->karmaneg.' WHERE inumber=' . $this->itemid;
101                 sql_query($query);
102         }
103
104         // checks if a vote is still allowed for an IP
105         function isVoteAllowed($ip) {
106                 $query = 'SELECT * FROM '.sql_table('karma')." WHERE itemid=$this->itemid and ip='".sql_real_escape_string($ip)."'";
107                 $res = sql_query($query);
108                 return (sql_num_rows($res) == 0);
109         }
110
111         // save IP in database so no multiple votes are possible
112         function saveIP() {
113                 $query = 'INSERT INTO '.sql_table('karma').' (itemid, ip) VALUES ('.$this->itemid.",'".sql_real_escape_string(serverVar('REMOTE_ADDR'))."')";
114                 sql_query($query);
115         }
116 }
117
118 ?>