Join script should rule out retired usernames
[bpt/portal.git] / poll.mlt
index 9cc9c60..200c96a 100644 (file)
--- a/poll.mlt
+++ b/poll.mlt
@@ -8,7 +8,7 @@ ref showNormal = true;
 if $"cmd" = "list" then
        showNormal := false %>
 
-<h2><b>All polls</b></h2>
+<h2>All polls</h2>
 
 <% foreach pol in Poll.listPolls () do %>
 <li> <a href="poll?id=<% #id pol %>"><% Web.html (#title pol) %></a>
@@ -21,18 +21,22 @@ elseif $"vote" <> "" then
        val id = Web.stoi ($"vote");
        val poll = Poll.lookupPoll id %>
 
-<table>
-<tr> <td align="right"><b>Poll#</b>:</td> <td><% id %></td> </tr>
-<tr> <td align="right"><b>Title</b>:</td> <td><% Web.html (#title poll) %></td> </tr>
-<tr> <td align="right"><b>Start</b>:</td> <td><% Web.html (#starts poll) %></td> </tr>
-<tr> <td align="right"><b>End</b>:</td> <td><% Web.html (#ends poll) %></td> </tr>
-<tr> <td align="right"><b>Votes/person</b>:</td> <td><% #votes poll %></td> </tr>
-<tr> <td align="right" valign="top"><b>Description</b>:</td> <td><% Web.htmlNl (#descr poll) %></td> </tr>
+<table class="blanks">
+<tr> <td>Poll#:</td> <td><% id %></td> </tr>
+<tr> <td>Title:</td> <td><% Web.html (#title poll) %></td> </tr>
+<tr> <td>Start:</td> <td><% Web.html (#starts poll) %></td> </tr>
+<tr> <td>End:</td> <td><% Web.html (#ends poll) %></td> </tr>
+<tr> <td>Votes/person:</td> <td><% #votes poll %></td> </tr>
+<tr> <td>Official:</td> <td><% if #official poll then "yes" else "no" end %></td> </tr>
+<tr> <td>Description:</td> <td><% Web.htmlNl (#descr poll) %></td> </tr>
 </table>
 
-<h3><b>Choices</b></h3>
+<% if #official poll and Poll.membershipLength (Init.getUserId ()) < Poll.votingMembershipRequirement then %>
+   <h3>You haven't been a member long enough to vote in an official poll.</h3>
+<% else %>
+<h3>Choices</h3>
 
-<form action="poll">
+<form action="poll" method="post">
 <input type="hidden" name="vote2" value="<% id %>">
 <% val choices = Poll.listChoicesWithMyVotes id;
 if #votes poll = 1 then %>
@@ -47,7 +51,8 @@ foreach (you, cho) in choices do %>
 <input type="submit" value="Vote">
 </form>
 
-<% elseif $"vote2" <> "" then
+<% end
+elseif $"vote2" <> "" then
        val id = Web.stoi ($"vote2");
        val poll = Poll.lookupPoll id;
        editingPoll := SOME id;
@@ -56,13 +61,15 @@ foreach (you, cho) in choices do %>
                  [""] => []
                | v => map Web.stoi v;
 
-       if length votes > #votes poll then
-               %><h3><b>You can't vote for that many different choices!</b></h3><%
+       if #official poll and Poll.membershipLength (Init.getUserId ()) < Poll.votingMembershipRequirement then
+               %><h3>You haven't been a member long enough to vote in an official poll.</h3><%
+       elseif length votes > #votes poll then
+               %><h3>You can't vote for that many different choices!</h3><%
        elseif not (Poll.noDupes votes) then
-               %><h3><b>You can't vote multiple times for the same choice!</b></h3><%
+               %><h3>You can't vote multiple times for the same choice!</h3><%
        else
                Poll.vote (Init.getUserId (), id, votes)
-               %><h3><b>Thanks for voting!</b></h3>
+               %><h3>Thanks for voting!</h3>
 <%     end
 
 elseif $"cmd" = "add" then
@@ -70,18 +77,19 @@ elseif $"cmd" = "add" then
        val starts = $"starts";
        val ends = $"ends";
        val votes = Web.stoi ($"votes");
+       val official = $"official" = "on";
        if title = "" then
-               %><h3><b>Your poll must have a title.</b></h3><%
+               %><h3>Your poll must have a title.</h3><%
        elseif not pollAdmin and not (Poll.dateGeNow starts) then
-               %><h3><b>That start date is in the past!</b></h3><%
+               %><h3>That start date is in the past!</h3><%
        elseif not pollAdmin and not (Poll.dateLe (starts, ends)) then
-               %><h3><b>The end date comes before the start date!</b></h3><%
+               %><h3>The end date comes before the start date!</h3><%
        elseif votes <= 0 then
-               %><h3><b>You must specify a positive number of votes per person.</b></h3><%
+               %><h3>You must specify a positive number of votes per person.</h3><%
        else
-               val id = Poll.addPoll (Init.getUserId(), title, $"descr", starts, ends, votes);
+               val id = Poll.addPoll (Init.getUserId(), title, $"descr", starts, ends, votes, official, false);
                editingPoll := SOME id;
-               %><h3><b>Poll added!</b></h3><%
+               %><h3>Poll added!</h3><%
        end
 
 elseif $"mod" <> "" then
@@ -89,16 +97,18 @@ elseif $"mod" <> "" then
        val poll = Poll.lookupPoll (Web.stoi ($"mod"));
 
        Poll.requireCanModify poll %>
-<h3><b>Modify poll</b></h3>
+<h3>Modify poll</h3>
 
-<form action="poll">
+<form action="poll" method="post">
 <input type="hidden" name="id" value="<% $"mod" %>">
-<table>
-<tr> <td align="right"><b>Title</b>:</td> <td><input name="title" value="<% Web.html (#title poll) %>"></td> </tr>
-<tr> <td align="right"><b>Start date</b>:</td> <td><input name="starts" value="<% Web.html (#starts poll) %>"></td> </tr>
-<tr> <td align="right"><b>End date</b>:</td> <td><input name="ends" value="<% Web.html (#ends poll) %>"></td> </tr>
-<tr> <td align="right"><b>Max votes/person</b>:</td> <td><input name="votes" value="<% #votes poll %>"></td> </tr>
-<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>
+<table class="blanks">
+<tr> <td>Title:</td> <td><input name="title" value="<% Web.html (#title poll) %>"></td> </tr>
+<tr> <td>Ready?</td> <td><input type="checkbox" name="ready" <% if #ready poll then " checked" else "" end %>></td> </tr>
+<tr> <td>Start date:</td> <td><input name="starts" value="<% Web.html (#starts poll) %>"></td> </tr>
+<tr> <td>End date:</td> <td><input name="ends" value="<% Web.html (#ends poll) %>"></td> </tr>
+<tr> <td>Max votes/person:</td> <td><input name="votes" value="<% #votes poll %>"></td> </tr>
+<tr> <td>Official:</td> <td><input type="checkbox" name="official"<% if #official poll then " checked" end %>></td> </tr>
+<tr> <td>Description:</td> <td><textarea name="descr" wrap="soft" rows="5" cols="80"><% Web.html (#descr poll) %></textarea></td> </tr>
 <tr> <td><input type="submit" name="cmd" value="Save"></td> </tr>
 </table>
 </form>
@@ -112,42 +122,50 @@ elseif $"mod" <> "" then
        val starts = $"starts";
        val ends = $"ends";
        val votes = Web.stoi ($"votes");
-       if title = "" then
-               %><h3><b>Your poll must have a title.</b></h3><%
+       val official = $"official" = "on";
+       val ready = $"ready" = "on";
+        if not (Poll.canModify poll) then
+               %><h3>You can't modify this poll anymore, because voting is already open.</h3><%
+       elseif title = "" then
+               %><h3>Your poll must have a title.</h3><%
        elseif not pollAdmin and not (Poll.dateGeNow starts) then
-               %><h3><b>That start date is in the past!</b></h3><%
+               %><h3>That start date is in the past!</h3><%
        elseif not pollAdmin and not (Poll.dateLe (starts, ends)) then
-               %><h3><b>The end date comes before the start date!</b></h3><%
+               %><h3>The end date comes before the start date!</h3><%
        elseif votes <= 0 then
-               %><h3><b>You must specify a positive number of votes per person.</b></h3><%
+               %><h3>You must specify a positive number of votes per person.</h3><%
        else
-               Poll.modPoll {poll with title = title, descr = $"descr", starts = starts, ends = ends, votes = votes};
+               Poll.modPoll {poll with title = title, descr = $"descr", starts = starts, ends = ends, votes = votes, official = official,
+                             ready = ready};
                editingPoll := SOME (#id poll);
-               %><h3><b>Poll record saved.</b></h3><%
+               %><h3>Poll record saved.</h3><%
        end
 
 elseif $"del" <> "" then
-       Group.requireGroupName "poll";
        showNormal := false;
-       val poll = Poll.lookupPoll (Web.stoi ($"del")) %>
-       <h3><b>Are you sure you want to delete poll <a href="poll?id=<% #id poll %>"><% Web.html (#title poll) %></a>?</b></h3>
+       val poll = Poll.lookupPoll (Web.stoi ($"del"));
+       Poll.requireCanModify poll %>
+       <h3>Are you sure you want to delete poll <a href="poll?id=<% #id poll %>"><% Web.html (#title poll) %></a>?</h3>
        <a href="poll?del2=<% $"del" %>">Yes, delete <% Web.html (#title poll) %>!</a>
 
 <% elseif $"del2" <> "" then
-       Group.requireGroupName "poll";
        val poll = Poll.lookupPoll (Web.stoi ($"del2"));
+       Poll.requireCanModify poll;
        Poll.deletePoll (Web.stoi ($"del2")) %>
-       <h3><b><% Web.html (#title poll) %> deleted!</b></h3>
+       <h3><% Web.html (#title poll) %> deleted!</h3>
 
 <% elseif $"addChoice" <> "" then
        val id = Web.stoi ($"addChoice");
        editingPoll := SOME id;
        val descr = $"descr";
-       if descr = "" then
-               %><h3><b>Your poll choice must have a description.</b></h3><%
+       val poll = Poll.lookupPoll id;
+        if not (Poll.canModify poll) then
+               %><h3>You can't modify this poll anymore, because voting is already open.</h3><%
+       elseif descr = "" then
+               %><h3>Your poll choice must have a description.</h3><%
        else
                val id = Poll.addChoice (id, Web.stor ($"seq"), descr);
-               %><h3><b>Choice added!</b></h3><%
+               %><h3>Choice added!</h3><%
        end
 
 elseif $"modChoice" <> "" then
@@ -157,11 +175,11 @@ elseif $"modChoice" <> "" then
        val poll = Poll.lookupPoll (#pol cho);
        Poll.requireCanModify poll %>
 
-<form action="poll">
+<form action="poll" method="post">
 <input type="hidden" name="saveChoice" value="<% id %>">
-<table>
-<tr> <td align="right"><b>Text</b>:</td> <td><input name="descr" value="<% Web.html (#descr cho) %>"></td> </tr>
-<tr> <td align="right"><b>Sequence#</b>:</td> <td><input name="seq" value="<% #seq cho %>"></td> </tr>
+<table class="blanks">
+<tr> <td>Text:</td> <td><input name="descr" value="<% Web.html (#descr cho) %>"></td> </tr>
+<tr> <td>Sequence#:</td> <td><input name="seq" value="<% #seq cho %>"></td> </tr>
 <tr> <td><input type="submit" value="Save"></td> </tr>
 </table>
 </form>
@@ -174,10 +192,10 @@ elseif $"modChoice" <> "" then
        editingPoll := SOME (#id poll);
        val descr = $"descr";
        if descr = "" then
-               %><h3><b>Your poll choice must have a description.</b></h3><%
+               %><h3>Your poll choice must have a description.</h3><%
        else
                Poll.modChoice {cho with seq = Web.stor ($"seq"), descr = descr};
-               %><h3><b>Choice saved!</b></h3><%
+               %><h3>Choice saved!</h3><%
        end
 
 elseif $"delChoice" <> "" then
@@ -186,7 +204,7 @@ elseif $"delChoice" <> "" then
        val poll = Poll.lookupPoll (#pol cho);
        Poll.requireCanModify poll;
        showNormal := false %>
-       <h3><b>Are you sure you want to delete choice "<% Web.html (#descr cho) %>"</a>?</b></h3>
+       <h3>Are you sure you want to delete choice "<% Web.html (#descr cho) %>"</a>?</h3>
        <a href="poll?delChoice2=<% $"delChoice" %>">Yes, delete "<% Web.html (#descr cho) %>"!</a>
 
 <% elseif $"delChoice2" <> "" then
@@ -196,7 +214,7 @@ elseif $"delChoice" <> "" then
        Poll.requireCanModify poll;
        Poll.deleteChoice id;
        editingPoll := SOME (#id poll) %>
-       <h3><b>"<% Web.html (#descr cho) %>" deleted!</b></h3>
+       <h3>"<% Web.html (#descr cho) %>" deleted!</h3>
 
 <% elseif $"report" <> "" then
        showNormal := false;
@@ -205,7 +223,7 @@ elseif $"delChoice" <> "" then
        val poll = Poll.lookupPoll id;
        val canModify = Poll.canModify poll %>
 
-<h3><b>Vote Report</b></h3>
+<h3>Vote Report</h3>
 
 <p>Voters:
 <%     ref first = true;
@@ -218,13 +236,14 @@ elseif $"delChoice" <> "" then
                %><a href="user?id=<% #id user %>"><% #name user %></a><%
        end %></p>
 
-<table>
-<tr> <td align="right"><b>Poll#</b>:</td> <td><% id %></td> </tr>
-<tr> <td align="right"><b>Title</b>:</td> <td><% Web.html (#title poll) %></td> </tr>
-<tr> <td align="right"><b>Start</b>:</td> <td><% Web.html (#starts poll) %></td> </tr>
-<tr> <td align="right"><b>End</b>:</td> <td><% Web.html (#ends poll) %></td> </tr>
-<tr> <td align="right"><b>Votes/person</b>:</td> <td><% #votes poll %></td> </tr>
-<tr> <td align="right" valign="top"><b>Description</b>:</td> <td><% Web.htmlNl (#descr poll) %></td> </tr>
+<table class="blanks">
+<tr> <td>Poll#</b>:</td> <td><% id %></td> </tr>
+<tr> <td>Title</b>:</td> <td><% Web.html (#title poll) %></td> </tr>
+<tr> <td>Start:</td> <td><% Web.html (#starts poll) %></td> </tr>
+<tr> <td>End:</td> <td><% Web.html (#ends poll) %></td> </tr>
+<tr> <td>Votes/person:</td> <td><% #votes poll %></td> </tr>
+<tr> <td>Official:</td> <td><% if #official poll then "yes" else "no" end %></td> </tr>
+<tr> <td>Description:</td> <td><% Web.htmlNl (#descr poll) %></td> </tr>
 </table>
 
 <br><hr><br>
@@ -256,17 +275,18 @@ end %>
                val poll = Poll.lookupPoll id;
                val canModify = Poll.canModify poll %>
 
-<table>
+<table class="blanks">
 <% if canModify then %><tr> <td></td> <td><a href="poll?mod=<% id %>">Edit poll data</a></td> </tr><% end %>
-<tr> <td align="right"><b>Poll#</b>:</td> <td><% id %></td> </tr>
-<tr> <td align="right"><b>Title</b>:</td> <td><% Web.html (#title poll) %></td> </tr>
-<tr> <td align="right"><b>Start</b>:</td> <td><% Web.html (#starts poll) %></td> </tr>
-<tr> <td align="right"><b>End</b>:</td> <td><% Web.html (#ends poll) %></td> </tr>
-<tr> <td align="right"><b>Votes/person</b>:</td> <td><% #votes poll %></td> </tr>
-<tr> <td align="right" valign="top"><b>Description</b>:</td> <td><% Web.htmlNl (#descr poll) %></td> </tr>
+<tr> <td>Poll#:</td> <td><% id %></td> </tr>
+<tr> <td>Title:</td> <td><% Web.html (#title poll) %></td> </tr>
+<tr> <td>Start:</td> <td><% Web.html (#starts poll) %></td> </tr>
+<tr> <td>End:</td> <td><% Web.html (#ends poll) %></td> </tr>
+<tr> <td>Votes/person:</td> <td><% #votes poll %></td> </tr>
+<tr> <td>Official:</td> <td><% if #official poll then "yes" else "no" end %></td> </tr>
+<tr> <td>Description:</td> <td><% Web.htmlNl (#descr poll) %></td> </tr>
 </table>
 
-<h3><b>Choices<% if Poll.takingVotes poll then %><a href="poll?vote=<% id %>">(Vote!)</a><% end %></b></h3>
+<h3>Choices<% if Poll.takingVotes poll then %><a href="poll?vote=<% id %>">(Vote!)</a><% end %></h3>
 
 <p><% Poll.countVoters (#id poll) %> people have voted.</p>
 
@@ -285,7 +305,6 @@ end %>
 <% end %>
 </table>
 
-<a href="poll?report=<% id %>">Vote Report</a>
 <% else
 foreach cho in Poll.listChoices id do %>
        <li> <% Web.html (#descr cho) %>
@@ -297,35 +316,56 @@ foreach cho in Poll.listChoices id do %>
 <% end
 end %>
 
+<a href="poll?report=<% id %>">Vote Report</a>
+
 <% if canModify then %>
 <br><hr><br>
-<h3><b>Add a new choice</b></h3>
+<h3><a href="?del=<% id %>">Delete this poll</a></h3>
+
+<h3>Add a new choice</h3>
 
-<form action="poll">
+<form action="poll" method="post">
 <input type="hidden" name="addChoice" value="<% id %>">
-<table>
-<tr> <td align="right"><b>Text</b>:</td> <td><input name="descr"></td> </tr>
-<tr> <td align="right"><b>Sequence#</b>:</td> <td><input name="seq" value="<% Poll.nextSeq id %>"></td> </tr>
+<table class="blanks">
+<tr> <td>Text:</td> <td><input name="descr"></td> </tr>
+<tr> <td>Sequence#:</td> <td><input name="seq" value="<% Poll.nextSeq id %>"></td> </tr>
 <tr> <td><input type="submit" value="Add"></td> </tr>
 </table>
 </form>
 
 <% end %>
 <%     | NONE =>
-if showNormal then %>
+if showNormal then
+
+val mlen = Poll.membershipLength (Init.getUserId ()) %>
+
+<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>
 
-<a href="poll?cmd=list">Show all polls</a><br>
+<% val polls = Poll.listCurrentPolls ();
+switch polls of
+       _::_ => %>
+<h3><a href="poll">Current polls</a></h3>
 
-<h3><b>Create a poll</b></h3>
+<% foreach pol in polls do %>
+<li> <a href="poll?id=<% #id pol %>"><% Web.html (#title pol) %></a>
+<% if Poll.takingVotes pol then %><a href="poll?vote=<% #id pol %>">[VOTE]</a><% end %>
+(<% Web.html (#starts pol) %> to <% Web.html (#ends pol) %>)</li>
+<% end
+end %>
 
-<form action="poll">
+<p><a href="poll?cmd=list">Show all polls</a></p>
+
+<h3>Create a poll</h3>
+
+<form action="poll" method="post">
 <input type="hidden" name="cmd" value="add">
-<table>
-<tr> <td align="right"><b>Title</b>:</td> <td><input name="title"></td> </tr>
-<tr> <td align="right"><b>Start date</b>:</td> <td><input name="starts"></td> </tr>
-<tr> <td align="right"><b>End date</b>:</td> <td><input name="ends"></td> </tr>
-<tr> <td align="right"><b>Max votes/person</b>:</td> <td><input name="votes"></td> </tr>
-<tr> <td align="right" valign="top"><b>Description</b>:</td> <td><textarea name="descr" wrap="soft" rows="5" cols="80"></textarea></td> </tr>
+<table class="blanks">
+<tr> <td>Title:</td> <td><input name="title"></td> </tr>
+<tr> <td>Start date:</td> <td><input name="starts"></td> </tr>
+<tr> <td>End date:</td> <td><input name="ends"></td> </tr>
+<tr> <td>Max votes/person:</td> <td><input name="votes"></td> </tr>
+<tr> <td>Official:</td> <td><input type="checkbox" name="official"></td> </tr>
+<tr> <td>Description:</td> <td><textarea name="descr" wrap="soft" rows="5" cols="80"></textarea></td> </tr>
 <tr> <td><input type="submit" value="Create"></td> </tr>
 </table>
 </form>