liquid_feedback_frontend

view app/main/vote/_action/update.lua @ 519:7492497005bd

Fixed voting for non javascript capable browser, removed browser warning
author bsw
date Mon Apr 16 23:27:07 2012 +0200 (2012-04-16)
parents b77e6a17ca77
children 75a95999a410
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
18 local move_up
19 local move_down
21 local tempvoting_string = param.get("scoring")
23 local tempvotings = {}
24 for match in tempvoting_string:gmatch("([^;]+)") do
25 for initiative_id, grade in match:gmatch("([^:;]+):([^:;]+)") do
26 tempvotings[tonumber(initiative_id)] = tonumber(grade)
27 if param.get("move_up_" .. initiative_id .. ".x", atom.integer) then
28 move_up = tonumber(initiative_id)
29 elseif param.get("move_down_" .. initiative_id .. ".x", atom.integer) then
30 move_down = tonumber(initiative_id)
31 end
32 end
33 end
35 if not move_down and not move_up then
36 local direct_voter = DirectVoter:by_pk(issue.id, app.session.member_id)
38 if param.get("discard", atom.boolean) then
39 if direct_voter then
40 direct_voter:destroy()
41 end
42 slot.put_into("notice", _"Your vote has been discarded. Delegation rules apply if set.")
43 return
44 end
46 if not direct_voter then
47 direct_voter = DirectVoter:new()
48 direct_voter.issue_id = issue.id
49 direct_voter.member_id = app.session.member_id
50 end
52 direct_voter:save()
54 local scoring = param.get("scoring")
56 for initiative_id, grade in scoring:gmatch("([^:;]+):([^:;]+)") do
57 local initiative_id = tonumber(initiative_id)
58 local grade = tonumber(grade)
59 local initiative = Initiative:by_id(initiative_id)
60 if initiative.issue.id ~= issue.id then
61 error("initiative from wrong issue")
62 end
63 local vote = Vote:by_pk(initiative_id, app.session.member.id)
64 if not vote then
65 vote = Vote:new()
66 vote.issue_id = issue.id
67 vote.initiative_id = initiative.id
68 vote.member_id = app.session.member.id
69 end
70 vote.grade = grade
71 vote:save()
72 end
74 else
76 local current_initiative_id = move_up or move_down
78 local current_grade = tempvotings[current_initiative_id] or 0
79 local is_alone = true
80 if current_grade == 0 then
81 is_alone = false
82 else
83 for initiative_id, grade in pairs(tempvotings) do
84 if current_initiative_id ~= initiative_id and grade == current_grade then
85 is_alone = false
86 break
87 end
88 end
89 end
91 if move_up and current_grade >= 0 and is_alone then
92 for initiative_id, grade in pairs(tempvotings) do
93 if grade > current_grade then
94 tempvotings[initiative_id] = grade - 1
95 end
96 end
98 elseif move_up and current_grade >= 0 and not is_alone then
99 for initiative_id, grade in pairs(tempvotings) do
100 if grade > current_grade then
101 tempvotings[initiative_id] = grade + 1
102 end
103 end
104 tempvotings[current_initiative_id] = current_grade + 1
106 elseif move_up and current_grade < 0 and is_alone then
107 tempvotings[current_initiative_id] = current_grade + 1
108 for initiative_id, grade in pairs(tempvotings) do
109 if grade < current_grade then
110 tempvotings[initiative_id] = grade + 1
111 end
112 end
114 elseif move_up and current_grade < 0 and not is_alone then
115 for initiative_id, grade in pairs(tempvotings) do
116 if grade <= current_grade then
117 tempvotings[initiative_id] = grade - 1
118 end
119 end
120 tempvotings[current_initiative_id] = current_grade
122 elseif move_down and current_grade <= 0 and is_alone then
123 for initiative_id, grade in pairs(tempvotings) do
124 if grade < current_grade then
125 tempvotings[initiative_id] = grade + 1
126 end
127 end
129 elseif move_down and current_grade <= 0 and not is_alone then
130 for initiative_id, grade in pairs(tempvotings) do
131 if grade < current_grade then
132 tempvotings[initiative_id] = grade - 1
133 end
134 end
135 tempvotings[current_initiative_id] = current_grade - 1
137 elseif move_down and current_grade > 0 and is_alone then
138 tempvotings[current_initiative_id] = current_grade - 1
139 for initiative_id, grade in pairs(tempvotings) do
140 if grade > current_grade then
141 tempvotings[initiative_id] = grade - 1
142 end
143 end
145 elseif move_down and current_grade > 0 and not is_alone then
146 for initiative_id, grade in pairs(tempvotings) do
147 if grade >= current_grade then
148 tempvotings[initiative_id] = grade + 1
149 end
150 end
151 tempvotings[current_initiative_id] = current_grade
153 end
155 local tempvotings_list = {}
156 for key, val in pairs(tempvotings) do
157 tempvotings_list[#tempvotings_list+1] = tostring(key) .. ":" .. tostring(val)
158 end
160 tempvoting_string = table.concat(tempvotings_list, ";")
162 request.redirect{
163 module = "vote",
164 view = "list",
165 params = {
166 issue_id = issue.id,
167 scoring = tempvoting_string
168 }
169 }
171 end

Impressum / About Us