]> gitweb.fluxo.info Git - semanticscuttle.git/commitdiff
Begin work on voting system
authorcweiske <cweiske@b3834d28-1941-0410-a4f8-b48e95affb8f>
Fri, 23 Oct 2009 06:50:08 +0000 (06:50 +0000)
committercweiske <cweiske@b3834d28-1941-0410-a4f8-b48e95affb8f>
Fri, 23 Oct 2009 06:50:08 +0000 (06:50 +0000)
git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@391 b3834d28-1941-0410-a4f8-b48e95affb8f

src/SemanticScuttle/Service/User.php
src/SemanticScuttle/Service/Vote.php [new file with mode: 0644]

index bc88c0b48b0734b1725267a68a4d12a41737a05c..22076fe827a86a54367786c6f87e9c980985a5d6 100644 (file)
@@ -15,9 +15,9 @@ class SemanticScuttle_Service_User extends SemanticScuttle_Service
     /**
      * Returns the single service instance
      *
-     * @param DB $db Database object
+     * @param sql_db $db Database object
      *
-     * @return SemanticScuttle_Service
+     * @return SemanticScuttle_Service_User
      */
        public static function getInstance($db)
     {
diff --git a/src/SemanticScuttle/Service/Vote.php b/src/SemanticScuttle/Service/Vote.php
new file mode 100644 (file)
index 0000000..e348512
--- /dev/null
@@ -0,0 +1,181 @@
+<?php
+
+/**
+ * SemanticScuttle voting system.
+ *
+ * Each registered user in SemanticScuttle may vote
+ * for or against a bookmark, counting +1 or -1.
+ * The sum of all votes determines the "voting" of a bookmark.
+ * Every user is equal in voting. Each vote is tied to a user.
+ *
+ * @internal
+ * Votes are saved in a separate "votes" table.
+ * Additionally to that, the voting of a bookmark is also
+ * stored in the bookmarks table. This is done to make
+ * sure lookups are really fast, since every bookmarks
+ * in a list shows its voting.
+ *
+ * @author Christian Weiske <cweiske@cweiske.de>
+ */
+class SemanticScuttle_Service_Vote extends SemanticScuttle_Service
+{
+    /**
+     * Database object
+     *
+     * @var sql_db
+     */
+    protected $db;
+
+
+
+    /**
+     * Returns the single service instance
+     *
+     * @param sql_db $db Database object
+     *
+     * @return SemanticScuttle_Service_Vote
+     */
+       public static function getInstance($db)
+    {
+               static $instance;
+               if (!isset($instance)) {
+            $instance = new self($db);
+        }
+               return $instance;
+       }
+
+
+
+    /**
+     * Create a new instance.
+     *
+     * @param sql_db $db Database object
+     */
+       protected function __construct($db)
+    {
+               $this->db = $db;
+               $this->tablename  = $GLOBALS['tableprefix'] . 'votes';
+       }
+
+
+
+    /**
+     * Returns the sum of votes for the given bookmark.
+     *
+     * @param integer $bookmark Bookmark ID
+     *
+     * @return integer Vote (can be positive, 0 or negative)
+     */
+    public function getVoting($bookmark)
+    {
+        //FIXME
+    }
+
+
+
+    /**
+     * Returns the number of users that voted for or
+     * against the given bookmark.
+     *
+     * @param integer $bookmark Bookmark ID
+     *
+     * @return integer Number of votes
+     */
+    public function getVotes($bookmark)
+    {
+        //FIXME
+    }
+
+
+
+    /**
+     * Returns if the user has already voted for 
+     * the given bookmark.
+     *
+     * @param integer $bookmark Bookmark ID
+     * @param integer $user     User ID
+     *
+     * @return boolean True if the user has already voted
+     */
+    public function hasVoted($bookmark, $user)
+    {
+        //FIXME
+    }
+
+
+
+    /**
+     * Returns the actual vote the given user
+     * gave the bookmark.
+     *
+     * @param integer $bookmark Bookmark ID
+     * @param integer $user     User ID
+     *
+     * @return integer Either 1 or -1.
+     */
+    public function getVote($bookmark, $user)
+    {
+        //FIXME
+    }
+
+
+
+    /**
+     * Let a user vote for the bookmark.
+     *
+     * @internal
+     * We check if voting is enabled or not,
+     * and if the user has already voted. 
+     * It is up to the calling code to make sure
+     * the user is authorized to vote.
+     *
+     * @param integer $bookmark Bookmark ID
+     * @param integer $user     User ID
+     * @param integer $vote     1 or -1
+     *
+     * @return boolean True if the vote was saved,
+     *                 false if there was a problem
+     *                 (i.e. already voted)
+     */
+    public function vote($bookmark, $user, $vote = 1)
+    {
+        //FIXME: check if voting is enabled (global conf var)
+
+        if ($this->hasVoted($bookmark, $user)) {
+            return false;
+        }
+
+        if ($vote != -1 && $vote != 1) {
+            return false;
+        }
+
+        $dbresult = $this->db->sql_query(
+            'INSERT INTO ' . $this->getTableName()
+            . ' SET'
+            . ' bid = ' . (int)$bookmark
+            . ',uid = ' . (int)$user
+            . ',vote = ' . (int)$vote
+        );
+        //FIXME: check for sql error
+        $this->db->sql_freeresult();
+        //FIXME: update bookmarks table
+    }
+
+
+
+    /**
+     * Re-calculates all votings for all bookmarks
+     * and updates the voting values in the bookmarks
+     * table.
+     *
+     * @return void
+     */
+    public function rewriteVotings()
+    {
+        //FIXME
+    }
+
+
+}
+
+?>
\ No newline at end of file