<% @header[("title", ["Polls"])]; val pollAdmin = Group.inGroupName "poll"; ref editingPoll = NONE; ref showNormal = true; if $"cmd" = "list" then showNormal := false %>

All polls

<% foreach pol in Poll.listPolls () do %>
  • <% Web.html (#title pol) %> <% if pollAdmin then %>[Delete] <% end %>
  • <% end elseif $"vote" <> "" then showNormal := false; val id = Web.stoi ($"vote"); val poll = Poll.lookupPoll id %>
    Poll#: <% id %>
    Title: <% Web.html (#title poll) %>
    Start: <% Web.html (#starts poll) %>
    End: <% Web.html (#ends poll) %>
    Votes/person: <% #votes poll %>
    Description: <% Web.htmlNl (#descr poll) %>

    Choices

    <% val choices = Poll.listChoicesWithMyVotes id; if #votes poll = 1 then %> <% end foreach (you, cho) in choices do %> <% end %>

    <% elseif $"vote2" <> "" then val id = Web.stoi ($"vote2"); val poll = Poll.lookupPoll id; editingPoll := SOME id; val votes = case Web.getMultiParam "v" of [""] => [] | v => map Web.stoi v; if length votes > #votes poll then %>

    You can't vote for that many different choices!

    <% elseif not (Poll.noDupes votes) then %>

    You can't vote multiple times for the same choice!

    <% else Poll.vote (Init.getUserId (), id, votes) %>

    Thanks for voting!

    <% end elseif $"cmd" = "add" then val title = $"title"; val starts = $"starts"; val ends = $"ends"; val votes = Web.stoi ($"votes"); if title = "" then %>

    Your poll must have a title.

    <% elseif not pollAdmin and not (Poll.dateGeNow starts) then %>

    That start date is in the past!

    <% elseif not pollAdmin and not (Poll.dateLe (starts, ends)) then %>

    The end date comes before the start date!

    <% elseif votes <= 0 then %>

    You must specify a positive number of votes per person.

    <% else val id = Poll.addPoll (Init.getUserId(), title, $"descr", starts, ends, votes); editingPoll := SOME id; %>

    Poll added!

    <% end elseif $"mod" <> "" then showNormal := false; val poll = Poll.lookupPoll (Web.stoi ($"mod")); Poll.requireCanModify poll %>

    Modify poll

    ">
    Title:
    Start date:
    End date:
    Max votes/person:
    Description:
    <% elseif $"cmd" = "Save" then val poll = Poll.lookupPoll (Web.stoi ($"id")); Poll.requireCanModify poll; val title = $"title"; val starts = $"starts"; val ends = $"ends"; val votes = Web.stoi ($"votes"); if title = "" then %>

    Your poll must have a title.

    <% elseif not pollAdmin and not (Poll.dateGeNow starts) then %>

    That start date is in the past!

    <% elseif not pollAdmin and not (Poll.dateLe (starts, ends)) then %>

    The end date comes before the start date!

    <% elseif votes <= 0 then %>

    You must specify a positive number of votes per person.

    <% else Poll.modPoll {poll with title = title, descr = $"descr", starts = starts, ends = ends, votes = votes}; editingPoll := SOME (#id poll); %>

    Poll record saved.

    <% end elseif $"del" <> "" then Group.requireGroupName "poll"; showNormal := false; val poll = Poll.lookupPoll (Web.stoi ($"del")) %>

    Are you sure you want to delete poll <% Web.html (#title poll) %>?

    ">Yes, delete <% Web.html (#title poll) %>! <% elseif $"del2" <> "" then Group.requireGroupName "poll"; val poll = Poll.lookupPoll (Web.stoi ($"del2")); Poll.deletePoll (Web.stoi ($"del2")) %>

    <% Web.html (#title poll) %> deleted!

    <% elseif $"addChoice" <> "" then val id = Web.stoi ($"addChoice"); editingPoll := SOME id; val descr = $"descr"; if descr = "" then %>

    Your poll choice must have a description.

    <% else val id = Poll.addChoice (id, Web.stor ($"seq"), descr); %>

    Choice added!

    <% end elseif $"modChoice" <> "" then showNormal := false; val id = Web.stoi ($"modChoice"); val cho = Poll.lookupChoice id; val poll = Poll.lookupPoll (#pol cho); Poll.requireCanModify poll %>
    Text:
    Sequence#:
    <% elseif $"saveChoice" <> "" then val id = Web.stoi ($"saveChoice"); val cho = Poll.lookupChoice id; val poll = Poll.lookupPoll (#pol cho); Poll.requireCanModify poll; editingPoll := SOME (#id poll); val descr = $"descr"; if descr = "" then %>

    Your poll choice must have a description.

    <% else Poll.modChoice {cho with seq = Web.stor ($"seq"), descr = descr}; %>

    Choice saved!

    <% end elseif $"delChoice" <> "" then val id = Web.stoi ($"delChoice"); val cho = Poll.lookupChoice id; val poll = Poll.lookupPoll (#pol cho); Poll.requireCanModify poll; showNormal := false %>

    Are you sure you want to delete choice "<% Web.html (#descr cho) %>"?

    ">Yes, delete "<% Web.html (#descr cho) %>"! <% elseif $"delChoice2" <> "" then val id = Web.stoi ($"delChoice2"); val cho = Poll.lookupChoice id; val poll = Poll.lookupPoll (#pol cho); Poll.requireCanModify poll; Poll.deleteChoice id; editingPoll := SOME (#id poll) %>

    "<% Web.html (#descr cho) %>" deleted!

    <% elseif $"report" <> "" then showNormal := false; val id = Web.stoi ($"report"); val poll = Poll.lookupPoll id; val canModify = Poll.canModify poll %>

    Vote Report

    Voters: <% ref first = true; foreach user in Poll.listPollVoters id do if first then first := false else %>, <% end %><% #name user %><% end %>

    Poll#: <% id %>
    Title: <% Web.html (#title poll) %>
    Start: <% Web.html (#starts poll) %>
    End: <% Web.html (#ends poll) %>
    Votes/person: <% #votes poll %>
    Description: <% Web.htmlNl (#descr poll) %>



    <% foreach (_, total, cho) in Poll.listChoicesWithVotes id do %> <% end %>
    Votes Choice Voters
    <% total %> <% Web.html (#descr cho) %> <% ref first = true; foreach user in Poll.listVoters (#id cho) do if first then first := false else %>, <% end %><% #name user %><% end %>
    <% elseif $"id" <> "" then editingPoll := SOME (Web.stoi ($"id")) end %> <% switch editingPoll of SOME id => val poll = Poll.lookupPoll id; val canModify = Poll.canModify poll %> <% if canModify then %><% end %>
    Edit poll data
    Poll#: <% id %>
    Title: <% Web.html (#title poll) %>
    Start: <% Web.html (#starts poll) %>
    End: <% Web.html (#ends poll) %>
    Votes/person: <% #votes poll %>
    Description: <% Web.htmlNl (#descr poll) %>

    Choices<% if Poll.takingVotes poll then %>(Vote!)<% end %>

    <% Poll.countVoters (#id poll) %> people have voted.

    <% if Poll.takingVotes poll then %> <% foreach (you, total, cho) in Poll.listChoicesWithVotes id do %> <% if canModify then %> <% end %> <% end %>
    You Total
    <% if you then %>X<% end %> <% total %> <% Web.html (#descr cho) %>(<% #seq cho %>) [Modify] [Delete]
    <% else foreach cho in Poll.listChoices id do %>
  • <% Web.html (#descr cho) %> <% if canModify then %> (<% #seq cho %>) [Modify] [Delete] <% end %>
  • <% end end %> Vote Report <% if canModify then %>


    Add a new choice

    Text:
    Sequence#:
    <% end %> <% | NONE => if showNormal then %> Show all polls

    Create a poll

    Title:
    Start date:
    End date:
    Max votes/person:
    Description:
    <% end end %> <% @footer[] %>