<!--#INCLUDE FILE="setup.asp" -->
<!--#INCLUDE FILE="checkuser.asp" -->
<!--#INCLUDE FILE="theme.asp" -->
<!--#INCLUDE FILE="include/char.asp" -->
<%
Locat="班级投票"


if membername="" Then
  	ErrMsg=ErrMsg+"<br>"+"<li>您还没有<a href=login.asp>登录</a>。"
	Call top(1,1)
	Call Error("Information",ErrMsg)
Else
	Dim ClsID,Degree,ClassName
	ClsID=Request("ClassID")
	joinstatus=chkjoinclass(ClsID)
	if joinstatus<>"" Then
		Call top(1,1)
		Call Error("Exclamation",joinstatus)
	Else
		Select Case Request("Action")
		Case "New"
			act="发起新投票"
			Call top(2,1)
			Call voteform()
			Call footer()
		Case "Save"
			act="发起新投票"
			Call top(2,1)
			Call SaveVote()
			Call footer()
		Case "Poll"
			act="投票"
			Call pagehead()
			Call poll()
		Case "ViewResult"
			act="查看投票结果"
			Call pagehead()
			Call ViewResult()
		Case "ViewHistory"
			act="查看以往投票"
			Call top(2,1)
			Call viewhistory()
			Call footer()
		Case "DelVote"
			act="管理历史投票"
			Call top(2,1)
			Call DeleteVote()
			Call footer()
		Case Else
			Call pagehead()
			Call ViewResult()
		End Select 
	End if
End if


Sub deletevote()

	Dim VoteID
	Dim Rs
	Dim FoundErr,ErrMsg
	VoteID=Request("VoteID")
	if not IsInteger(VoteID) Then
		ErrMsg=ErrMsg+"<br>"+"<li>非法操作！系统参数错误！"
		Call Error("Critical",ErrMsg)
		Exit Sub
	End if

	VoteID=Clng(VoteID)

	Set rs=conn.execute("Select Max(VoteID) from VoteBox where classid="&ClsID)
	if rs(0)=VoteID Then
		Errmsg=Errmsg+"<br>"+"<li>操作失败，不能删除当前的班级投票，只能删除历史投票！"
		Call Error("Critical",ErrMsg)
		Exit Sub
	End if
	Set rs=Nothing

	Set rs=conn.Execute("Select classid from VoteBox where VoteID="&VoteID)
	if rs.Eof and rs.bof Then
		Errmsg=Errmsg+"<br>"+"<li>操作失败！该班级投票不存在。"
		founderr=True
	Else
		if not sysadmin Then
			if rs(0)<>Clng(ClsID) Then
				ErrMsg=ErrMsg+"<br>"+"<li>非法操作，您无权删除其他班级的投票！"
				FoundErr=True
			ElseIf ChkRights(6)=False Then
				ErrMsg=ErrMsg+"<br>"+"<li>操作失败，您无权删除本班的班级投票！"
				FoundErr=True
			End if
		End if
	End if

	if FoundErr Then
		Call Error("Exclamation",ErrMsg)
	Else

		Conn.Execute("delete from VoteBox where classid="&ClsID&" and VoteID="&VoteID)
		Call success("管理投票","成功删除历史投票！")
	End if

End Sub


Sub VoteForm()


	if ChkRights(6)=False Then
		ErrMsg=ErrMsg+"<Br>"+"<li>操作失败，您没有发起班级投票的权限！"
		Call Error("Exclamation",ErrMsg)
	Else
		Response.Write "<BR><BR><form Action=Poll.asp?ClassID="&ClsID&"&Action=Save method=post>"&_
		"<TABLE border=0 cellspacing=1 cellpadding=5 align=center width=600 Class=TableBorder>"&_
		"<tr Class=LightTableBody><th align=center width=""100%"" colspan=2>发起新投票</th></tr>"&_
		"<tr Class=LightTableBody><td valign=top ><B>投票主题</B><BR><li>100个字节以内</td><td>&nbsp;<INPUT TYPE=text size=50 NAME=VoteTitle></td></tr>"&_
		"<tr Class=LightTableBody><td width=220><B>投票选项</B><BR><li>每行一个投票项，最多可以有8个.<li>每个投票项不能超过25个字节.</td><td width=400>&nbsp;<TEXTAREA NAME=VoteItem ROWS=8 COLS=50></TEXTAREA></td></tr>"&_
		"<tr Class=LightTableBody><td><B>投票类型</B></td><td>&nbsp;<INPUT TYPE=radio NAME=VoteType value=False>单选投票&nbsp;&nbsp;<INPUT TYPE=radio NAME=VoteType value=True>多选投票</td></tr>"&_
		"<tr Class=DarkTableBody><td align=right colspan=2><INPUT TYPE=submit value=提&nbsp;&nbsp;交 class=button style=""width:80px"">&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>"&_
		"</table></form>"
	End if

