Add [X]HTML5 DTD to header
[hcoop/portal.git] / poll.mlt
CommitLineData
e68ddb80
AC
1<% @header[("title", ["Polls"])];
2
3val pollAdmin = Group.inGroupName "poll";
4
5ref editingPoll = NONE;
6ref showNormal = true;
7
8if $"cmd" = "list" then
9 showNormal := false %>
10
b6dd1aaf 11<h2>All polls</h2>
e68ddb80
AC
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
56dbfc30
AC
19elseif $"vote" <> "" then
20 showNormal := false;
21 val id = Web.stoi ($"vote");
22 val poll = Poll.lookupPoll id %>
23
b6dd1aaf
AC
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>
a0a15865 30<tr> <td>Official:</td> <td><% if #official poll then "yes" else "no" end %></td> </tr>
b6dd1aaf 31<tr> <td>Description:</td> <td><% Web.htmlNl (#descr poll) %></td> </tr>
56dbfc30
AC
32</table>
33
a0a15865
AC
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 %>
b6dd1aaf 37<h3>Choices</h3>
56dbfc30 38
a4ccdb5e 39<form action="poll" method="post">
56dbfc30
AC
40<input type="hidden" name="vote2" value="<% id %>">
41<% val choices = Poll.listChoicesWithMyVotes id;
42if #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
48foreach (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
a0a15865
AC
54<% end
55elseif $"vote2" <> "" then
56dbfc30
AC
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
a0a15865
AC
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
b6dd1aaf 67 %><h3>You can't vote for that many different choices!</h3><%
56dbfc30 68 elseif not (Poll.noDupes votes) then
b6dd1aaf 69 %><h3>You can't vote multiple times for the same choice!</h3><%
56dbfc30
AC
70 else
71 Poll.vote (Init.getUserId (), id, votes)
b6dd1aaf 72 %><h3>Thanks for voting!</h3>
56dbfc30
AC
73<% end
74
e68ddb80
AC
75elseif $"cmd" = "add" then
76 val title = $"title";
77 val starts = $"starts";
78 val ends = $"ends";
79 val votes = Web.stoi ($"votes");
a0a15865 80 val official = $"official" = "on";
e68ddb80 81 if title = "" then
b6dd1aaf 82 %><h3>Your poll must have a title.</h3><%
93f77ca7 83 elseif not pollAdmin and not (Poll.dateGeNow starts) then
b6dd1aaf 84 %><h3>That start date is in the past!</h3><%
93f77ca7 85 elseif not pollAdmin and not (Poll.dateLe (starts, ends)) then
b6dd1aaf 86 %><h3>The end date comes before the start date!</h3><%
e68ddb80 87 elseif votes <= 0 then
b6dd1aaf 88 %><h3>You must specify a positive number of votes per person.</h3><%
e68ddb80 89 else
ed97a006 90 val id = Poll.addPoll (Init.getUserId(), title, $"descr", starts, ends, votes, official, false);
e68ddb80 91 editingPoll := SOME id;
b6dd1aaf 92 %><h3>Poll added!</h3><%
e68ddb80
AC
93 end
94
95elseif $"mod" <> "" then
96 showNormal := false;
97 val poll = Poll.lookupPoll (Web.stoi ($"mod"));
98
99 Poll.requireCanModify poll %>
b6dd1aaf 100<h3>Modify poll</h3>
e68ddb80 101
a4ccdb5e 102<form action="poll" method="post">
e68ddb80 103<input type="hidden" name="id" value="<% $"mod" %>">
b6dd1aaf
AC
104<table class="blanks">
105<tr> <td>Title:</td> <td><input name="title" value="<% Web.html (#title poll) %>"></td> </tr>
ed97a006 106<tr> <td>Ready?</td> <td><input type="checkbox" name="ready" <% if #ready poll then " checked" else "" end %>></td> </tr>
b6dd1aaf
AC
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>
a0a15865 110<tr> <td>Official:</td> <td><input type="checkbox" name="official"<% if #official poll then " checked" end %>></td> </tr>
b6dd1aaf 111<tr> <td>Description:</td> <td><textarea name="descr" wrap="soft" rows="5" cols="80"><% Web.html (#descr poll) %></textarea></td> </tr>
e68ddb80
AC
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");
a0a15865 125 val official = $"official" = "on";
ed97a006
AC
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
b6dd1aaf 130 %><h3>Your poll must have a title.</h3><%
93f77ca7 131 elseif not pollAdmin and not (Poll.dateGeNow starts) then
b6dd1aaf 132 %><h3>That start date is in the past!</h3><%
93f77ca7 133 elseif not pollAdmin and not (Poll.dateLe (starts, ends)) then
b6dd1aaf 134 %><h3>The end date comes before the start date!</h3><%
e68ddb80 135 elseif votes <= 0 then
b6dd1aaf 136 %><h3>You must specify a positive number of votes per person.</h3><%
e68ddb80 137 else
ed97a006
AC
138 Poll.modPoll {poll with title = title, descr = $"descr", starts = starts, ends = ends, votes = votes, official = official,
139 ready = ready};
e68ddb80 140 editingPoll := SOME (#id poll);
b6dd1aaf 141 %><h3>Poll record saved.</h3><%
e68ddb80
AC
142 end
143
144elseif $"del" <> "" then
e68ddb80 145 showNormal := false;
6aa0cd21
AC
146 val poll = Poll.lookupPoll (Web.stoi ($"del"));
147 Poll.requireCanModify poll %>
b6dd1aaf 148 <h3>Are you sure you want to delete poll <a href="poll?id=<% #id poll %>"><% Web.html (#title poll) %></a>?</h3>
e68ddb80
AC
149 <a href="poll?del2=<% $"del" %>">Yes, delete <% Web.html (#title poll) %>!</a>
150
151<% elseif $"del2" <> "" then
e68ddb80 152 val poll = Poll.lookupPoll (Web.stoi ($"del2"));
6aa0cd21 153 Poll.requireCanModify poll;
e68ddb80 154 Poll.deletePoll (Web.stoi ($"del2")) %>
b6dd1aaf 155 <h3><% Web.html (#title poll) %> deleted!</h3>
e68ddb80
AC
156
157<% elseif $"addChoice" <> "" then
158 val id = Web.stoi ($"addChoice");
159 editingPoll := SOME id;
160 val descr = $"descr";
ed97a006
AC
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
b6dd1aaf 165 %><h3>Your poll choice must have a description.</h3><%
e68ddb80
AC
166 else
167 val id = Poll.addChoice (id, Web.stor ($"seq"), descr);
b6dd1aaf 168 %><h3>Choice added!</h3><%
e68ddb80
AC
169 end
170
171elseif $"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
a4ccdb5e 178<form action="poll" method="post">
e68ddb80 179<input type="hidden" name="saveChoice" value="<% id %>">
b6dd1aaf
AC
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>
e68ddb80
AC
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
b6dd1aaf 195 %><h3>Your poll choice must have a description.</h3><%
e68ddb80
AC
196 else
197 Poll.modChoice {cho with seq = Web.stor ($"seq"), descr = descr};
b6dd1aaf 198 %><h3>Choice saved!</h3><%
e68ddb80
AC
199 end
200
201elseif $"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 %>
b6dd1aaf 207 <h3>Are you sure you want to delete choice "<% Web.html (#descr cho) %>"</a>?</h3>
e68ddb80
AC
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) %>
b6dd1aaf 217 <h3>"<% Web.html (#descr cho) %>" deleted!</h3>
e68ddb80 218
56dbfc30
AC
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
b6dd1aaf 226<h3>Vote Report</h3>
56dbfc30 227
1d2cae17
AC
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
b6dd1aaf
AC
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>
bf47b57b 242<tr> <td>Start:</td> <td><% Web.html (#starts poll) %></td> </tr>
b6dd1aaf
AC
243<tr> <td>End:</td> <td><% Web.html (#ends poll) %></td> </tr>
244<tr> <td>Votes/person:</td> <td><% #votes poll %></td> </tr>
a0a15865 245<tr> <td>Official:</td> <td><% if #official poll then "yes" else "no" end %></td> </tr>
b6dd1aaf 246<tr> <td>Description:</td> <td><% Web.htmlNl (#descr poll) %></td> </tr>
56dbfc30
AC
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><%
265end %>
266</table>
267
e68ddb80
AC
268<% elseif $"id" <> "" then
269 editingPoll := SOME (Web.stoi ($"id"))
270
271end %>
272
273<% switch editingPoll of
274 SOME id =>
56dbfc30
AC
275 val poll = Poll.lookupPoll id;
276 val canModify = Poll.canModify poll %>
e68ddb80 277
b6dd1aaf 278<table class="blanks">
e68ddb80 279<% if canModify then %><tr> <td></td> <td><a href="poll?mod=<% id %>">Edit poll data</a></td> </tr><% end %>
b6dd1aaf
AC
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>
a0a15865 285<tr> <td>Official:</td> <td><% if #official poll then "yes" else "no" end %></td> </tr>
b6dd1aaf 286<tr> <td>Description:</td> <td><% Web.htmlNl (#descr poll) %></td> </tr>
e68ddb80
AC
287</table>
288
b6dd1aaf 289<h3>Choices<% if Poll.takingVotes poll then %><a href="poll?vote=<% id %>">(Vote!)</a><% end %></h3>
e68ddb80 290
1d2cae17
AC
291<p><% Poll.countVoters (#id poll) %> people have voted.</p>
292
56dbfc30
AC
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
56dbfc30
AC
308<% else
309foreach cho in Poll.listChoices id do %>
e68ddb80
AC
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>
56dbfc30
AC
316<% end
317end %>
e68ddb80 318
b90b0980
AC
319<a href="poll?report=<% id %>">Vote Report</a>
320
e68ddb80
AC
321<% if canModify then %>
322<br><hr><br>
6aa0cd21
AC
323<h3><a href="?del=<% id %>">Delete this poll</a></h3>
324
b6dd1aaf 325<h3>Add a new choice</h3>
e68ddb80 326
a4ccdb5e 327<form action="poll" method="post">
e68ddb80 328<input type="hidden" name="addChoice" value="<% id %>">
b6dd1aaf
AC
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>
e68ddb80
AC
332<tr> <td><input type="submit" value="Add"></td> </tr>
333</table>
334</form>
335
336<% end %>
337<% | NONE =>
972c70b7 338if showNormal then
e68ddb80 339
a0a15865
AC
340val 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 ();
972c70b7
AC
345switch 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
354end %>
355
356<p><a href="poll?cmd=list">Show all polls</a></p>
e68ddb80 357
b6dd1aaf 358<h3>Create a poll</h3>
e68ddb80 359
a4ccdb5e 360<form action="poll" method="post">
e68ddb80 361<input type="hidden" name="cmd" value="add">
b6dd1aaf
AC
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>
a0a15865 367<tr> <td>Official:</td> <td><input type="checkbox" name="official"></td> </tr>
b6dd1aaf 368<tr> <td>Description:</td> <td><textarea name="descr" wrap="soft" rows="5" cols="80"></textarea></td> </tr>
e68ddb80
AC
369<tr> <td><input type="submit" value="Create"></td> </tr>
370</table>
371</form>
372
373<% end
374end %>
375
376<% @footer[] %>