%
dim UserRating
dim Ratings
dim AvgRating
dim LinkID
dim rate_link_sql
sub request_rate_link
''' request form inputs from this form
UserRating = request("UserRating")
Ratings = request("Ratings")
AvgRating = request("AvgRating")
LinkID = request("LinkID")
end sub
sub validate_rate_link
''' request and validate data entered from this form
UserRating = trim(request("UserRating"))
Ratings = trim(request("Ratings"))
AvgRating = trim(request("AvgRating"))
LinkID = trim(request("LinkID"))
end sub
sub get_defaults_rate_link
''' set default values for this form
UserRating = 7
end sub
sub db_select_rate_link
sql = "SELECT " & _
"Ratings, " & _
"AvgRating, " & _
"LinkID FROM Links" & _
" WHERE " & _
"Links.LinkID = " & to_sql(LinkID,"number") & ""
on error resume next
set rs = cn.Execute(sql)
if err.number <> 0 then
b_error = true
elseif rs.EOF then
b_results = false
else
UserRating = rs("UserRating")
Ratings = rs("Ratings")
AvgRating = rs("AvgRating")
LinkID = rs("LinkID")
end if
rs.Close
on error goto 0
end sub
sub db_insert_rate_link
sql = "INSERT INTO Links" & _
"(" & _
"Ratings," & _
"AvgRating" & _
") VALUES (" & to_sql(Ratings,"number") & "," & _
"" & to_sql(AvgRating,"text") & ")" & _
""
'response.write sql
on error resume next
cn.Execute(sql)
if err.Number <> 0 then
b_error = true
else
set rs = cn.Execute("SELECT @@IDENTITY")
LinkID = rs(0)
rs.Close
end if
on error goto 0
end sub
sub db_update_rate_link
sql = "UPDATE Links SET " & _
"AvgRating = (AvgRating + " & to_sql(UserRating,"number") & ")/2, " & _
"Ratings = Ratings + 1 WHERE " & _
"LinkID = " & LinkID & ""
'response.write sql
on error resume next
cn.execute(sql)
if err.number <> 0 then
b_error = true
end if
on error goto 0
end sub
sub db_delete_rate_link
sql = "DELETE FROM Links" & _
" WHERE " & _
"LinkID = " & to_sql(LinkID,"number") & ""
'response.write sql
on error resume next
cn.Execute(sql)
if err.number <> 0 then
b_error = true
end if
on error goto 0
end sub
do_search = request("do_search")
''' request form keys and inputs
LinkID = request("LinkID")
''' request action
action = lcase(request("action"))
''' action case handler
select case action
case "select_rate_link"
' select the requested key record from database
if LinkID <> "" then
db_select_rate_link
else
b_error = true
error_list.add "edit_rate_link", "Specify record to select."
end if
case "insert_rate_link"
' request form data and insert a new record into database
validate_rate_link
if not b_error then
db_insert_rate_link
response.write ""
end if
case "update_rate_link"
' request form data and update an existing database record
validate_rate_link
if not b_error then
if LinkID <> "" then
''' make sure this IP has not voted already
set rs = cn.Execute("SELECT LinkId FROM LinkRatings WHERE LinkId=" & LinkId & " AND IP='" & Request.ServerVariables("REMOTE_ADDR") & "'")
if rs.EOF then
''' update link ratings table
db_update_rate_link
cn.Execute ("INSERT INTO LinkRatings (LinkId, UserId, Rating, IP) VALUES (" & LinkId & "," & to_sql(session("user_id"),"number") & "," & UserRating & ",'" & Request.ServerVariables("REMOTE_ADDR") & "')")
msg_list.add "thanks", "Thank you"
else
msg_list.add "sorry", "You have already rated this resource."
end if
else
rs.Close
b_error = true
error_list.add "update_rate_link", "Specify record to update."
end if
end if
case "delete_rate_link"
' delete the requested key database record
if LinkID <> "" then
db_delete_rate_link
response.redirect request.servervariables("script_name") & "?msg=The+record+was+deleted."
else
b_error = true
error_list.add "delete_rate_link", "Specify record to delete."
end if
end select
':: handle the default case(s) for each form (separate from action= parameter)
if LinkID <> "" then
db_select_rate_link
get_defaults_rate_link
end if
%>
Rate It - WorkSource Seattle King County
<%
display_errs
display_msg
%>
Rating
<%
':: assure that any db resources are freed
on error resume next
rs.Close
set rs = NOTHING
cn.Close
set cn = NOTHING
user_cn.Close
set user_cn = NOTHING
on error goto 0
%>