You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

151 lines
5.7 KiB

  1. /client/proc/create_poll()
  2. set name = "Create Poll"
  3. set category = "Admin"
  4. if(!check_rights(R_POLL))
  5. return
  6. if(!SSdbcore.Connect())
  7. to_chat(src, "<span class='danger'>Failed to establish database connection.</span>", confidential = TRUE)
  8. return
  9. var/polltype = input("Choose poll type.","Poll Type") as null|anything in list("Single Option","Text Reply","Rating","Multiple Choice", "Instant Runoff Voting")
  10. var/choice_amount = 0
  11. switch(polltype)
  12. if("Single Option")
  13. polltype = POLLTYPE_OPTION
  14. if("Text Reply")
  15. polltype = POLLTYPE_TEXT
  16. if("Rating")
  17. polltype = POLLTYPE_RATING
  18. if("Multiple Choice")
  19. polltype = POLLTYPE_MULTI
  20. choice_amount = input("How many choices should be allowed?","Select choice amount") as num|null
  21. switch(choice_amount)
  22. if(0)
  23. to_chat(src, "Multiple choice poll must have at least one choice allowed.", confidential = TRUE)
  24. return
  25. if(1)
  26. polltype = POLLTYPE_OPTION
  27. if(null)
  28. return
  29. if ("Instant Runoff Voting")
  30. polltype = POLLTYPE_IRV
  31. else
  32. return 0
  33. var/starttime = SQLtime()
  34. var/endtime = input("Set end time for poll as format YYYY-MM-DD HH:MM:SS. All times in server time. HH:MM:SS is optional and 24-hour. Must be later than starting time for obvious reasons.", "Set end time", SQLtime()) as text|null
  35. if(!endtime)
  36. return
  37. endtime = sanitizeSQL(endtime)
  38. var/datum/DBQuery/query_validate_time = SSdbcore.NewQuery("SELECT IF(STR_TO_DATE('[endtime]','%Y-%c-%d %T') > NOW(), STR_TO_DATE('[endtime]','%Y-%c-%d %T'), 0)")
  39. if(!query_validate_time.warn_execute() || QDELETED(usr) || !src)
  40. qdel(query_validate_time)
  41. return
  42. if(query_validate_time.NextRow())
  43. var/checktime = text2num(query_validate_time.item[1])
  44. if(!checktime)
  45. to_chat(src, "Datetime entered is improperly formatted or not later than current server time.", confidential = TRUE)
  46. qdel(query_validate_time)
  47. return
  48. endtime = query_validate_time.item[1]
  49. qdel(query_validate_time)
  50. var/adminonly
  51. switch(alert("Admin only poll?",,"Yes","No","Cancel"))
  52. if("Yes")
  53. adminonly = 1
  54. if("No")
  55. adminonly = 0
  56. else
  57. return
  58. var/dontshow
  59. switch(alert("Hide poll results from tracking until completed?",,"Yes","No","Cancel"))
  60. if("Yes")
  61. dontshow = 1
  62. if("No")
  63. dontshow = 0
  64. else
  65. return
  66. var/sql_ckey = sanitizeSQL(ckey)
  67. var/question = input("Write your question","Question") as message|null
  68. if(!question)
  69. return
  70. question = sanitizeSQL(question)
  71. var/list/sql_option_list = list()
  72. if(polltype != POLLTYPE_TEXT)
  73. var/add_option = 1
  74. while(add_option)
  75. var/option = input("Write your option","Option") as message|null
  76. if(!option)
  77. return
  78. option = sanitizeSQL(option)
  79. var/default_percentage_calc = 0
  80. if(polltype != POLLTYPE_IRV)
  81. switch(alert("Should this option be included by default when poll result percentages are generated?",,"Yes","No","Cancel"))
  82. if("Yes")
  83. default_percentage_calc = 1
  84. if("No")
  85. default_percentage_calc = 0
  86. else
  87. return
  88. var/minval = 0
  89. var/maxval = 0
  90. var/descmin = ""
  91. var/descmid = ""
  92. var/descmax = ""
  93. if(polltype == POLLTYPE_RATING)
  94. minval = input("Set minimum rating value.","Minimum rating") as num|null
  95. if(minval)
  96. minval = sanitizeSQL(minval)
  97. else if(minval == null)
  98. return
  99. maxval = input("Set maximum rating value.","Maximum rating") as num|null
  100. if(maxval)
  101. maxval = sanitizeSQL(maxval)
  102. if(minval >= maxval)
  103. to_chat(src, "Maximum rating value can't be less than or equal to minimum rating value", confidential = TRUE)
  104. continue
  105. else if(maxval == null)
  106. return
  107. descmin = input("Optional: Set description for minimum rating","Minimum rating description") as message|null
  108. if(descmin)
  109. descmin = sanitizeSQL(descmin)
  110. else if(descmin == null)
  111. return
  112. descmid = input("Optional: Set description for median rating","Median rating description") as message|null
  113. if(descmid)
  114. descmid = sanitizeSQL(descmid)
  115. else if(descmid == null)
  116. return
  117. descmax = input("Optional: Set description for maximum rating","Maximum rating description") as message|null
  118. if(descmax)
  119. descmax = sanitizeSQL(descmax)
  120. else if(descmax == null)
  121. return
  122. sql_option_list += list(list("text" = "'[option]'", "minval" = "'[minval]'", "maxval" = "'[maxval]'", "descmin" = "'[descmin]'", "descmid" = "'[descmid]'", "descmax" = "'[descmax]'", "default_percentage_calc" = "'[default_percentage_calc]'"))
  123. switch(alert(" ",,"Add option","Finish", "Cancel"))
  124. if("Add option")
  125. add_option = 1
  126. if("Finish")
  127. add_option = 0
  128. else
  129. return 0
  130. var/m1 = "[key_name(usr)] has created a new server poll. Poll type: [polltype] - Admin Only: [adminonly ? "Yes" : "No"] - Question: [question]"
  131. var/m2 = "[key_name_admin(usr)] has created a new server poll. Poll type: [polltype] - Admin Only: [adminonly ? "Yes" : "No"]<br>Question: [question]"
  132. var/datum/DBQuery/query_polladd_question = SSdbcore.NewQuery("INSERT INTO [format_table_name("poll_question")] (polltype, starttime, endtime, question, adminonly, multiplechoiceoptions, createdby_ckey, createdby_ip, dontshow) VALUES ('[polltype]', '[starttime]', '[endtime]', '[question]', '[adminonly]', '[choice_amount]', '[sql_ckey]', INET_ATON('[address]'), '[dontshow]')")
  133. if(!query_polladd_question.warn_execute())
  134. qdel(query_polladd_question)
  135. return
  136. qdel(query_polladd_question)
  137. if(polltype != POLLTYPE_TEXT)
  138. var/pollid = 0
  139. var/datum/DBQuery/query_get_id = SSdbcore.NewQuery("SELECT LAST_INSERT_ID()")
  140. if(!query_get_id.warn_execute())
  141. qdel(query_get_id)
  142. return
  143. if(query_get_id.NextRow())
  144. pollid = query_get_id.item[1]
  145. qdel(query_get_id)
  146. for(var/list/i in sql_option_list)
  147. i |= list("pollid" = "'[pollid]'")
  148. SSdbcore.MassInsert(format_table_name("poll_option"), sql_option_list, warn = 1)
  149. log_admin(m1)
  150. message_admins(m2)