/**
* Datum which holds details of a running poll loaded from the database and supplementary info.
*
* Used to minimize the need for querying this data every time it's needed.
*
*/
/datum/poll_question
///Reference list of the options for this poll, not used by text response polls.
var/list/options = list()
///Table id of this poll, will be null until poll has been created.
var/poll_id
///The type of poll to be created, must be POLLTYPE_OPTION, POLLTYPE_TEXT, POLLTYPE_RATING, POLLTYPE_MULTI or POLLTYPE_IRV.
var/poll_type
///Count of how many players have voted or responded to this poll.
var/poll_votes
///Ckey of the poll's original author
var/created_by
///Date and time the poll opens, timestamp format is YYYY-MM-DD HH:MM:SS.
var/start_datetime
///Date and time the poll will run until, timestamp format is YYYY-MM-DD HH:MM:SS.
var/end_datetime
///The title text of the poll, shows up on the list of polls.
var/question
///Supplementary text displayed only when responding to a poll.
var/subtitle
///Hides the poll from any client without a holder datum.
var/admin_only
///The number of responses allowed in a multiple-choice poll, more can be selected but won't be recorded.
var/options_allowed
///Hint for statbus, not used by the game; Stops the results of a poll from being displayed until the end_datetime is reached.
var/dont_show
///Allows a player to change their vote to a poll they've already voted on, off by default.
var/allow_revoting
///Indicates if a poll has been submitted or loaded from the DB so the management panel will open with edit functions.
var/edit_ready = FALSE
///Holds duration data when creating or editing a poll and refreshing the poll creation window.
var/duration
///Holds interval data when creating or editing a poll and refreshing the poll creation window.
var/interval
///Indicates a poll is set to not start in the future, still visible for editing but not voting on.
var/future_poll
/**
* Datum which holds details of a poll option loaded from the database.
*
* Used to minimize the need for querying this data every time it's needed.
*
*/
/datum/poll_option
///Reference to the poll this option belongs to
var/datum/poll_question/parent_poll
///Table id of this option, will be null until poll has been created.
var/option_id
///Description/name of this option
var/text
///For rating polls, the minimum selectable value allowed; Supported value range is -2147483648 to 2147483647
var/min_val
///For rating polls, the maximum selectable value allowed; Supported value range is -2147483648 to 2147483647
var/max_val
///Optional for rating polls, description shown next to the minimum value
var/desc_min = ""
///Optional for rating polls, description shown next to the rounded whole middle value
var/desc_mid = ""
///Optional for rating polls, description shown next to the maximum value
var/desc_max = ""
///Hint for statbus, not used by the game; If this option should be included by default when calculating the resulting percentages of all options for this poll
var/default_percentage_calc
/**
* Shows a list of all current and future polls and buttons to edit or delete them or create a new poll.
*
*/
/datum/admins/proc/poll_list_panel()
var/list/output = list("Current and future polls Note when editing polls or their options changes are not saved until you press Submit Poll. New PollReload Polls
")
for(var/p in GLOB.polls)
var/datum/poll_question/poll = p
output += {"[poll.question]
Edit Delete
"}
if(poll.subtitle)
output += " [poll.subtitle]"
output += " [poll.future_poll ? "Starts" : "Started"] at [poll.start_datetime] | Ends at [poll.end_datetime]"
if(poll.admin_only)
output += " | Admin only"
if(poll.dont_show)
output += " | Hidden from tracking until complete"
output += " | [poll.poll_votes] players have [poll.poll_type == POLLTYPE_TEXT ? "responded" : "voted"]"
var/datum/browser/panel = new(usr, "plpanel", "Poll list Panel", 700, 400)
panel.set_content(jointext(output, ""))
panel.open()
/**
* Show the options for creating a poll or editing its parameters along with its linked options.
*
*/
/datum/admins/proc/poll_management_panel(datum/poll_question/poll)
var/list/output = list("
First enter the poll question details and press Initialize Question.
Then add poll options and press Submit Poll to save and create the question and options. No options are required for Text Reply polls.
Which poll type should I use?
"}
else
output += ""
if(poll.edit_ready)
output += {"
"}
if(poll.poll_type == POLLTYPE_TEXT)
output += "Clear poll responses [poll.poll_votes] players have responded"
else
output += "Clear poll votes [poll.poll_votes] players have voted"
if(poll.poll_type == POLLTYPE_TEXT)
output += ""
else
output += "Add Option "
if(length(poll.options))
for(var/o in poll.options)
var/datum/poll_option/option = o
option_count++
output += {"Option [option_count]
Edit Delete [option.text]
"}
if(poll.poll_type == POLLTYPE_RATING)
output += {" Minimum value: [option.min_val] | Maximum value: [option.max_val]
Minimum description: [option.desc_min]
Middle description: [option.desc_mid]
Maximum description: [option.desc_max]
"}
output += ""
var/datum/browser/panel = new(usr, "pmpanel", "Poll Management Panel", 780, 640)
panel.add_stylesheet("admin_panelscss", 'html/admin/admin_panels.css')
if(usr.client.prefs.read_preference(/datum/preference/toggle/tgui_fancy)) //some browsers (IE8) have trouble with unsupported css3 elements that break the panel's functionality, so we won't load those if a user is in no frills tgui mode since that's for similar compatability support
panel.add_stylesheet("admin_panelscss3", 'html/admin/admin_panels_css3.css')
panel.set_content(jointext(output, ""))
panel.open()
/**
* Processes topic data from poll management panel.
*
* Reads through returned form data and assigns data to the poll datum, creating a new one if required, before passing it to be saved.
* Also does some simple error checking to ensure the poll will be valid before creation.
*
*/
/datum/admins/proc/poll_parse_href(list/href_list, datum/poll_question/poll)
if(!check_rights(R_POLL))
return
if(!SSdbcore.Connect())
to_chat(usr, span_danger("Failed to establish database connection."), confidential = TRUE)
return
var/list/error_state = list()
var/new_poll = FALSE
var/clear_votes = FALSE
var/submit_ready = FALSE
if(!poll)
poll = new(creator = usr.client.ckey)
new_poll = TRUE
if(new_poll)
poll.poll_type = href_list["polltype"]
switch(href_list["radioduration"])
if("runfor")
poll.duration = text2num(href_list["duration"])
poll.interval = href_list["durationtype"]
if("rununtil")
if(href_list["enddatetimetext"] != "YYYY-MM-DD HH:MM:SS")
poll.duration = href_list["enddatetimetext"]
if(new_poll)
poll.end_datetime = poll.duration
if(!poll.duration)
error_state += "No duration was provided."
switch(href_list["radiostart"])
if("startnow")
poll.start_datetime = null
if("startdatetime")
if(href_list["startdatetimetext"] && href_list["startdatetimetext"] != "YYYY-MM-DD HH:MM:SS")
poll.start_datetime = href_list["startdatetimetext"]
else
error_state += "Start datetime was selected but none was provided."
if(href_list["question"])
poll.question = href_list["question"]
else
error_state += "No question was provided."
poll.subtitle = href_list["subtitle"]
if(href_list["adminonly"])
poll.admin_only = TRUE
else
poll.admin_only = FALSE
if(href_list["dontshow"])
poll.dont_show = TRUE
else
poll.dont_show = FALSE
if(href_list["allowrevoting"])
poll.allow_revoting = TRUE
else
poll.allow_revoting = FALSE
if(href_list["clearvotesedit"])
clear_votes = TRUE
if(href_list["submitpoll"])
submit_ready = TRUE
if(poll.poll_type == POLLTYPE_MULTI)
if(text2num(href_list["optionsallowed"]))
poll.options_allowed = text2num(href_list["optionsallowed"])
if(poll.options_allowed == 1)
error_state += "Multiple choice polls require more than one option allowed, use a standard option poll for singlular voting."
if(poll.options_allowed < 0)
error_state += "Multiple choice options allowed cannot be negative."
else
error_state += "Multiple choice poll was selected but no number of allowed options was provided."
if(submit_ready && poll.poll_type != POLLTYPE_TEXT && !length(poll.options))
error_state += "This poll type requires at least one option."
if(error_state.len)
if(poll.edit_ready)
to_chat(usr, span_danger("Not all edits were applied because the following errors were present:\n[error_state.Join("\n")]"), confidential = TRUE)
else
to_chat(usr, span_danger("Poll not [new_poll ? "initialized" : "submitted"] because the following errors were present:\n[error_state.Join("\n")]"), confidential = TRUE)
if(new_poll)
qdel(poll)
return
if(submit_ready)
var/db = poll.edit_ready //if the poll is new it will need its options inserted for the first time
poll.save_poll_data(clear_votes)
if(!db)
poll.save_all_options()
poll_management_panel(poll)
/datum/poll_question/New(id, polltype, starttime, endtime, question, subtitle, adminonly, multiplechoiceoptions, dontshow, allow_revoting, vote_count, creator, future, dbload = FALSE)
poll_id = text2num(id)
poll_type = polltype
start_datetime = starttime
end_datetime = endtime
src.question = question
src.subtitle = subtitle
admin_only = text2num(adminonly)
options_allowed = text2num(multiplechoiceoptions)
dont_show = text2num(dontshow)
src.allow_revoting = text2num(allow_revoting)
poll_votes = text2num(vote_count) || 0
created_by = creator
future_poll = text2num(future)
edit_ready = dbload
GLOB.polls += src
/datum/poll_question/Destroy()
GLOB.polls -= src
return ..()
/**
* Sets a poll and its associated data as deleted in the database.
*
* Calls the procedure set_poll_deleted to set the deleted column to 1 for each row in the poll_ tables matching the poll id used.
* Then deletes each option datum and finally the poll itself.
*
*/
/datum/poll_question/proc/delete_poll()
if(!check_rights(R_POLL))
return
if(!SSdbcore.Connect())
to_chat(usr, span_danger("Failed to establish database connection."), confidential = TRUE)
return
var/datum/db_query/query_delete_poll = SSdbcore.NewQuery(
"CALL set_poll_deleted(:poll_id)",
list("poll_id" = poll_id)
)
if(!query_delete_poll.warn_execute())
qdel(query_delete_poll)
return
qdel(query_delete_poll)
for(var/o in options)
var/datum/poll_option/option = o
qdel(option)
GLOB.polls -= src
qdel(src)
/**
* Inserts or updates a poll question to the database.
*
* Uses INSERT ON DUPLICATE KEY UPDATE to handle both inserting and updating at once.
* The start and end datetimes and poll id for new polls is then retrieved for the poll datum.
* Arguments:
* * clear_votes - When true will call clear_poll_votes() to delete all votes matching this poll id.
*
*/
/datum/poll_question/proc/save_poll_data(clear_votes)
if(!check_rights(R_POLL))
return
if(!SSdbcore.Connect())
to_chat(usr, span_danger("Failed to establish database connection."), confidential = TRUE)
return
var/new_poll = !poll_id
if(poll_type != POLLTYPE_MULTI)
options_allowed = null
var/admin_ckey = created_by
var/admin_ip = usr.client.address
var/end_datetime_sql
if (interval in list("SECOND", "MINUTE", "HOUR", "DAY", "WEEK", "MONTH", "YEAR"))
end_datetime_sql = "NOW() + INTERVAL :duration [interval]"
else
end_datetime_sql = ":duration"
var/kn = key_name(usr)
var/kna = key_name_admin(usr)
var/datum/db_query/query_save_poll = SSdbcore.NewQuery({"
INSERT INTO [format_table_name("poll_question")] (id, polltype, created_datetime, starttime, endtime, question, subtitle, adminonly, multiplechoiceoptions, createdby_ckey, createdby_ip, dontshow, allow_revoting)
VALUES (:poll_id, :poll_type, NOW(), COALESCE(:start_datetime, NOW()), [end_datetime_sql], :question, :subtitle, :admin_only, :options_allowed, :admin_ckey, INET_ATON(:admin_ip), :dont_show, :allow_revoting)
ON DUPLICATE KEY UPDATE starttime = :start_datetime, endtime = [end_datetime_sql], question = :question, subtitle = :subtitle, adminonly = :admin_only, multiplechoiceoptions = :options_allowed, dontshow = :dont_show, allow_revoting = :allow_revoting
"}, list(
"poll_id" = poll_id, "poll_type" = poll_type, "start_datetime" = start_datetime, "duration" = duration,
"question" = question, "subtitle" = subtitle, "admin_only" = admin_only, "options_allowed" = options_allowed,
"admin_ckey" = admin_ckey, "admin_ip" = admin_ip, "dont_show" = dont_show, "allow_revoting" = allow_revoting
))
if(!query_save_poll.warn_execute())
qdel(query_save_poll)
return
if (!poll_id)
poll_id = query_save_poll.last_insert_id
qdel(query_save_poll)
var/datum/db_query/query_get_poll_id_start_endtime = SSdbcore.NewQuery(
"SELECT starttime, endtime, IF(starttime > NOW(), 1, 0) FROM [format_table_name("poll_question")] WHERE id = :poll_id",
list("poll_id" = poll_id)
)
if(!query_get_poll_id_start_endtime.warn_execute())
qdel(query_get_poll_id_start_endtime)
return
if(query_get_poll_id_start_endtime.NextRow())
start_datetime = query_get_poll_id_start_endtime.item[1]
end_datetime = query_get_poll_id_start_endtime.item[2]
future_poll = text2num(query_get_poll_id_start_endtime.item[3])
qdel(query_get_poll_id_start_endtime)
if(clear_votes)
clear_poll_votes()
edit_ready = TRUE
var/msg = "has [new_poll ? "created a new" : "edited a"][admin_only ? " admin only" : ""] server poll. Question: [question]"
if(admin_only)
log_admin_private("[kn] [msg]")
else
log_admin("[kn] [msg]")
message_admins("[kna] [msg]")
/**
* Saves all options of a poll to the database.
*
* Saves all the created options for a poll when it's submitted to the DB for the first time and associated an id with the options.
* Insertion and id querying for each option is done separately to ensure data integrity; this is less performant, but not significantly.
* Using MassInsert() would mean having to query a list of rows by poll_id or matching by fields afterwards, which doesn't guarantee accuracy.
*
*/
/datum/poll_question/proc/save_all_options()
if(!SSdbcore.Connect())
to_chat(usr, span_danger("Failed to establish database connection."), confidential = TRUE)
return
for(var/o in options)
var/datum/poll_option/option = o
option.save_option()
/**
* Deletes all votes or text replies for this poll, depending on its type.
*
*/
/datum/poll_question/proc/clear_poll_votes()
if(!check_rights(R_POLL))
return
if(!SSdbcore.Connect())
to_chat(usr, span_danger("Failed to establish database connection."), confidential = TRUE)
return
var/table = "poll_vote"
if(poll_type == POLLTYPE_TEXT)
table = "poll_textreply"
var/datum/db_query/query_clear_poll_votes = SSdbcore.NewQuery(
"UPDATE [format_table_name(table)] SET deleted = 1 WHERE pollid = :poll_id",
list("poll_id" = poll_id)
)
if(!query_clear_poll_votes.warn_execute())
qdel(query_clear_poll_votes)
return
qdel(query_clear_poll_votes)
poll_votes = 0
to_chat(usr, span_danger("Poll [poll_type == POLLTYPE_TEXT ? "responses" : "votes"] cleared."), confidential = TRUE)
/**
* Show the options for creating a poll option or editing its parameters.
*
*/
/datum/admins/proc/poll_option_panel(datum/poll_question/poll, datum/poll_option/option)
var/list/output = list("