rev |
line source |
bsw@879
|
1 local cancel = param.get("cancel") and true or false
|
bsw@879
|
2 if cancel then return end
|
bsw@879
|
3
|
bsw/jbe@5
|
4 local issue = Issue:new_selector():add_where{ "id = ?", param.get("issue_id", atom.integer) }:for_share():single_object_mode():exec()
|
bsw/jbe@5
|
5
|
bsw/jbe@1309
|
6 -- TODO patch for project voting
|
bsw/jbe@1309
|
7 if config.alternative_voting and config.alternative_voting[tostring(issue.policy.id)] then
|
bsw/jbe@1309
|
8 return false
|
bsw/jbe@1309
|
9 end
|
bsw/jbe@1309
|
10
|
bsw/jbe@1309
|
11
|
bsw@1045
|
12 local preview = (param.get("preview") or param.get("edit")) and true or false
|
bsw@879
|
13
|
bsw@281
|
14 if not app.session.member:has_voting_right_for_unit_id(issue.area.unit_id) then
|
bsw/jbe@1309
|
15 return execute.view { module = "index", view = "403" }
|
bsw@281
|
16 end
|
bsw@281
|
17
|
bsw@1045
|
18 local update_comment = param.get("update_comment") and true or false
|
bsw@879
|
19
|
bsw@964
|
20 if issue.state ~= "voting" and not issue.closed then
|
bsw@964
|
21 slot.put_into("error", _"Voting has not started yet.")
|
bsw/jbe@5
|
22 return false
|
bsw/jbe@5
|
23 end
|
bsw/jbe@5
|
24
|
bsw@1045
|
25 if (issue.phase_finished or issue.closed) and preview then
|
bsw@1045
|
26 return
|
bsw@1045
|
27 end
|
bsw@1045
|
28
|
bsw@964
|
29 if issue.phase_finished or issue.closed and not update_comment then
|
bsw@964
|
30 slot.put_into("error", _"This issue is already closed.")
|
bsw/jbe@5
|
31 return false
|
bsw/jbe@5
|
32 end
|
bsw/jbe@5
|
33
|
bsw@521
|
34 local direct_voter = DirectVoter:by_pk(issue.id, app.session.member_id)
|
bsw@521
|
35
|
bsw@521
|
36 if param.get("discard", atom.boolean) then
|
bsw@521
|
37 if direct_voter then
|
bsw@521
|
38 direct_voter:destroy()
|
bsw@521
|
39 end
|
bsw@521
|
40 slot.put_into("notice", _"Your vote has been discarded. Delegation rules apply if set.")
|
bsw@521
|
41 return
|
bsw@521
|
42 end
|
bsw@26
|
43
|
bsw@519
|
44 local move_up
|
bsw@519
|
45 local move_down
|
bsw@26
|
46
|
bsw@519
|
47 local tempvoting_string = param.get("scoring")
|
bsw@519
|
48
|
bsw@519
|
49 local tempvotings = {}
|
bsw@879
|
50 if not update_comment then
|
bsw@879
|
51 for match in tempvoting_string:gmatch("([^;]+)") do
|
bsw@879
|
52 for initiative_id, grade in match:gmatch("([^:;]+):([^:;]+)") do
|
bsw@879
|
53 tempvotings[tonumber(initiative_id)] = tonumber(grade)
|
bsw@879
|
54 if param.get("move_up_" .. initiative_id .. ".x", atom.integer) then
|
bsw@879
|
55 move_up = tonumber(initiative_id)
|
bsw@879
|
56 elseif param.get("move_down_" .. initiative_id .. ".x", atom.integer) then
|
bsw@879
|
57 move_down = tonumber(initiative_id)
|
bsw@879
|
58 end
|
bsw@519
|
59 end
|
bsw@519
|
60 end
|
bsw@519
|
61 end
|
bsw/jbe@19
|
62
|
bsw/jbe@19
|
63 if not move_down and not move_up then
|
bsw@879
|
64 if not preview then
|
bsw@879
|
65 if not direct_voter then
|
bsw@879
|
66 if issue.closed then
|
bsw@879
|
67 slot.put_into("error", _"This issue is already closed.")
|
bsw@879
|
68 return false
|
bsw@879
|
69 else
|
bsw@879
|
70 direct_voter = DirectVoter:new()
|
bsw@879
|
71 direct_voter.issue_id = issue.id
|
bsw@879
|
72 direct_voter.member_id = app.session.member_id
|
bsw@879
|
73 direct_voter:save()
|
bsw@879
|
74
|
bsw@879
|
75 direct_voter = DirectVoter:by_pk(issue.id, app.session.member_id)
|
bsw@879
|
76 end
|
bsw@879
|
77 end
|
bsw@879
|
78
|
bsw@1045
|
79 local formatting_engine
|
bsw@1045
|
80 if config.enforce_formatting_engine then
|
bsw@1045
|
81 formatting_engine = config.enforce_formatting_engine
|
bsw@1045
|
82 else
|
bsw@1045
|
83 formatting_engine = param.get("formatting_engine")
|
bsw@1045
|
84 local formatting_engine_valid = false
|
bsw@1045
|
85 for i, fe in ipairs(config.formatting_engines) do
|
bsw@1045
|
86 if formatting_engine == fe.id then
|
bsw@1045
|
87 formatting_engine_valid = true
|
bsw@1045
|
88 end
|
bsw@1045
|
89 end
|
bsw@1045
|
90 if not formatting_engine_valid then
|
bsw/jbe@1309
|
91 slot.put_into("error", "invalid formatting engine!")
|
bsw/jbe@1309
|
92 return false
|
bsw@1045
|
93 end
|
bsw@1045
|
94 end
|
bsw@1045
|
95
|
bsw@925
|
96 local comment = param.get("comment")
|
bsw@879
|
97
|
bsw@879
|
98 if comment ~= direct_voter.comment then
|
bsw@879
|
99 if #comment > 0 then
|
bsw@879
|
100 direct_voter.formatting_engine = formatting_engine
|
bsw@879
|
101 direct_voter.comment = comment
|
bsw@880
|
102 if issue.closed then
|
bsw@880
|
103 direct_voter.comment_changed = 'now'
|
bsw@880
|
104 end
|
bsw@879
|
105 direct_voter:render_content(true)
|
bsw@879
|
106 else
|
bsw@879
|
107 direct_voter.formatting_engine = null
|
bsw@879
|
108 direct_voter.comment = null
|
bsw@880
|
109 if issue.closed then
|
bsw@880
|
110 direct_voter.comment_changed = 'now'
|
bsw@880
|
111 end
|
bsw@879
|
112 end
|
bsw@879
|
113 end
|
bsw@879
|
114 direct_voter:save()
|
bsw@879
|
115
|
bsw/jbe@19
|
116 end
|
bsw/jbe@19
|
117
|
bsw@879
|
118 if not update_comment then
|
bsw@879
|
119 local scoring = param.get("scoring")
|
bsw/jbe@5
|
120
|
bsw@879
|
121 for initiative_id, grade in scoring:gmatch("([^:;]+):([^:;]+)") do
|
bsw@879
|
122 local initiative_id = tonumber(initiative_id)
|
bsw@879
|
123 local grade = tonumber(grade)
|
bsw@879
|
124 local initiative = Initiative:by_id(initiative_id)
|
bsw@879
|
125 if initiative.issue.id ~= issue.id then
|
bsw/jbe@1309
|
126 return execute.view { module = "index", view = "403" }
|
bsw@879
|
127 end
|
bsw@879
|
128 if not preview and not issue.closed then
|
bsw@879
|
129 local vote = Vote:by_pk(initiative_id, app.session.member.id)
|
bsw@879
|
130 if not vote then
|
bsw@879
|
131 vote = Vote:new()
|
bsw@879
|
132 vote.issue_id = issue.id
|
bsw@879
|
133 vote.initiative_id = initiative.id
|
bsw@879
|
134 vote.member_id = app.session.member.id
|
bsw@879
|
135 end
|
bsw@879
|
136 vote.grade = grade
|
bsw@879
|
137 vote:save()
|
bsw@879
|
138 end
|
bsw/jbe@19
|
139 end
|
bsw@879
|
140 end
|
bsw@879
|
141
|
bsw@879
|
142 if not preview and not cancel then
|
bsw@879
|
143 request.redirect{
|
bsw@879
|
144 module = "issue",
|
bsw@879
|
145 view = "show",
|
bsw@879
|
146 id = issue.id
|
bsw@879
|
147 }
|
bsw/jbe@19
|
148 end
|
bsw/jbe@19
|
149
|
bsw/jbe@19
|
150 else
|
bsw/jbe@19
|
151
|
bsw/jbe@19
|
152 local current_initiative_id = move_up or move_down
|
bsw/jbe@5
|
153
|
bsw/jbe@19
|
154 local current_grade = tempvotings[current_initiative_id] or 0
|
bsw/jbe@19
|
155 local is_alone = true
|
bsw/jbe@19
|
156 if current_grade == 0 then
|
bsw/jbe@19
|
157 is_alone = false
|
bsw/jbe@19
|
158 else
|
bsw/jbe@19
|
159 for initiative_id, grade in pairs(tempvotings) do
|
bsw/jbe@19
|
160 if current_initiative_id ~= initiative_id and grade == current_grade then
|
bsw/jbe@19
|
161 is_alone = false
|
bsw/jbe@19
|
162 break
|
bsw/jbe@19
|
163 end
|
bsw/jbe@19
|
164 end
|
bsw/jbe@19
|
165 end
|
bsw/jbe@5
|
166
|
bsw/jbe@19
|
167 if move_up and current_grade >= 0 and is_alone then
|
bsw/jbe@19
|
168 for initiative_id, grade in pairs(tempvotings) do
|
bsw/jbe@19
|
169 if grade > current_grade then
|
bsw/jbe@19
|
170 tempvotings[initiative_id] = grade - 1
|
bsw/jbe@19
|
171 end
|
bsw/jbe@19
|
172 end
|
bsw/jbe@5
|
173
|
bsw/jbe@19
|
174 elseif move_up and current_grade >= 0 and not is_alone then
|
bsw/jbe@19
|
175 for initiative_id, grade in pairs(tempvotings) do
|
bsw/jbe@19
|
176 if grade > current_grade then
|
bsw/jbe@19
|
177 tempvotings[initiative_id] = grade + 1
|
bsw/jbe@19
|
178 end
|
bsw/jbe@19
|
179 end
|
bsw/jbe@19
|
180 tempvotings[current_initiative_id] = current_grade + 1
|
bsw/jbe@19
|
181
|
bsw/jbe@19
|
182 elseif move_up and current_grade < 0 and is_alone then
|
bsw/jbe@19
|
183 tempvotings[current_initiative_id] = current_grade + 1
|
bsw/jbe@19
|
184 for initiative_id, grade in pairs(tempvotings) do
|
bsw/jbe@19
|
185 if grade < current_grade then
|
bsw/jbe@19
|
186 tempvotings[initiative_id] = grade + 1
|
bsw/jbe@19
|
187 end
|
bsw/jbe@19
|
188 end
|
bsw/jbe@19
|
189
|
bsw/jbe@19
|
190 elseif move_up and current_grade < 0 and not is_alone then
|
bsw/jbe@19
|
191 for initiative_id, grade in pairs(tempvotings) do
|
bsw/jbe@19
|
192 if grade <= current_grade then
|
bsw/jbe@19
|
193 tempvotings[initiative_id] = grade - 1
|
bsw/jbe@19
|
194 end
|
bsw/jbe@19
|
195 end
|
bsw/jbe@19
|
196 tempvotings[current_initiative_id] = current_grade
|
bsw/jbe@19
|
197
|
bsw/jbe@19
|
198 elseif move_down and current_grade <= 0 and is_alone then
|
bsw/jbe@19
|
199 for initiative_id, grade in pairs(tempvotings) do
|
bsw/jbe@19
|
200 if grade < current_grade then
|
bsw/jbe@19
|
201 tempvotings[initiative_id] = grade + 1
|
bsw/jbe@19
|
202 end
|
bsw/jbe@19
|
203 end
|
bsw/jbe@19
|
204
|
bsw/jbe@19
|
205 elseif move_down and current_grade <= 0 and not is_alone then
|
bsw/jbe@19
|
206 for initiative_id, grade in pairs(tempvotings) do
|
bsw/jbe@19
|
207 if grade < current_grade then
|
bsw/jbe@19
|
208 tempvotings[initiative_id] = grade - 1
|
bsw/jbe@19
|
209 end
|
bsw/jbe@19
|
210 end
|
bsw/jbe@19
|
211 tempvotings[current_initiative_id] = current_grade - 1
|
bsw/jbe@19
|
212
|
bsw/jbe@19
|
213 elseif move_down and current_grade > 0 and is_alone then
|
bsw/jbe@19
|
214 tempvotings[current_initiative_id] = current_grade - 1
|
bsw/jbe@19
|
215 for initiative_id, grade in pairs(tempvotings) do
|
bsw/jbe@19
|
216 if grade > current_grade then
|
bsw/jbe@19
|
217 tempvotings[initiative_id] = grade - 1
|
bsw/jbe@19
|
218 end
|
bsw/jbe@19
|
219 end
|
bsw/jbe@19
|
220
|
bsw/jbe@19
|
221 elseif move_down and current_grade > 0 and not is_alone then
|
bsw/jbe@19
|
222 for initiative_id, grade in pairs(tempvotings) do
|
bsw/jbe@19
|
223 if grade >= current_grade then
|
bsw/jbe@19
|
224 tempvotings[initiative_id] = grade + 1
|
bsw/jbe@19
|
225 end
|
bsw/jbe@19
|
226 end
|
bsw/jbe@19
|
227 tempvotings[current_initiative_id] = current_grade
|
bsw/jbe@19
|
228
|
bsw/jbe@5
|
229 end
|
bsw/jbe@19
|
230
|
bsw/jbe@19
|
231 local tempvotings_list = {}
|
bsw/jbe@19
|
232 for key, val in pairs(tempvotings) do
|
bsw/jbe@19
|
233 tempvotings_list[#tempvotings_list+1] = tostring(key) .. ":" .. tostring(val)
|
bsw/jbe@5
|
234 end
|
bsw/jbe@19
|
235
|
bsw/jbe@19
|
236 tempvoting_string = table.concat(tempvotings_list, ";")
|
bsw/jbe@19
|
237
|
bsw/jbe@19
|
238 request.redirect{
|
bsw/jbe@19
|
239 module = "vote",
|
bsw/jbe@19
|
240 view = "list",
|
bsw/jbe@19
|
241 params = {
|
bsw/jbe@19
|
242 issue_id = issue.id,
|
bsw/jbe@19
|
243 scoring = tempvoting_string
|
bsw/jbe@19
|
244 }
|
bsw/jbe@19
|
245 }
|
bsw/jbe@19
|
246
|
bsw/jbe@5
|
247 end
|