OpenNTF.org - How to implement a voting syst
My Links (Not logged in)
Code Bin Search
 
Hosted by Prominic.NET
Rate This Code
5 - brilliant stuff
4 - very nice
3 - average
2 - needs work
1 - bad
   OpenNTF Code Bin
About This Code
Brief Description:
How to implement a voting system on your website. 
Rating:
Rating: 4 , Number of votes: 1 
Contributor:
Andrew Jones 
Category:
Lotus Formula, Lotusscript 
Type:
 
Last Modified:
17 Jun 2002 
OpenNTF Disclaimer

All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.

Code / Description
Usage / Example


This solution uses two almost identical LotusScript agents. One to vote "Yes" and one to vote "No".
In this case, the voting is on an Intranet, where people are required to login. The system will know if the (authorized) user already voted, and will not allow more than one vote per question. This solution assumes that each user is logged in. If the voting should be deployed on a public website, cookies could be used, or the IP address could be used instead of the username.

The result will look like this:


1. Create a form in the database, and call it "VoteRecord". It should look like this:

User: [UserName]
Vote: [Vote]
Question ID#: [QuestionID]
Created: [Created]


All fields are plain text, editable. The field Created has Default Value set to
@Created.


2. Create a view called "Questions". It contain two columns and is used to display the question on the homepage (or wherever you like to have it).
The selection formula looks like this:
SELECT Form="Question" & Date >@Today
This will display all questions with a end-date after today's date. This way you can enter a number of questions and have the old questions go away at certain dates.
In the view, column 1 display the field Date (make this one Categorized and sort it in ascending order to get the oldest at the top), column 2 display the field Question and column 3 has the formula
@Text(@DocumentUniqueID) to display the DocID of the question.

3. Now it is time to display the question on a webpage together with two buttons.
Create a computed text with the following formula:

@Subset(@DbColumn("";"";"Questions";2);1)

This will get the text of the oldest, still valid question from the view and display it.
To create the buttons, I use pass-through HTML:

<A HREF=/database.nsf/VoteYes?OpenAgent&ID=<Computed Text>><IMG SRC=yes.gif BORDER=0></A>

The computed text has the formula
@Subset(@DbColumn("";"";"Questions";3);1)
This will get the DocID for the question and pass it to the agent.
Do the same thing for the "No" button, just change the GIF and call the agent VoteNo instead of VoteYes.

4. Finally it is time to create the agent(s).
To support the agent, you need four GIF images:
* red.gif is a 1x1 pixel big red GIF used to create the red "No" bar
* blue.gif is a similar sized blue GIF for the "Yes" bar
* chart_left.gif is the vertical bar for the chart (simply a black vertical line a few pixels wide).
* chart_bottom is the bottom scale for the chart, a line about 150 pixels wide with the numbers 50% and 100% below the line.
You can find the images at
http://www.martinsson.net/khm/homepage.nsf/pages/chart.htm together with a partial screenshot that show what the end result looks like.
Put the images as image resources in the database.


Create the agent VoteYes with the following code:

Sub Initialize
Dim session As New NotesSession
Dim collection As NotesDocumentCollection
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim doc2 As NotesDocument
Dim SearchString As String
Dim UNID As String
Dim UserName As String
Dim yesvotes As Integer
Dim novotes As Integer
Dim yespercent As Integer
Dim nopercent As Integer
Dim namesfound As Integer


Set session = New NotesSession
Set doc2 = session.DocumentContext
UserName = doc2.Remote_User(0)
UNID = doc2.Query_String(0)
UNID = Right(UNID,32)


Print |
<META HTTP-EQUIV="Refresh" CONTENT="5;URL=http://www.domain.com/">|



Code


SearchString = |@Contains(QuestionID;"|+UNID+|") & @Contains(UserName;"| + Username + |")|
Set db=session.CurrentDatabase
Set collection = db.Search(SearchString,Nothing,0)
namesfound = collection.Count
If namesfound=0 Then
Set doc = New NotesDocument(db)
doc.Form="VoteRecord"
doc.UserName=UserName
doc.vote="Yes"
doc.QuestionID=UNID
Call doc.Save(True,True)
Print "
<CENTER><FONT SIZE=2 FACE=Arial><B>Current votes:</B></FONT></CENTER>"
SearchString = |@Contains(QuestionID;"|+UNID+|") & @Contains(Vote;"Yes")|
Set collection = db.Search(SearchString,Nothing,0)
yesvotes = collection.Count
SearchString = |@Contains(QuestionID;"|+UNID+|") & @Contains(Vote;"No")|
Set collection = db.Search(SearchString,Nothing,0)
novotes = collection.Count


yesvotes = yesvotes
novotes = novotes
If yesvotes = 0 Then
yespercent=0
Else
yespercent=yesvotes/(yesvotes+novotes) * 100
End If
If novotes = 0 Then
nopercent=0
Else
nopercent=novotes/(yesvotes+novotes) * 100
End If


Print |

<TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0>
<TR>
<TD WIDTH=140>
<FONT FACE=Arial SIZE=-1><B>Yes:
| & yesvotes & | (| & yespercent & |%)</B></FONT><br>
</TD>
<TD WIDTH=9 ROWSPAN=2 VALIGN=bottom>
<IMG SRC=chart_left.gif><BR>
</TD>
<TD WIDTH=190>
<IMG SRC=blue.gif HEIGHT=15 WIDTH=
| & Abs(yespercent * 1.5) & |>
</TD>
</TR>
<TR>
<TD WIDTH=140>
<FONT FACE=Arial SIZE=-1><B>No:
| & novotes & | (| & nopercent & |%)</B></FONT><br>
</TD>
<TD WIDTH=190>
<IMG SRC=red.gif HEIGHT=15 WIDTH=
| & Abs(nopercent * 1.5) & |>
</TD>
</TR>
<TR>
<TD WIDTH=140>
</TD>
<TD COLSPAN=2 VALIGN=top>
<IMG SRC=chart_bottom.gif>
</TD>
</TR>
</TABLE>

|


End If
If namesfound>0 Then
Print "
<CENTER><FONT SIZE=2 FACE=Arial><B>You, or someone else using the name " + Username + ", has already voted.<BR>You can only vote once, you cheater!</B></FONT></CENTER>"
Print |
<BR><CENTER><FONT SIZE=-2 FACE="Arial, Helvetica" STYLE="font-size: 8pt; font-weight: bold; font-family: Arial, Helvetica; text-decoration: none;">|
Print "
<A HREF=http://www.domain.com/database.nsf/VoteResult?OpenAgent&ID=" + UNID +">Current Results</A></FONT></CENTER><BR>"
End If
End Sub



Create a copy of this agent, called VoteNo and replace the line doc.vote="Yes" with doc.vote="No".
In the end of the agent, there is a piece of HTML code that call a third agent, which I have not described here yet. That agent is a subset of the agent above, it only display the result withouth voting or checking if the user already voted.
The agent uses the CGI variable Remote_User to identify the name of the authorized user.

Good luck!
 Comments
Posted by Peter Zolnay on 10/13/2003 03:21:30 AMnot working
Hi Andrew, the agent does not work on my server, I have R6.0.2CF2. I get an error message:
HTTP Web Server: Lotus Notes Exception - Unsupported trigger and search in the background or embedded agent.
Do You have an Idea where could be a problem?
Posted by Chris Crompton on 03/04/2004 03:59:55 PMtrigger
You need to set the agent to act on all documents in the database
 Add your comment!