bsw/jbe@1309: if not app.session.member then bsw/jbe@1309: return bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: local cancel = param.get("cancel") and true or false bsw/jbe@1309: if cancel then return true end bsw/jbe@1309: bsw/jbe@1309: local issue = Issue:new_selector():add_where{ "id = ?", param.get("issue_id", atom.integer) }:for_share():single_object_mode():exec() bsw/jbe@1309: bsw/jbe@1309: bsw/jbe@1309: if not app.session.member:has_voting_right_for_unit_id(issue.area.unit_id) then bsw/jbe@1309: return execute.view { module = "index", view = "403" } bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: if issue.state ~= "voting" and not issue.closed then bsw/jbe@1309: slot.put_into("error", _"Voting has not started yet.") bsw/jbe@1309: return false bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: if issue.phase_finished or issue.closed and not update_comment then bsw/jbe@1309: slot.put_into("error", _"This issue is already closed.") bsw/jbe@1309: return false bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: local direct_voter = DirectVoter:by_pk(issue.id, app.session.member_id) bsw/jbe@1309: bsw/jbe@1309: if param.get("discard") then bsw/jbe@1309: if direct_voter then bsw/jbe@1309: direct_voter:destroy() bsw/jbe@1309: end bsw/jbe@1309: slot.put_into("notice", _"Your vote has been discarded. Delegation rules apply if set.") bsw/jbe@1309: return bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: local initiatives = issue:get_reference_selector("initiatives") bsw/jbe@1309: :add_where("initiative.admitted") bsw/jbe@1309: :add_order_by("initiative.satisfied_supporter_count DESC") bsw/jbe@1309: :exec() bsw/jbe@1309: bsw/jbe@1309: local vote_for_initiative_id = tonumber(param.get("vote_for_initiative_id")) bsw/jbe@1309: bsw/jbe@1309: local voted = 0 bsw/jbe@1309: bsw/jbe@1309: for i, initiative in ipairs(initiatives) do bsw/jbe@1309: if initiative.id == vote_for_initiative_id then bsw/jbe@1309: voted = voted + 1 bsw/jbe@1309: end bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: if voted ~= 1 then bsw/jbe@1309: slot.put_into("error", _"Please choose one project to vote for.") bsw/jbe@1309: return false bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: if not direct_voter then bsw/jbe@1309: direct_voter = DirectVoter:new() bsw/jbe@1309: direct_voter.issue_id = issue.id bsw/jbe@1309: direct_voter.member_id = app.session.member_id bsw/jbe@1309: direct_voter:save() bsw/jbe@1309: else bsw/jbe@1309: local votes = Vote:new_selector() bsw/jbe@1309: :add_where{ "vote.issue_id = ?", issue.id } bsw/jbe@1309: :add_where{ "vote.member_id = ?", app.session.member_id } bsw/jbe@1309: :exec() bsw/jbe@1309: for i, vote in ipairs(votes) do bsw/jbe@1309: vote:destroy() bsw/jbe@1309: end bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: for i, initiative in ipairs(initiatives) do bsw/jbe@1309: local vote = Vote:new() bsw/jbe@1309: vote.issue_id = issue.id bsw/jbe@1309: vote.initiative_id = initiative.id bsw/jbe@1309: vote.member_id = app.session.member_id bsw/jbe@1309: if initiative.id == vote_for_initiative_id then bsw/jbe@1309: vote.grade = 1 bsw/jbe@1309: else bsw/jbe@1309: vote.grade = 0 bsw/jbe@1309: end bsw/jbe@1309: vote:save() bsw/jbe@1309: end