How do I need to create a dot system for users like SO and yahoo answers? (PHP)
I am creating a voting system for a Q&A project where if a user asks a question he / she will lose -5 points; answer question +5, vote question +1, etc. (kind of like SO and yahoo answers)
-> To create basic arithmetic, I have a "users_points" table that links the user_id and their total scores.
+---+---------+
| 1 | 100 |
+---+---------+
| 2 | 54 |
+---+---------+
-> Basically, if users are doing a specific task, it will be + or - dots. How to prevent users from responding to a vote 100 times. ex: I want the user to be able to vote only once per question, etc.
a source to share
You must create the table votes, which has columns: user_id, question_id, delta
. Delta is the value of the vote, which should be 1 or -1 (this way you can just do SUM(delta)
to find the value of the question question).
To get unique, create a unique index on (user_id, question_id).
a source to share
I would keep a detailed log of what the user has voted on. Thus, you can have a table with these columns:
-
user_id
(user ID) -
item_type
(either "question" or "answer" - use ENUM or foreign key for element type table) -
item_id
(actual item id like question id or answer id) -
up_down
(ENUM or BOOLEAN representing whether it was top or bottom).
To calculate the user "rep", your application logic will go through votes and add +5 if the question was voted, -1 if the answer was voted, etc.
You would probably store the aggregate values in the users / questions / answers tables for easy access. But the big advantage of keeping the log in the background is that you can recalculate your reputation regularly and you don't lose anything. You can also indicate which users have voted on what happens on the fly (for example SO shows which answers you prefer to orange when you come back to the question later).
Or just use Stack Exchange , it would be easier;)
a source to share
