Yii 1.1: poll

Allows you to create custom polls for both anonymous and authenticated users to vote on.
30 followers

The Poll extension for the Yii framework allows you to create custom polls for both anonymous and authenticated users to vote on.

Features include:

  • Basic Gii-created CRUD functionality
  • Portlet to load on additional pages, which displays either the latest or a user-specified poll
  • Polls can be either open or closed to voting
  • IP restricted voting for anonymous users (enabled by default)
  • Authenticated users can cancel their votes on open polls
  • If IP restriction is enabled, you can also configure the ability for anonymous users to cancel their votes (disabled by default)
  • Users must vote on a poll in order to see the results (enabled by default)

Requirements

This has been tested with 1.1.8, but should work with any version.

You must also:

  • Setup a User active record with an integer ID returned by Yii::app()->user->id.*
  • Add an anonymous user in the database with ID 0 (and either set the User ID to default to 0 in the database, or in your UserIdentity file)

*An example of this type of setup is explained here: Building a Blog System using Yii

The next step is to install the database schema located in '/data/poll.sql', modifying the table prefixes as needed, as well as the foreign key constraint referencing the user's ID.

Configuration

return array(
   ...
   'import' => array(
     'application.modules.poll.models.*',
     'application.modules.poll.components.*',
   ),
   'modules' => array(
     'poll' => array(
       // Force users to vote before seeing results
       'forceVote' => TRUE,
       // Restrict anonymous votes by IP address,
       // otherwise it's tied only to user_id 
       'ipRestrict' => TRUE,
       // Allow guests to cancel their votes
       // if ipRestrict is enabled
       'allowGuestCancel' => FALSE,
     ),
   ),
);

Usage

The Poll extension has the basic Gii-created CRUD functionality, as well as a portlet to load elsewhere.

To load the latest poll:

$this->widget('EPoll');

To load a specific poll:

$this->widget('EPoll', array('poll_id' => 1));

Changes

February 9, 2012

  • 0.9.2b
    • Duplicate widget submission bug fix
  • 0.9.2
    • Added ability to export poll results
    • Rewrote URLs

October 18, 2011

  • 0.9.1b
    • Bug fixes, renamed controllers to lowercase for URL mapping
  • 0.9.1
    • Moved configuration from app to module

October 17, 2011

  • 0.9.0
    • Initial release

Resources

Total 11 comments

#15329 report it
jwerner at 2013/10/29 03:41pm
Problems with IP Restriction / IPv6 Addresses

Using the provided SQL schema, stopping a user to vote more than once if he has an IPv6 address fails.

The reason is that the ip address field in the table poll_vote is too small:

CREATE TABLE `poll_vote` (
  ...
  `ip_address` varchar(16) NOT NULL DEFAULT '',

So IPv6 addresses get truncated to 16 chars.

For IPv6 addresses, it should be varchar(50) or so (tbd).

The test Poll model/userCanVote() fails:

// Add IP restricted attributes if needed
    if (Yii::app()->getModule('poll')->ipRestrict === TRUE && Yii::app()->user->isGuest) {
      $where[] = 'ip_address=:ip_address';
      $params[':ip_address'] = $_SERVER['REMOTE_ADDR']; // <=======
    }

Also the rule in the PollVote model needs to be changed:

public function rules()
  {
    return array(
      ...
      array('ip_address', 'length', 'max'=>50), // was: max=>16
    );
  }

Regards, Joachim

#10592 report it
Douglas Lopez at 2012/11/06 08:56am
The suggestion does not work but I figured out how to solve it

Thanks for your suggestion to include 'schemaCachingDuration' => 0, at db'=>array( .....

but it did not work

However, I deleted the braces at public function tableName() { return 'poll'; } and it works.

Now, I have another problem: I can see anything in my view at

<?php $this->widget('EPoll'); ?>

I double checked all steps you mentioned but nothing is shown. Any suggestion? Thanks your help Best regards, Douglas

#10585 report it
Wiseon3 at 2012/11/06 03:57am
Re: Poll Error - Table poll can not be found

@Douglas Lopez You probably enabled database schema caching, in your config. To fix it, modify it so it refreshes the cache and then put it how it was

'components'=>array(
        ......
        'db'=>array(
             ......
            'schemaCachingDuration' => 0,
             ......
        ),
    ),
#10575 report it
Douglas Lopez at 2012/11/05 04:22pm
Poll Error - Table poll can not be found

I am trying to implement the Poll extension in my application.

After creating the tables: poll, poll_choice and poll_vote (in postgres, after the corresponding conversion from mysql ), and include in config/main.php

    'ext.poll.models.*',
    'ext.poll.components.*',

and .... 'modules'=>array( ...

    'poll' => array(
           // Force users to vote before seeing results
           'forceVote' => TRUE,
           // Restrict anonymous votes by IP address,
           // otherwise it's tied only to user_id 
           'ipRestrict' => TRUE,
           // Allow guests to cancel their votes
           // if ipRestrict is enabled
           'allowGuestCancel' => FALSE,
         ),

I did the generation of the corresponging models for these 3 tables. and after including, per your instruction, in my view, the following statement: <?php $this->widget('EPoll'); ?>

The system produced the following error:

Poll table "{{poll}}" defined in the active record class "Poll" can not be found in the data base. I did some sql statements by using pgadmin and tables looks good.

What Did I do wrong? Please your help is appreciated. Best regards, Douglas López

#10017 report it
NCS_One at 2012/09/30 12:34am
Question.

I also would like to know what you mean by:

"Add an anonymous user in the database with ID 0 (and either set the User ID to default to 0 in the database, or in your UserIdentity file)"

#9532 report it
Eduardo Leiva at 2012/08/21 02:44am
Two tips:

1) If you cant create a poll try:

Http: //foo.com/poll/poll/create

2) If you get:

The Table "{{poll}}" For Active Record Class "poll" Set In Config File:

db => array(
'tablePrefix' => ''
...
...
}

:D

#7041 report it
bit0123 at 2012/02/20 05:12pm
Understanding problem

Good work. But What have you tried to say by:

  1. Setup a User active record with an integer ID returned by Yii::app()->user->id.*
  2. Add an anonymous user in the database with ID 0 (and either set the User ID to default to 0 in the database, or in your UserIdentity file)

Please help. I am in problem.

#5562 report it
imehesz at 2011/10/20 05:51pm
WANT IT!

No man, WE WANT THIS NOW :)

#5535 report it
Matt Kelliher at 2011/10/18 10:26am
Good Call

That is a good call on the configuration, I'll update the code and also put up a demo on my server when I get a minute.

#5522 report it
dckurushin at 2011/10/18 04:42am
good idea to add some picture

Hi... if you could add some picture of the poll...

Or create demo at some free hosting...

#5515 report it
Asgaroth at 2011/10/18 12:54am
good

Looks nice but why would you put configuration in the app params and not directly in the module configuration?

Leave a comment

Please to leave your comment.

Create extension