liquid_feedback_frontend
view app/main/vote/_action/update.lua @ 286:c587d8762e62
Registration process updated for Core 2.0, lockable member fields, notification settings
author | bsw |
---|---|
date | Sat Feb 25 11:51:37 2012 +0100 (2012-02-25) |
parents | b77e6a17ca77 |
children | 7492497005bd |
line source
1 local issue = Issue:new_selector():add_where{ "id = ?", param.get("issue_id", atom.integer) }:for_share():single_object_mode():exec()
3 if not app.session.member:has_voting_right_for_unit_id(issue.area.unit_id) then
4 error("access denied")
5 end
7 if issue.closed then
8 slot.put_into("error", _"This issue is already closed.")
9 return false
10 end
12 if issue.state ~= "voting" then
13 slot.put_into("error", _"Voting has not started yet.")
14 return false
15 end
19 local move_up = param.get("move_up", atom.integer)
20 local move_down = param.get("move_down", atom.integer)
22 if not move_down and not move_up then
23 local direct_voter = DirectVoter:by_pk(issue.id, app.session.member_id)
25 if param.get("discard", atom.boolean) then
26 if direct_voter then
27 direct_voter:destroy()
28 end
29 slot.put_into("notice", _"Your vote has been discarded. Delegation rules apply if set.")
30 return
31 end
33 if not direct_voter then
34 direct_voter = DirectVoter:new()
35 direct_voter.issue_id = issue.id
36 direct_voter.member_id = app.session.member_id
37 end
39 direct_voter:save()
41 local scoring = param.get("scoring")
43 for initiative_id, grade in scoring:gmatch("([^:;]+):([^:;]+)") do
44 local initiative_id = tonumber(initiative_id)
45 local grade = tonumber(grade)
46 local initiative = Initiative:by_id(initiative_id)
47 if initiative.issue.id ~= issue.id then
48 error("initiative from wrong issue")
49 end
50 local vote = Vote:by_pk(initiative_id, app.session.member.id)
51 if not vote then
52 vote = Vote:new()
53 vote.issue_id = issue.id
54 vote.initiative_id = initiative.id
55 vote.member_id = app.session.member.id
56 end
57 vote.grade = grade
58 vote:save()
59 end
61 else
63 local tempvoting_string = param.get("scoring")
65 local tempvotings = {}
66 for match in tempvoting_string:gmatch("([^;]+)") do
67 for initiative_id, grade in match:gmatch("([^:;]+):([^:;]+)") do
68 tempvotings[tonumber(initiative_id)] = tonumber(grade)
69 end
70 end
72 local current_initiative_id = move_up or move_down
74 local current_grade = tempvotings[current_initiative_id] or 0
75 local is_alone = true
76 if current_grade == 0 then
77 is_alone = false
78 else
79 for initiative_id, grade in pairs(tempvotings) do
80 if current_initiative_id ~= initiative_id and grade == current_grade then
81 is_alone = false
82 break
83 end
84 end
85 end
87 if move_up and current_grade >= 0 and is_alone then
88 for initiative_id, grade in pairs(tempvotings) do
89 if grade > current_grade then
90 tempvotings[initiative_id] = grade - 1
91 end
92 end
94 elseif move_up and current_grade >= 0 and not is_alone then
95 for initiative_id, grade in pairs(tempvotings) do
96 if grade > current_grade then
97 tempvotings[initiative_id] = grade + 1
98 end
99 end
100 tempvotings[current_initiative_id] = current_grade + 1
102 elseif move_up and current_grade < 0 and is_alone then
103 tempvotings[current_initiative_id] = current_grade + 1
104 for initiative_id, grade in pairs(tempvotings) do
105 if grade < current_grade then
106 tempvotings[initiative_id] = grade + 1
107 end
108 end
110 elseif move_up and current_grade < 0 and not is_alone then
111 for initiative_id, grade in pairs(tempvotings) do
112 if grade <= current_grade then
113 tempvotings[initiative_id] = grade - 1
114 end
115 end
116 tempvotings[current_initiative_id] = current_grade
118 elseif move_down and current_grade <= 0 and is_alone then
119 for initiative_id, grade in pairs(tempvotings) do
120 if grade < current_grade then
121 tempvotings[initiative_id] = grade + 1
122 end
123 end
125 elseif move_down and current_grade <= 0 and not is_alone then
126 for initiative_id, grade in pairs(tempvotings) do
127 if grade < current_grade then
128 tempvotings[initiative_id] = grade - 1
129 end
130 end
131 tempvotings[current_initiative_id] = current_grade - 1
133 elseif move_down and current_grade > 0 and is_alone then
134 tempvotings[current_initiative_id] = current_grade - 1
135 for initiative_id, grade in pairs(tempvotings) do
136 if grade > current_grade then
137 tempvotings[initiative_id] = grade - 1
138 end
139 end
141 elseif move_down and current_grade > 0 and not is_alone then
142 for initiative_id, grade in pairs(tempvotings) do
143 if grade >= current_grade then
144 tempvotings[initiative_id] = grade + 1
145 end
146 end
147 tempvotings[current_initiative_id] = current_grade
149 end
151 local tempvotings_list = {}
152 for key, val in pairs(tempvotings) do
153 tempvotings_list[#tempvotings_list+1] = tostring(key) .. ":" .. tostring(val)
154 end
156 tempvoting_string = table.concat(tempvotings_list, ";")
158 request.redirect{
159 module = "vote",
160 view = "list",
161 params = {
162 issue_id = issue.id,
163 scoring = tempvoting_string
164 }
165 }
167 end