| 1 | <% @header[("title", ["Polls"])]; |
| 2 | |
| 3 | val pollAdmin = Group.inGroupName "poll"; |
| 4 | |
| 5 | ref editingPoll = NONE; |
| 6 | ref showNormal = true; |
| 7 | |
| 8 | if $"cmd" = "list" then |
| 9 | showNormal := false %> |
| 10 | |
| 11 | <h2>All polls</h2> |
| 12 | |
| 13 | <% foreach pol in Poll.listPolls () do %> |
| 14 | <li> <a href="poll?id=<% #id pol %>"><% Web.html (#title pol) %></a> |
| 15 | <% if pollAdmin then %><a href="poll?del=<% #id pol %>">[Delete]</a> <% end %> |
| 16 | </li> |
| 17 | <% end |
| 18 | |
| 19 | elseif $"vote" <> "" then |
| 20 | showNormal := false; |
| 21 | val id = Web.stoi ($"vote"); |
| 22 | val poll = Poll.lookupPoll id %> |
| 23 | |
| 24 | <table class="blanks"> |
| 25 | <tr> <td>Poll#:</td> <td><% id %></td> </tr> |
| 26 | <tr> <td>Title:</td> <td><% Web.html (#title poll) %></td> </tr> |
| 27 | <tr> <td>Start:</td> <td><% Web.html (#starts poll) %></td> </tr> |
| 28 | <tr> <td>End:</td> <td><% Web.html (#ends poll) %></td> </tr> |
| 29 | <tr> <td>Votes/person:</td> <td><% #votes poll %></td> </tr> |
| 30 | <tr> <td>Official:</td> <td><% if #official poll then "yes" else "no" end %></td> </tr> |
| 31 | <tr> <td>Description:</td> <td><% Web.htmlNl (#descr poll) %></td> </tr> |
| 32 | </table> |
| 33 | |
| 34 | <% if #official poll and Poll.membershipLength (Init.getUserId ()) < Poll.votingMembershipRequirement then %> |
| 35 | <h3>You haven't been a member long enough to vote in an official poll.</h3> |
| 36 | <% else %> |
| 37 | <h3>Choices</h3> |
| 38 | |
| 39 | <form action="poll" method="post"> |
| 40 | <input type="hidden" name="vote2" value="<% id %>"> |
| 41 | <% val choices = Poll.listChoicesWithMyVotes id; |
| 42 | if #votes poll = 1 then %> |
| 43 | <select name="v"> |
| 44 | <option value="">Abstain</option> |
| 45 | <% else %> |
| 46 | <select name="v" multiple size="<% length choices %>"> |
| 47 | <% end |
| 48 | foreach (you, cho) in choices do %> |
| 49 | <option value="<% #id cho %>"<% if you then %> selected<% end %>><% Web.html (#descr cho) %></option> |
| 50 | <% end %></select><br><br> |
| 51 | <input type="submit" value="Vote"> |
| 52 | </form> |
| 53 | |
| 54 | <% end |
| 55 | elseif $"vote2" <> "" then |
| 56 | val id = Web.stoi ($"vote2"); |
| 57 | val poll = Poll.lookupPoll id; |
| 58 | editingPoll := SOME id; |
| 59 | |
| 60 | val votes = case Web.getMultiParam "v" of |
| 61 | [""] => [] |
| 62 | | v => map Web.stoi v; |
| 63 | |
| 64 | if #official poll and Poll.membershipLength (Init.getUserId ()) < Poll.votingMembershipRequirement then |
| 65 | %><h3>You haven't been a member long enough to vote in an official poll.</h3><% |
| 66 | elseif length votes > #votes poll then |
| 67 | %><h3>You can't vote for that many different choices!</h3><% |
| 68 | elseif not (Poll.noDupes votes) then |
| 69 | %><h3>You can't vote multiple times for the same choice!</h3><% |
| 70 | else |
| 71 | Poll.vote (Init.getUserId (), id, votes) |
| 72 | %><h3>Thanks for voting!</h3> |
| 73 | <% end |
| 74 | |
| 75 | elseif $"cmd" = "add" then |
| 76 | val title = $"title"; |
| 77 | val starts = $"starts"; |
| 78 | val ends = $"ends"; |
| 79 | val votes = Web.stoi ($"votes"); |
| 80 | val official = $"official" = "on"; |
| 81 | if title = "" then |
| 82 | %><h3>Your poll must have a title.</h3><% |
| 83 | elseif not pollAdmin and not (Poll.dateGeNow starts) then |
| 84 | %><h3>That start date is in the past!</h3><% |
| 85 | elseif not pollAdmin and not (Poll.dateLe (starts, ends)) then |
| 86 | %><h3>The end date comes before the start date!</h3><% |
| 87 | elseif votes <= 0 then |
| 88 | %><h3>You must specify a positive number of votes per person.</h3><% |
| 89 | else |
| 90 | val id = Poll.addPoll (Init.getUserId(), title, $"descr", starts, ends, votes, official); |
| 91 | editingPoll := SOME id; |
| 92 | %><h3>Poll added!</h3><% |
| 93 | end |
| 94 | |
| 95 | elseif $"mod" <> "" then |
| 96 | showNormal := false; |
| 97 | val poll = Poll.lookupPoll (Web.stoi ($"mod")); |
| 98 | |
| 99 | Poll.requireCanModify poll %> |
| 100 | <h3>Modify poll</h3> |
| 101 | |
| 102 | <form action="poll" method="post"> |
| 103 | <input type="hidden" name="id" value="<% $"mod" %>"> |
| 104 | <table class="blanks"> |
| 105 | <tr> <td>Title:</td> <td><input name="title" value="<% Web.html (#title poll) %>"></td> </tr> |
| 106 | <tr> <td>Start date:</td> <td><input name="starts" value="<% Web.html (#starts poll) %>"></td> </tr> |
| 107 | <tr> <td>End date:</td> <td><input name="ends" value="<% Web.html (#ends poll) %>"></td> </tr> |
| 108 | <tr> <td>Max votes/person:</td> <td><input name="votes" value="<% #votes poll %>"></td> </tr> |
| 109 | <tr> <td>Official:</td> <td><input type="checkbox" name="official"<% if #official poll then " checked" end %>></td> </tr> |
| 110 | <tr> <td>Description:</td> <td><textarea name="descr" wrap="soft" rows="5" cols="80"><% Web.html (#descr poll) %></textarea></td> </tr> |
| 111 | <tr> <td><input type="submit" name="cmd" value="Save"></td> </tr> |
| 112 | </table> |
| 113 | </form> |
| 114 | |
| 115 | <% elseif $"cmd" = "Save" then |
| 116 | val poll = Poll.lookupPoll (Web.stoi ($"id")); |
| 117 | |
| 118 | Poll.requireCanModify poll; |
| 119 | |
| 120 | val title = $"title"; |
| 121 | val starts = $"starts"; |
| 122 | val ends = $"ends"; |
| 123 | val votes = Web.stoi ($"votes"); |
| 124 | val official = $"official" = "on"; |
| 125 | if title = "" then |
| 126 | %><h3>Your poll must have a title.</h3><% |
| 127 | elseif not pollAdmin and not (Poll.dateGeNow starts) then |
| 128 | %><h3>That start date is in the past!</h3><% |
| 129 | elseif not pollAdmin and not (Poll.dateLe (starts, ends)) then |
| 130 | %><h3>The end date comes before the start date!</h3><% |
| 131 | elseif votes <= 0 then |
| 132 | %><h3>You must specify a positive number of votes per person.</h3><% |
| 133 | else |
| 134 | Poll.modPoll {poll with title = title, descr = $"descr", starts = starts, ends = ends, votes = votes, official = official}; |
| 135 | editingPoll := SOME (#id poll); |
| 136 | %><h3>Poll record saved.</h3><% |
| 137 | end |
| 138 | |
| 139 | elseif $"del" <> "" then |
| 140 | Group.requireGroupName "poll"; |
| 141 | showNormal := false; |
| 142 | val poll = Poll.lookupPoll (Web.stoi ($"del")) %> |
| 143 | <h3>Are you sure you want to delete poll <a href="poll?id=<% #id poll %>"><% Web.html (#title poll) %></a>?</h3> |
| 144 | <a href="poll?del2=<% $"del" %>">Yes, delete <% Web.html (#title poll) %>!</a> |
| 145 | |
| 146 | <% elseif $"del2" <> "" then |
| 147 | Group.requireGroupName "poll"; |
| 148 | val poll = Poll.lookupPoll (Web.stoi ($"del2")); |
| 149 | Poll.deletePoll (Web.stoi ($"del2")) %> |
| 150 | <h3><% Web.html (#title poll) %> deleted!</h3> |
| 151 | |
| 152 | <% elseif $"addChoice" <> "" then |
| 153 | val id = Web.stoi ($"addChoice"); |
| 154 | editingPoll := SOME id; |
| 155 | val descr = $"descr"; |
| 156 | if descr = "" then |
| 157 | %><h3>Your poll choice must have a description.</h3><% |
| 158 | else |
| 159 | val id = Poll.addChoice (id, Web.stor ($"seq"), descr); |
| 160 | %><h3>Choice added!</h3><% |
| 161 | end |
| 162 | |
| 163 | elseif $"modChoice" <> "" then |
| 164 | showNormal := false; |
| 165 | val id = Web.stoi ($"modChoice"); |
| 166 | val cho = Poll.lookupChoice id; |
| 167 | val poll = Poll.lookupPoll (#pol cho); |
| 168 | Poll.requireCanModify poll %> |
| 169 | |
| 170 | <form action="poll" method="post"> |
| 171 | <input type="hidden" name="saveChoice" value="<% id %>"> |
| 172 | <table class="blanks"> |
| 173 | <tr> <td>Text:</td> <td><input name="descr" value="<% Web.html (#descr cho) %>"></td> </tr> |
| 174 | <tr> <td>Sequence#:</td> <td><input name="seq" value="<% #seq cho %>"></td> </tr> |
| 175 | <tr> <td><input type="submit" value="Save"></td> </tr> |
| 176 | </table> |
| 177 | </form> |
| 178 | |
| 179 | <% elseif $"saveChoice" <> "" then |
| 180 | val id = Web.stoi ($"saveChoice"); |
| 181 | val cho = Poll.lookupChoice id; |
| 182 | val poll = Poll.lookupPoll (#pol cho); |
| 183 | Poll.requireCanModify poll; |
| 184 | editingPoll := SOME (#id poll); |
| 185 | val descr = $"descr"; |
| 186 | if descr = "" then |
| 187 | %><h3>Your poll choice must have a description.</h3><% |
| 188 | else |
| 189 | Poll.modChoice {cho with seq = Web.stor ($"seq"), descr = descr}; |
| 190 | %><h3>Choice saved!</h3><% |
| 191 | end |
| 192 | |
| 193 | elseif $"delChoice" <> "" then |
| 194 | val id = Web.stoi ($"delChoice"); |
| 195 | val cho = Poll.lookupChoice id; |
| 196 | val poll = Poll.lookupPoll (#pol cho); |
| 197 | Poll.requireCanModify poll; |
| 198 | showNormal := false %> |
| 199 | <h3>Are you sure you want to delete choice "<% Web.html (#descr cho) %>"</a>?</h3> |
| 200 | <a href="poll?delChoice2=<% $"delChoice" %>">Yes, delete "<% Web.html (#descr cho) %>"!</a> |
| 201 | |
| 202 | <% elseif $"delChoice2" <> "" then |
| 203 | val id = Web.stoi ($"delChoice2"); |
| 204 | val cho = Poll.lookupChoice id; |
| 205 | val poll = Poll.lookupPoll (#pol cho); |
| 206 | Poll.requireCanModify poll; |
| 207 | Poll.deleteChoice id; |
| 208 | editingPoll := SOME (#id poll) %> |
| 209 | <h3>"<% Web.html (#descr cho) %>" deleted!</h3> |
| 210 | |
| 211 | <% elseif $"report" <> "" then |
| 212 | showNormal := false; |
| 213 | val id = Web.stoi ($"report"); |
| 214 | |
| 215 | val poll = Poll.lookupPoll id; |
| 216 | val canModify = Poll.canModify poll %> |
| 217 | |
| 218 | <h3>Vote Report</h3> |
| 219 | |
| 220 | <p>Voters: |
| 221 | <% ref first = true; |
| 222 | foreach user in Poll.listPollVoters id do |
| 223 | if first then |
| 224 | first := false |
| 225 | else |
| 226 | %>, <% |
| 227 | end |
| 228 | %><a href="user?id=<% #id user %>"><% #name user %></a><% |
| 229 | end %></p> |
| 230 | |
| 231 | <table class="blanks"> |
| 232 | <tr> <td>Poll#</b>:</td> <td><% id %></td> </tr> |
| 233 | <tr> <td>Title</b>:</td> <td><% Web.html (#title poll) %></td> </tr> |
| 234 | <tr> <td>Start:</td> <td><% Web.html (#starts poll) %></td> </tr> |
| 235 | <tr> <td>End:</td> <td><% Web.html (#ends poll) %></td> </tr> |
| 236 | <tr> <td>Votes/person:</td> <td><% #votes poll %></td> </tr> |
| 237 | <tr> <td>Official:</td> <td><% if #official poll then "yes" else "no" end %></td> </tr> |
| 238 | <tr> <td>Description:</td> <td><% Web.htmlNl (#descr poll) %></td> </tr> |
| 239 | </table> |
| 240 | |
| 241 | <br><hr><br> |
| 242 | |
| 243 | <table> |
| 244 | <tr> <td><b>Votes</b></td> <td><b>Choice</b></td> <td><b>Voters</b></td> </tr> |
| 245 | <% foreach (_, total, cho) in Poll.listChoicesWithVotes id do %> |
| 246 | <tr> <td><% total %></td> <td><% Web.html (#descr cho) %></td> <td> |
| 247 | <% ref first = true; |
| 248 | foreach user in Poll.listVoters (#id cho) do |
| 249 | if first then |
| 250 | first := false |
| 251 | else |
| 252 | %>, <% |
| 253 | end |
| 254 | %><a href="user?id=<% #id user %>"><% #name user %></a><% |
| 255 | end |
| 256 | %></td> </tr><% |
| 257 | end %> |
| 258 | </table> |
| 259 | |
| 260 | <% elseif $"id" <> "" then |
| 261 | editingPoll := SOME (Web.stoi ($"id")) |
| 262 | |
| 263 | end %> |
| 264 | |
| 265 | <% switch editingPoll of |
| 266 | SOME id => |
| 267 | val poll = Poll.lookupPoll id; |
| 268 | val canModify = Poll.canModify poll %> |
| 269 | |
| 270 | <table class="blanks"> |
| 271 | <% if canModify then %><tr> <td></td> <td><a href="poll?mod=<% id %>">Edit poll data</a></td> </tr><% end %> |
| 272 | <tr> <td>Poll#:</td> <td><% id %></td> </tr> |
| 273 | <tr> <td>Title:</td> <td><% Web.html (#title poll) %></td> </tr> |
| 274 | <tr> <td>Start:</td> <td><% Web.html (#starts poll) %></td> </tr> |
| 275 | <tr> <td>End:</td> <td><% Web.html (#ends poll) %></td> </tr> |
| 276 | <tr> <td>Votes/person:</td> <td><% #votes poll %></td> </tr> |
| 277 | <tr> <td>Official:</td> <td><% if #official poll then "yes" else "no" end %></td> </tr> |
| 278 | <tr> <td>Description:</td> <td><% Web.htmlNl (#descr poll) %></td> </tr> |
| 279 | </table> |
| 280 | |
| 281 | <h3>Choices<% if Poll.takingVotes poll then %><a href="poll?vote=<% id %>">(Vote!)</a><% end %></h3> |
| 282 | |
| 283 | <p><% Poll.countVoters (#id poll) %> people have voted.</p> |
| 284 | |
| 285 | <% if Poll.takingVotes poll then %> |
| 286 | <table> |
| 287 | <tr> <td><b>You</b></td> <td><b>Total</b></td> </tr> |
| 288 | <% foreach (you, total, cho) in Poll.listChoicesWithVotes id do %> |
| 289 | <tr> <td align="center"><% if you then %>X<% end %></td> |
| 290 | <td align="center"><% total %></td> |
| 291 | <td><% Web.html (#descr cho) %></td> |
| 292 | <% if canModify then %> |
| 293 | <td><i>(<% #seq cho %>)</i> |
| 294 | <a href="poll?modChoice=<% #id cho %>">[Modify]</a> |
| 295 | <a href="poll?delChoice=<% #id cho %>">[Delete]</a></td> |
| 296 | <% end %></tr> |
| 297 | <% end %> |
| 298 | </table> |
| 299 | |
| 300 | <% else |
| 301 | foreach cho in Poll.listChoices id do %> |
| 302 | <li> <% Web.html (#descr cho) %> |
| 303 | <% if canModify then %> |
| 304 | <i>(<% #seq cho %>)</i> |
| 305 | <a href="poll?modChoice=<% #id cho %>">[Modify]</a> |
| 306 | <a href="poll?delChoice=<% #id cho %>">[Delete]</a> |
| 307 | <% end %></li> |
| 308 | <% end |
| 309 | end %> |
| 310 | |
| 311 | <a href="poll?report=<% id %>">Vote Report</a> |
| 312 | |
| 313 | <% if canModify then %> |
| 314 | <br><hr><br> |
| 315 | <h3>Add a new choice</h3> |
| 316 | |
| 317 | <form action="poll" method="post"> |
| 318 | <input type="hidden" name="addChoice" value="<% id %>"> |
| 319 | <table class="blanks"> |
| 320 | <tr> <td>Text:</td> <td><input name="descr"></td> </tr> |
| 321 | <tr> <td>Sequence#:</td> <td><input name="seq" value="<% Poll.nextSeq id %>"></td> </tr> |
| 322 | <tr> <td><input type="submit" value="Add"></td> </tr> |
| 323 | </table> |
| 324 | </form> |
| 325 | |
| 326 | <% end %> |
| 327 | <% | NONE => |
| 328 | if showNormal then |
| 329 | |
| 330 | val mlen = Poll.membershipLength (Init.getUserId ()) %> |
| 331 | |
| 332 | <p>You have been an HCoop member for <% mlen %> days, so you <b>are<% if mlen < Poll.votingMembershipRequirement then %> not<% end %></b> eligible to vote in official polls.</p> |
| 333 | |
| 334 | <% val polls = Poll.listCurrentPolls (); |
| 335 | switch polls of |
| 336 | _::_ => %> |
| 337 | <h3><a href="poll">Current polls</a></h3> |
| 338 | |
| 339 | <% foreach pol in polls do %> |
| 340 | <li> <a href="poll?id=<% #id pol %>"><% Web.html (#title pol) %></a> |
| 341 | <% if Poll.takingVotes pol then %><a href="poll?vote=<% #id pol %>">[VOTE]</a><% end %> |
| 342 | (<% Web.html (#starts pol) %> to <% Web.html (#ends pol) %>)</li> |
| 343 | <% end |
| 344 | end %> |
| 345 | |
| 346 | <p><a href="poll?cmd=list">Show all polls</a></p> |
| 347 | |
| 348 | <h3>Create a poll</h3> |
| 349 | |
| 350 | <form action="poll" method="post"> |
| 351 | <input type="hidden" name="cmd" value="add"> |
| 352 | <table class="blanks"> |
| 353 | <tr> <td>Title:</td> <td><input name="title"></td> </tr> |
| 354 | <tr> <td>Start date:</td> <td><input name="starts"></td> </tr> |
| 355 | <tr> <td>End date:</td> <td><input name="ends"></td> </tr> |
| 356 | <tr> <td>Max votes/person:</td> <td><input name="votes"></td> </tr> |
| 357 | <tr> <td>Official:</td> <td><input type="checkbox" name="official"></td> </tr> |
| 358 | <tr> <td>Description:</td> <td><textarea name="descr" wrap="soft" rows="5" cols="80"></textarea></td> </tr> |
| 359 | <tr> <td><input type="submit" value="Create"></td> </tr> |
| 360 | </table> |
| 361 | </form> |
| 362 | |
| 363 | <% end |
| 364 | end %> |
| 365 | |
| 366 | <% @footer[] %> |