/client/proc/create_poll()
set name = "Create Server Poll"
set category = "Server"
if(!check_rights(R_SERVER))
return
if(!GLOB.dbcon.IsConnected())
to_chat(src, "Failed to establish database connection.")
return
create_poll_window()
/client/proc/create_poll_window(var/errormessage = "")
if(!check_rights(R_SERVER))
return
var/output={"
Create Player Poll
[errormessage]
"}
src << browse(output, "window=createplayerpoll;size=950x500")
/client/proc/create_poll_function(href_list)
if(!check_rights(R_SERVER))
return
var/polltype = href_list["createpoll"]
if(polltype != POLLTYPE_OPTION && polltype != POLLTYPE_TEXT && polltype != POLLTYPE_RATING && polltype != POLLTYPE_MULTI)
create_poll_window("Invalid poll type")
return
var/choice_amount = 0
if(polltype == POLLTYPE_MULTI)
choice_amount = text2num(href_list["choiceamount"])
if(!isnum(choice_amount) || choice_amount < 1)
create_poll_window("Invalid choice amount. Must be at least 1.")
return
var/poll_len = text2num(href_list["createpollduration"])
if(!isnum(poll_len) || poll_len < 1)
create_poll_window("Invalid poll duration. Must be at least 1.")
return
var/adminonly = text2num(href_list["createpolladminonly"]) ? 1 : 0
var/sql_ckey = sanitizeSQL(ckey)
var/question = href_list["createpollquestion"]
if(!question)
create_poll_window("Question cannot be blank.")
return
question = sanitizeSQL(question)
var/starttime
var/endtime
var/DBQuery/query = GLOB.dbcon.NewQuery("SELECT Now() AS starttime, ADDDATE(Now(), INTERVAL [poll_len] DAY) AS endtime")
query.Execute()
while(query.NextRow())
starttime = query.item[1]
endtime = query.item[2]
var/pollquery = "INSERT INTO [format_table_name("poll_question")] (polltype, starttime, endtime, question, adminonly, multiplechoiceoptions, createdby_ckey, createdby_ip) VALUES ('[polltype]', '[starttime]', '[endtime]', '[question]', '[adminonly]', '[choice_amount]', '[sql_ckey]', '[address]')"
var/idquery = "SELECT id FROM [format_table_name("poll_question")] WHERE question = '[question]' AND starttime = '[starttime]' AND endtime = '[endtime]' AND createdby_ckey = '[sql_ckey]' AND createdby_ip = '[address]'"
var/list/option_queries = list()
if(polltype == POLLTYPE_MULTI || polltype == POLLTYPE_RATING || polltype == POLLTYPE_OPTION)
var/numoptions = text2num(href_list["createpollnumoptions"])
if(!numoptions)
create_poll_window("Invalid number of options")
return
for(var/I in 1 to numoptions)
var/option = href_list["createpolloption[I]option"]
if(!option)
create_poll_window("Invalid option name for option [I]")
return
option = sanitizeSQL(option)
var/percentagecalc = 0
if(text2num(href_list["createpolloption[I]percentagecalc"]))
percentagecalc = 1
var/minval = 0
var/maxval = 0
var/descmin = ""
var/descmid = ""
var/descmax = ""
if(polltype == POLLTYPE_RATING)
minval = text2num(href_list["createpolloption[I]minval"])
if(!minval)
create_poll_window("Invalid minimum value for option [I]")
return
maxval = text2num(href_list["createpolloption[I]maxval"])
if(!maxval)
create_poll_window("Invalid maximum value for option [I]")
return
if(minval >= maxval)
create_poll_window("Minimum rating value can't be more than maximum rating value")
return
descmin = href_list["createpolloption[I]descmin"]
if(descmin)
descmin = sanitizeSQL(descmin)
descmid = href_list["createpolloption[I]descmid"]
if(descmid)
descmid = sanitizeSQL(descmid)
descmax = href_list["createpolloption[I]descmax"]
if(descmax)
descmax = sanitizeSQL(descmax)
option_queries += "INSERT INTO [format_table_name("poll_option")] (pollid, text, percentagecalc, minval, maxval, descmin, descmid, descmax) VALUES ('{POLLID}', '[option]', '[percentagecalc]', '[minval]', '[maxval]', '[descmin]', '[descmid]', '[descmax]')"
query = GLOB.dbcon.NewQuery(pollquery)
if(!query.Execute())
var/err = query.ErrorMsg()
create_poll_window("An SQL error has occured while creating your poll")
log_game("SQL ERROR adding new poll question to table. Error : \[[err]\]\n")
return
var/pollid = 0
query = GLOB.dbcon.NewQuery(idquery)
if(!query.Execute())
var/err = query.ErrorMsg()
create_poll_window("An SQL error has occured while creating your poll")
log_game("SQL ERROR obtaining id from poll_question table. Error : \[[err]\]\n")
return
if(query.NextRow())
pollid = text2num(query.item[1])
for(var/querytext in option_queries)
query = GLOB.dbcon.NewQuery(replacetext(idquery, "{POLLID}", pollid))
if(!query.Execute())
var/err = query.ErrorMsg()
create_poll_window("An SQL error has occured while creating your poll")
log_game("SQL ERROR obtaining id from poll_question table. Error : \[[err]\]\n")
return
create_poll_window("Your poll has been successfully created")
return pollid