Lets say I have 18 choices in a poll. How do I set how many votes/choices the user gets?

For example I want a user to vote for three choices out of the 18 instead of simply voting for one.

Thanks,

Joshua

Issue fork poll-2746713

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

JoshuaF created an issue. See original summary.

JoshuaF’s picture

Issue summary: View changes
berdir’s picture

Category: Support request » Feature request

This is currently not possible, changing to a feature request.

Might not be so easy, needs a new setting to set allowed choices and switch to checkboxes if > 1. And figure out how to store and show.

Open for patches, but we currently have no plans to work on this.

JoshuaF’s picture

Any update on this Berdir?

I would love to upgrade to a newer version of Drupal but none of the polling modules I have found can compare to Advanced Poll from Drupal 7.

- Only show poll results after polls close
- Set a specific date and time to close poll
- multiple choice polls if there is more than one possible answer

Those are a few of the features I've seen that are missing and I require :(

berdir’s picture

Modules don't develop themself, someone needs to work and/or pay for that :)

We currently have no need for any of those features and therefore no plans to work on them, but if someone provides patches, then I'd be OK with including them, assuming the usual things: Test coverage, backwards compatible, configurable as necessary, ...

Feel free to get in touch with us (MD Systems) if you want an estimate to implement those

rasikap’s picture

Assigned: Unassigned » rasikap
rasikap’s picture

StatusFileSize
new4.96 KB

PFA of the patch which supports multiple choice poll.

Thanks,
Rasika P

rasikap’s picture

Assigned: rasikap » Unassigned
naheemsays’s picture

same feature but a different route and without a patch: #3050637: Allow multiple types of polling

That approach may lead to a better architecture, though i am stuck on the preceding step of #3042995: Convert votes into entities.

berdir’s picture

Status: Active » Needs work
Issue tags: +Needs tests
  1. +++ b/poll.install
    @@ -125,3 +126,38 @@ function poll_update_8001() {
    +/**
    + * Add 'answer_type' field to 'poll' entities.
    + */
    +function poll_update_8002() {
    +  // Drop index and primary key and add new serial key.
    +  $schema = \Drupal::database()->schema();
    +  $schema->dropIndex('poll_vote', 'chid');
    +  $schema->dropIndex('poll_vote', 'hostname');
    +  $schema->dropIndex('poll_vote', 'uid');
    +  $schema->dropPrimaryKey('poll_vote');
    +  $serial_key_schema = [
    +    'type' => 'serial',
    +    'unsigned' => TRUE,
    +    'not null' => TRUE,
    +    'description' => "The {users}'s vote id.",
    +  ];
    +
    +  $schema->addField('poll_vote', 'id', $serial_key_schema, ['primary key' => ['id']]);
    

    This looks like a copy of the anonymous vote restrictions issue with these poll_vote changes that don't belong there.

  2. +++ b/src/Entity/Poll.php
    @@ -293,6 +301,23 @@ class Poll extends ContentEntityBase implements PollInterface {
    +    $fields['answer_type'] = BaseFieldDefinition::create('list_string')
    +      ->setLabel(t('Answer Type'))
    +      ->setDescription(t('Choose if user can select single or multiple answers.'))
    +      ->setSetting('allowed_values', [
    +        'single' => t('Single Answer'),
    +        'multiple' => t('Multiple Answers'),
    +      ])
    +      ->setDefaultValue('single')
    

    Wondering if that is flexible enough. That means there is no limit on the number of votes someone can make, what if you want to limit to 2 or so?

    Could possibly be a follow-up.

  3. +++ b/src/Form/PollViewForm.php
    @@ -83,9 +83,11 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
           $options = $this->poll->getOptions();
    +      $answer_type = $this->poll->answer_type->value;
    +      $field_type = $answer_type == 'single' ? 'radios' : 'checkboxes';
           if ($options) {
    

    Probably makes sense to add a method for this, could be $poll->allowMultipleChoices() that returns TRUE/FALSE. Or maybe getAllowedChoicesCount(), that would currently either return 1 or count($choices)?

  4. +++ b/src/Form/PollViewForm.php
    @@ -333,15 +335,26 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
    +    if ($answer_type == 'single') {
    +      $options['chid'] = $chid;
    +      $vote_storage->saveVote($options);
    +    }
    +    else {
    +      $chids = array_filter($chid);
    +      foreach ($chids as $choice) {
    +        $options['chid'] = $choice;
    +        $vote_storage->saveVote($options);
    +      }
    +    }
    

    I fear this isn't quite so easy.

    If you have 3 choices, select all 3 and submit then it will show 33% on each and 3 votes?

    But that's not really the expected result? I'd expect them all to show 100% and 1 vote. Of course that makes this much more complicated, as we need to be able to group multiple vote records together or something like that.

    Either way, this will definitely also need tests.

sushyl’s picture

StatusFileSize
new5.01 KB
new3.31 KB

The last patch is not applying due to some changes in the module, here's the reroll patch.

sushyl’s picture

If you have 3 choices, select all 3 and submit then it will show 33% on each and 3 votes?

But that's not really the expected result? I'd expect them all to show 100% and 1 vote. Of course, that makes this much more complicated, as we need to be able to group multiple vote records together or something like that.

@Berdir, I am not sure about this.
If one user votes for all three choices, since the votes are not weighted based on who submitted them, each of the choices will be considered as individual votes so the 33% result seems fine since everyone will have the same number of votes
(votes for choice x/total submitted votes).

ruslan piskarov’s picture

Hey @sushyl. Thank you for a patch. I have tested #11 and first looking, works well for my cases.

ivnish’s picture

Status: Needs work » Needs review
bramdriesen’s picture

Version: 8.x-1.x-dev » 2.0.x-dev
Status: Needs review » Needs work
Related issues: +#3032307: Rounding error

The rounding issue is being discussed in #3032307: Rounding error so I won't be bothering about that here.

Quite sure this needs a re-roll and preferably on the 2.0.x branch as a issue fork. This also needs tests and will potentially break others.

bramdriesen’s picture

Issue tags: +Needs reroll, +Needs issue fork

prem suthar made their first commit to this issue’s fork.

szloredan’s picture

StatusFileSize
new6.32 KB

Added patch for v2

bramdriesen’s picture

Issue tags: -Needs issue fork

#10 Is also not addressed

bramdriesen’s picture

Issue tags: -Needs reroll

Reworked a bit. Still some work and testing to do. Still needs to implement the limit field (numeric).

Not sure why in my mind I was thinking we needed the ID similar to the anonymous voting.

bramdriesen’s picture

Issue tags: +Needs reroll

Needs a re-roll after the anonymous vote restrictions got added. Probably removes some duplicate code from that issue as well (remark 1 of #10)

alexortega_98’s picture

StatusFileSize
new5.04 KB

Re-roll for 2.0.0-alpha5 version.

janc48’s picture

Any ambition to bring this feature into release? I think it is quite worthy and would be happy to use it.

bramdriesen’s picture

Last time I looked at this was 2 Drupalcons ago :-) but then my focus was also on the anonymous vote restrictions. I can have a look at this again during contribution day.