bsw/jbe@19: local issue = Issue:by_id(param.get("issue_id"), atom.integer) bsw/jbe@19: bsw/jbe@19: local member_id = param.get("member_id", atom.integer) bsw/jbe@19: local member jbe@945: local readonly = false bsw/jbe@19: jbe@950: local preview = param.get("preview") and true or false poelzi@156: bsw/jbe@19: if member_id then bsw/jbe@19: if not issue.closed then bsw/jbe@19: error("access denied") bsw/jbe@19: end bsw/jbe@19: member = Member:by_id(member_id) bsw/jbe@19: readonly = true bsw/jbe@19: end bsw/jbe@19: poelzi@138: if issue.closed then poelzi@156: if not member then poelzi@158: member = app.session.member poelzi@158: end poelzi@156: readonly = true poelzi@138: end poelzi@138: bsw@1045: if preview then bsw@1045: readonly = true bsw@1045: end bsw@1045: bsw@879: local submit_button_text = _"Finish voting" bsw@1045: local edit_button_text = _"Edit again" bsw@879: bsw@879: if issue.closed then bsw@1045: submit_button_text = _"Save voting comment" bsw@1045: edit_button_text = _"Edit voting comment" bsw@879: end bsw@879: bsw@1045: execute.view { bsw@1045: module = "issue", view = "_head", params = { issue = issue } bsw@1045: } bsw@1045: bsw@879: local direct_voter bsw@879: bsw/jbe@19: if member then bsw@879: direct_voter = DirectVoter:by_pk(issue.id, member.id) bsw/jbe@19: else bsw/jbe@19: member = app.session.member bsw@879: direct_voter = DirectVoter:by_pk(issue.id, member.id) bsw/jbe@19: end bsw/jbe@19: bsw/jbe@19: bsw@879: bsw/jbe@19: local tempvoting_string = param.get("scoring") bsw/jbe@19: bsw/jbe@19: local tempvotings = {} bsw/jbe@19: if tempvoting_string then bsw/jbe@19: for match in tempvoting_string:gmatch("([^;]+)") do bsw/jbe@19: for initiative_id, grade in match:gmatch("([^:;]+):([^:;]+)") do bsw/jbe@19: tempvotings[tonumber(initiative_id)] = tonumber(grade) bsw/jbe@19: end bsw/jbe@5: end bsw/jbe@19: end bsw/jbe@5: bsw@95: local initiatives = issue:get_reference_selector("initiatives"):add_where("initiative.admitted"):add_order_by("initiative.satisfied_supporter_count DESC"):exec() bsw/jbe@5: bsw/jbe@5: local min_grade = -1; bsw/jbe@5: local max_grade = 1; bsw/jbe@5: bsw/jbe@5: for i, initiative in ipairs(initiatives) do bsw/jbe@5: -- TODO performance bsw/jbe@19: initiative.vote = Vote:by_pk(initiative.id, member.id) bsw/jbe@19: if tempvotings[initiative.id] then bsw/jbe@19: initiative.vote = {} bsw/jbe@19: initiative.vote.grade = tempvotings[initiative.id] bsw/jbe@19: end bsw/jbe@5: if initiative.vote then bsw/jbe@5: if initiative.vote.grade > max_grade then bsw/jbe@5: max_grade = initiative.vote.grade bsw/jbe@5: end bsw/jbe@5: if initiative.vote.grade < min_grade then bsw/jbe@5: min_grade = initiative.vote.grade bsw/jbe@5: end bsw/jbe@5: end bsw/jbe@5: end bsw/jbe@5: bsw/jbe@5: local sections = {} bsw/jbe@5: for i = min_grade, max_grade do bsw/jbe@5: sections[i] = {} bsw/jbe@5: for j, initiative in ipairs(initiatives) do bsw/jbe@5: if (initiative.vote and initiative.vote.grade == i) or (not initiative.vote and i == 0) then bsw/jbe@5: sections[i][#(sections[i])+1] = initiative bsw/jbe@5: end bsw/jbe@5: end bsw/jbe@5: end bsw/jbe@5: bsw/jbe@19: local approval_count, disapproval_count = 0, 0 bsw/jbe@19: for i = min_grade, -1 do bsw/jbe@19: if #sections[i] > 0 then bsw/jbe@19: disapproval_count = disapproval_count + 1 bsw/jbe@19: end bsw/jbe@19: end bsw/jbe@19: local approval_count = 0 bsw/jbe@19: for i = 1, max_grade do bsw/jbe@19: if #sections[i] > 0 then bsw/jbe@19: approval_count = approval_count + 1 bsw/jbe@19: end bsw/jbe@19: end bsw/jbe@5: bsw/jbe@19: if not readonly then bsw/jbe@19: slot.put('') bsw/jbe@19: slot.put('') bsw/jbe@19: end bsw/jbe@19: bsw/jbe@19: ui.script{ bsw/jbe@19: script = function() bsw/jbe@19: slot.put( bsw/jbe@19: "voting_text_approval_single = ", encode.json(_"Approval [single entry]"), ";\n", bsw/jbe@19: "voting_text_approval_multi = ", encode.json(_"Approval [many entries]"), ";\n", bsw/jbe@19: "voting_text_first_preference_single = ", encode.json(_"Approval (first preference) [single entry]"), ";\n", bsw/jbe@19: "voting_text_first_preference_multi = ", encode.json(_"Approval (first preference) [many entries]"), ";\n", bsw/jbe@19: "voting_text_second_preference_single = ", encode.json(_"Approval (second preference) [single entry]"), ";\n", bsw/jbe@19: "voting_text_second_preference_multi = ", encode.json(_"Approval (second preference) [many entries]"), ";\n", bsw/jbe@19: "voting_text_third_preference_single = ", encode.json(_"Approval (third preference) [single entry]"), ";\n", bsw/jbe@19: "voting_text_third_preference_multi = ", encode.json(_"Approval (third preference) [many entries]"), ";\n", bsw/jbe@19: "voting_text_numeric_preference_single = ", encode.json(_"Approval (#th preference) [single entry]"), ";\n", bsw/jbe@19: "voting_text_numeric_preference_multi = ", encode.json(_"Approval (#th preference) [many entries]"), ";\n", bsw/jbe@19: "voting_text_abstention_single = ", encode.json(_"Abstention [single entry]"), ";\n", bsw/jbe@19: "voting_text_abstention_multi = ", encode.json(_"Abstention [many entries]"), ";\n", bsw/jbe@19: "voting_text_disapproval_above_one_single = ", encode.json(_"Disapproval (prefer to lower block) [single entry]"), ";\n", bsw/jbe@19: "voting_text_disapproval_above_one_multi = ", encode.json(_"Disapproval (prefer to lower block) [many entries]"), ";\n", bsw/jbe@19: "voting_text_disapproval_above_many_single = ", encode.json(_"Disapproval (prefer to lower blocks) [single entry]"), ";\n", bsw/jbe@19: "voting_text_disapproval_above_many_multi = ", encode.json(_"Disapproval (prefer to lower blocks) [many entries]"), ";\n", bsw/jbe@19: "voting_text_disapproval_above_last_single = ", encode.json(_"Disapproval (prefer to last block) [single entry]"), ";\n", bsw/jbe@19: "voting_text_disapproval_above_last_multi = ", encode.json(_"Disapproval (prefer to last block) [many entries]"), ";\n", bsw/jbe@19: "voting_text_disapproval_single = ", encode.json(_"Disapproval [single entry]"), ";\n", bsw/jbe@19: "voting_text_disapproval_multi = ", encode.json(_"Disapproval [many entries]"), ";\n" bsw/jbe@19: ) bsw/jbe@19: end bsw/jbe@19: } bsw/jbe@5: bsw@1045: if issue.state == "finished_with_winner" bsw@1045: or issue.state == "finished_without_winner" bsw@1045: then bsw@1045: bsw@1045: local members_selector = Member:new_selector() bsw@1045: :join("delegating_voter", nil, "delegating_voter.member_id = member.id") bsw@1045: :add_where{ "delegating_voter.issue_id = ?", issue.id } bsw@1045: :add_where{ "delegating_voter.delegate_member_ids[1] = ?", member.id } bsw@1045: :add_field("delegating_voter.weight", "voter_weight") bsw@1045: :join("issue", nil, "issue.id = delegating_voter.issue_id") bsw@1045: bsw@1045: ui.sidebar( "tab-members", function() bsw@1045: ui.sidebarHead(function() bsw@1045: ui.heading{ level = 2, content = _"Incoming delegations" } bsw@1045: end) bsw@1045: execute.view{ bsw@1045: module = "member", bsw@1045: view = "_list", bsw@1045: params = { bsw@1045: members_selector = members_selector, bsw@1045: trustee = member, bsw@1045: issue = issue, bsw@1045: initiative = initiative, bsw@1045: for_votes = true, no_filter = true, bsw@1045: member_class = "sidebarRow sidebarRowNarrow", bsw/jbe@5: } bsw@1045: } bsw@1045: end) bsw@1045: end bsw@1045: bsw@1045: bsw@1045: ui.section( function() bsw@1045: bsw@1045: ui.sectionHead( function() bsw@1045: if preview then bsw@1045: ui.heading { level = 1, content = _"Preview of voting ballot" } bsw@1045: elseif readonly then bsw@1045: local str = _("Ballot of '#{member_name}'", bsw@1045: {member_name = string.format('%s', bsw@1045: encode.url{ bsw@1045: module = "member", bsw@1045: view = "show", bsw@1045: id = member.id, bsw@1045: }, bsw@1045: encode.html(member.name)) bsw@1045: } bsw@1045: ) bsw@1045: ui.heading { level = 1, content = function () slot.put ( str ) end } bsw@1045: else bsw@1045: ui.heading { level = 1, content = _"Voting" } bsw/jbe@19: end bsw@1045: end ) bsw@1045: bsw@1045: ui.sectionRow( function() bsw@1045: bsw@1045: ui.form{ bsw@1045: record = direct_voter, bsw@1045: attr = { bsw@1045: id = "voting_form", bsw@1045: class = readonly and "voting_form_readonly" or "voting_form_active" bsw@1045: }, bsw@1045: module = "vote", bsw@1045: action = "update", bsw@1045: params = { issue_id = issue.id }, bsw/jbe@5: content = function() bsw@1045: if not readonly or preview then bsw@1045: local scoring = param.get("scoring") bsw@1045: if not scoring then bsw@1045: for i, initiative in ipairs(initiatives) do bsw@1045: local vote = initiative.vote bsw@1045: if vote then bsw@1045: tempvotings[initiative.id] = vote.grade bsw@1045: else bsw@1045: tempvotings[initiative.id] = 0 bsw@1045: end bsw@1045: end bsw@1045: local tempvotings_list = {} bsw@1045: for key, val in pairs(tempvotings) do bsw@1045: tempvotings_list[#tempvotings_list+1] = tostring(key) .. ":" .. tostring(val) bsw@1045: end bsw@1045: if #tempvotings_list > 0 then bsw@1045: scoring = table.concat(tempvotings_list, ";") bsw@1045: else bsw@1045: scoring = "" bsw@1045: end bsw/jbe@5: end bsw@1045: slot.put('') bsw@1045: end bsw@1045: if preview then bsw@1045: ui.heading{ level = 2, content = _"Your choice" } bsw@1045: elseif not readonly then bsw@1045: ui.heading{ level = 2, content = _"Make your choice by placing the initiatives" } bsw@1045: end bsw@1045: bsw@1045: ui.container{ bsw@1045: attr = { id = "voting" }, bsw@1045: content = function() bsw@1045: local approval_index, disapproval_index = 0, 0 bsw@1045: for grade = max_grade, min_grade, -1 do bsw@1045: local entries = sections[grade] bsw@1045: local class bsw@1045: if grade > 0 then bsw@1045: class = "approval" bsw@1045: elseif grade < 0 then bsw@1045: class = "disapproval" bsw@1045: else bsw@1045: class = "abstention" bsw@1045: end bsw@1045: if bsw@1045: #entries > 0 or bsw@1045: (grade == 1 and not approval_used) or bsw@1045: (grade == -1 and not disapproval_used) or bsw@1045: grade == 0 bsw@1045: then bsw@1045: ui.container{ bsw@1045: attr = { class = class }, bsw@1045: content = function() bsw@1045: local heading bsw@1045: if class == "approval" then bsw@1045: approval_used = true bsw@1045: approval_index = approval_index + 1 bsw@1045: if approval_count > 1 then bsw@1045: if approval_index == 1 then bsw@1045: if #entries == 1 then bsw@1045: heading = _"Approval (first preference) [single entry]" bsw@1045: else bsw@1045: heading = _"Approval (first preference) [many entries]" bsw@1045: end bsw@1045: elseif approval_index == 2 then bsw@1045: if #entries == 1 then bsw@1045: heading = _"Approval (second preference) [single entry]" bsw@1045: else bsw@1045: heading = _"Approval (second preference) [many entries]" bsw@1045: end bsw@1045: elseif approval_index == 3 then bsw@1045: if #entries == 1 then bsw@1045: heading = _"Approval (third preference) [single entry]" bsw@1045: else bsw@1045: heading = _"Approval (third preference) [many entries]" bsw@1045: end bsw@1045: else bsw@1045: if #entries == 1 then bsw@1045: heading = _"Approval (#th preference) [single entry]" bsw@1045: else bsw@1045: heading = _"Approval (#th preference) [many entries]" bsw@1045: end bsw@1045: end bsw/jbe@19: else bsw@1045: if #entries == 1 then bsw@1045: heading = _"Approval [single entry]" bsw@1045: else bsw@1045: heading = _"Approval [many entries]" bsw@1045: end bsw/jbe@19: end bsw@1045: elseif class == "abstention" then bsw@1045: if #entries == 1 then bsw@1045: heading = _"Abstention [single entry]" bsw@1045: else bsw@1045: heading = _"Abstention [many entries]" bsw@1045: end bsw@1045: elseif class == "disapproval" then bsw@1045: disapproval_used = true bsw@1045: disapproval_index = disapproval_index + 1 bsw@1045: if disapproval_count > disapproval_index + 1 then bsw@1045: if #entries == 1 then bsw@1045: heading = _"Disapproval (prefer to lower blocks) [single entry]" bsw@1045: else bsw@1045: heading = _"Disapproval (prefer to lower blocks) [many entries]" bsw@1045: end bsw@1045: elseif disapproval_count == 2 and disapproval_index == 1 then bsw@1045: if #entries == 1 then bsw@1045: heading = _"Disapproval (prefer to lower block) [single entry]" bsw@1045: else bsw@1045: heading = _"Disapproval (prefer to lower block) [many entries]" bsw@1045: end bsw@1045: elseif disapproval_index == disapproval_count - 1 then bsw@1045: if #entries == 1 then bsw@1045: heading = _"Disapproval (prefer to last block) [single entry]" bsw@1045: else bsw@1045: heading = _"Disapproval (prefer to last block) [many entries]" bsw@1045: end bsw/jbe@19: else bsw@1045: if #entries == 1 then bsw@1045: heading = _"Disapproval [single entry]" bsw@1045: else bsw@1045: heading = _"Disapproval [many entries]" bsw@1045: end bsw/jbe@19: end bsw/jbe@19: end bsw@1045: ui.tag { bsw@1045: tag = "div", bsw@1045: attr = { class = "cathead" }, bsw@1045: content = heading bsw@1045: } bsw@1045: for i, initiative in ipairs(entries) do bsw/jbe@19: ui.container{ bsw@1045: attr = { bsw@1045: class = "movable", bsw@1045: id = "entry_" .. tostring(initiative.id) bsw@1045: }, bsw/jbe@19: content = function() bsw@1045: local initiators_selector = initiative:get_reference_selector("initiating_members") bsw@1045: :add_where("accepted") bsw@1045: local initiators = initiators_selector:exec() bsw@1045: local initiator_names = {} bsw@1045: for i, initiator in ipairs(initiators) do bsw@1045: initiator_names[#initiator_names+1] = initiator.name bsw@1045: end bsw@1045: local initiator_names_string = table.concat(initiator_names, ", ") bsw@1045: ui.container{ bsw@1045: attr = { style = "float: right; position: relative;" }, bsw@1045: content = function() bsw@1045: ui.link{ bsw@1045: attr = { class = "clickable" }, bsw@1045: content = _"Show", bsw@1045: module = "initiative", bsw@1045: view = "show", bsw@1045: id = initiative.id bsw@1045: } bsw@1045: slot.put(" ") bsw@1045: ui.link{ bsw@1045: attr = { class = "clickable", target = "_blank" }, bsw@1045: content = _"(new window)", bsw@1045: module = "initiative", bsw@1045: view = "show", bsw@1045: id = initiative.id bsw@1045: } bsw@1045: if not readonly then bsw@1045: slot.put(" ") bsw@1045: ui.image{ attr = { class = "grabber" }, static = "icons/grabber.png" } bsw@1045: end bsw@1045: end bsw/jbe@19: } bsw/jbe@19: if not readonly then bsw@1045: ui.container{ bsw@1045: attr = { style = "float: left; position: relative;" }, bsw@1045: content = function() bsw@1045: ui.tag{ bsw@1045: tag = "input", bsw@1045: attr = { bsw@1045: onclick = "if (jsFail) return true; voting_moveUp(this.parentNode.parentNode); return(false);", bsw@1045: name = "move_up_" .. tostring(initiative.id), bsw@1045: class = not disabled and "clickable" or nil, bsw@1045: type = "image", bsw@1045: src = encode.url{ static = "icons/move_up.png" }, bsw@1045: alt = _"Move up" bsw@1045: } bsw@1045: } bsw@1045: slot.put(" ") bsw@1045: ui.tag{ bsw@1045: tag = "input", bsw@1045: attr = { bsw@1045: onclick = "if (jsFail) return true; voting_moveDown(this.parentNode.parentNode); return(false);", bsw@1045: name = "move_down_" .. tostring(initiative.id), bsw@1045: class = not disabled and "clickable" or nil, bsw@1045: type = "image", bsw@1045: src = encode.url{ static = "icons/move_down.png" }, bsw@1045: alt = _"Move down" bsw@286: } bsw@286: } bsw@1045: slot.put(" ") bsw@1045: end bsw/jbe@19: } bsw/jbe@19: end bsw@1045: ui.container{ bsw@1045: content = function() bsw@1045: ui.tag{ content = "i" .. initiative.id .. ": " } bsw@1045: ui.tag{ content = initiative.shortened_name } bsw@1045: slot.put("
") bsw@1045: for i, initiator in ipairs(initiators) do bsw@1045: ui.link{ bsw@1045: attr = { class = "clickable" }, bsw@1045: content = function () bsw@1045: execute.view{ bsw@1045: module = "member_image", bsw@1045: view = "_show", bsw@1045: params = { bsw@1045: member = initiator, bsw@1045: image_type = "avatar", bsw@1045: show_dummy = true, bsw@1045: class = "micro_avatar", bsw@1045: popup_text = text bsw@1045: } bsw@1045: } bsw@1045: end, bsw@1045: module = "member", view = "show", id = initiator.id bsw@1045: } bsw@1045: slot.put(" ") bsw@1045: ui.tag{ content = initiator.name } bsw@1045: slot.put(" ") bsw@1045: end bsw@1045: end bsw@1045: } bsw/jbe@19: end bsw/jbe@6: } bsw/jbe@6: end bsw@1045: end bsw@1045: } bsw@1045: end bsw@1045: end bsw@1045: end bsw@1045: } bsw@1045: if app.session.member_id and preview then bsw@1045: local formatting_engine = param.get("formatting_engine") or config.enforce_formatting_engine bsw@1045: local comment = param.get("comment") bsw@1045: if comment and #comment > 0 then bsw@1045: local rendered_comment = format.wiki_text(comment, formatting_engine) bsw@1045: ui.heading{ level = "2", content = _"Voting comment" } bsw@1045: ui.container { attr = { class = "member_statement" }, content = function() bsw@1045: slot.put(rendered_comment) bsw@1045: end } bsw@1045: slot.put("
") bsw@1045: end bsw@1045: end bsw@1045: if (readonly or direct_voter and direct_voter.comment) and not preview and not (app.session.member_id == member.id) then bsw@1045: local text bsw@1045: if direct_voter and direct_voter.comment_changed then bsw@1045: text = _("Voting comment (last updated: #{timestamp})", { timestamp = format.timestamp(direct_voter.comment_changed) }) bsw@1045: elseif direct_voter and direct_voter.comment then bsw@1045: text = _"Voting comment" bsw@1045: end bsw@1045: if text then bsw@1045: ui.heading{ level = "2", content = text } bsw@1045: end bsw@1045: if direct_voter and direct_voter.comment then bsw@1045: local rendered_comment = direct_voter:get_content('html') bsw@1045: ui.container { attr = { class = "member_statement" }, content = function() bsw@1045: slot.put(rendered_comment) bsw@1045: end } bsw@1045: slot.put("
") bsw@1045: end bsw@1045: end bsw@1045: if app.session.member_id and app.session.member_id == member.id then bsw@1045: if (not readonly or direct_voter) and not preview then bsw@1045: ui.container{ content = function() bsw@1045: if not config.enforce_formatting_engine then bsw@1045: ui.field.select{ bsw@1045: label = _"Wiki engine for statement", bsw@1045: name = "formatting_engine", bsw@1045: foreign_records = config.formatting_engines, bsw@1045: attr = {id = "formatting_engine"}, bsw@1045: foreign_id = "id", bsw@1045: foreign_name = "name", bsw@1045: value = param.get("formatting_engine") or direct_voter and direct_voter.formatting_engine bsw@1045: } bsw@1045: end bsw@1045: ui.heading { level = 2, content = _"Voting comment (optional)" } bsw@1045: ui.field.text{ bsw@1045: name = "comment", bsw@1045: multiline = true, bsw@1045: value = param.get("comment") or direct_voter and direct_voter.comment, bsw@1045: attr = { style = "height: 10ex; width: 100%;" }, bsw@1045: } bsw@1045: end } bsw@1045: end bsw@1045: bsw@1045: if preview then bsw@1045: if not config.enforce_formatting_engine then bsw@1045: ui.field.hidden{ name = "formatting_engine", value = param.get("formatting_engine") } bsw@1045: end bsw@1045: ui.field.hidden{ name = "comment", value = param.get("comment") or direct_voter and direct_voter.comment } bsw@1045: end bsw@1045: bsw@1045: if not readonly or direct_voter or preview then bsw@1045: ui.container{ content = function() bsw@1045: if preview then bsw@1045: slot.put(" ") bsw@1045: ui.tag{ bsw@1045: tag = "input", bsw@1045: attr = { bsw@1045: type = "submit", bsw@1045: class = "btn btn-default", bsw@1045: name = issue.closed and "update_comment" or nil, bsw@1045: value = submit_button_text -- finish voting / update comment bsw/jbe@19: } bsw@1045: } bsw/jbe@5: end bsw@1045: if not preview then bsw@1045: ui.tag{ bsw@1045: tag = "input", bsw@1045: attr = { bsw@1045: type = "submit", bsw@1045: name = "preview", bsw@1045: class = "btn btn-default", bsw@1045: value = _"Preview", bsw@1045: } bsw@1045: } bsw@1045: else bsw@1045: slot.put(" ") bsw@1045: ui.tag{ bsw@1045: tag = "input", bsw@1045: attr = { bsw@1045: type = "submit", bsw@1045: name = "edit", bsw@1045: class = "btn-link", bsw@1045: value = edit_button_text, bsw@1045: } bsw@1045: } bsw@1045: end bsw@1045: end } bsw/jbe@19: end bsw/jbe@5: end bsw/jbe@5: end bsw/jbe@5: } bsw@1045: slot.put("
") bsw@1045: ui.link{ bsw@1045: text = _"Cancel", bsw@1045: module = "issue", bsw@1045: view = "show", bsw@1045: id = issue.id bsw@1045: } bsw@1045: if direct_voter then bsw@1045: slot.put(" | ") bsw@1045: ui.link { bsw@1045: module = "vote", action = "update", bsw@1045: params = { bsw@1045: issue_id = issue.id, bsw@1045: discard = true bsw@1045: }, bsw@1045: routing = { bsw@1045: default = { bsw@1045: mode = "redirect", bsw@1045: module = "issue", bsw@1045: view = "show", bsw@1045: id = issue.id bsw@1045: } bsw@1045: }, bsw@1045: text = _"Discard my vote" bsw@1045: } bsw@879: end bsw/jbe@5: bsw@1045: end ) bsw@1045: end )