OSDN Git Service

Merge branch 'skinnable-master'
[nucleus-jp/nucleus-next.git] / nucleus / libs / KARMA.php
index 6370305..5471c04 100644 (file)
@@ -1,3 +1,121 @@
+<<<<<<< HEAD
+<?php\r
+/*\r
+ * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
+ * Copyright (C) 2002-2012 The Nucleus Group\r
+ *\r
+ * This program is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License\r
+ * as published by the Free Software Foundation; either version 2\r
+ * of the License, or (at your option) any later version.\r
+ * (see nucleus/documentation/index.html#license for more info)\r
+ */\r
+/**\r
+ * Class representing the karma votes for a certain item\r
+ *\r
+ * @license http://nucleuscms.org/license.txt GNU General Public License\r
+ * @copyright Copyright (C) 2002-2012 The Nucleus Group\r
+ * @version $Id: KARMA.php 1470 2010-11-29 22:10:16Z ftruscot $\r
+ */\r
+class Karma\r
+{\r
+       // id of item about which this object contains information\r
+       var $itemid;\r
+       \r
+       // indicates if the karma vote info has already been intialized from the DB\r
+       var $inforead;\r
+       \r
+       // amount of positive/negative votes\r
+       var $karmapos;\r
+       var $karmaneg;\r
+       \r
+       function KARMA($itemid, $initpos = 0, $initneg = 0, $initread = 0) {\r
+               // itemid\r
+               $this->itemid = intval($itemid);\r
+\r
+               // have we read the karma info yet?\r
+               $this->inforead = intval($initread);\r
+\r
+               // number of positive and negative votes\r
+               $this->karmapos = intval($initpos);\r
+               $this->karmaneg = intval($initneg);\r
+       }\r
+\r
+       function getNbPosVotes() {\r
+               if (!$this->inforead) $this->readFromDatabase();\r
+               return $this->karmapos;\r
+       }\r
+       function getNbNegVotes() {\r
+               if (!$this->inforead) $this->readFromDatabase();\r
+               return $this->karmaneg;\r
+       }\r
+       function getNbOfVotes() {\r
+               if (!$this->inforead) $this->readFromDatabase();\r
+               return ($this->karmapos + $this->karmaneg);\r
+       }\r
+       function getTotalScore() {\r
+               if (!$this->inforead) $this->readFromDatabase();\r
+               return ($this->karmapos - $this->karmaneg);\r
+       }\r
+\r
+       function setNbPosVotes($val) {\r
+               $this->karmapos = intval($val);\r
+       }\r
+       function setNbNegVotes($val) {\r
+               $this->karmaneg = intval($val);\r
+       }\r
+\r
+\r
+       // adds a positive vote\r
+       function votePositive() {\r
+               $newKarma = $this->getNbPosVotes() + 1;\r
+               $this->setNbPosVotes($newKarma);\r
+               $this->writeToDatabase();\r
+               $this->saveIP();\r
+       }\r
+\r
+       // adds a negative vote\r
+       function voteNegative() {\r
+               $newKarma = $this->getNbNegVotes() + 1;\r
+               $this->setNbNegVotes($newKarma);\r
+               $this->writeToDatabase();\r
+               $this->saveIP();\r
+       }\r
+\r
+\r
+\r
+       // these methods shouldn't be called directly\r
+       function readFromDatabase() {\r
+               $query = 'SELECT ikarmapos, ikarmaneg FROM '.sql_table('item').' WHERE inumber=' . $this->itemid;\r
+               $res = DB::getRow($query);\r
+\r
+               $this->karmapos = $res['ikarmapos'];\r
+               $this->karmaneg = $res['ikarmaneg'];\r
+               $this->inforead = 1;\r
+       }\r
+\r
+\r
+       function writeToDatabase() {\r
+               $query = 'UPDATE '.sql_table('item').' SET ikarmapos=' . $this->karmapos . ', ikarmaneg='.$this->karmaneg.' WHERE inumber=' . $this->itemid;\r
+               DB::execute($query);\r
+       }\r
+\r
+       // checks if a vote is still allowed for an IP\r
+       function isVoteAllowed($ip) {\r
+               $query = 'SELECT * FROM '.sql_table('karma')." WHERE itemid={$this->itemid} and ip=". DB::quoteValue($ip);\r
+               $res = DB::getResult($query);\r
+               return ($res->rowCount() == 0);\r
+       }\r
+\r
+       // save IP in database so no multiple votes are possible\r
+       function saveIP() {\r
+               $query = 'INSERT INTO ' . sql_table('karma') .' (itemid, ip) VALUES (' . $this->itemid . ','. DB::quoteValue(serverVar('REMOTE_ADDR')) .')';\r
+               DB::execute($query);\r
+       }\r
+}\r
+\r
+?>\r
+=======
 <?php
 /*
  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
@@ -113,4 +231,5 @@ class Karma
        }
 }
 
-?>
\ No newline at end of file
+?>
+>>>>>>> skinnable-master