<% '################################################### '# FinPoll v. 0.9 (08/04/2002) '# by Soeren Pedersen '# '# Module : processpoll.asp '# '# See ../readme.txt for instructions on how to get '# this script working '# '# Code is guarantied to be sloppy, uncommented and '# so on, but should work (It does for me). '# Provided "as is", and you use the software at '# your own risk. '# Please feel free to copy, paste, use or abuse '# the code. No copyright notices or other boring stuff are necessary. '# '# Feel free to contact me@soerenpedersen.com for '# any question/comment/suggestion. '# '################################################### '################################################### '# Setup '################################################### ' // Virtuel Path. (Where the files are located) PollFilePath="../poll/" ' // Admin Password pollAdminModePassword = "knudsen" 'Do NOT change the next line if request("AdminMode")=pollAdminModePassword then PollFilePath="" 'pollDBLocation. Where the database is located pollDBLocation=server.mappath(PollFilePath&"poll.mDB") ' // PollDebug Mode. ' // if true = Show Error Msg if any Errors. And don't set the cookie date to now + 356days ' // Notice that there is very limited errorhandling. pollDebug=false ' #################### ' ## Look and feel ## ' #################### ' // pollBgImage : Used for background color. pollBgImage = "#ffffff" ' // pollGraphImage : Used for graph bars. pollGraphImage = "red_dot.gif" ' // pollLinesImage : Used for thin lines. pollLinesImage = "215D84.gif" ' // pollLinesEnabled : Show thin lines or not. Set to true/false. pollLinesEnabled = true ' // pollFont : Used for text pollFont = "" ' // pollHeading : Heading for the poll. ' // Use a image to make it look nice, otherwise uncomment the other line and change the text and/or font pollHeading = "" 'pollHeading = "Online Poll" 'Should the "Total votes" text be shown to users. Set to true or false pollShowTotalVotes = true '##################################################################### '## Text used in script. Change this to your own words/Language ## '##################################################################### pollTxtTotalVotes="Antal votes" '################################################### '# Start '################################################### dim pollDB,pollRS,pollConstr dim QID,sQuestion,ShowResultflag set pollDB=server.createobject("ADODB.Connection") set pollRS=server.createobject("ADODB.Recordset") pollConstr = "Provider=MICROSOFT.JET.OLEDB.4.0; Data Source="&pollDBLocation ' // Get active QID and Question from database Question // FinPollOpenDB() if request("QID")<>"" then QID=request("QID") pollRS.open "select * from Questions where QID="&cstr(QID),pollDB,1,2 else pollRS.open "select * from Questions where Active=1",pollDB,1,2 end if if not (pollRS.bof and pollRS.eof) then if QID="" then QID=pollRS("QID") end if sQuestion=pollRS("Question") else call DBProcessError("Error : Could not find active poll") end if pollRS.close pollDB.close 'Get the cookie cookieContent = request.cookies("FinPol" & cstr(QID)&sQuestion) ' // See what we should do if (cookieContent<>"" OR request("AdminMode")=pollAdminModePassword) then 'Cookie is set = vote has already been cast OR Admin mode 'So show the result call ShowResult() elseif (cookieContent="" and request.form("finPollOption")<>"") then 'Vote has been cast and cookie is empty call CastVote() else 'Show the poll call ShowPoll() end if ' // Cleanup set pollRS=nothing set pollDB=nothing ' // The End '############################################ '# CastVote() '# Update database with vote and set cookie '############################################ function CastVote() answer=request.form("finPollOption") FinPollOpenDB() pollRS.open "select * from Poll where QID="&cstr(QID),pollDB,3,3,1 if not (pollRS.bof and pollRS.eof) then '// The poll ID is OK // 'Update database pollRS.find ("AnID = "+cstr(answer)) pollRS("Answer")=pollRS("Answer")+1 pollRS.update '// set a cookie for this poll // '// first clear the response object response.clear response.cookies("FinPol" & cstr(QID)&sQuestion)=Answer if pollDebug<>true then response.cookies("FinPol" & cstr(QID)&sQuestion).expires=Date + 365 end if else call DBProcessError("A poll with ID No."+cstr(QID)+" could not been found.") end if pollRS.close pollDB.close 'Cleanup since we are doing a redirect set pollRS=nothing set pollDB=nothing '// Redirect the user using ?prd=1 so Netscape don't throw up a object moved error// Response.redirect(Request.ServerVariables("SCRIPT_NAME")&"?prd=1&"&request("QueryStringBackup")) end function '############################################ '# ShowPoll() '# Show the poll '############################################ function ShowPoll() FinPollOpenDB() pollRS.open "select * from Poll where QID="+cstr(QID)+" order by AnID",pollDB,1,1 if not (pollRS.bof and pollRS.eof) then %>
"> <% if (pollLinesEnabled=true) then%> <% end if%><% while not pollRS.eof i=i+1 %> <% pollRS.movenext wend %> <% if (pollLinesEnabled=true) then%> <% end if%>
<%=pollFont%><%=sQuestion%>


