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