| 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, false); |
| 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>Ready?</td> <td><input type="checkbox" name="ready" <% if #ready poll then " checked" else "" end %>></td> </tr> |
| 107 | <tr> <td>Start date:</td> <td><input name="starts" value="<% Web.html (#starts poll) %>"></td> </tr> |
| 108 | <tr> <td>End date:</td> <td><input name="ends" value="<% Web.html (#ends poll) %>"></td> </tr> |
| 109 | <tr> <td>Max votes/person:</td> <td><input name="votes" value="<% #votes poll %>"></td> </tr> |
| 110 | <tr> <td>Official:</td> <td><input type="checkbox" name="official"<% if #official poll then " checked" end %>></td> </tr> |
| 111 | <tr> <td>Description:</td> <td><textarea name="descr" wrap="soft" rows="5" cols="80"><% Web.html (#descr poll) %></textarea></td> </tr> |
| 112 | <tr> <td><input type="submit" name="cmd" value="Save"></td> </tr> |
| 113 | </table> |
| 114 | </form> |
| 115 | |
| 116 | <% elseif $"cmd" = "Save" then |
| 117 | val poll = Poll.lookupPoll (Web.stoi ($"id")); |
| 118 | |
| 119 | Poll.requireCanModify poll; |
| 120 | |
| 121 | val title = $"title"; |
| 122 | val starts = $"starts"; |
| 123 | val ends = $"ends"; |
| 124 | val votes = Web.stoi ($"votes"); |
| 125 | val official = $"official" = "on"; |
| 126 | val ready = $"ready" = "on"; |
| 127 | if not (Poll.canModify poll) then |
| 128 | %><h3>You can't modify this poll anymore, because voting is already open.</h3><% |
| 129 | elseif title = "" then |
| 130 | %><h3>Your poll must have a title.</h3><% |
| 131 | elseif not pollAdmin and not (Poll.dateGeNow starts) then |
| 132 | %><h3>That start date is in the past!</h3><% |
| 133 | elseif not pollAdmin and not (Poll.dateLe (starts, ends)) then |
| 134 | %><h3>The end date comes before the start date!</h3><% |
| 135 | elseif votes <= 0 then |
| 136 | %><h3>You must specify a positive number of votes per person.</h3><% |
| 137 | else |
| 138 | Poll.modPoll {poll with title = title, descr = $"descr", starts = starts, ends = ends, votes = votes, official = official, |
| 139 | ready = ready}; |
| 140 | editingPoll := SOME (#id poll); |
| 141 | %><h3>Poll record saved.</h3><% |
| 142 | end |
| 143 | |
| 144 | elseif $"del" <> "" then |
| 145 | showNormal := false; |
| 146 | val poll = Poll.lookupPoll (Web.stoi ($"del")); |
| 147 | Poll.requireCanModify poll %> |
| 148 | <h3>Are you sure you want to delete poll <a href="poll?id=<% #id poll %>"><% Web.html (#title poll) %></a>?</h3> |
| 149 | <a href="poll?del2=<% $"del" %>">Yes, delete <% Web.html (#title poll) %>!</a> |
| 150 | |
| 151 | <% elseif $"del2" <> "" then |
| 152 | val poll = Poll.lookupPoll (Web.stoi ($"del2")); |
| 153 | Poll.requireCanModify poll; |
| 154 | Poll.deletePoll (Web.stoi ($"del2")) %> |
| 155 | <h3><% Web.html (#title poll) %> deleted!</h3> |
| 156 | |
| 157 | <% elseif $"addChoice" <> "" then |
| 158 | val id = Web.stoi ($"addChoice"); |
| 159 | editingPoll := SOME id; |
| 160 | val descr = $"descr"; |
| 161 | val poll = Poll.lookupPoll id; |
| 162 | if not (Poll.canModify poll) then |
| 163 | %><h3>You can't modify this poll anymore, because voting is already open.</h3><% |
| 164 | elseif descr = "" then |
| 165 | %><h3>Your poll choice must have a description.</h3><% |
| 166 | else |
| 167 | val id = Poll.addChoice (id, Web.stor ($"seq"), descr); |
| 168 | %><h3>Choice added!</h3><% |
| 169 | end |
| 170 | |
| 171 | elseif $"modChoice" <> "" then |
| 172 | showNormal := false; |
| 173 | val id = Web.stoi ($"modChoice"); |
| 174 | val cho = Poll.lookupChoice id; |
| 175 | val poll = Poll.lookupPoll (#pol cho); |
| 176 | Poll.requireCanModify poll %> |
| 177 | |
| 178 | <form action="poll" method="post"> |
| 179 | <input type="hidden" name="saveChoice" value="<% id %>"> |
| 180 | <table class="blanks"> |
| 181 | <tr> <td>Text:</td> <td><input name="descr" value="<% Web.html (#descr cho) %>"></td> </tr> |
| 182 | <tr> <td>Sequence#:</td> <td><input name="seq" value="<% #seq cho %>"></td> </tr> |
| 183 | <tr> <td><input type="submit" value="Save"></td> </tr> |
| 184 | </table> |
| 185 | </form> |
| 186 | |
| 187 | <% elseif $"saveChoice" <> "" then |
| 188 | val id = Web.stoi ($"saveChoice"); |
| 189 | val cho = Poll.lookupChoice id; |
| 190 | val poll = Poll.lookupPoll (#pol cho); |
| 191 | Poll.requireCanModify poll; |
| 192 | editingPoll := SOME (#id poll); |
| 193 | val descr = $"descr"; |
| 194 | if descr = "" then |
| 195 | %><h3>Your poll choice must have a description.</h3><% |
| 196 | else |
| 197 | Poll.modChoice {cho with seq = Web.stor ($"seq"), descr = descr}; |
| 198 | %><h3>Choice saved!</h3><% |
| 199 | end |
| 200 | |
| 201 | elseif $"delChoice" <> "" then |
| 202 | val id = Web.stoi ($"delChoice"); |
| 203 | val cho = Poll.lookupChoice id; |
| 204 | val poll = Poll.lookupPoll (#pol cho); |
| 205 | Poll.requireCanModify poll; |
| 206 | showNormal := false %> |
| 207 | <h3>Are you sure you want to delete choice "<% Web.html (#descr cho) %>"</a>?</h3> |
| 208 | <a href="poll?delChoice2=<% $"delChoice" %>">Yes, delete "<% Web.html (#descr cho) %>"!</a> |
| 209 | |
| 210 | <% elseif $"delChoice2" <> "" then |
| 211 | val id = Web.stoi ($"delChoice2"); |
| 212 | val cho = Poll.lookupChoice id; |
| 213 | val poll = Poll.lookupPoll (#pol cho); |
| 214 | Poll.requireCanModify poll; |
| 215 | Poll.deleteChoice id; |
| 216 | editingPoll := SOME (#id poll) %> |
| 217 | <h3>"<% Web.html (#descr cho) %>" deleted!</h3> |
| 218 | |
| 219 | <% elseif $"report" <> "" then |
| 220 | showNormal := false; |
| 221 | val id = Web.stoi ($"report"); |
| 222 | |
| 223 | val poll = Poll.lookupPoll id; |
| 224 | val canModify = Poll.canModify poll %> |
| 225 | |
| 226 | <h3>Vote Report</h3> |
| 227 | |
| 228 | <p>Voters: |
| 229 | <% ref first = true; |
| 230 | foreach user in Poll.listPollVoters id do |
| 231 | if first then |
| 232 | first := false |
| 233 | else |
| 234 | %>, <% |
| 235 | end |
| 236 | %><a href="user?id=<% #id user %>"><% #name user %></a><% |
| 237 | end %></p> |
| 238 | |
| 239 | <table class="blanks"> |
| 240 | <tr> <td>Poll#</b>:</td> <td><% id %></td> </tr> |
| 241 | <tr> <td>Title</b>:</td> <td><% Web.html (#title poll) %></td> </tr> |
| 242 | <tr> <td>Start:</td> <td><% Web.html (#starts poll) %></td> </tr> |
| 243 | <tr> <td>End:</td> <td><% Web.html (#ends poll) %></td> </tr> |
| 244 | <tr> <td>Votes/person:</td> <td><% #votes poll %></td> </tr> |
| 245 | <tr> <td>Official:</td> <td><% if #official poll then "yes" else "no" end %></td> </tr> |
| 246 | <tr> <td>Description:</td> <td><% Web.htmlNl (#descr poll) %></td> </tr> |
| 247 | </table> |
| 248 | |
| 249 | <br><hr><br> |
| 250 | |
| 251 | <table> |
| 252 | <tr> <td><b>Votes</b></td> <td><b>Choice</b></td> <td><b>Voters</b></td> </tr> |
| 253 | <% foreach (_, total, cho) in Poll.listChoicesWithVotes id do %> |
| 254 | <tr> <td><% total %></td> <td><% Web.html (#descr cho) %></td> <td> |
| 255 | <% ref first = true; |
| 256 | foreach user in Poll.listVoters (#id cho) do |
| 257 | if first then |
| 258 | first := false |
| 259 | else |
| 260 | %>, <% |
| 261 | end |
| 262 | %><a href="user?id=<% #id user %>"><% #name user %></a><% |
| 263 | end |
| 264 | %></td> </tr><% |
| 265 | end %> |
| 266 | </table> |
| 267 | |
| 268 | <% elseif $"id" <> "" then |
| 269 | editingPoll := SOME (Web.stoi ($"id")) |
| 270 | |
| 271 | end %> |
| 272 | |
| 273 | <% switch editingPoll of |
| 274 | SOME id => |
| 275 | val poll = Poll.lookupPoll id; |
| 276 | val canModify = Poll.canModify poll %> |
| 277 | |
| 278 | <table class="blanks"> |
| 279 | <% if canModify then %><tr> <td></td> <td><a href="poll?mod=<% id %>">Edit poll data</a></td> </tr><% end %> |
| 280 | <tr> <td>Poll#:</td> <td><% id %></td> </tr> |
| 281 | <tr> <td>Title:</td> <td><% Web.html (#title poll) %></td> </tr> |
| 282 | <tr> <td>Start:</td> <td><% Web.html (#starts poll) %></td> </tr> |
| 283 | <tr> <td>End:</td> <td><% Web.html (#ends poll) %></td> </tr> |
| 284 | <tr> <td>Votes/person:</td> <td><% #votes poll %></td> </tr> |
| 285 | <tr> <td>Official:</td> <td><% if #official poll then "yes" else "no" end %></td> </tr> |
| 286 | <tr> <td>Description:</td> <td><% Web.htmlNl (#descr poll) %></td> </tr> |
| 287 | </table> |
| 288 | |
| 289 | <h3>Choices<% if Poll.takingVotes poll then %><a href="poll?vote=<% id %>">(Vote!)</a><% end %></h3> |
| 290 | |
| 291 | <p><% Poll.countVoters (#id poll) %> people have voted.</p> |
| 292 | |
| 293 | <% if Poll.takingVotes poll then %> |
| 294 | <table> |
| 295 | <tr> <td><b>You</b></td> <td><b>Total</b></td> </tr> |
| 296 | <% foreach (you, total, cho) in Poll.listChoicesWithVotes id do %> |
| 297 | <tr> <td align="center"><% if you then %>X<% end %></td> |
| 298 | <td align="center"><% total %></td> |
| 299 | <td><% Web.html (#descr cho) %></td> |
| 300 | <% if canModify then %> |
| 301 | <td><i>(<% #seq cho %>)</i> |
| 302 | <a href="poll?modChoice=<% #id cho %>">[Modify]</a> |
| 303 | <a href="poll?delChoice=<% #id cho %>">[Delete]</a></td> |
| 304 | <% end %></tr> |
| 305 | <% end %> |
| 306 | </table> |
| 307 | |
| 308 | <% else |
| 309 | foreach cho in Poll.listChoices id do %> |
| 310 | <li> <% Web.html (#descr cho) %> |
| 311 | <% if canModify then %> |
| 312 | <i>(<% #seq cho %>)</i> |
| 313 | <a href="poll?modChoice=<% #id cho %>">[Modify]</a> |
| 314 | <a href="poll?delChoice=<% #id cho %>">[Delete]</a> |
| 315 | <% end %></li> |
| 316 | <% end |
| 317 | end %> |
| 318 | |
| 319 | <a href="poll?report=<% id %>">Vote Report</a> |
| 320 | |
| 321 | <% if canModify then %> |
| 322 | <br><hr><br> |
| 323 | <h3><a href="?del=<% id %>">Delete this poll</a></h3> |
| 324 | |
| 325 | <h3>Add a new choice</h3> |
| 326 | |
| 327 | <form action="poll" method="post"> |
| 328 | <input type="hidden" name="addChoice" value="<% id %>"> |
| 329 | <table class="blanks"> |
| 330 | <tr> <td>Text:</td> <td><input name="descr"></td> </tr> |
| 331 | <tr> <td>Sequence#:</td> <td><input name="seq" value="<% Poll.nextSeq id %>"></td> </tr> |
| 332 | <tr> <td><input type="submit" value="Add"></td> </tr> |
| 333 | </table> |
| 334 | </form> |
| 335 | |
| 336 | <% end %> |
| 337 | <% | NONE => |
| 338 | if showNormal then |
| 339 | |
| 340 | val mlen = Poll.membershipLength (Init.getUserId ()) %> |
| 341 | |
| 342 | <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> |
| 343 | |
| 344 | <% val polls = Poll.listCurrentPolls (); |
| 345 | switch polls of |
| 346 | _::_ => %> |
| 347 | <h3><a href="poll">Current polls</a></h3> |
| 348 | |
| 349 | <% foreach pol in polls do %> |
| 350 | <li> <a href="poll?id=<% #id pol %>"><% Web.html (#title pol) %></a> |
| 351 | <% if Poll.takingVotes pol then %><a href="poll?vote=<% #id pol %>">[VOTE]</a><% end %> |
| 352 | (<% Web.html (#starts pol) %> to <% Web.html (#ends pol) %>)</li> |
| 353 | <% end |
| 354 | end %> |
| 355 | |
| 356 | <p><a href="poll?cmd=list">Show all polls</a></p> |
| 357 | |
| 358 | <h3>Create a poll</h3> |
| 359 | |
| 360 | <form action="poll" method="post"> |
| 361 | <input type="hidden" name="cmd" value="add"> |
| 362 | <table class="blanks"> |
| 363 | <tr> <td>Title:</td> <td><input name="title"></td> </tr> |
| 364 | <tr> <td>Start date:</td> <td><input name="starts"></td> </tr> |
| 365 | <tr> <td>End date:</td> <td><input name="ends"></td> </tr> |
| 366 | <tr> <td>Max votes/person:</td> <td><input name="votes"></td> </tr> |
| 367 | <tr> <td>Official:</td> <td><input type="checkbox" name="official"></td> </tr> |
| 368 | <tr> <td>Description:</td> <td><textarea name="descr" wrap="soft" rows="5" cols="80"></textarea></td> </tr> |
| 369 | <tr> <td><input type="submit" value="Create"></td> </tr> |
| 370 | </table> |
| 371 | </form> |
| 372 | |
| 373 | <% end |
| 374 | end %> |
| 375 | |
| 376 | <% @footer[] %> |