OSDN Git Service

本家Nucleus CMSの開発を補助するためにコミット
[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 1470 2010-11-29 22:10:16Z ftruscot $
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 = sql_query($query);
90                 $obj = sql_fetch_object($res);
91
92                 $this->karmapos = $obj->ikarmapos;
93                 $this->karmaneg = $obj->ikarmaneg;
94                 $this->inforead = 1;
95         }
96
97
98         function writeToDatabase() {
99                 $query = 'UPDATE '.sql_table('item').' SET ikarmapos=' . $this->karmapos . ', ikarmaneg='.$this->karmaneg.' WHERE inumber=' . $this->itemid;
100                 sql_query($query);
101         }
102
103         // checks if a vote is still allowed for an IP
104         function isVoteAllowed($ip) {
105                 $query = 'SELECT * FROM '.sql_table('karma')." WHERE itemid=$this->itemid and ip='".sql_real_escape_string($ip)."'";
106                 $res = sql_query($query);
107                 return (sql_num_rows($res) == 0);
108         }
109
110         // save IP in database so no multiple votes are possible
111         function saveIP() {
112                 $query = 'INSERT INTO '.sql_table('karma').' (itemid, ip) VALUES ('.$this->itemid.",'".sql_real_escape_string(serverVar('REMOTE_ADDR'))."')";
113                 sql_query($query);
114         }
115 }
116
117 ?>