liquid_feedback_frontend

view app/main/vote/_action/update.lua @ 521:75a95999a410

Fixed error when discard voting
author bsw
date Mon Apr 16 23:43:38 2012 +0200 (2012-04-16)
parents 7492497005bd
children ea3d3757ddc3
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
17 local direct_voter = DirectVoter:by_pk(issue.id, app.session.member_id)
19 if param.get("discard", atom.boolean) then
20 if direct_voter then
21 direct_voter:destroy()
22 end
23 slot.put_into("notice", _"Your vote has been discarded. Delegation rules apply if set.")
24 return
25 end
27 local move_up
28 local move_down
30 local tempvoting_string = param.get("scoring")
32 local tempvotings = {}
33 for match in tempvoting_string:gmatch("([^;]+)") do
34 for initiative_id, grade in match:gmatch("([^:;]+):([^:;]+)") do
35 tempvotings[tonumber(initiative_id)] = tonumber(grade)
36 if param.get("move_up_" .. initiative_id .. ".x", atom.integer) then
37 move_up = tonumber(initiative_id)
38 elseif param.get("move_down_" .. initiative_id .. ".x", atom.integer) then
39 move_down = tonumber(initiative_id)
40 end
41 end
42 end
44 if not move_down and not move_up then
45 if not direct_voter then
46 direct_voter = DirectVoter:new()
47 direct_voter.issue_id = issue.id
48 direct_voter.member_id = app.session.member_id
49 end
51 direct_voter:save()
53 local scoring = param.get("scoring")
55 for initiative_id, grade in scoring:gmatch("([^:;]+):([^:;]+)") do
56 local initiative_id = tonumber(initiative_id)
57 local grade = tonumber(grade)
58 local initiative = Initiative:by_id(initiative_id)
59 if initiative.issue.id ~= issue.id then
60 error("initiative from wrong issue")
61 end
62 local vote = Vote:by_pk(initiative_id, app.session.member.id)
63 if not vote then
64 vote = Vote:new()
65 vote.issue_id = issue.id
66 vote.initiative_id = initiative.id
67 vote.member_id = app.session.member.id
68 end
69 vote.grade = grade
70 vote:save()
71 end
73 else
75 local current_initiative_id = move_up or move_down
77 local current_grade = tempvotings[current_initiative_id] or 0
78 local is_alone = true
79 if current_grade == 0 then
80 is_alone = false
81 else
82 for initiative_id, grade in pairs(tempvotings) do
83 if current_initiative_id ~= initiative_id and grade == current_grade then
84 is_alone = false
85 break
86 end
87 end
88 end
90 if move_up and current_grade >= 0 and is_alone then
91 for initiative_id, grade in pairs(tempvotings) do
92 if grade > current_grade then
93 tempvotings[initiative_id] = grade - 1
94 end
95 end
97 elseif move_up and current_grade >= 0 and not is_alone then
98 for initiative_id, grade in pairs(tempvotings) do
99 if grade > current_grade then
100 tempvotings[initiative_id] = grade + 1
101 end
102 end
103 tempvotings[current_initiative_id] = current_grade + 1
105 elseif move_up and current_grade < 0 and is_alone then
106 tempvotings[current_initiative_id] = current_grade + 1
107 for initiative_id, grade in pairs(tempvotings) do
108 if grade < current_grade then
109 tempvotings[initiative_id] = grade + 1
110 end
111 end
113 elseif move_up and current_grade < 0 and not is_alone then
114 for initiative_id, grade in pairs(tempvotings) do
115 if grade <= current_grade then
116 tempvotings[initiative_id] = grade - 1
117 end
118 end
119 tempvotings[current_initiative_id] = current_grade
121 elseif move_down and current_grade <= 0 and is_alone then
122 for initiative_id, grade in pairs(tempvotings) do
123 if grade < current_grade then
124 tempvotings[initiative_id] = grade + 1
125 end
126 end
128 elseif move_down and current_grade <= 0 and not is_alone then
129 for initiative_id, grade in pairs(tempvotings) do
130 if grade < current_grade then
131 tempvotings[initiative_id] = grade - 1
132 end
133 end
134 tempvotings[current_initiative_id] = current_grade - 1
136 elseif move_down and current_grade > 0 and is_alone then
137 tempvotings[current_initiative_id] = current_grade - 1
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_down 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
152 end
154 local tempvotings_list = {}
155 for key, val in pairs(tempvotings) do
156 tempvotings_list[#tempvotings_list+1] = tostring(key) .. ":" .. tostring(val)
157 end
159 tempvoting_string = table.concat(tempvotings_list, ";")
161 request.redirect{
162 module = "vote",
163 view = "list",
164 params = {
165 issue_id = issue.id,
166 scoring = tempvoting_string
167 }
168 }
170 end

Impressum / About Us