<%=pollFont%>
<%=pollRS("AnswerText")%>
 
Afgiv din stemme her
<% end if pollRS.close pollDB.close end function '############################################ '# ShowResult() '# Show the poll '############################################ function ShowResult() FinPollOpenDB() pollRS.open "select * from Poll where QID="+cstr(QID),pollDB,1,1,1 if not (pollRS.bof and pollRS.eof) then set rsTotal=server.createobject("ADODB.Recordset") rsTotal.open "Select sum (Answer) from Poll where QID="+cstr(QID), pollDB, 1,1,1 total=rsTotal(0) rsTotal.close set rsTotal=nothing i=0 pollRS.movefirst %>
">
<% if (pollLinesEnabled=true) then%> <% end if%><% while not pollRS.eof i=i+1 %> <% pollRS.movenext wend %> <% if (pollShowTotalVotes=true OR request("AdminMode")=pollAdminModePassword) then %><% if (pollLinesEnabled=true) then%> <% end if%> <% end if %>
<%=pollFont%><%=sQuestion%>


<%=pollFont%> <%=pollFont%> <%=pollRS("AnswerText")%>
<%=calculate(pollRS("Answer"),Total)%> <%if ToTal<>0 then Response.write(formatpercent(pollRS("Answer")/Total,0))%> <% if request("AdminMode")=pollAdminModePassword then response.write("
"&cstr(pollRS("Answer"))& " Votes") end if TotalVotes=TotalVotes+cInt(pollRS("Answer")) %>


<%=pollFont%><%=pollTxtTotalVotes%> : <%=TotalVotes%>
<% end if pollRS.close pollDB.close end function '############################################ '# DBProcessError(pollDebugmsg) '# pollDebugmsg = What msg to display '############################################ function DBProcessError(pollDebugmsg) if pollDebug=true then Response.write(pollDebugmsg) end if end function '############################################ '# FinPollOpenDB() '# Open database, show error if any problems '############################################ sub FinPollOpenDB() On ERROR Resume next pollDB.open pollConstr, "Admin","" if Err.number<>0 then Response.write("FinPoll Error : Could not open DataBase, make sure PollFilePath are set correctly.") Response.end end if ON ERROR GOTO 0 end sub '############################################ '# Calculate(Result, total) '# returns a string containg several img tags for displaying .gif images, '# which represent the graph bar '############################################ Function Calculate(Result, total) Const Max = 12 '// maximum length of the result bar as no. of gif images // DIM sGif sGif = "" Dim NX, i, sRes sRes = "" if total = 0 then total=result if total <>0 then NX = Int(Result / total * Max + 1.5) For i = 1 To NX sRes = sRes + sGif Next end if Calculate = sRes End Function %>