aboutsummaryrefslogtreecommitdiff
path: root/src/SemanticScuttle/Service/Vote.php
blob: 6db940cc9073220e43024665e0c0f163351f5846 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
 * SemanticScuttle - your social bookmark manager.
 *
 * PHP version 5.
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @license  GPL http://www.gnu.org/licenses/gpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */

/**
 * 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.
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author Christian Weiske <cweiske@cweiske.de>
 * @license  GPL http://www.gnu.org/licenses/gpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */
class SemanticScuttle_Service_Vote extends SemanticScuttle_DbService
{
    /**
     * 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.
     *
     * @internal
     * Uses the "votes" table to retrieve the votes, which
     * has high costs. It is more efficient to get the sum of
     * all votes for a bookmark from the bookmarks table,
     * field bVoting.
     *
     * @param integer $bookmark Bookmark ID
     *
     * @return integer Vote (can be positive, 0 or negative)
     */
    public function getVoting($bookmark)
    {
        $query = 'SELECT SUM(vote) as sum FROM ' . $this->getTableName()
            . ' WHERE bid = "' . $this->db->sql_escape($bookmark) . '"';

        if (!($dbres = $this->db->sql_query_limit($query, 1, 0))) {
            message_die(
                GENERAL_ERROR, 'Could not get voting',
                '', __LINE__, __FILE__, $query, $this->db
            );
            //FIXME: throw exception
            return false;
        }

        $row = $this->db->sql_fetchrow($dbres);
        $this->db->sql_freeresult($dbres);

        return (int)$row['sum'];
    }//public function getVoting(..)



    /**
     * 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)
    {
        $query = 'SELECT COUNT(vote) as count FROM '
            . $this->getTableName()
            . ' WHERE bid = "' . $this->db->sql_escape($bookmark) . '"';

        if (!($dbres = $this->db->sql_query_limit($query, 1, 0))) {
            message_die(
                GENERAL_ERROR, 'Could not get vote count',
                '', __LINE__, __FILE__, $query, $this->db
            );
            //FIXME: throw exception
            return false;
        }

        $row = $this->db->sql_fetchrow($dbres);
        $this->db->sql_freeresult($dbres);

        return (int)$row['count'];
    }



    /**
     * 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)
    {
        $query = 'SELECT COUNT(vote) as count FROM '
            . $this->getTableName()
            . ' WHERE'
            . ' bid = "' . $this->db->sql_escape($bookmark) . '"'
            . ' AND uid = "' . $this->db->sql_escape($user) . '"';

        if (!($dbres = $this->db->sql_query_limit($query, 1, 0))) {
            message_die(
                GENERAL_ERROR, 'Could not get vote count',
                '', __LINE__, __FILE__, $query, $this->db
            );
            //FIXME: throw exception
            return null;
        }

        $row = $this->db->sql_fetchrow($dbres);
        $this->db->sql_freeresult($dbres);

        return (int)$row['count'] == 1;
    }



    /**
     * 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, null when not voted.
     */
    public function getVote($bookmark, $user)
    {
        $query = 'SELECT vote FROM ' . $this->getTableName()
            . ' WHERE'
            . ' bid = "' . $this->db->sql_escape($bookmark) . '"'
            . ' AND uid = "' . $this->db->sql_escape($user) . '"';

        if (!($dbres = $this->db->sql_query_limit($query, 1, 0))) {
            message_die(
                GENERAL_ERROR, 'Could not get vote count',
                '', __LINE__, __FILE__, $query, $this->db
            );
            //FIXME: throw exception
            return false;
        }

        $row = $this->db->sql_fetchrow($dbres);
        $this->db->sql_freeresult($dbres);

        if (!$row) {
            return null;
        }

        return $row['vote'];
    }



    /**
     * 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)
    {
        if ($GLOBALS['enableVoting'] == false) {
            return false;
        }

        if ($this->hasVoted($bookmark, $user)) {
            return false;
        }

        if ($vote != -1 && $vote != 1) {
            return false;
        }

        $res = $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($res);

        //update bookmark table
        $bm  = SemanticScuttle_Service_Factory::get('Bookmark');
        $res = $this->db->sql_query(
            'UPDATE ' . $bm->getTableName()
            . ' SET bVoting = bVoting + ' . (int)$vote
        );
        $this->db->sql_freeresult($res);

        return true;
    }



    /**
     * Re-calculates all votings for all bookmarks
     * and updates the voting values in the bookmarks
     * table.
     *
     * @return void
     */
    public function rewriteVotings()
    {
        throw new Exception('Not implemented yet');
        //FIXME
        //SELECT bid, SUM( vote ) FROM sc_votes GROUP BY bid
    }



    /**
     * Delete all votes from the database table.
     * Used in unit tests.
     *
     * @return void
     */
    public function deleteAll()
    {
        $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`';
        $this->db->sql_query($query);
    }


}

?>