End Sub


Sub SaveVote()

if chkpost=False Then
	ErrMsg=ErrMsg+"<Br>"+"<li>您提交的数据不合法，请不要从同学录外部提交信息。"
	Call Error("Critical",ErrMsg)
	Exit Sub
End if

if ChkRights(6)=False Then
	ErrMsg=ErrMsg+"<Br>"+"<li>操作失败，您没有发起班级投票的权限！"
	Call Error("Exclamation",ErrMsg)
	Exit Sub
End if

VoteTitle=Checkstr(trim(Request.Form("VoteTitle")))

if VoteTitle="" Then
	FoundErr=True
	ErrMsg=ErrMsg+"<Br>"+"<li>投票主题不应为空。"
ElseIf strLength(VoteTitle)>100 Then
	FoundErr=True
   	ErrMsg=ErrMsg+"<Br>"+"<li>投票主题不能超过100个字符！"
End if

VoteItem=Checkstr(trim(Request.Form("VoteItem")))

if VoteItem="" Then
	ErrMsg=ErrMsg+"<Br>"+"<li>没有填写投票选项。"
   	FoundErr=True
ElseIf strLength(VoteItem)>255 Then
   	ErrMsg=ErrMsg+"<Br>"+"<li>投票选项内容不得大于255个字符！"
   	FoundErr=True
Else

VoteItem=Split(VoteItem,vbCrLf)

VoteItem1=""
VoteNum=""
j=0
for i=0 to UBound(VoteItem)
if trim(VoteItem(i))<>"" Then
	if strLength(VoteItem(i))>25 Then
	 	ErrMsg=ErrMsg+"<Br>"+"<li>每个投票项不能超过25个字节"
	   	FoundErr=True
		Exit for
	Else
		VoteItem1=VoteItem1&VoteItem(i)&vbCrLf
		VoteNum=VoteNum&"0"&","
		j=j+1
		if j>8 Then
		 	ErrMsg=ErrMsg+"<Br>"+"<li>不能多于8个投票项！"
		   	FoundErr=True
			Exit for
		End if
	End if
End if
next

if j<2 Then
	ErrMsg=ErrMsg+"<Br>"+"<li>不能少于2个投票项！"
   	FoundErr=True
End if


VoteItem1=Left(VoteItem1,Len(VoteItem1)-1)
VoteNum=Left(VoteNum,Len(VoteNum)-1)

End if

VoteType=Request.Form("VoteType")
if VoteType="" Then
	ErrMsg=ErrMsg+"<Br>"+"<li>请选择投票类型。"
   	FoundErr=True
Else
	VoteType=Cbool(Request.Form("VoteType"))
End if

if FoundErr Then
	Call Error("Information",ErrMsg)
Else

Conn.Execute("insert into VoteBox (VoteTitle,VoteItem,VoteType,UserName,ClassID,Vote) values ('"&VoteTitle&"','"&VoteItem1&"',"&VoteType&",'"&membername&"',"&ClsID&",'"&VoteNum&"')")

Call success("发起新投票","成功为本班发起新一轮投票！")


End if

End Sub


