More Checkout payment options
[bpt/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
a0a15865 90 val id = Poll.addPoll (Init.getUserId(), title, $"descr", starts, ends, votes, official);
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>
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>
a0a15865 109<tr> <td>Official:</td> <td><input type="checkbox" name="official"<% if #official poll then " checked" end %>></td> </tr>
b6dd1aaf 110<tr> <td>Description:</td> <td><textarea name="descr" wrap="soft" rows="5" cols="80"><% Web.html (#descr poll) %></textarea></td> </tr>
e68ddb80
AC
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");
a0a15865 124 val official = $"official" = "on";
e68ddb80 125 if title = "" then
b6dd1aaf 126 %><h3>Your poll must have a title.</h3><%
93f77ca7 127 elseif not pollAdmin and not (Poll.dateGeNow starts) then
b6dd1aaf 128 %><h3>That start date is in the past!</h3><%
93f77ca7 129 elseif not pollAdmin and not (Poll.dateLe (starts, ends)) then
b6dd1aaf 130 %><h3>The end date comes before the start date!</h3><%
e68ddb80 131 elseif votes <= 0 then
b6dd1aaf 132 %><h3>You must specify a positive number of votes per person.</h3><%
e68ddb80 133 else
a0a15865 134 Poll.modPoll {poll with title = title, descr = $"descr", starts = starts, ends = ends, votes = votes, official = official};
e68ddb80 135 editingPoll := SOME (#id poll);
b6dd1aaf 136 %><h3>Poll record saved.</h3><%
e68ddb80
AC
137 end
138
139elseif $"del" <> "" then
140 Group.requireGroupName "poll";
141 showNormal := false;
142 val poll = Poll.lookupPoll (Web.stoi ($"del")) %>
b6dd1aaf 143 <h3>Are you sure you want to delete poll <a href="poll?id=<% #id poll %>"><% Web.html (#title poll) %></a>?</h3>
e68ddb80
AC
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")) %>
b6dd1aaf 150 <h3><% Web.html (#title poll) %> deleted!</h3>
e68ddb80
AC
151
152<% elseif $"addChoice" <> "" then
153 val id = Web.stoi ($"addChoice");
154 editingPoll := SOME id;
155 val descr = $"descr";
156 if descr = "" then
b6dd1aaf 157 %><h3>Your poll choice must have a description.</h3><%
e68ddb80
AC
158 else
159 val id = Poll.addChoice (id, Web.stor ($"seq"), descr);
b6dd1aaf 160 %><h3>Choice added!</h3><%
e68ddb80
AC
161 end
162
163elseif $"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
a4ccdb5e 170<form action="poll" method="post">
e68ddb80 171<input type="hidden" name="saveChoice" value="<% id %>">
b6dd1aaf
AC
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>
e68ddb80
AC
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
b6dd1aaf 187 %><h3>Your poll choice must have a description.</h3><%
e68ddb80
AC
188 else
189 Poll.modChoice {cho with seq = Web.stor ($"seq"), descr = descr};
b6dd1aaf 190 %><h3>Choice saved!</h3><%
e68ddb80
AC
191 end
192
193elseif $"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 %>
b6dd1aaf 199 <h3>Are you sure you want to delete choice "<% Web.html (#descr cho) %>"</a>?</h3>
e68ddb80
AC
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) %>
b6dd1aaf 209 <h3>"<% Web.html (#descr cho) %>" deleted!</h3>
e68ddb80 210
56dbfc30
AC
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
b6dd1aaf 218<h3>Vote Report</h3>
56dbfc30 219
1d2cae17
AC
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
b6dd1aaf
AC
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>
bf47b57b 234<tr> <td>Start:</td> <td><% Web.html (#starts poll) %></td> </tr>
b6dd1aaf
AC
235<tr> <td>End:</td> <td><% Web.html (#ends poll) %></td> </tr>
236<tr> <td>Votes/person:</td> <td><% #votes poll %></td> </tr>
a0a15865 237<tr> <td>Official:</td> <td><% if #official poll then "yes" else "no" end %></td> </tr>
b6dd1aaf 238<tr> <td>Description:</td> <td><% Web.htmlNl (#descr poll) %></td> </tr>
56dbfc30
AC
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><%
257end %>
258</table>
259
e68ddb80
AC
260<% elseif $"id" <> "" then
261 editingPoll := SOME (Web.stoi ($"id"))
262
263end %>
264
265<% switch editingPoll of
266 SOME id =>
56dbfc30
AC
267 val poll = Poll.lookupPoll id;
268 val canModify = Poll.canModify poll %>
e68ddb80 269
b6dd1aaf 270<table class="blanks">
e68ddb80 271<% if canModify then %><tr> <td></td> <td><a href="poll?mod=<% id %>">Edit poll data</a></td> </tr><% end %>
b6dd1aaf
AC
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>
a0a15865 277<tr> <td>Official:</td> <td><% if #official poll then "yes" else "no" end %></td> </tr>
b6dd1aaf 278<tr> <td>Description:</td> <td><% Web.htmlNl (#descr poll) %></td> </tr>
e68ddb80
AC
279</table>
280
b6dd1aaf 281<h3>Choices<% if Poll.takingVotes poll then %><a href="poll?vote=<% id %>">(Vote!)</a><% end %></h3>
e68ddb80 282
1d2cae17
AC
283<p><% Poll.countVoters (#id poll) %> people have voted.</p>
284
56dbfc30
AC
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
56dbfc30
AC
300<% else
301foreach cho in Poll.listChoices id do %>
e68ddb80
AC
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>
56dbfc30
AC
308<% end
309end %>
e68ddb80 310
b90b0980
AC
311<a href="poll?report=<% id %>">Vote Report</a>
312
e68ddb80
AC
313<% if canModify then %>
314<br><hr><br>
b6dd1aaf 315<h3>Add a new choice</h3>
e68ddb80 316
a4ccdb5e 317<form action="poll" method="post">
e68ddb80 318<input type="hidden" name="addChoice" value="<% id %>">
b6dd1aaf
AC
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>
e68ddb80
AC
322<tr> <td><input type="submit" value="Add"></td> </tr>
323</table>
324</form>
325
326<% end %>
327<% | NONE =>
972c70b7 328if showNormal then
e68ddb80 329
a0a15865
AC
330val 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 ();
972c70b7
AC
335switch 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
344end %>
345
346<p><a href="poll?cmd=list">Show all polls</a></p>
e68ddb80 347
b6dd1aaf 348<h3>Create a poll</h3>
e68ddb80 349
a4ccdb5e 350<form action="poll" method="post">
e68ddb80 351<input type="hidden" name="cmd" value="add">
b6dd1aaf
AC
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>
a0a15865 357<tr> <td>Official:</td> <td><input type="checkbox" name="official"></td> </tr>
b6dd1aaf 358<tr> <td>Description:</td> <td><textarea name="descr" wrap="soft" rows="5" cols="80"></textarea></td> </tr>
e68ddb80
AC
359<tr> <td><input type="submit" value="Create"></td> </tr>
360</table>
361</form>
362
363<% end
364end %>
365
366<% @footer[] %>