*
* @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.
+ * Additionally to that, the voting and number of votes
+ * of a bookmark is also stored in the bookmarks table.
+ * This is done to make sure lookups are really fast, since
+ * every bookmark in a list shows its voting.
*
* @category Bookmarking
* @package SemanticScuttle
* Returns the number of users that voted for or
* against the given bookmark.
*
+ * @internal
+ * This method uses the votes table to calculate the
+ * number of votes for a bookmark. In normal life, it
+ * is more efficient to use the "bVotes" field in the
+ * bookmarks table.
+ *
* @param integer $bookmark Bookmark ID
*
* @return integer Number of votes
$res = $this->db->sql_query(
$sql='UPDATE ' . $bm->getTableName()
. ' SET bVoting = bVoting + ' . (int)$vote
+ . ' , bVotes = bVotes + 1'
. ' WHERE bId = ' . (int)$bookmark
);
$this->db->sql_freeresult($res);
$res = $this->db->sql_query(
$sql='UPDATE ' . $bm->getTableName()
. ' SET bVoting = bVoting - ' . (int)$vote
+ . ' , bVotes = bVotes - 1'
. ' WHERE bId = ' . (int)$bookmark
);
$this->db->sql_freeresult($res);
$bm = SemanticScuttle_Service_Factory::get('Bookmark');
$query = 'UPDATE ' . $bm->getTableName() . ' as B SET bVoting = '
. '(SELECT SUM(vote) FROM ' . $this->getTableName() . ' as V'
+ . ' WHERE V.bId = B.bId GROUP BY bid)'
+ . ', bVotes = '
+ . '(SELECT COUNT(vote) FROM ' . $this->getTableName() . ' as V'
. ' WHERE V.bId = B.bId GROUP BY bid)';
$this->db->sql_query($query);
}
{
$bid = $this->addBookmark();
$this->assertEquals(0, $this->vs->getVoting($bid));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(0, $bm['bVoting']);
+ $this->assertEquals(0, $bm['bVotes']);
}
$bid = $this->addBookmark();
$this->vs->vote($bid, 1, 1);
$this->assertEquals(1, $this->vs->getVoting($bid));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(1, $bm['bVoting']);
+ $this->assertEquals(1, $bm['bVotes']);
}
$bid = $this->addBookmark();
$this->vs->vote($bid, 1, -1);
$this->assertEquals(-1, $this->vs->getVoting($bid));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(-1, $bm['bVoting']);
+ $this->assertEquals(1, $bm['bVotes']);
}
$this->vs->vote($bid, 3, 1);
$this->vs->vote($bid, 4, 1);
$this->assertEquals(2, $this->vs->getVoting($bid));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(2, $bm['bVoting']);
+ $this->assertEquals(4, $bm['bVotes']);
}
{
$bid = $this->addBookmark();
$this->assertEquals(0, $this->vs->getVotes($bid));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(0, $bm['bVoting']);
+ $this->assertEquals(0, $bm['bVotes']);
}
$bid = $this->addBookmark();
$this->vs->vote($bid, 1, 1);
$this->assertEquals(1, $this->vs->getVotes($bid));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(1, $bm['bVoting']);
+ $this->assertEquals(1, $bm['bVotes']);
}
$this->vs->vote($bid, 3, 1);
$this->vs->vote($bid, 4, 1);
$this->assertEquals(4, $this->vs->getVotes($bid));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(2, $bm['bVoting']);
+ $this->assertEquals(4, $bm['bVotes']);
}
$bm2 = $this->bs->getBookmark($bid2);
$this->assertEquals(0, $bm2['bVoting']);
+ $this->assertEquals(0, $bm2['bVotes']);
}
$this->assertTrue($this->vs->vote($bid, $uid, 1));
$this->assertTrue($this->vs->vote($bid, $uid, 1));
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(1, $bm['bVoting']);
+ $this->assertEquals(1, $bm['bVotes']);
+
$bid = $this->addBookmark();
$this->assertTrue($this->vs->vote($bid, $uid, -1));
$this->assertTrue($this->vs->vote($bid, $uid, 1));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(1, $bm['bVoting']);
+ $this->assertEquals(1, $bm['bVotes']);
}
$bid = $this->addBookmark();
$this->assertTrue($this->vs->vote($bid, $uid, 1));
$this->assertEquals(1, $this->vs->getVote($bid, $uid));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(1, $bm['bVoting']);
+ $this->assertEquals(1, $bm['bVotes']);
}
$bid = $this->addBookmark();
$this->assertTrue($this->vs->vote($bid, $uid, -1));
$this->assertEquals(-1, $this->vs->getVote($bid, $uid));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(-1, $bm['bVoting']);
+ $this->assertEquals(1, $bm['bVotes']);
}
$b = $this->bs->getBookmark($bid);
$this->assertEquals(1, $b['bVoting']);
+ $this->assertEquals(1, $b['bVotes']);
//change vote
$this->assertTrue($this->vs->vote($bid, $uid, -1));
$b = $this->bs->getBookmark($bid);
$this->assertEquals(-1, $b['bVoting']);
+ $this->assertEquals(1, $b['bVotes']);
}
$b = $this->bs->getBookmark($bid);
$this->assertEquals(-1, $b['bVoting']);
+ $this->assertEquals(1, $b['bVotes']);
//change vote
$this->assertTrue($this->vs->vote($bid, $uid, 1));
$b = $this->bs->getBookmark($bid);
$this->assertEquals(1, $b['bVoting']);
+ $this->assertEquals(1, $b['bVotes']);
}
$b = $this->bs->getBookmark($bid);
$this->assertEquals(1, $b['bVoting']);
+ $this->assertEquals(1, $b['bVotes']);
//change vote
$this->assertTrue($this->vs->vote($bid, $uid, 1));
$b = $this->bs->getBookmark($bid);
$this->assertEquals(1, $b['bVoting']);
+ $this->assertEquals(1, $b['bVotes']);
}
$b = $this->bs->getBookmark($bid);
$this->assertEquals(-1, $b['bVoting']);
+ $this->assertEquals(1, $b['bVotes']);
//change vote to same value
$this->assertTrue($this->vs->vote($bid, $uid, -1));
$b = $this->bs->getBookmark($bid);
$this->assertEquals(-1, $b['bVoting']);
+ $this->assertEquals(1, $b['bVotes']);
}
$bm = $this->bs->getBookmark($bid);
$this->assertEquals(1, $bm['bVoting']);
+ $this->assertEquals(1, $bm['bVotes']);
$this->vs->deleteAll();
//we assume that $vs->deleteAll() does *not* reset
//voting in bookmarks table
$bm = $this->bs->getBookmark($bid);
$this->assertEquals(1, $bm['bVoting']);
+ $this->assertEquals(1, $bm['bVotes']);
$this->vs->rewriteVotings();
$bm = $this->bs->getBookmark($bid);
//now it should be reset to 0
$this->assertEquals(0, $bm['bVoting']);
+ $this->assertEquals(0, $bm['bVotes']);
}
}//class VoteTest extends TestBase