Sub poll()

	response.write "<table width=100% height=100% bgcolor="&BodyColor&"><tr><td valign=top>"

	VoteID=Request("VoteID")
	Set VoteRs=server.createobject("adodb.recordset")
	VoteSQL="Select top 1 * from VoteBox where classid="&clsid&" order by VoteID desc"
	VoteRs.open VoteSQL,conn,1,3

	if not isinteger(VoteID) or VoteRs("VoteID")<>Clng(VoteID) Then
		ErrMsg=ErrMsg+"<Br>"+"<Li>投票失败，您正试图执行非法操作！"
		Call Error("Critical",Errmsg)
		Exit Sub
	End if

	if instr(VoteRs("VoteUser"),membername)>0 Then
		Errmsg=Errmsg+"<br>"+"<li>	对不起，您已经投过票了，不能重复投票！"
		Founderr=True
	ElseIf Request("vote")="" Then
		ErrMsg=ErrMsg+"<Br>"+"<Li>投票失败，您没有选择投票项！"
		Founderr=True
	End if

	if Founderr Then
		Call Error("Information",Errmsg)
		Exit Sub
	End if

	VoteNum=Split(VoteRs("Vote"),",")

	for i=0 to ubound(VoteNum)
		if VoteRs("VoteType")=False Then
			if i=cint(Request("vote")) Then
				VoteNum(i)=VoteNum(i)+1
			End if
		Else
			if instr(Request("vote"),cstr(i))>0 Then
				VoteNum(i)=VoteNum(i)+1
			End if
		End if
		NewVoteNum=""&NewVoteNum&""&VoteNum(i)&","
	next

	NewVoteNum=left(NewVoteNum,len(NewVoteNum)-1)

	VoteRs("Vote")=NewVoteNum
	VoteRs("VoteUser")=VoteRs("VoteUser")&vbCr&membername

	VoteRs.update
	VoteRs.close
	Set VoteRs=Nothing

	UpdatePoint="update student Set point=point+"&PointSet(24)&" where userid='"&membername&"'"
	Conn.Execute UpdatePoint

	rURL="Poll.asp?ClassID="&ClsID&"&Action=ViewResult"
	rtitle="投票成功"
	rmsg="本页面将在2秒后自动转到投票结果页面<br><ul>"&_
			"<li><a href="&rURL&">查看投票结果</a></li>"&_
			"</ul>"
	Redirect 2,Rurl,Rtitle,Rmsg

End Sub


Sub viewresult()

	dim VoteRs,VoteNum
	dim VoteItem,TotalVote

	response.write "<table width=100% height=100% bgcolor="&BodyColor&"><tr><td valign=top>"

	if Degree>1 or sysadmin Then
		Response.Write "<BR><TABLE border=0 cellspacing=0 cellpadding=2 align=center width=426>"&_
			"<TR><TD><a href=Poll.asp?ClassID="&ClsID&"&Action=New target=""_blank"">"&_
			"<IMG SRC=Images/newpoll.gif BORDER=0 ALT=发起新投票></a></TD></TR></table>"
	End if

	Set VoteRs=conn.execute("Select top 1 VoteTitle,VoteItem,Vote,VoteUser,UserName from VoteBox where classid="&ClsID&" order by VoteID desc")

	if isnull(VoteRs(3)) Then
		VoteUserNum=0
	Else
		VoteUserNum=ubound(Split(VoteRs(3),vbCr))
	End if

	VoteNum=Split(VoteRs(2),",")
	TotalVote=0
	for i=0 to ubound(VoteNum)
		TotalVote=TotalVote+VoteNum(i)
	next 

	Response.Write "<TABLE border=0 cellspacing=1 cellpadding=5 align=center width=426 Class=TableBorder>"&_
			"<TR Class=LightTableBody><TD colspan=3 align=center>"&VoteRs(0)&"</TD></TR>"&_
			"<tr Class=DarkTableBody><th align=center width=*>选&nbsp;&nbsp;项</th><th align=center width=200>比&nbsp;&nbsp;例</th><th align=center width=26>票数</th></tr>"

	VoteItem=Split(VoteRs(1),vbCrLf)

	for i=0 to Ubound(VoteItem)

	if TotalVote=0 Then
		ratio=0
		linewidth=2
	Else
		ratio=Round(VoteNum(i)/TotalVote,3)
		linewidth=cint(180*ratio)
	End if

	Response.Write "<tr Class=LightTableBody><td>"&VoteItem(i)&"</td>"&_
		"<td width=200><img src=images/line.gif height=9 width="&linewidth&">&nbsp;"&Round(ratio*100,2)&"%</td>"&_
		"<td align=center>"&VoteNum(i)&"</td></tr>"

	next

	Response.Write "<tr Class=DarkTableBody><td colspan=3>"&_
		"<table width=""100%"" border=0><tr><td>共有<font color=brown>"&_
		"<b>"&VoteUserNum&"</b></font>人参与投票</td><td align=right>投票发起人："

	if VoteRs(4)="风月同学录" Then
		Response.Write "风月同学录"
	Else
		Response.Write "<a href=Profile.asp?Name="&VoteRs(4)&"&Action=showdetail target=_blank>"&_
		GetRealName(VoteRs(4))&"</a>"
	End if

	Response.Write "</td></tr></table></td></tr></table>"
	Set VoteRs=Nothing

