This site has been archived and you can no longer log in or post new messages. For up-to-date community resources please visit ezplatform.com

eZ Community » Forums » Extensions » recently rated objects
expandshrink

recently rated objects

recently rated objects

Thursday 06 October 2011 11:55:58 pm - 6 replies

Hello,

I am working on a site that uses the ezstarrating extension. We have a news ticker that runs on the homepage that shows most recent activity -- when a new article is published, when a comment is posted on an article, when a new member joins. The editors have asked whether star rating can be added to this ticker as well, so it might say something like "Jan just gave Article Name a 4 star rating". Is this possible?

The other ones are easy, because I can do a simple list fetch function sorted by publish date. I can't see how to get started doing something similar with the star rating datatype. If anyone can point me in the right direction, I'd really appreciate it. Thank you!

Friday 07 October 2011 2:13:52 am

Hello Lyzy,

Welcome to the eZ Community!

I'm in love with this feature idea!

I've not used ezstarrating enough recently to speak with much authority here

but I like this idea so much I took a look at the extension.

I'm not sure if this would work for your needs but I noticed this method, 'fetchNodeByRating' which seemed to support a number of various content fetching features

https://github.com/ezsystems/ezstarrating/blob/master/packages/ezstarrating_extension/ezextension/ezstarrating/classes/ezsrratingobject.php#LC229

Mentioned in the CHANGELOG, 

https://github.com/ezsystems/ezstarrating/blob/master/packages/ezstarrating_extension/ezextension/ezstarrating/CHANGELOG#LC54

Which made me wonder if these could be potential starting points. Like using the 'fetch_by_starrating' operator to fetch content within your templates? This operator uses the PHP class method ezsrRatingObject::fetchNodeByRating

https://github.com/ezsystems/ezstarrating/blob/master/packages/ezstarrating_extension/ezextension/ezstarrating/autoloads/ezsrtemplateoperators.php#LC45

If the above tools do not help you reach your goal, you may need to extend them further (go pull requests!) or create your own custom tools. I hope this helps ...

 

Cheers,

Heath

Friday 07 October 2011 4:47:37 am

Yes, here's the code, I'm not using i18n features, it's almost ready to use, I think, I've just tested and it works:

{foreach fetch_starrating_data(hash('sort_by', array('created_at', false()), 'limit',10)) as $rate}
        {def $userObj=fetch('content', 'object',hash('object_id', $rate.user_id))
             $article=fetch('content', 'object',hash('object_id', $rate.contentobject_id))}
        {$userObj.name} just gave '{$article.name}' {$rate.rating} star rating.<br />
{/foreach}

It was very, very simple to learn about that, I first read ezsrTemplateOperators, so I found all ez star rating operators, then I navigated to ezsrRatingDataObject::fetchByConds, so I analised the entire function, and the ezsrRatingDataObject class, so I found that it stores 'created_at' value, and as long we can pass a hash array with a param named 'sort_by', and as long ez uses it this way:

$sorts = array( $params['sort_by'][0] => ( $params['sort_by'][1] ? 'asc' : 'desc' ) );

I suppose [0] is the name of the sort param (created_at) and [1] is a boolean (false() in this case). I also noted we can pass a limit value, so I also passed one. As you can see very simple =D

But I think including these operators in official ez doc would be a better idea...

Modified on Friday 07 October 2011 5:05:24 am by Thiago Campos Viana

Friday 07 October 2011 5:05:00 am

@ Thiago

Wow, Absolutely Brilliant Thiago!

Very easy to read and understand example, +1

I also think your absolutely right common ezpublish extensions by ezsystems internals (operators/datatypes/modules/etc) are not documented nearly as well as the core is on doc.ez.no

@ Lyzy

Let us know how all this info / examples work for you, inquiring minds want to know happy.gif Emoticon

 

Cheers,

Heath

Modified on Friday 07 October 2011 5:08:41 am by // Heath

Friday 07 October 2011 6:22:15 pm

Wow, thanks so much for the quick and helpful replies! I will work on this today and let you know how it goes happy.gif Emoticon

 

So, this is what I've ended up with:

{def $ratingLimit = 5}
    {def $i = 0}
    {foreach fetch_starrating_data(hash('sort_by', array('created_at', false()), 'limit', 1000)) as $rating}
        {set $userObj=fetch('content', 'object',hash('object_id', $rating.user_id))}
        {set $article=fetch('content', 'object',hash('object_id', $rating.contentobject_id))}
    {if or($article.class_identifier|eq('agu_course'), $article.class_identifier|eq('agu_lesson'), $article.class_identifier|eq('agu_partner'))}
    {set $i = $i|inc}
        {$userObj.name} just gave <a href={$article.main_node.url_alias|ezurl()}>{$article.name}</a> a {$rating.rating} star rating.<br />
    {/if}
    {if $i|eq($ratingLimit}{break}{/if}
{/foreach}

 

I am not sure if this is the most efficient way to make sure that the 5 most recent star ratings left on either a course, lesson, or partner page get displayed-- if either Thiago or Heath sees a better way to accomplish this, please do let me know. Otherwise, thank you so, so, so much both of you for getting this working!

Modified on Friday 07 October 2011 7:57:47 pm by Lyzy Lusterman

Friday 07 October 2011 9:27:31 pm

Hello Lyzy,

I'm so glad that our sharing has helped you reach your goal. You are very welcome happy.gif Emoticon

Real thanks should go to Thiago as he very quickly produced a working code example (great job!).

If you ever have need of eZ Publish help again in the future please come back and ask your questions again here in the forums.

Cheers,

Heath

Friday 07 October 2011 9:33:20 pm

@Lyzy

Will you fetch 1000 objects just to display five? Not recommended, do something like this:

{def $i = 0
     $ratingLimit = 5
     $offset=0}
 
 
{while lt($i,$ratingLimit)}
 
    
    {foreach fetch_starrating_data(hash('offset', $offset,'sort_by', array('created_at', false()), 'limit', $ratingLimit)) as $rating}
        
        {if or($article.class_identifier|eq('agu_course'), $article.class_identifier|eq('agu_lesson'), $article.class_identifier|eq('agu_partner'))}
            {set $i = $i|inc}
            {def $userObj=fetch('content', 'object',hash('object_id', $rating.user_id))}
            {def $article=fetch('content', 'object',hash('object_id', $rating.contentobject_id))}
 
            {$userObj.name} just gave <a href={$article.main_node.url_alias|ezurl()}>{$article.name}</a> a {$rating.rating} star rating.<br />
        {/if}
        {if $i|eq($ratingLimit}{break}{else}{set $offset=sum($offset,$ratingLimit)}{/if}
    {/foreach}
 
{/while}

It would be better if we could filter by class or something like a class_attribute_id array, but we can do the above code to avoid it.

PS: Fixed offset problem.

Modified on Saturday 08 October 2011 2:41:35 am by Thiago Campos Viana

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from