lapis-community
lapis-community copied to clipboard
Create TopicPolls
Feature Porposal: Add Polls to forum topics
Summary
This feature request aims to add the ability to create and manage polls within the web forum. The feature will include models for polls, poll choices, and poll votes. Polls will have a question and duration, poll choices will cache the number of votes, and poll votes will have a 'counted' column to control if the vote should be included in the scoring.
Polls can be configured:
- vote_type: Allows users to vote on a single choice or multiple choices
- anonymous: Control if results display the voter's name or not
Proposed Models
TopicPolls
id(integer, primary key)topic_id(integer, foreign key)poll_question(string, required)description(text, optional)anonymous(boolean, default: true): Determines if the poll votes should be anonymoushide_results(boolean, default: false, optional): Determines if the poll results should be hidden from users even if poll has endedstart_date(datetime, default: now, required)end_date(datetime, required)created_at(datetime)updated_at(datetime)vote_type(enum, values: ['single', 'multiple'], default: 'single'): Determines if users can vote on a single choice or multiple choices
PollChoices
id(integer, primary key)poll_id(integer, foreign key to TopicPolls, required)choice_text(string, required)description(text, optional)vote_count(integer, default: 0)created_at(datetime)updated_at(datetime)position(integer, required)
PollVotes
id(integer, primary key)poll_choice_id(integer, foreign key to PollChoices, required)user_id(integer, foreign key to Users, required)counted(boolean, default: true)created_at(datetime)updated_at(datetime)
Typical Poll Operation
-
Creating a Poll
- User optionally creates a new poll with a question and duration during the creation of a topic.
- User adds multiple choices for the poll.
-
Editing a Poll
- User can edit the question, choices and duration of a poll.
- Editing and deleting a poll option should be responded.
- Deleting poll option should delete an existing votes
-
Voting on a Poll
- Users cast their votes by selecting a choice.
- The vote is recorded in the PollVotes table.
- The
vote_countin the PollChoices table is incremented ifcountedis true.
-
Poll Duration Expiry
- After the poll's
end_dateno more votes are allowed - The results are displayed on the topic upage
- After the poll's
Implementation
-
[x] Database Migration
- [x] Create migration files for TopicPolls, PollChoices, and PollVotes.
-
[x] Model Creation
- [x] Create TopicPolls model.
- [x] Create PollChoices model.
- [x] Create PollVotes model.
- [x] Implement relations in all models
- [x] Topics:
has_one polls - [x] TopicPolls:
has_many poll_choices - [x] TopicPolls:
belongs_to topics - [x] PollChoices:
belongs_to topic_polls - [x] PollChoices:
has_many poll_votes - [x] PollVotes:
belongs_to poll_choice - [x] PollVotes:
belongs_to user
- [x] Topics:
- [x] Implement methods on models
- [x] TopicPolls:
- [x]
is_open: Can the poll currently be voted on? - [x]
total_vote_count: Calculate the total number of votes for the poll - [x]
allowed_to_edit: Check if a user is allowed to edit the poll - [x]
allowed_to_vote: Check if a user is allowed to vote in the poll - [x]
name_for_display: Get the display name for the poll
- [x]
- [x] PollChoices:
- [x]
recount: Recalculate the vote count for the choice - [x]
delete: Delete the poll choice and its associated votes - [x]
vote: Set the vote for the user, aware of vote_type for the poll
- [x]
- [x] PollVotes:
- [x]
set_counted: Toggle thecountedstatus of a vote, and updating thetotal_votesif necessary - [x]
create: Insert a new vote with increment if necessary - [x]
delete: Remove a vote and decrement the vote count if necessary
- [x]
- [x] TopicPolls:
-
[ ] Flows and Actions
- [ ] Update topic creation flow to include poll creation
- [ ] Create logic for poll creation.
- [ ]
TopicPollsFlow- [ ] Implement poll voting logic
- [ ] Create logic for editing a poll.
- [x] Implement
validate_paramsmethod for input validation. - [x] Implement
set_choicesmethod to manage poll choices (create, update, delete).
- [ ] Update topic creation flow to include poll creation
-
[ ] Testing
- [ ] Write models unit tests
- [ ] Write flow unit test
Additional Considerations
- Add controls to set the
countedfield based on who is voting to prevent spam/abuse