End Sub


Sub viewhistory()

VoteSQL="Select VoteTitle,VoteItem,Vote,VoteUser,UserName,VoteID from VoteBox Where classid="&_
		clsid&" and VoteID<(Select top 1 VoteID from VoteBox where classid="&_
		clsid&" order by VoteID desc) order by VoteID desc"

Set VoteRs=conn.execute(VoteSQL)

if VoteRs.EOF and VoteRs.BOF Then

	Errmsg=Errmsg+"<br>"+"<li>对不起，本班除了当前投票，还没有任何以往调查！"
	Call Error("Information",Errmsg)
	Exit Sub
Else

if Degree>1 or sysadmin Then
	Response.Write "<BR><TABLE border=0 cellspacing=0 cellpadding=2 align=center width=500>"&_
		"<TR><TD><a href=Poll.asp?ClassID="&ClsID&"&Action=New>"&_
		"<IMG SRC=Images/newpoll.gif BORDER=0 ALT=发起新投票></a></TD></TR></table>"
End if

Do until VoteRs.eof
if isnull(VoteRs("VoteUser")) Then
	VoteUserNum=0
Else
	VoteUserNum=ubound(Split(VoteRs("VoteUser"),vbCr))
End if

VoteNum=Split(VoteRs("Vote"),",")
TotalVote=0
for i=0 to ubound(VoteNum)
	TotalVote=TotalVote+VoteNum(i)
next 

Response.Write "<TABLE border=0 cellspacing=1 cellpadding=5 align=center width=500 Class=TableBorder>"&_
		"<TR Class=LightTableBody><TD colspan=3 align=center>"&_
			"<table width=""100%"" cellspacing=0 cellpadding=0 border=0>"&_
			"<tr><td align=center>"&VoteRs("VoteTitle")&"</td><td width=1>"

if Degree>1 or sysadmin Then
	Response.Write "<a onclick=""return confirm('你确认要删除这个投票吗?');"" href=Poll.asp?ClassID="&ClsId&"&Action=DelVote&VoteID="&VoteRs("VoteID")&"><IMG SRC=Images/delete.gif width=16 BORDER=0></a>"
End if
		
Response.Write "</td></tr></table></TD></TR>"&_
		"<tr Class=DarkTableBody><th align=center width=*>选&nbsp;&nbsp;项</th><th align=center width=225>比&nbsp;&nbsp;例</th><th align=center width=26>票数</th></tr>"

VoteItem=Split(VoteRs("VoteItem"),vbCrLf)

for i=0 to Ubound(VoteItem)

if TotalVote=0 Then
	ratio=0
	linewidth=2
Else
	ratio=Round(VoteNum(i)/TotalVote,3)
	linewidth=cint(200*ratio)
End if

Response.Write "<tr Class=LightTableBody><td>"&VoteItem(i)&"</td>"&_
	"<td width=250><img src=images/line.gif height=9 width="&linewidth&">&nbsp;"&Round(ratio*100,2)&"%</td>"&_
	"<td align=center>"&VoteNum(i)&"</td></tr>"

next

Response.Write "<tr Class=DarkTableBody><td colspan=3>"&_
	"<table width=""100%"" border=0><tr><td>共有<font color=brown>"&_
	"<b>"&VoteUserNum&"</b></font>人参与投票</td><td align=right>投票发起人："

if VoteRs("UserName")="风月同学录" Then
	Response.Write "风月同学录"
Else
	Response.Write "<a href=Profile.asp?Name="&VoteRs("UserName")&"&Action=showdetail>"&_
	GetRealName(VoteRs("UserName"))&"</a>"
End if

Response.Write "</td></tr></table></td></tr></table><BR>"

VoteRs.MoveNext
Loop


End if

Set VoteRs=Nothing
End Sub



%>
