3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
4 * Copyright (C) 2002-2012 The Nucleus Group
\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
13 * Class representing the karma votes for a certain item
\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
21 // id of item about which this object contains information
\r
24 // indicates if the karma vote info has already been intialized from the DB
\r
27 // amount of positive/negative votes
\r
31 function KARMA($itemid, $initpos = 0, $initneg = 0, $initread = 0) {
\r
33 $this->itemid = intval($itemid);
\r
35 // have we read the karma info yet?
\r
36 $this->inforead = intval($initread);
\r
38 // number of positive and negative votes
\r
39 $this->karmapos = intval($initpos);
\r
40 $this->karmaneg = intval($initneg);
\r
43 function getNbPosVotes() {
\r
44 if (!$this->inforead) $this->readFromDatabase();
\r
45 return $this->karmapos;
\r
47 function getNbNegVotes() {
\r
48 if (!$this->inforead) $this->readFromDatabase();
\r
49 return $this->karmaneg;
\r
51 function getNbOfVotes() {
\r
52 if (!$this->inforead) $this->readFromDatabase();
\r
53 return ($this->karmapos + $this->karmaneg);
\r
55 function getTotalScore() {
\r
56 if (!$this->inforead) $this->readFromDatabase();
\r
57 return ($this->karmapos - $this->karmaneg);
\r
60 function setNbPosVotes($val) {
\r
61 $this->karmapos = intval($val);
\r
63 function setNbNegVotes($val) {
\r
64 $this->karmaneg = intval($val);
\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
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
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
91 $this->karmapos = $res['ikarmapos'];
\r
92 $this->karmaneg = $res['ikarmaneg'];
\r
93 $this->inforead = 1;
\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
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
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