liquid_feedback_frontend
view app/main/vote/_action/update.lua @ 1045:701a5cf6b067
Imported LiquidFeedback Frontend 3.0 branch
author | bsw |
---|---|
date | Thu Jul 10 01:19:48 2014 +0200 (2014-07-10) |
parents | 1997cf1da04b |
children | 32cc544d5a5b |
line source
1 local cancel = param.get("cancel") and true or false
2 if cancel then return end
4 local issue = Issue:new_selector():add_where{ "id = ?", param.get("issue_id", atom.integer) }:for_share():single_object_mode():exec()
6 local preview = (param.get("preview") or param.get("edit")) and true or false
8 if not app.session.member:has_voting_right_for_unit_id(issue.area.unit_id) then
9 error("access denied")
10 end
12 local update_comment = param.get("update_comment") and true or false
14 if issue.state ~= "voting" and not issue.closed then
15 slot.put_into("error", _"Voting has not started yet.")
16 return false
17 end
19 if (issue.phase_finished or issue.closed) and preview then
20 return
21 end
23 if issue.phase_finished or issue.closed and not update_comment then
24 slot.put_into("error", _"This issue is already closed.")
25 return false
26 end
28 local direct_voter = DirectVoter:by_pk(issue.id, app.session.member_id)
30 if param.get("discard", atom.boolean) then
31 if direct_voter then
32 direct_voter:destroy()
33 end
34 slot.put_into("notice", _"Your vote has been discarded. Delegation rules apply if set.")
35 return
36 end
38 local move_up
39 local move_down
41 local tempvoting_string = param.get("scoring")
43 local tempvotings = {}
44 if not update_comment then
45 for match in tempvoting_string:gmatch("([^;]+)") do
46 for initiative_id, grade in match:gmatch("([^:;]+):([^:;]+)") do
47 tempvotings[tonumber(initiative_id)] = tonumber(grade)
48 if param.get("move_up_" .. initiative_id .. ".x", atom.integer) then
49 move_up = tonumber(initiative_id)
50 elseif param.get("move_down_" .. initiative_id .. ".x", atom.integer) then
51 move_down = tonumber(initiative_id)
52 end
53 end
54 end
55 end
57 if not move_down and not move_up then
58 if not preview then
59 if not direct_voter then
60 if issue.closed then
61 slot.put_into("error", _"This issue is already closed.")
62 return false
63 else
64 direct_voter = DirectVoter:new()
65 direct_voter.issue_id = issue.id
66 direct_voter.member_id = app.session.member_id
67 direct_voter:save()
69 direct_voter = DirectVoter:by_pk(issue.id, app.session.member_id)
70 end
71 end
73 local formatting_engine
74 if config.enforce_formatting_engine then
75 formatting_engine = config.enforce_formatting_engine
76 else
77 formatting_engine = param.get("formatting_engine")
78 local formatting_engine_valid = false
79 for i, fe in ipairs(config.formatting_engines) do
80 if formatting_engine == fe.id then
81 formatting_engine_valid = true
82 end
83 end
84 if not formatting_engine_valid then
85 error("invalid formatting engine!")
86 end
87 end
89 local comment = param.get("comment")
91 if comment ~= direct_voter.comment then
92 if #comment > 0 then
93 direct_voter.formatting_engine = formatting_engine
94 direct_voter.comment = comment
95 if issue.closed then
96 direct_voter.comment_changed = 'now'
97 end
98 direct_voter:render_content(true)
99 else
100 direct_voter.formatting_engine = null
101 direct_voter.comment = null
102 if issue.closed then
103 direct_voter.comment_changed = 'now'
104 end
105 end
106 end
107 direct_voter:save()
109 end
111 if not update_comment then
112 local scoring = param.get("scoring")
114 for initiative_id, grade in scoring:gmatch("([^:;]+):([^:;]+)") do
115 local initiative_id = tonumber(initiative_id)
116 local grade = tonumber(grade)
117 local initiative = Initiative:by_id(initiative_id)
118 if initiative.issue.id ~= issue.id then
119 error("initiative from wrong issue")
120 end
121 if not preview and not issue.closed then
122 local vote = Vote:by_pk(initiative_id, app.session.member.id)
123 if not vote then
124 vote = Vote:new()
125 vote.issue_id = issue.id
126 vote.initiative_id = initiative.id
127 vote.member_id = app.session.member.id
128 end
129 vote.grade = grade
130 vote:save()
131 end
132 end
133 end
135 if not preview and not cancel then
136 request.redirect{
137 module = "issue",
138 view = "show",
139 id = issue.id
140 }
141 end
143 else
145 local current_initiative_id = move_up or move_down
147 local current_grade = tempvotings[current_initiative_id] or 0
148 local is_alone = true
149 if current_grade == 0 then
150 is_alone = false
151 else
152 for initiative_id, grade in pairs(tempvotings) do
153 if current_initiative_id ~= initiative_id and grade == current_grade then
154 is_alone = false
155 break
156 end
157 end
158 end
160 if move_up and current_grade >= 0 and is_alone then
161 for initiative_id, grade in pairs(tempvotings) do
162 if grade > current_grade then
163 tempvotings[initiative_id] = grade - 1
164 end
165 end
167 elseif move_up and current_grade >= 0 and not is_alone then
168 for initiative_id, grade in pairs(tempvotings) do
169 if grade > current_grade then
170 tempvotings[initiative_id] = grade + 1
171 end
172 end
173 tempvotings[current_initiative_id] = current_grade + 1
175 elseif move_up and current_grade < 0 and is_alone then
176 tempvotings[current_initiative_id] = current_grade + 1
177 for initiative_id, grade in pairs(tempvotings) do
178 if grade < current_grade then
179 tempvotings[initiative_id] = grade + 1
180 end
181 end
183 elseif move_up and current_grade < 0 and not is_alone then
184 for initiative_id, grade in pairs(tempvotings) do
185 if grade <= current_grade then
186 tempvotings[initiative_id] = grade - 1
187 end
188 end
189 tempvotings[current_initiative_id] = current_grade
191 elseif move_down and current_grade <= 0 and is_alone then
192 for initiative_id, grade in pairs(tempvotings) do
193 if grade < current_grade then
194 tempvotings[initiative_id] = grade + 1
195 end
196 end
198 elseif move_down and current_grade <= 0 and not is_alone then
199 for initiative_id, grade in pairs(tempvotings) do
200 if grade < current_grade then
201 tempvotings[initiative_id] = grade - 1
202 end
203 end
204 tempvotings[current_initiative_id] = current_grade - 1
206 elseif move_down and current_grade > 0 and is_alone then
207 tempvotings[current_initiative_id] = current_grade - 1
208 for initiative_id, grade in pairs(tempvotings) do
209 if grade > current_grade then
210 tempvotings[initiative_id] = grade - 1
211 end
212 end
214 elseif move_down and current_grade > 0 and not is_alone then
215 for initiative_id, grade in pairs(tempvotings) do
216 if grade >= current_grade then
217 tempvotings[initiative_id] = grade + 1
218 end
219 end
220 tempvotings[current_initiative_id] = current_grade
222 end
224 local tempvotings_list = {}
225 for key, val in pairs(tempvotings) do
226 tempvotings_list[#tempvotings_list+1] = tostring(key) .. ":" .. tostring(val)
227 end
229 tempvoting_string = table.concat(tempvotings_list, ";")
231 request.redirect{
232 module = "vote",
233 view = "list",
234 params = {
235 issue_id = issue.id,
236 scoring = tempvoting_string
237 }
238 }
240 end