liquid_feedback_frontend
diff app/main/registration/_action/update_vote.lua @ 1309:32cc544d5a5b
Cumulative patch for upcoming frontend version 4
| author | bsw/jbe |
|---|---|
| date | Sun Jul 15 14:07:29 2018 +0200 (2018-07-15) |
| parents | |
| children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/app/main/registration/_action/update_vote.lua Sun Jul 15 14:07:29 2018 +0200 1.3 @@ -0,0 +1,81 @@ 1.4 +if not app.session.member then 1.5 + return 1.6 +end 1.7 + 1.8 +local cancel = param.get("cancel") and true or false 1.9 +if cancel then return true end 1.10 + 1.11 +local issue = Issue:new_selector():add_where{ "id = ?", param.get("issue_id", atom.integer) }:for_share():single_object_mode():exec() 1.12 + 1.13 + 1.14 +if not app.session.member:has_voting_right_for_unit_id(issue.area.unit_id) then 1.15 + return execute.view { module = "index", view = "403" } 1.16 +end 1.17 + 1.18 +if issue.state ~= "voting" and not issue.closed then 1.19 + slot.put_into("error", _"Voting has not started yet.") 1.20 + return false 1.21 +end 1.22 + 1.23 +if issue.phase_finished or issue.closed and not update_comment then 1.24 + slot.put_into("error", _"This issue is already closed.") 1.25 + return false 1.26 +end 1.27 + 1.28 +local direct_voter = DirectVoter:by_pk(issue.id, app.session.member_id) 1.29 + 1.30 +if param.get("discard") then 1.31 + if direct_voter then 1.32 + direct_voter:destroy() 1.33 + end 1.34 + slot.put_into("notice", _"Your vote has been discarded. Delegation rules apply if set.") 1.35 + return 1.36 +end 1.37 + 1.38 +local initiatives = issue:get_reference_selector("initiatives") 1.39 + :add_where("initiative.admitted") 1.40 + :add_order_by("initiative.satisfied_supporter_count DESC") 1.41 + :exec() 1.42 + 1.43 +local vote_for_initiative_id = tonumber(param.get("vote_for_initiative_id")) 1.44 + 1.45 +local voted = 0 1.46 + 1.47 +for i, initiative in ipairs(initiatives) do 1.48 + if initiative.id == vote_for_initiative_id then 1.49 + voted = voted + 1 1.50 + end 1.51 +end 1.52 + 1.53 +if voted ~= 1 then 1.54 + slot.put_into("error", _"Please choose one project to vote for.") 1.55 + return false 1.56 +end 1.57 + 1.58 +if not direct_voter then 1.59 + direct_voter = DirectVoter:new() 1.60 + direct_voter.issue_id = issue.id 1.61 + direct_voter.member_id = app.session.member_id 1.62 + direct_voter:save() 1.63 +else 1.64 + local votes = Vote:new_selector() 1.65 + :add_where{ "vote.issue_id = ?", issue.id } 1.66 + :add_where{ "vote.member_id = ?", app.session.member_id } 1.67 + :exec() 1.68 + for i, vote in ipairs(votes) do 1.69 + vote:destroy() 1.70 + end 1.71 +end 1.72 + 1.73 +for i, initiative in ipairs(initiatives) do 1.74 + local vote = Vote:new() 1.75 + vote.issue_id = issue.id 1.76 + vote.initiative_id = initiative.id 1.77 + vote.member_id = app.session.member_id 1.78 + if initiative.id == vote_for_initiative_id then 1.79 + vote.grade = 1 1.80 + else 1.81 + vote.grade = 0 1.82 + end 1.83 + vote:save() 1.84 +end