Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Imports APNSoft.WebControls

Partial Public Class Rating_RateArticle
    Inherits System.Web.UI.Page

    'Declarations
    Public Votes As Integer = 0 'The number of votes
    Private TotalRatingSum As Integer = 0 'Total rating sum


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

        'Get the Votes from session
        Dim _Votes As Object = Session("Votes")
        If _Votes IsNot Nothing Then
            Votes = CInt(Fix(_Votes))
        End If

        'Get the TotalRatingSum from session
        Dim _TotalRatingSum As Object = Session("TotalRatingSum")
        If _TotalRatingSum IsNot Nothing Then
            TotalRatingSum = CInt(Fix(_TotalRatingSum))
        End If

        'Display default value
        myRating.SetValue(GetAverageRating())

        'Apply event-handling method
        AddHandler myRating.OnSegmentClick, AddressOf myRating_OnSegmentClick
    End Sub


    'Calculates average rating
    Public Function GetAverageRating() As Double
        Dim _Result As Double = 0

        If Votes > 0 Then
            _Result = Math.Round(CDbl(TotalRatingSum) / CDbl(Votes), 2)
        End If

        Return _Result
    End Function


    'The event-handling method
    Private Sub myRating_OnSegmentClick(ByVal sender As Object, ByVal e As APNSoft.WebControls.RatingEventArgs)

        'Get component instance
        Dim myRating As APNSoftRating = CType(sender, APNSoftRating)

        'Get clicked Segment
        Dim mySegment As RatingSegment = e.RatingSegment

        'Add the vote
        Votes += 1
        Session("Votes") = Votes 'Save in session

        'Add vote value
        TotalRatingSum = TotalRatingSum + mySegment.value
        Session("TotalRatingSum") = TotalRatingSum 'Save in session

        'Return new values to the client (via Parameter)
        e.Parameter = Votes.ToString() & "_" & _
            GetAverageRating()
    End Sub

End Class