Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create TopicPolls #6

Open
31 of 40 tasks
leafo opened this issue Jun 3, 2024 · 0 comments
Open
31 of 40 tasks

Create TopicPolls #6

leafo opened this issue Jun 3, 2024 · 0 comments

Comments

@leafo
Copy link
Owner

leafo commented Jun 3, 2024

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 anonymous
  • hide_results (boolean, default: false, optional): Determines if the poll results should be hidden from users even if poll has ended
  • start_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

  1. 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.
  2. 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
  3. Voting on a Poll

    • Users cast their votes by selecting a choice.
    • The vote is recorded in the PollVotes table.
    • The vote_count in the PollChoices table is incremented if counted is true.
  4. Poll Duration Expiry

    • After the poll's end_date no more votes are allowed
    • The results are displayed on the topic upage

Implementation

  • Database Migration

    • Create migration files for TopicPolls, PollChoices, and PollVotes.
  • Model Creation

    • Create TopicPolls model.
    • Create PollChoices model.
    • Create PollVotes model.
    • Implement relations in all models
      • Topics: has_one polls
      • TopicPolls: has_many poll_choices
      • TopicPolls: belongs_to topics
      • PollChoices: belongs_to topic_polls
      • PollChoices: has_many poll_votes
      • PollVotes: belongs_to poll_choice
      • PollVotes: belongs_to user
    • Implement methods on models
      • TopicPolls:
        • is_open: Can the poll currently be voted on?
        • total_vote_count: Calculate the total number of votes for the poll
        • allowed_to_edit: Check if a user is allowed to edit the poll
        • allowed_to_vote: Check if a user is allowed to vote in the poll
        • name_for_display: Get the display name for the poll
      • PollChoices:
        • recount: Recalculate the vote count for the choice
        • delete: Delete the poll choice and its associated votes
        • vote: Set the vote for the user, aware of vote_type for the poll
      • PollVotes:
        • set_counted: Toggle the counted status of a vote, and updating the total_votes if necessary
        • create: Insert a new vote with increment if necessary
        • delete: Remove a vote and decrement the vote count if necessary
  • 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.
      • Implement validate_params method for input validation.
      • Implement set_choices method to manage poll choices (create, update, delete).
  • Testing

    • Write models unit tests
    • Write flow unit test

Additional Considerations

  • Add controls to set the counted field based on who is voting to prevent spam/abuse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant