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
good
Looks nice but why would you put configuration in the app params and not directly in the module configuration?
Understanding problem
Good work. But
What have you tried to say by:
Please help. I am in problem.
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
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)"
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
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, ...... ), ),
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
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:
[sql] 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
Do you have yii2 version ?
Do you have, or, are you planning to create yii2 version of this extension ?
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.