If anyones interested I have also found a solution to returning the new rating stars/circle. All you need is a little tweak of the ajax javascript, a small change to the template in the ratings:form template parameter and an addtional template containing the new rating.
Using my last example above as a base change the javascript submit to:
$(document).ready(function() {
$(".profile-rating-form").submit(function() {
$(this).ajaxSubmit({
success: function(responseText, statusText){
if(responseText == "OK"){
var url = "/profiles/rating_ajax_return_value/" + $("input[@name=entry_url_title]",this).val();
$.get(url, function(data){
alert("success");
$("#rating").fadeOut("normal", function(){
$(this).empty().append(data).fadeIn();
});
});
} else {
alert("error");
}
});
}
});
return false;
});
Lets break it down a bit
$(document).ready(function() {
$(".profile-rating-form").submit(function() {
// ajax handling code
return false;
});
});
Wait until the document has loaded and attach a submit handler to each form with the “profile-rating-form” class. When the form is submitted intercept the normal submit function with AJAX and return false to deny the browser the default form submission behaviour.
Now for the AJAX hadling code. Our new and improved albeit somewhat minimal AJAX response template looks (as declared in the ratings:form template parameter) looks like this:
{if success}OK{/if}
{if failure}{message}{/if}
If the submission is OK the template returns ‘OK’ or the error message.
The AJAX handler firsts tests if the AJAX call is a success and then tests the rating has been submitted correctly.
success: function(responseText, statusText){
// if the rating has been submitted correctly
if(responseText == "OK"){
// get the updated results with a second AJAX call
// else
} else {
// alert the error returned in the template
alert(responseText);
}
});
The second AJAX call needs to know which entry was just rated so we need to add a new hidden form with the entry url title to the form:
<input type="hidden" name="entry_url_title" value="{url_title}" />
A simple get submit will return a new template with just the ratings value rendered. First construct the target URL from the extra hidden field.
var url = "/profiles/rating_ajax_return_value/" + $("input[@name=entry_url_title]",this).val();
Then submit with our second AJAX call. Once the data is returned the original rating div is faded out, emptied, filled in with new data and faded back in.
$.get(url, function(data){
//alert("success");
$("#rating").fadeOut("normal", function(){
$(this).empty().append(data).fadeIn();
});
});
The target of the GET Ajax call looks like:
{exp:weblog:entries weblog="profiles" limit="1"}
<div id="rating">
{exp:rating:stats entry_id="{entry_id}" theme="default"}
{stars_overall_avg}
Based on {overall_count} review{if overall_count != "1"}s{/if}
{/exp:rating:stats}
</div>
{/exp:weblog:entries}
Thats It enjoy.
Leevi,
This is really cool. It seems like I could recode the module a bit to make all this easier. I have that runaway loop prevention in place which prevents you from folding rating module methods into your return template, but maybe I could loosen that up a bit. There’s only a certain kind of method call that can result in infinite loops. I’ll have a look as soon as I can.
I’ve added the duplicate check functionality as well as the form title attribute that we talked about offline.
Thanks again for all of your ideas and thanks for posting code for others to see.
mk
Mitchell,
Firstly, thanks for the last set of updates. It would be great if you could return any of the ratings:stats information in the return template.
That would even allow us to render the return template as xml.
<response>
<status>{if success}success{/if}{if failure}error{/if}</status>
{if failure}<message>{message}</message>{/if}
{if success}
<stats>{exp:rating:stats}... enter stats ...{/exp:rating:stats}</stats>
{/if}
</response>
I have made even more progress since my last post refering to ratings and AJAX. I now have a star rating system that only hits the DB once for the post and automatically updates the html with JS rather than a second get request. I’ll post the code soon for everyone to see and use on my site.
Which leads me to one more feature request. The only thing that I need to know to calcualte the new rating on the client side is the max possible rating.
The max possible rating is calculated by:
<?php print({overall_count} * max_rating); ?>
The only problem with this is that PHP needs to be enabled in the template… maybe a bad thing depending on your situation.
So heres the request:
Of course this would only apply to ratings and not reviews.
Thoughts?
I wasn’t sure if this was the right thread to post this, but at the same time, I wasn’t sure if I should start a new thread, so apologies for any confusion.
I just installed Rating 2.0.8, and have begun adding it to my site. However, when I attempt to submit a rating, I get the following error:
MySQL ERROR:
Error Number: 1054
Description: Unknown column 'rating_count' in 'field list'
Query: UPDATE `exp_weblog_titles` SET `rating_avg` = '3', `rating_total` = '1', `rating_count` = '0' WHERE entry_id = '3'
When I view the “exp_weblog_titles” table, sure enough, there’s no “rating_count” column. Is there a step that I missed in the installation process? Or is there something buggy with this version of the module?
Hi opuszine,
Good catch on that bug. I have a new build of Rating 2.0.8 available which fixes that issue. Please deinstall your current build, upload the mcp.rating.php file of the new build and reinstall. That should fix you up.
Thank you for noting that.
Note to upgraders, you guys will not be affected by this bug. It’s only an install problem.
mk
Okay, so I’ve got the Rating 2.0 module installed just fine. However, I’ve run into two unrelated (I think) issues that I can’t seem to work my way through, despite going through the documentation and these here forums.
Issue #1
I’ve got a page set up that’s displaying the highest rated entries of a particular weblog. Which it does. However, some of the content that is getting pulled out is getting screwed up. Here’s the template code (with a few extraneous bits removed):
{exp:rating:rankings weblog="{my_weblog}" limit="10" sort="desc"}
<li>
<a href="http://{url_title_path={my_template_group}/details}" class="image">{exp:extract_url}{site_photo}{/exp:extract_url}</a>
<h2>{title}</h2>
</li>
{/exp:rating:rankings}
This snippet does what it’s supposed to. However, the {site_photo} tag, which is a custom field, is being returned incorrectly. Here’s the HTML source that is returned:
<li>
<a href="http://mysite.com/index.php/site/details/entry_title/" class="image">& #123;filedir_1& #125;foo.jpg</a>
<h2>Entry Title</h2>
</li>
You’ll notice that the https://ellislab.com/asset/images/team-photo/ tag in the image tag is not being rendered correctly (or, to be more specific, the brackets surrounding the tag), while the {title} tag is. At first I thought that the {exp:extract_url} might be causing it, but the https://ellislab.com/asset/images/team-photo/ is still rendered incorrectly without it.
Could there be some sort of issue with custom fields?
Issue #2
This is one where the solution is probably right in from of me. On the same site, I’m working on a page that ranks the entries in terms of the number of votes they’ve received, irregardless of the their ratings. I’ve gone through the documentation, but I can’t quite seem to figure out how to do this. It would seem to me to use {exp:rating:rankings}, but I’m not quite sure how.
Any help would be greatly appreciated. Thanks.
Opuszine,
The first bug looks like a problem in my code. 2.0.9 is ready for download and should address that issue. Let me know if it does not.
As for the second, there is not currently a nice clean way to order entries based on how many ratings they have received. Included in the package is an extension and instructions for an EE hack to get you started. For now, that will be a feature on the list.
mk
The first bug looks like a problem in my code. 2.0.9 is ready for download and should address that issue. Let me know if it does not.
Looks like that did it. Thanks again for the quick response.
As for the second, there is not currently a nice clean way to order entries based on how many ratings they have received. Included in the package is an extension and instructions for an EE hack to get you started. For now, that will be a feature on the list.
Thanks, I’ll look into it.
Update: I think I got it to work. It was actually much easier than I thought it would be. Thanks again for the help
Hello,
the sort parameter with the value of “asc” does not seem to work in the rankings.
See code below:
{exp:rating:rankings weblog="{master_weblog_name}" form_name="rating_form" limit="8" rdf="off" sort="asc" disable="member|trackbacks"}
It does work, however, with sort=”desc”:
{exp:rating:rankings weblog="{master_weblog_name}" form_name="rating_form" limit="8" rdf="off" sort="desc" disable="member|trackbacks"}
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.