Rating for Developers

From Sense/Net Wiki
Jump to: navigation, search

Overview

Example title
On this page you can find informations about usage of portlets, applications and other features for rating functionality.

Details

Portlets

There is no portlet for the rating functionality. It is only a field on the content and it uses a control, a field control and some javascript.

Applications

There is no application for the rating functionality. It uses an AJAX based MVC function (StarVotes.mvc) when someone rates a content.


The control

Link to the control..

TODO://Finish this!

The field control

Link to the field control..

TODO://Finish this!

The javascripts

There are two javascript for the rating functionality. The first (RateControl.js) is an embedded resource which is loaded by the rate control (RateControl.cs). It gets parameters from the C# code (server side) into the javascript (client side). The script registrates the control with it's properties into the SenseNet javascript library. After that it creates it's prototype (initializing) and then it registers the class as a type.

The parameter passing is shown in this code:

SN.RatingControl.initialize(this.ContentId, this.StarsId, this.HoverPanelId, this.IsReadOnly, this.RateValue);

The parameters are:

  • ContentId: the ID of the current content which the rating control is on
  • StarsId: the ID of the radiobuttonlist on the content
  • HoverPanelId: the ID of the HoverPanel of the current content
  • IsReadOnly: gives the value if the rating control is readonly or not
  • RateValue: this is a javascript object with many parameters

The parameters of the RateValue:

    • AverageRate: the average value of the rates
    • CountVotes: number of the votes per value
    • EnableGrouping: value for the grouping (enabled or not)
    • ErrorMessage: if an error occur it will be in this parameter
    • HoverPanelData:
    • MaxVotes: number of the maximum vote
    • PercentageVotes: percentage occurence of the votes
    • SelectedValue: the currently selected value
    • Split: value for spliting the stars into parts
    • Success: value for the rating if it was successfull or not
    • SumVotes: summary of the votes


The initialize method can be found in the other javascript file (SN.Rating.js). The SN.Rating.js makes the stars from the radiobuttons with jQuery Rating plugin. This can be seen here:

$("#" + starsId + " input[type=radio]").rating('enable');

If the hover panel is enabled - which gives a cumulative statistics about the rating - it will be shown when you mouseovering the rating stars. The updateHoverPanel function updates the values in this hover panel. So when someone rates on the content the rate value will immediately appear in the hover panel because it is working in AJAX using an MVC function. This AJAX call is this code:

callback: function(value, link) {
                $.ajax({ url: window.location.protocol + '//' + window.location.host + '/StarVotes.mvc/Rate?id=' + contentId + '&vote=' + value + '&isgrouping=' + rateValue.EnableGrouping,
                    beforeSend: function(a) { $("#" + starsId + " input[type=radio]").rating('disable'); },
                    context: hoverPanelId,
                    success: function(arg) {
                        SN.RatingControl.updateHoverPanel(hoverPanelId, arg);
 
                        if (!arg.Success) {
                            var error = arg.ErrorMessage;
                            if (error == null) {
                                error = "Error has occured!";
                            }
                            alert(error);
                            return false;
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        alert("Unexcepted error!");
                    }
                });
            }

The MVC method can be found in RateControl.cs:

public class StarVotesController : System.Web.Mvc.Controller
    {
        public JsonResult Rate(int? id, int? vote, bool isGrouping)
        {
            VoteData newVote = VoteData.CreateVoteData(5, 1);
            try
            {
                bool isOK = false;
 
                if (id != null && vote != null)
                {
                    using (new SystemAccount())
                    {
                        var content = ContentRepository.Content.Load(id.Value);
                        if (content != null)
                        {
                            newVote.SelectedValue = vote.Value;
                            content.Fields["Rate"].SetData(newVote);
                            if (content.IsValid)
                            {
                                try
                                {
                                    content.Save();
                                    isOK = true;
                                }
                                catch (Exception ex) //logged
                                {
                                    Logger.WriteException(ex);
                                    newVote.ErrorMessage = "You can not vote right now!";
                                }
                            }
                            newVote = content.Fields["Rate"].OriginalValue as VoteData;
                            newVote.EnableGrouping = isGrouping;
                        }
                    }
                }
                newVote.Success = isOK;
            }
            catch (Exception ex)
            {
                Logger.WriteException(ex);
                newVote.Success = false;
            }
            return Json(newVote);
        }
    }

Examples

Related links

References