liquid_feedback_frontend

view app/main/vote/_action/update.lua @ 925:815995d95962

Do not trim wiki formatted voting comment input
author bsw
date Mon Oct 15 21:43:46 2012 +0200 (2012-10-15)
parents fe39c1fb541b
children e616d8f16f14
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("preview2") == "1" 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") == "1" and true or false
14 if issue.closed and not update_comment then
15 slot.put_into("error", _"This issue is already closed.")
16 return false
17 end
19 if issue.state ~= "voting" and not issue.closed then
20 slot.put_into("error", _"Voting has not started yet.")
21 return false
22 end
24 local direct_voter = DirectVoter:by_pk(issue.id, app.session.member_id)
26 if param.get("discard", atom.boolean) then
27 if direct_voter then
28 direct_voter:destroy()
29 end
30 slot.put_into("notice", _"Your vote has been discarded. Delegation rules apply if set.")
31 return
32 end
34 local move_up
35 local move_down
37 local tempvoting_string = param.get("scoring")
39 local tempvotings = {}
40 if not update_comment then
41 for match in tempvoting_string:gmatch("([^;]+)") do
42 for initiative_id, grade in match:gmatch("([^:;]+):([^:;]+)") do
43 tempvotings[tonumber(initiative_id)] = tonumber(grade)
44 if param.get("move_up_" .. initiative_id .. ".x", atom.integer) then
45 move_up = tonumber(initiative_id)
46 elseif param.get("move_down_" .. initiative_id .. ".x", atom.integer) then
47 move_down = tonumber(initiative_id)
48 end
49 end
50 end
51 end
53 if not move_down and not move_up then
54 if not preview then
55 if not direct_voter then
56 if issue.closed then
57 slot.put_into("error", _"This issue is already closed.")
58 return false
59 else
60 direct_voter = DirectVoter:new()
61 direct_voter.issue_id = issue.id
62 direct_voter.member_id = app.session.member_id
63 direct_voter:save()
65 direct_voter = DirectVoter:by_pk(issue.id, app.session.member_id)
66 end
67 end
69 local formatting_engine = param.get("formatting_engine")
70 local comment = param.get("comment")
72 if comment ~= direct_voter.comment then
73 if #comment > 0 then
74 direct_voter.formatting_engine = formatting_engine
75 direct_voter.comment = comment
76 if issue.closed then
77 direct_voter.comment_changed = 'now'
78 end
79 direct_voter:render_content(true)
80 else
81 direct_voter.formatting_engine = null
82 direct_voter.comment = null
83 if issue.closed then
84 direct_voter.comment_changed = 'now'
85 end
86 end
87 end
88 direct_voter:save()
90 end
92 if not update_comment then
93 local scoring = param.get("scoring")
95 for initiative_id, grade in scoring:gmatch("([^:;]+):([^:;]+)") do
96 local initiative_id = tonumber(initiative_id)
97 local grade = tonumber(grade)
98 local initiative = Initiative:by_id(initiative_id)
99 if initiative.issue.id ~= issue.id then
100 error("initiative from wrong issue")
101 end
102 if not preview and not issue.closed then
103 local vote = Vote:by_pk(initiative_id, app.session.member.id)
104 if not vote then
105 vote = Vote:new()
106 vote.issue_id = issue.id
107 vote.initiative_id = initiative.id
108 vote.member_id = app.session.member.id
109 end
110 vote.grade = grade
111 vote:save()
112 end
113 end
114 end
116 if not preview and not cancel then
117 request.redirect{
118 module = "issue",
119 view = "show",
120 id = issue.id
121 }
122 end
124 else
126 local current_initiative_id = move_up or move_down
128 local current_grade = tempvotings[current_initiative_id] or 0
129 local is_alone = true
130 if current_grade == 0 then
131 is_alone = false
132 else
133 for initiative_id, grade in pairs(tempvotings) do
134 if current_initiative_id ~= initiative_id and grade == current_grade then
135 is_alone = false
136 break
137 end
138 end
139 end
141 if move_up and current_grade >= 0 and 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
148 elseif move_up and current_grade >= 0 and not is_alone then
149 for initiative_id, grade in pairs(tempvotings) do
150 if grade > current_grade then
151 tempvotings[initiative_id] = grade + 1
152 end
153 end
154 tempvotings[current_initiative_id] = current_grade + 1
156 elseif move_up and current_grade < 0 and is_alone then
157 tempvotings[current_initiative_id] = current_grade + 1
158 for initiative_id, grade in pairs(tempvotings) do
159 if grade < current_grade then
160 tempvotings[initiative_id] = grade + 1
161 end
162 end
164 elseif move_up and current_grade < 0 and not is_alone then
165 for initiative_id, grade in pairs(tempvotings) do
166 if grade <= current_grade then
167 tempvotings[initiative_id] = grade - 1
168 end
169 end
170 tempvotings[current_initiative_id] = current_grade
172 elseif move_down and current_grade <= 0 and is_alone then
173 for initiative_id, grade in pairs(tempvotings) do
174 if grade < current_grade then
175 tempvotings[initiative_id] = grade + 1
176 end
177 end
179 elseif move_down and current_grade <= 0 and not is_alone then
180 for initiative_id, grade in pairs(tempvotings) do
181 if grade < current_grade then
182 tempvotings[initiative_id] = grade - 1
183 end
184 end
185 tempvotings[current_initiative_id] = current_grade - 1
187 elseif move_down and current_grade > 0 and is_alone then
188 tempvotings[current_initiative_id] = current_grade - 1
189 for initiative_id, grade in pairs(tempvotings) do
190 if grade > current_grade then
191 tempvotings[initiative_id] = grade - 1
192 end
193 end
195 elseif move_down and current_grade > 0 and not is_alone then
196 for initiative_id, grade in pairs(tempvotings) do
197 if grade >= current_grade then
198 tempvotings[initiative_id] = grade + 1
199 end
200 end
201 tempvotings[current_initiative_id] = current_grade
203 end
205 local tempvotings_list = {}
206 for key, val in pairs(tempvotings) do
207 tempvotings_list[#tempvotings_list+1] = tostring(key) .. ":" .. tostring(val)
208 end
210 tempvoting_string = table.concat(tempvotings_list, ";")
212 request.redirect{
213 module = "vote",
214 view = "list",
215 params = {
216 issue_id = issue.id,
217 scoring = tempvoting_string
218 }
219 }
221 end

Impressum / About Us