Forums / Developer / Polls - Fetch to display User's Vote Value?

Polls - Fetch to display User's Vote Value?

Author Message

Paul Wilson

Saturday 18 February 2006 1:57:05 am

Hi,

<b>Question:</b> Has anyone created a fetch function to obtain the object/value of a user's poll response? Any clues or suggestions on ways forward gratefully accepted!

<b>Situation:</b> I want to adapt the standard EZ Publish Poll functions to show the vote that a (registered) user has made. Why? Because I want to allow users to revisit old polls and see how they voted. This might mean adding something like "your vote was for [option label]"

<b>Background:</b>
The standard EZ Publish Poll functions allow a user to enter their vote, and then a simple chart of the Poll's results is displayed. This is assisted by the "collected_info_count" and "collected_info_count_list" functions, but even though there is a "user_identifier" for ezinformationcollection objects, I can't see an easy means for accessing this value.

Thanks.

Paul Wilson

Saturday 18 February 2006 9:27:21 pm

Looking at EZP 3.7.2 or 3, fetch functions seem to be defined in:

<b>/kernel/content/ezcontentfunctioncollection.php</b>

And this refers to:

<b>/kernel/classes/ezinformationcollection.php</b>

This file contains a function called <b>"fetchByUserIdentifier"</b> which "fetches the information collection by user identifier" (line 419). But this seems to be internal to EZ Publish or not documented. Perhaps there is a way to access this or a completely different method somewhere.

Paul Wilson

Monday 20 February 2006 6:46:37 am

In case it is of use to anyone, here's where I got to...

<b>A Solution</b>
It isn't particularly pretty, but the code below shows a solution I put together. NB: I modified the poll class to include an additional information collector text-line for "user_id" - I realise this seems to be already stored somehow, but I couldn't figure out how to access it.

<b>Overview</b>
I added this code to the poll_result.tpl file so that the (registered) user's response to a Poll is can be displayed in the template. This code only identifies that response and displays it.

The code searches through collected_information attributes for ones that match the Poll attributes (question, and the "user_id" attribute I added). This is compared to the object_id for the Poll and the current user's id. If there is a match, then the user's response is stored in the $user_result variable.

The code follows ... in unrefined form. (Using EZP 3.7.2)

{def $user=fetch( 'user', 'current_user' )}
{let $user_id=$user.contentobject_id}
{def 	$total_counted=0
	$nullresult='F'	
	$current_collected_item=1
	$current_collected_user=$nullresult
	$current_collected_option=$nullresult
	$user_result=$nullresult
	$all_counted=0
	$my_total_count=fetch( 'content', 'collected_info_count', hash( 'object_id', $object.id ))
	$attribute_origin_object_id=""
	$collection=""
	$search_finished='FALSE'}

{while ne($search_finished,'TRUE')}
	{set $collection=fetch( 'content', 'collected_info_collection',
                        hash( 'collection_id',  $current_collected_item,
                              'contentobject_id', $object.id ))}
	{if ne($collection, FALSE)}
	{foreach $collection.attributes as $attribute}
		{if eq($attribute.contentobject_attribute.is_a, 'ezstring')}
			{set $current_collected_user=$attribute.data_text}
		{/if}
		{if eq($attribute.contentobject_attribute.is_a, 'ezoption')}
			{set $current_collected_option= $attribute.contentobject_attribute.content.option_list[$:attribute.data_int].value}
		{/if}
		{set $attribute_origin_object_id= $attribute.contentobject_attribute.contentobject_id}
	{/foreach}
	{section validobjecttype=eq($object.id, $attribute_origin_object_id)}
		{if eq($current_collected_user, $user_id)}
			{set $user_result=$current_collected_option}
		{/if}
		{* set $total_counted=inc($total_counted) *}
	{/section}
	{/if}
	{set $current_collected_item=inc($current_collected_item)}
	{if ne($user_result, $nullresult)}
		{set $search_finished='TRUE'}
	{/if}
{/while}
YOUR POLL SELECTION: {$user_result} <br />