Polls Plugin
- In this chapter:
- Frontend/Template Engine
-
Poll plugin
Plugin Author: Sebastian Goebel
Date: September, 2008Description: The poll plugin allows users to vote on a topic and then to display the results. From the administrative interface, authorized users can create and delete new polls, change the questions , and add and edit new possible answers.
The polls plugin modifies the templating language in four parts:- poll (17 properties)
- poll answer (8 properties)
- list of polls
- list of poll answers
Each of these is discussed in the next few pages.
The general poll object properties are used in your templates for managing such things such as the language the poll will use, the overall number of votes, and the title of the poll.
Frontend/Template Engine
- In this article:
- Examples of templates using Polls plugin objects
- Poll object properties
- Poll Answer - object properties
- List of Polls - object properties
- List of Poll Answers - object properties
Examples of templates using Polls plugin objects
It's usually best to see an example of what it is we're talking about, so here is a sample template that makes use of the polls plugin. This template is called section-polls.tpl. Feel free to copy this and use it as a base for your own polls implementation:
{{ if $included }}
<style>
.poll_bar {
border:1px solid #000;
background-color: #dfdfdf;
height: 20px;
vertical-align: center;
float: left;
}
</style>
<script>
function play(url)
{
var tag;
if (navigator.appName=="Microsoft Internet Explorer") {
tag = '<bgsound src="../'+url+'" loop="false">';
} else {
tag = '<embed src="../'+url+'" hidden="true" border="0" width="0" height="0" autostart="true" loop="false">';
}
$('player_div').innerHTML = tag;
}
function stop()
{
$('player_div').innerHTML = '';
}
</script>
<div {{ /if }}
{{ poll_form template='poll-form.tpl' submit_button=false ajax=true }}
{{ $campsite->poll->title }}<br>
{{*
Question: {{ $campsite->poll->question }}<br>
Voting Begin: {{ $campsite->poll->date_begin|date_format }}<br>
Voting End: {{ $campsite->poll->date_end|date_format }}<br>
*}}
Votes: {{ $campsite->poll->votes }}<br>
<div
{{ list_poll_answers order="byvalue desc" }}
{{ pollanswer_ajax }}
<div <div {{ $campsite->pollanswer->answer }}
({{ $campsite->pollanswer->percentage|string_format:"%d" }})%
</div>
{{ /pollanswer_ajax }}
<div {{ list_pollanswer_attachments }}
{{ if $campsite->attachment->mime_type|substr:0:5 == 'audio' }}
<a href="javascript: void(0);" onClick="play('{{ uri options="articleattachment" }}')">
<img src="..//css/is_shown.png" border="0">
</a>
<a href="javascript: void(0);" onClick="stop()">
<img src="..//css/unlink.png" border="0">
</a>
{{ /if }}
{{ /list_pollanswer_attachments }}
<div Give a note:
{{ section name=foo start=1 loop=6 }}
{{ pollanswer_ajax value=$smarty.section.foo.index }}{{ $smarty.section.foo.index }}{{ /pollanswer_ajax }}
{{ /section }}
<br>
{{ $campsite->pollanswer->votes }} votes |
Ø{{ $campsite->pollanswer->average_value|string_format:"%.1f" }} |
sum: {{ $campsite->pollanswer->value }}
<div {{ /list_poll_answers }}
{{ /poll_form }}
{{ if $included }}
</div>
<div id="player_div"></div>
{{ /if }}
The next example of a template is the section page with the poll included. This template is called poll-form.tpl:
{{ include file="html_header.tpl" }}
<table <tr>
<td valign="top">
<div id="breadcrumb">
{{ breadcrumb }}
</div>
{{** main content area **}}
<table <tr>
<td>
{{ if $campsite->poll->defined }}
<h3>Poll Details</h3>
{{ include file='poll-form.tpl' included=true}}
<br>
<a href="{{ uri options="template section-polls.tpl" }}">All Polls</a>
{{ else }}
<p>
<tr>
<th align="left">Name</th>
<th>Voting Begin</th>
<th>Voting End</th>
<th>Current</th>
<th>Allowed/Votes Cast</th>
<th>Votes</th>
</tr>
<tr><td colspan="6"><hr></td></tr>
{{ local }}
{{ list_polls name="polls_list" length="10" order='bylastmodified ASC' }}
<tr align="center">
<td align="left">
<a href="{{ uri options="template section-polls.tpl" }}&f_poll_nr={{ $campsite->poll->number }}&f_poll_language_id={{ $campsite->poll->language_id }}">
{{ $campsite->poll->name }}
</a>
</td>
<td>{{ $campsite->poll->date_begin|date_format }}</td>
<td>{{ $campsite->poll->date_end|date_format }}</td>
<td>{{ if $campsite->poll->is_current }} Y {{ else }} N {{ /if }}</td>
<td>{{ $campsite->poll->votes_per_user }}/{{ $campsite->poll->user_vote_count }}</td>
<td>{{ $campsite->poll->votes }}
</tr>
{{ if $campsite->current_list->at_end }}
<tr><td colspan="6"><hr></td></tr>
<tr>
<td>{{ $campsite->current_list->count }} Items</td>
<td colspan="5">
{{ if $campsite->current_list->has_previous }}
<a href="{{ uripath }}?p_polls_list_start={{ $campsite->current_list->previous }}">previous</a>
{{ else }}
previous
{{ /if }}
|
{{ if $campsite->current_list->has_next }}
<a href="{{ uripath }}?p_polls_list_start={{ $campsite->current_list->next }}">next</a>
{{ else }}
next
{{ /if }}
</td>
</tr>
{{ /if }}
{{ /list_polls }}
{{ /local }}
{{ /if }}
</td>
</tr>
</table>
{{** end main content area **}}
</td>
<td valign="top">
{{ include file="html_rightbar.tpl" }}
</td>
</tr>
</table>
{{ include file="html_footer.tpl" }}
Poll object properties
Poll object properties
The poll object has following properties:
- number: the internal number of the poll
- language_id: the language identifier
- defined: indicates if the poll (defined by number and language) exists in database
- title: the title for the poll
- name: synonym for title
- question: the question (or theme) of the poll
- date_begin: date when voting starts
- date_end: date when voting ends
- answers: number of different answers
- is_current: is the poll within the voting period
- votes_per_user: how often can a single user vote (mostly 1)
- user_vote_count: how often the current user voted already (stored in session and cookie, so it can be lost)
- votes: how many votes were made
- is_votable: can the poll voted by this user. must be within voting period, and votes made by current user do not excite number of allowed votes
- votes_overall: how many votes for all translations of this poll were made
- percentage_overall: percentage votes of this poll in relation to all translations of this poll
- last_modified: timestamp of last modification throught the admin
Poll Answer - object properties
The poll answer object has following properties:
- poll_nr: number of the correspondending poll
- language_id: language identifier for this answer and correspondending poll
- number: the number of this answer
- answer: the answer text to vote for
- votes: how many votes were given for this answer
- percentage: the percentage of votes for this answer, in relation to all answers of this poll
- percentage_overall: the percentage of votes for this answer, in relation to all answers of all translations of this poll
- last_modified: last modification throught the admin
List of Polls - object properties
Purpose:
Select the list of polls according to the given constraints and current environmental variables. The code between "{{ list_polls }}" statement and "{{ /list_polls }}" is repeated for every poll in the list.
Syntax:
{{ list_polls [length="<integer_value>"] [columns="<integer_value>"]
[constraints="<list_of_poll_constraints>"]
[item="<item_constraint>"]
[order="<order_condition>"]>
}}
<list_of_instructions>
{{ /list_polls }}
where:
- length="<integer_value>": <integer_value> specifies list_length and forces the list to have at most list_length items. If the list contains more items than list_length items the ones not fitting in can be listed using If NextItems/PreviousItems statements.
- columns="<integer_value>": <integer_value> specifies columns_number and sets an environment variable. This is incremented as if the items would be placed in a table cell. The counting starts from one and the variable is incremented for every new element. When it reaches the maximum value it is reset to one. This is very useful in building tables of data. For details see If List.
- item="<item_constraint>": <item_constraint> sprecifies the item where the polls have to been assigned to. This can be publication, issue, section or article. The assignment can be done in admin interface on the proper item edit/properties page. The
| <list_of_poll_constraints>= | [<poll_constraint>] <list_of_poll_constraints> | <poll_constraint> |
| <poll_constraint>= | number <integer_operator> <integer_value> | language_id <integer_operator> <integer_value> | parent_poll_nr <integer_operator> <integer_value> | is_extended <switch_operator> <switch_value> | name <string_operator> <string_value> | votes <integer_operator> <integer_value> | votes_overall <integer_operator> <integer_value> | is_current <boolean_operator> <boolean_value> | <date_constraint> |
<date_constraint>=
|
begin <date_operator> <date_value> |
| <item_constraint>= | publication | issue | section | article |
| <order_condition>= | bynumber desc|asc | bylanguage | bytitle | byname | byquestion | bybegin | byend | byanswers | byvotes | byvotes_overall | bypercentage_overall | bylastmodified |
List of Poll Answers - object propertiesPurpose: Select the list of poll answers according to the current poll. The code between "{{ list_pollanswers }}" statement and "{{ /list_pollanswers }}" is repeated for every interview in the list. Syntax:
{{ list_pollanswers [length="<integer_value>"] [columns="<integer_value>"]
[constraints="<list_of_interview_constraints>"]
[order="<order_condition>"]>
}} <list_of_instructions>
{{ /list_pollanswers }}
where:
<list_of_instructions> may contain any statement except: "set_interview".
|
||||||||||||||||||||





