Poll administration
[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
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
19elseif $"cmd" = "add" then
20 val title = $"title";
21 val starts = $"starts";
22 val ends = $"ends";
23 val votes = Web.stoi ($"votes");
24 if title = "" then
25 %><h3><b>Your poll must have a title.</b></h3><%
26 elseif not (Poll.dateGeNow starts) then
27 %><h3><b>That start date is in the past!</b></h3><%
28 elseif not (Poll.dateLe (starts, ends)) then
29 %><h3><b>The end date comes before the start date!</b></h3><%
30 elseif votes <= 0 then
31 %><h3><b>You must specify a positive number of votes per person.</b></h3><%
32 else
33 val id = Poll.addPoll (Init.getUserId(), title, $"descr", starts, ends, votes);
34 editingPoll := SOME id;
35 %><h3><b>Poll added!</b></h3><%
36 end
37
38elseif $"mod" <> "" then
39 showNormal := false;
40 val poll = Poll.lookupPoll (Web.stoi ($"mod"));
41
42 Poll.requireCanModify poll %>
43<h3><b>Modify poll</b></h3>
44
45<form action="poll">
46<input type="hidden" name="id" value="<% $"mod" %>">
47<table>
48<tr> <td align="right"><b>Title</b>:</td> <td><input name="title" value="<% Web.html (#title poll) %>"></td> </tr>
49<tr> <td align="right"><b>Start date</b>:</td> <td><input name="starts" value="<% Web.html (#starts poll) %>"></td> </tr>
50<tr> <td align="right"><b>End date</b>:</td> <td><input name="ends" value="<% Web.html (#ends poll) %>"></td> </tr>
51<tr> <td align="right"><b>Max votes/person</b>:</td> <td><input name="votes" value="<% #votes poll %>"></td> </tr>
52<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>
53<tr> <td><input type="submit" name="cmd" value="Save"></td> </tr>
54</table>
55</form>
56
57<% elseif $"cmd" = "Save" then
58 val poll = Poll.lookupPoll (Web.stoi ($"id"));
59
60 Poll.requireCanModify poll;
61
62 val title = $"title";
63 val starts = $"starts";
64 val ends = $"ends";
65 val votes = Web.stoi ($"votes");
66 if title = "" then
67 %><h3><b>Your poll must have a title.</b></h3><%
68 elseif not (Poll.dateGeNow starts) then
69 %><h3><b>That start date is in the past!</b></h3><%
70 elseif not (Poll.dateLe (starts, ends)) then
71 %><h3><b>The end date comes before the start date!</b></h3><%
72 elseif votes <= 0 then
73 %><h3><b>You must specify a positive number of votes per person.</b></h3><%
74 else
75 Poll.modPoll {poll with title = title, descr = $"descr", starts = starts, ends = ends, votes = votes};
76 editingPoll := SOME (#id poll);
77 %><h3><b>Poll record saved.</b></h3><%
78 end
79
80elseif $"del" <> "" then
81 Group.requireGroupName "poll";
82 showNormal := false;
83 val poll = Poll.lookupPoll (Web.stoi ($"del")) %>
84 <h3><b>Are you sure you want to delete poll <a href="poll?id=<% #id poll %>"><% Web.html (#title poll) %></a>?</b></h3>
85 <a href="poll?del2=<% $"del" %>">Yes, delete <% Web.html (#title poll) %>!</a>
86
87<% elseif $"del2" <> "" then
88 Group.requireGroupName "poll";
89 val poll = Poll.lookupPoll (Web.stoi ($"del2"));
90 Poll.deletePoll (Web.stoi ($"del2")) %>
91 <h3><b><% Web.html (#title poll) %> deleted!</b></h3>
92
93<% elseif $"addChoice" <> "" then
94 val id = Web.stoi ($"addChoice");
95 editingPoll := SOME id;
96 val descr = $"descr";
97 if descr = "" then
98 %><h3><b>Your poll choice must have a description.</b></h3><%
99 else
100 val id = Poll.addChoice (id, Web.stor ($"seq"), descr);
101 %><h3><b>Choice added!</b></h3><%
102 end
103
104elseif $"modChoice" <> "" then
105 showNormal := false;
106 val id = Web.stoi ($"modChoice");
107 val cho = Poll.lookupChoice id;
108 val poll = Poll.lookupPoll (#pol cho);
109 Poll.requireCanModify poll %>
110
111<form action="poll">
112<input type="hidden" name="saveChoice" value="<% id %>">
113<table>
114<tr> <td align="right"><b>Text</b>:</td> <td><input name="descr" value="<% Web.html (#descr cho) %>"></td> </tr>
115<tr> <td align="right"><b>Sequence#</b>:</td> <td><input name="seq" value="<% #seq cho %>"></td> </tr>
116<tr> <td><input type="submit" value="Save"></td> </tr>
117</table>
118</form>
119
120<% elseif $"saveChoice" <> "" then
121 val id = Web.stoi ($"saveChoice");
122 val cho = Poll.lookupChoice id;
123 val poll = Poll.lookupPoll (#pol cho);
124 Poll.requireCanModify poll;
125 editingPoll := SOME (#id poll);
126 val descr = $"descr";
127 if descr = "" then
128 %><h3><b>Your poll choice must have a description.</b></h3><%
129 else
130 Poll.modChoice {cho with seq = Web.stor ($"seq"), descr = descr};
131 %><h3><b>Choice saved!</b></h3><%
132 end
133
134elseif $"delChoice" <> "" then
135 val id = Web.stoi ($"delChoice");
136 val cho = Poll.lookupChoice id;
137 val poll = Poll.lookupPoll (#pol cho);
138 Poll.requireCanModify poll;
139 showNormal := false %>
140 <h3><b>Are you sure you want to delete choice "<% Web.html (#descr cho) %>"</a>?</b></h3>
141 <a href="poll?delChoice2=<% $"delChoice" %>">Yes, delete "<% Web.html (#descr cho) %>"!</a>
142
143<% elseif $"delChoice2" <> "" then
144 val id = Web.stoi ($"delChoice2");
145 val cho = Poll.lookupChoice id;
146 val poll = Poll.lookupPoll (#pol cho);
147 Poll.requireCanModify poll;
148 Poll.deleteChoice id;
149 editingPoll := SOME (#id poll) %>
150 <h3><b>"<% Web.html (#descr cho) %>" deleted!</b></h3>
151
152<% elseif $"id" <> "" then
153 editingPoll := SOME (Web.stoi ($"id"))
154
155end %>
156
157<% switch editingPoll of
158 SOME id =>
159 val pol = Poll.lookupPoll id;
160 val canModify = Poll.canModify pol %>
161
162<table>
163<% if canModify then %><tr> <td></td> <td><a href="poll?mod=<% id %>">Edit poll data</a></td> </tr><% end %>
164<tr> <td align="right"><b>Poll#</b>:</td> <td><% id %></td> </tr>
165<tr> <td align="right"><b>Title</b>:</td> <td><% Web.html (#title pol) %></td> </tr>
166<tr> <td align="right"><b>Start</b>:</td> <td><% Web.html (#starts pol) %></td> </tr>
167<tr> <td align="right"><b>End</b>:</td> <td><% Web.html (#ends pol) %></td> </tr>
168<tr> <td align="right"><b>Votes/person</b>:</td> <td><% #votes pol %></td> </tr>
169<tr> <td align="right" valign="top"><b>Description</b>:</td> <td><% Web.htmlNl (#descr pol) %></td> </tr>
170</table>
171
172<h3><b>Choices</b></h3>
173
174<% foreach cho in Poll.listChoices id do %>
175 <li> <% Web.html (#descr cho) %>
176<% if canModify then %>
177<i>(<% #seq cho %>)</i>
178<a href="poll?modChoice=<% #id cho %>">[Modify]</a>
179<a href="poll?delChoice=<% #id cho %>">[Delete]</a>
180<% end %></li>
181<% end %>
182
183<% if canModify then %>
184<br><hr><br>
185<h3><b>Add a new choice</b></h3>
186
187<form action="poll">
188<input type="hidden" name="addChoice" value="<% id %>">
189<table>
190<tr> <td align="right"><b>Text</b>:</td> <td><input name="descr"></td> </tr>
191<tr> <td align="right"><b>Sequence#</b>:</td> <td><input name="seq" value="<% Poll.nextSeq id %>"></td> </tr>
192<tr> <td><input type="submit" value="Add"></td> </tr>
193</table>
194</form>
195
196<% end %>
197<% | NONE =>
198if showNormal then %>
199
200<a href="poll?cmd=list">Show all polls</a><br>
201
202<h3><b>Create a poll</b></h3>
203
204<form action="poll">
205<input type="hidden" name="cmd" value="add">
206<table>
207<tr> <td align="right"><b>Title</b>:</td> <td><input name="title"></td> </tr>
208<tr> <td align="right"><b>Start date</b>:</td> <td><input name="starts"></td> </tr>
209<tr> <td align="right"><b>End date</b>:</td> <td><input name="ends"></td> </tr>
210<tr> <td align="right"><b>Max votes/person</b>:</td> <td><input name="votes"></td> </tr>
211<tr> <td align="right" valign="top"><b>Description</b>:</td> <td><textarea name="descr" wrap="soft" rows="5" cols="80"></textarea></td> </tr>
212<tr> <td><input type="submit" value="Create"></td> </tr>
213</table>
214</form>
215
216<% end
217end %>
218
219<% @footer[] %>