Skip to content

phalkunz/silverstripe-polls

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Polls module

Maintainer

Saophalkun Ponlu

Mateusz Uzdowski

Requirements

SilverStripe 2.4.x

Installation

  1. Include the module folder in your project root folder and rename it to "polls"
  2. Rebuild database schema (dev/build?flush=1)

Features

  • Each visitor, determined by browser cookie, can only vote once
  • Uses Google chart API
  • Supports single and multiple-choice polls

Usage

CMS usage

  1. Log in the CMS
  2. Go to the Poll section
  3. Create a poll, press Add, then add a few poll options
  4. The further steps depend on how the PollForm has been implemented

Embed and configure the PollForm

The PollForm knows how to render itself, and is able to render both the selection form and the chart, depending on the value detected in the cookie. To embed the PollForm you can create it through your SiteTree object.

class Page extends SiteTree {
	static $has_one = array(
		'Poll' => 'Poll'
	);

	...

	function PollForm() {
		$pollForm = new PollForm($this, 'PollForm', $this->Poll());	
		// Customise some options
		$pollForm->setChartOption('height', 300);
		$pollForm->setChartOption('width', 300);
		$pollForm->setChartOption('colours', array('FF0000', '00FF00'));
		return $pollForm;
	}

	...
}

To customise the chart even more, you can subclass the PollForm and redefine the getChart method. If you want to make a site-wide changes, you can use a decorator as well. For example the following will give you a text-only rendering of results.

class PollFormDecorator extends DataObjectDecorator {
	function replaceChart() {
		$choices = $this->owner->Poll()->Choices('', '"Order" ASC');

		$results = array();
		if ($choices) foreach($choices as $choice) {
			$results[] = "{$choice->Title}: {$choice->Votes}";
		}

		return implode($results, '<br/>');
	}
}

Object::add_extension('PollForm', 'PollFormDecorator');

Then, if you need to modify the form and chart template, have a look at PollForm.ss. Here is the default setup.

<% if Poll.Visible %>
	<h3>$Poll.Title</h3>
	<% if Image %>
		$Poll.Image.ResizedImage(300,200)
	<% end_if %>
	<% if Description %>
		$Poll.Description
	<% end_if %>

	<% if Poll.isVoted %>
		$Chart
	<% else %>
		$DefaultForm
	<% end_if %>
<% end_if %>

About

New poll module by Phalkunz

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 94.7%
  • Scheme 5.3%