summaryrefslogtreecommitdiff
path: root/src/SemanticScuttle/Service/Vote.php
blob: e348512940215604716eea1411f253f4d1c1e972 (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
<?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
    }


}

?>