This page last changed on Feb 23, 2012 by jed.wheeler@involver.com.

Overview

Provides the ability to display a poll for users to vote on. For information on customization of the Poll feature block with Javascript, please see the ?sml.tag.Poll page.


Block Attributes

name type required description
name string no Unique name to identify the specific feature instance that you are using in your template code. This is useful if you have multiple instances of the same feature on the same page. Defaults to 'default'.

Context Variables Attributes

name type description
poll.poll_items
Array of poll_item context variables Returns an array of active polls that have been configured.
poll.all_poll_items
Array of poll_item context variables Returns an array (limit 50) of poll items regardless of status.
poll.closed_poll_items
Array of poll_item context variables Returns an array (limit 50) of closed poll items.
poll.total_poll_items
integer
Returns the total poll items regardless of status.
poll.headline
string Returns the headline for the poll.
poll.logged_out_voting
boolean Returns a boolean indicating if logged out voting is supported or not.
poll.action_link_text
string Returns the action link text.
poll.stream_update_prefix
string Returns the stream update prefix.

Configuration

Configuration of the Poll SML feature is very straightforward. The only required fields are the question and answers. To create a new poll click add new poll, and specify your question and answers in the popup box. You may optionally choose to allow logged out users to vote and you may optionally choose an image to display for the poll.


Caching Considerations

No special caching considerations.


The most basic poll configuration is

{% poll name: "mypoll" %}{% endpoll %}

This code will produce the Polls Feature Block default template. As usual, you click "Configure" in the Feature Block list to bring up its control panel.

Displaying the Results of the Poll
(Place this code within your for loop where the current_poll is available.)

<h2>Results</h2>

<p>Total Responses: {{current_poll.total_responses}}</p>

<ul>
    {% for result in current_poll.results %}
        <li>{{result[0]}} - {{result[1]}}</li>
    {% endfor %}
</ul>

If you don't want to use the default submission AJAX, something like the following will be in order:

<script type="text/javascript">
function register_vote(question_id, option_id, option_name) {
	ajax = new Ajax();
	ajax.responseType = Ajax.JSON;
	ajax.requireLogin = false;

	ajax.ondone = function(data) {
	      // do something to show the user that their vote matters
	   };

	ajax.onerror = function(data) {
	      // handle your error
	   };

	var params = {"option":option_id,
	"fb_page_id":{% facebook_page_id %}};

	ajax.post(INVOLVER_HOST + '/polls/respond/' + question_id + '.json', params);

	return false;
}
</script>

Poll with View Results link but otherwise like the default layout:

{% poll %}
    {% for current_poll in poll.poll_items limit: 1 %}
        <div id="poll_results">
            <div id="poll_{{current_poll.id}}_question" class="question">
                <div id="poll-wrapper">
                    <img src="{{ current_poll.image_url }}" alt="">

                    <h3>{{ current_poll.title }}</h3>

                    <form id="poll_{{current_poll.id}}_form">
                        <ul>
                            {% for option in current_poll.options %}
                                <li>
                                    <label>
                                    <input type="radio" name="option" value="{{ forloop.index0 }}" onclick="respond_to_poll({{ current_poll.id }}, {{ forloop.index0 }});">
                                    {{ option }}
                                    </label>
                                </li>
                            {% endfor %}
                        </ul>
                    </form>
                </div><!-- /#poll-wrapper -->
            </div><!-- /.question -->

            <p><a href="#_" onclick="new sml.tag.Poll().showResults({{current_poll.id}});">View Results</a></p>
        </div><!-- /#poll_results -->
    {% endfor %}
{% endpoll %}

Poll with Submit Button and Optional View Results Button:

{% poll %}
  {% for current_poll in poll.poll_items limit: 1 %}
    <script type="text/javascript">
      function submitPoll(pollId) {
        var checkedRadio;
        for (i=0;i<{{current_poll.options.size}};i++) {
          var handler = sml.get('poll_item_' + i);
          if (handler.isChecked()) {
            checkedRadio = i;
          }
        }

        if (checkedRadio == undefined) {
          return false;
        }
        respond_to_poll(pollId, checkedRadio); return false;
      }
    </script>

    <p id="poll_{{ current_poll }}_question">{{ current_poll.title }}</p>

    <ul>
     {% for option in current_poll.options %}
      <li>
        <label>
        <input type="radio" name="option" value="{{ forloop.index0 }}" id="poll_item_{{forloop.index0}}">
        {{ option }}
        </label>
      </li>
     {% endfor %}
    </ul>

    <p>
      <a href="#_" onclick="new sml.tag.Poll().showResults({{current_poll.id}});">View Results</a>
      <input type="button" src="{% editable_image name:'button' src_only:true %}" id="submit" value="Vote" onclick="submitPoll({{current_poll.id}})">
    </p>
  {% endfor %}
{% endpoll %}

FAQ

This feature block currently has no FAQ questions.


Related Topics

For additional information on some related components, please see the following.


Document generated by Confluence on Feb 12, 2013 09:09