Using Contrib.Stars on custom content

Topics: Customizing Orchard, Writing modules
Feb 15, 2013 at 3:09 PM

I have the Contrib.Stars module on a custom content type that allows a user to create a page with a few custom fields. This content is then displayed through a widget that queries for the page part and displays the information in the custom fields on the homepage. I query different pages depending on certain user properties.

I want to have the 5-star rating that Contrib.Stars/Voting offers on both the page and widget, and want the widget to reflect the votes on the page and vice versa. I am having a difficult time getting the stars to show on the widget (the page works fine). The problem is that I cannot display the pages rating on the widget, and cannot simply attach the stars to the widget because that is not where the content lives. It has to come from the custom content type that is the basis for the page.

Any suggestions are appreciated, even if it is telling me that this is impossible. At a minimum, I want to be able to display the star rating, even if users cannot vote directly on the widget.
Feb 15, 2013 at 3:35 PM
Are you using a ProjectionWidget to render the page? If so, check the DisplayType being used to render the page. By default, that is "Summary".
Chances are that the Contrib.Stars module has a file that suppresses the shape from rendering when an item is being rendered using the "Summary" display type. If that's the case, all you would have to do is override that rule in the file in your theme.
Feb 15, 2013 at 3:45 PM
Thank you for your response. I am not using a ProjectionWidget, rather I use a custom widget. The driver of the custom widget queries a different page depending on the user properties and then displays it. The widget is basically just a place holder. Since the stars part is not actually included in the widget (it is only on the page), I won't be able to render the stars using the file, correct?
Feb 15, 2013 at 3:54 PM
If you are using ContentManager.BuildDisplay in your custom widget driver to render the page, then it would use as well to determine what shapes get rendered in which zones. Then again I know nothing of your implementation, so it is a difficult to say why Starts aren't showing.
Feb 15, 2013 at 4:41 PM
Edited Feb 15, 2013 at 4:43 PM
Ok, let me try to explain the implementation:

The first part is a custom content type called news, and the part is called newsPart. It is basically just the out of the box page content type but I've also included some custom fields. The admin creates a news page and then selects a tag for the page. It also has Star_Part attached to it. Then the admin can publish this page and it becomes viewable on the site. The stars are working just fine on the page.

The second part is another custom content type called newsWidget. This widget gets the users information and then queries the newsPart in the driver for the most recently published article that has a tag that matches the users information. Then, it displays the custom fields that were entered when the admin created/edited the page. Since I query the newsPart I thought I could grab the rating value as well, since Star_Part is attached to newsPart. The value exists but was returned as 0. But on the actual news page itself, the rating is non-zero, and I verified this with the shape tracer. I can't just place the voting on the widget itself, since it is not tied to the actual content that is being voted on. It is just the vehicle for which this content is displayed. ContentManager.BuildDisplay is rendering the widget shape and not that of the newsPart. EDIT: I should add that the widget appears on the homepage, the news page is elsewhere on the site.

I hope this helps you get a better understanding. If you need more I can post code or try to do a better job explaining. Thank you again for your help!
Feb 15, 2013 at 4:57 PM
Edited Feb 15, 2013 at 4:59 PM
Ok, please have a look at the driver for the Star / Rating part, I suspect that the actual rating is done there, which would explain why the values are 0 when you access the part directly.
I would therefore suggest to use IContentManager.BuildDisplay to render the content item instead of manually accessing the parts and fields. You can use a custom Display Type if that helps with implementing the templates for the content item being rendered by your widget and use in combination with that custom display type to determine which shapes to render at what local zone (also be sure to check out Bertrand's latest post)
Feb 15, 2013 at 8:28 PM
Edited Feb 15, 2013 at 8:32 PM
Thanks. I've been trying to make your suggestion work but I'm doing it wrong. I was thinking this would work:
var newsItem = _contentManager
      .Query<RegionNewsPart, RegionNewsPartRecord>("RegionNews")
      .Select(x => _orchardServices.ContentManager.BuildDisplay(x, "WidgetView"));
But newsItem comes back null. Do you have a correction for this code? _contentManager and _orchardServices are of type IContentManager and IOrchardServices, respectively.
Feb 21, 2013 at 7:54 PM
I am having the same problem. Any update on this?

Feb 21, 2013 at 8:11 PM
That's very weird. You should at least get back an instance of IEnumerable<dynamic> (which in turn may yield zero results).
Perhaps there's an exception occurring that's being swallowed? If that's true, you should be able to see it in the logs.
Alternatively, turn on "Common Language Runtime Esceptions" in the Exceptions window (DEBUG -> Exceptions).
Feb 22, 2013 at 3:35 PM
I've resolved to just cut the rating functionality widget side and instead just display the stars. I do this by getting the URL of the content, then doing a jquery $.load and grab the stars by div as follows

$.load(URLgoeshere #content .star-rating)
Thank you for all of your help, and if I return to this later and find a solution that allows user to rate the stars on the widget I'll update.