liquid_feedback_frontend
view app/main/registration/_action/update_vote.lua @ 1532:3c15fea3f1c0
Added FirstLife group mirroring
author | bsw |
---|---|
date | Sun Oct 04 16:31:47 2020 +0200 (2020-10-04) |
parents | 32cc544d5a5b |
children |
line source
1 if not app.session.member then
2 return
3 end
5 local cancel = param.get("cancel") and true or false
6 if cancel then return true end
8 local issue = Issue:new_selector():add_where{ "id = ?", param.get("issue_id", atom.integer) }:for_share():single_object_mode():exec()
11 if not app.session.member:has_voting_right_for_unit_id(issue.area.unit_id) then
12 return execute.view { module = "index", view = "403" }
13 end
15 if issue.state ~= "voting" and not issue.closed then
16 slot.put_into("error", _"Voting has not started yet.")
17 return false
18 end
20 if issue.phase_finished or issue.closed and not update_comment then
21 slot.put_into("error", _"This issue is already closed.")
22 return false
23 end
25 local direct_voter = DirectVoter:by_pk(issue.id, app.session.member_id)
27 if param.get("discard") then
28 if direct_voter then
29 direct_voter:destroy()
30 end
31 slot.put_into("notice", _"Your vote has been discarded. Delegation rules apply if set.")
32 return
33 end
35 local initiatives = issue:get_reference_selector("initiatives")
36 :add_where("initiative.admitted")
37 :add_order_by("initiative.satisfied_supporter_count DESC")
38 :exec()
40 local vote_for_initiative_id = tonumber(param.get("vote_for_initiative_id"))
42 local voted = 0
44 for i, initiative in ipairs(initiatives) do
45 if initiative.id == vote_for_initiative_id then
46 voted = voted + 1
47 end
48 end
50 if voted ~= 1 then
51 slot.put_into("error", _"Please choose one project to vote for.")
52 return false
53 end
55 if not direct_voter then
56 direct_voter = DirectVoter:new()
57 direct_voter.issue_id = issue.id
58 direct_voter.member_id = app.session.member_id
59 direct_voter:save()
60 else
61 local votes = Vote:new_selector()
62 :add_where{ "vote.issue_id = ?", issue.id }
63 :add_where{ "vote.member_id = ?", app.session.member_id }
64 :exec()
65 for i, vote in ipairs(votes) do
66 vote:destroy()
67 end
68 end
70 for i, initiative in ipairs(initiatives) do
71 local vote = Vote:new()
72 vote.issue_id = issue.id
73 vote.initiative_id = initiative.id
74 vote.member_id = app.session.member_id
75 if initiative.id == vote_for_initiative_id then
76 vote.grade = 1
77 else
78 vote.grade = 0
79 end
80 vote:save()
81 end