]> gitweb.fluxo.info Git - semanticscuttle.git/commitdiff
allow changing of votes
authorcweiske <cweiske@b3834d28-1941-0410-a4f8-b48e95affb8f>
Mon, 2 Nov 2009 09:35:38 +0000 (09:35 +0000)
committercweiske <cweiske@b3834d28-1941-0410-a4f8-b48e95affb8f>
Mon, 2 Nov 2009 09:35:38 +0000 (09:35 +0000)
git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@493 b3834d28-1941-0410-a4f8-b48e95affb8f

src/SemanticScuttle/Service/Vote.php
tests/VoteTest.php

index 89088f0a2efb6ae9992769ffe237bdb803410e87..c0c4995c14b293a08ea8d0e20d337e11f0129965 100644 (file)
@@ -225,12 +225,12 @@ class SemanticScuttle_Service_Vote extends SemanticScuttle_DbService
             return false;
         }
 
-        if ($this->hasVoted($bookmark, $user)) {
+        if ($vote != -1 && $vote != 1) {
             return false;
         }
 
-        if ($vote != -1 && $vote != 1) {
-            return false;
+        if ($this->hasVoted($bookmark, $user)) {
+            $this->removeVote($bookmark, $user);
         }
 
         $res = $this->db->sql_query(
@@ -257,6 +257,41 @@ class SemanticScuttle_Service_Vote extends SemanticScuttle_DbService
 
 
 
+    /**
+     * Removes a vote from the database
+     *
+     * @param integer $bookmark Bookmark ID
+     * @param integer $user     User ID
+     *
+     * @return boolean True if all went well, false if not
+     */
+    protected function removeVote($bookmark, $user)
+    {
+        $vote = $this->getVote($bookmark, $user);
+        if ($vote === null) {
+            return false;
+        }
+
+        //remove from votes table
+        $query = 'DELETE FROM ' . $this->getTableName()
+            . ' WHERE bId = ' . (int)$bookmark
+            . ' AND uId = ' . (int)$user;
+        $this->db->sql_query($query);
+
+        //change voting sum in bookmarks table
+        $bm  = SemanticScuttle_Service_Factory::get('Bookmark');
+        $res = $this->db->sql_query(
+            $sql='UPDATE ' . $bm->getTableName()
+            . ' SET bVoting = bVoting - ' . (int)$vote
+            . ' WHERE bId = ' . (int)$bookmark
+        );
+        $this->db->sql_freeresult($res);
+
+        return true;
+    }
+
+
+
     /**
      * Re-calculates all votings for all bookmarks
      * and updates the voting values in the bookmarks
index 9d6d904921319b05a5bfcc18a9452982e4eb0379..b211dcd6ee039572e8ffdfd2e9a56d6afc6a0c8d 100644 (file)
@@ -35,6 +35,13 @@ class VoteTest extends TestBase
      */
     protected $vs = null;
 
+    /**
+     * Bookmark service instance.
+     *
+     * @var SemanticScuttle_Service_Bookmark
+     */
+    protected $bs = null;
+
 
 
     /**
@@ -293,11 +300,11 @@ class VoteTest extends TestBase
         $uid = 1;
         $bid = $this->addBookmark();
         $this->assertTrue($this->vs->vote($bid, $uid, 1));
-        $this->assertFalse($this->vs->vote($bid, $uid, 1));
+        $this->assertTrue($this->vs->vote($bid, $uid, 1));
 
         $bid = $this->addBookmark();
         $this->assertTrue($this->vs->vote($bid, $uid, -1));
-        $this->assertFalse($this->vs->vote($bid, $uid, 1));
+        $this->assertTrue($this->vs->vote($bid, $uid, 1));
     }
 
 
@@ -332,6 +339,122 @@ class VoteTest extends TestBase
 
 
 
+    /**
+     * Verify that changing the vote from positive to negative
+     * works.
+     *
+     * @return void
+     */
+    public function testVoteChangePosNeg()
+    {
+        $uid = 1;
+        $bid = $this->addBookmark();
+
+        $this->assertTrue($this->vs->vote($bid, $uid, 1));
+        $this->assertEquals(1, $this->vs->getVote($bid, $uid));
+        $this->assertEquals(1, $this->vs->getVotes($bid));
+
+        $b = $this->bs->getBookmark($bid);
+        $this->assertEquals(1, $b['bVoting']);
+
+        //change vote
+        $this->assertTrue($this->vs->vote($bid, $uid, -1));
+        $this->assertEquals(-1, $this->vs->getVote($bid, $uid));
+        $this->assertEquals(1, $this->vs->getVotes($bid));
+
+        $b = $this->bs->getBookmark($bid);
+        $this->assertEquals(-1, $b['bVoting']);
+    }
+
+
+
+    /**
+     * Verify that changing the vote from negative to positive
+     * works.
+     *
+     * @return void
+     */
+    public function testVoteChangeNegPos()
+    {
+        $uid = 1;
+        $bid = $this->addBookmark();
+
+        $this->assertTrue($this->vs->vote($bid, $uid, -1));
+        $this->assertEquals(-1, $this->vs->getVote($bid, $uid));
+        $this->assertEquals(1, $this->vs->getVotes($bid));
+
+        $b = $this->bs->getBookmark($bid);
+        $this->assertEquals(-1, $b['bVoting']);
+
+        //change vote
+        $this->assertTrue($this->vs->vote($bid, $uid, 1));
+        $this->assertEquals(1, $this->vs->getVote($bid, $uid));
+        $this->assertEquals(1, $this->vs->getVotes($bid));
+
+        $b = $this->bs->getBookmark($bid);
+        $this->assertEquals(1, $b['bVoting']);
+    }
+
+
+
+    /**
+     * Verify that changing the vote from postitive to positive
+     * has no strange effects
+     *
+     * @return void
+     */
+    public function testVoteChangePosPos()
+    {
+        $uid = 1;
+        $bid = $this->addBookmark();
+
+        $this->assertTrue($this->vs->vote($bid, $uid, 1));
+        $this->assertEquals(1, $this->vs->getVote($bid, $uid));
+        $this->assertEquals(1, $this->vs->getVotes($bid));
+
+        $b = $this->bs->getBookmark($bid);
+        $this->assertEquals(1, $b['bVoting']);
+
+        //change vote
+        $this->assertTrue($this->vs->vote($bid, $uid, 1));
+        $this->assertEquals(1, $this->vs->getVote($bid, $uid));
+        $this->assertEquals(1, $this->vs->getVotes($bid));
+
+        $b = $this->bs->getBookmark($bid);
+        $this->assertEquals(1, $b['bVoting']);
+    }
+
+
+
+    /**
+     * Verify that changing the vote from postitive to positive
+     * has no strange effects
+     *
+     * @return void
+     */
+    public function testVoteChangeNegNeg()
+    {
+        $uid = 1;
+        $bid = $this->addBookmark();
+
+        $this->assertTrue($this->vs->vote($bid, $uid, -1));
+        $this->assertEquals(-1, $this->vs->getVote($bid, $uid));
+        $this->assertEquals(1, $this->vs->getVotes($bid));
+
+        $b = $this->bs->getBookmark($bid);
+        $this->assertEquals(-1, $b['bVoting']);
+
+        //change vote to same value
+        $this->assertTrue($this->vs->vote($bid, $uid, -1));
+        $this->assertEquals(-1, $this->vs->getVote($bid, $uid));
+        $this->assertEquals(1, $this->vs->getVotes($bid));
+
+        $b = $this->bs->getBookmark($bid);
+        $this->assertEquals(-1, $b['bVoting']);
+    }
+
+
+
     /**
      * Test that rewriting votings does work
      *