liquid_feedback_frontend

view app/main/vote/_action/update.lua @ 879:ea3d3757ddc3

Added support for voting comments
author bsw
date Mon Aug 20 01:00:09 2012 +0200 (2012-08-20)
parents 75a95999a410
children fe39c1fb541b
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 = util.trim(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 direct_voter.comment_changed = 'now'
77 direct_voter:render_content(true)
78 else
79 direct_voter.formatting_engine = null
80 direct_voter.comment = null
81 direct_voter.comment_changed = 'now'
82 end
83 end
84 direct_voter:save()
86 end
88 if not update_comment then
89 local scoring = param.get("scoring")
91 for initiative_id, grade in scoring:gmatch("([^:;]+):([^:;]+)") do
92 local initiative_id = tonumber(initiative_id)
93 local grade = tonumber(grade)
94 local initiative = Initiative:by_id(initiative_id)
95 if initiative.issue.id ~= issue.id then
96 error("initiative from wrong issue")
97 end
98 if not preview and not issue.closed then
99 local vote = Vote:by_pk(initiative_id, app.session.member.id)
100 if not vote then
101 vote = Vote:new()
102 vote.issue_id = issue.id
103 vote.initiative_id = initiative.id
104 vote.member_id = app.session.member.id
105 end
106 vote.grade = grade
107 vote:save()
108 end
109 end
110 end
112 if not preview and not cancel then
113 request.redirect{
114 module = "issue",
115 view = "show",
116 id = issue.id
117 }
118 end
120 else
122 local current_initiative_id = move_up or move_down
124 local current_grade = tempvotings[current_initiative_id] or 0
125 local is_alone = true
126 if current_grade == 0 then
127 is_alone = false
128 else
129 for initiative_id, grade in pairs(tempvotings) do
130 if current_initiative_id ~= initiative_id and grade == current_grade then
131 is_alone = false
132 break
133 end
134 end
135 end
137 if move_up and current_grade >= 0 and is_alone then
138 for initiative_id, grade in pairs(tempvotings) do
139 if grade > current_grade then
140 tempvotings[initiative_id] = grade - 1
141 end
142 end
144 elseif move_up and current_grade >= 0 and not is_alone then
145 for initiative_id, grade in pairs(tempvotings) do
146 if grade > current_grade then
147 tempvotings[initiative_id] = grade + 1
148 end
149 end
150 tempvotings[current_initiative_id] = current_grade + 1
152 elseif move_up and current_grade < 0 and is_alone then
153 tempvotings[current_initiative_id] = current_grade + 1
154 for initiative_id, grade in pairs(tempvotings) do
155 if grade < current_grade then
156 tempvotings[initiative_id] = grade + 1
157 end
158 end
160 elseif move_up and current_grade < 0 and not 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
166 tempvotings[current_initiative_id] = current_grade
168 elseif move_down and current_grade <= 0 and is_alone then
169 for initiative_id, grade in pairs(tempvotings) do
170 if grade < current_grade then
171 tempvotings[initiative_id] = grade + 1
172 end
173 end
175 elseif move_down and current_grade <= 0 and not is_alone then
176 for initiative_id, grade in pairs(tempvotings) do
177 if grade < current_grade then
178 tempvotings[initiative_id] = grade - 1
179 end
180 end
181 tempvotings[current_initiative_id] = current_grade - 1
183 elseif move_down and current_grade > 0 and is_alone then
184 tempvotings[current_initiative_id] = current_grade - 1
185 for initiative_id, grade in pairs(tempvotings) do
186 if grade > current_grade then
187 tempvotings[initiative_id] = grade - 1
188 end
189 end
191 elseif move_down and current_grade > 0 and not 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
197 tempvotings[current_initiative_id] = current_grade
199 end
201 local tempvotings_list = {}
202 for key, val in pairs(tempvotings) do
203 tempvotings_list[#tempvotings_list+1] = tostring(key) .. ":" .. tostring(val)
204 end
206 tempvoting_string = table.concat(tempvotings_list, ";")
208 request.redirect{
209 module = "vote",
210 view = "list",
211 params = {
212 issue_id = issue.id,
213 scoring = tempvoting_string
214 }
215 }
217 end

Impressum / About Us