liquid_feedback_frontend
view app/main/vote/_action/update.lua @ 26:3036f2732b83
Allow discarding own voting
author | bsw |
---|---|
date | Sun Feb 21 15:11:48 2010 +0100 (2010-02-21) |
parents | 00d1004545f1 |
children | ec86db506312 |
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 issue.closed then
4 slot.put_into("error", _"This issue is already closed.")
5 return false
6 end
8 if issue.state ~= "voting" then
9 slot.put_into("error", _"Voting has not started yet.")
10 return false
11 end
15 local move_up = param.get("move_up", atom.integer)
16 local move_down = param.get("move_down", atom.integer)
18 if not move_down and not move_up then
19 local direct_voter = DirectVoter:by_pk(issue.id, app.session.member_id)
21 if param.get("discard", atom.boolean) then
22 if direct_voter then
23 direct_voter:destroy()
24 end
25 slot.put_into("notice", _"Your vote has been discarded. Delegation rules apply if set.")
26 return
27 end
29 if not direct_voter then
30 direct_voter = DirectVoter:new()
31 direct_voter.issue_id = issue.id
32 direct_voter.member_id = app.session.member_id
33 end
35 direct_voter.autoreject = false
36 direct_voter:save()
38 local scoring = param.get("scoring")
40 for initiative_id, grade in scoring:gmatch("([^:;]+):([^:;]+)") do
41 local initiative_id = tonumber(initiative_id)
42 local grade = tonumber(grade)
43 local initiative = Initiative:by_id(initiative_id)
44 if initiative.issue.id ~= issue.id then
45 error("initiative from wrong issue")
46 end
47 local vote = Vote:by_pk(initiative_id, app.session.member.id)
48 if not vote then
49 vote = Vote:new()
50 vote.issue_id = issue.id
51 vote.initiative_id = initiative.id
52 vote.member_id = app.session.member.id
53 end
54 vote.grade = grade
55 vote:save()
56 end
58 else
60 local tempvoting_string = param.get("scoring")
62 local tempvotings = {}
63 for match in tempvoting_string:gmatch("([^;]+)") do
64 for initiative_id, grade in match:gmatch("([^:;]+):([^:;]+)") do
65 tempvotings[tonumber(initiative_id)] = tonumber(grade)
66 end
67 end
69 local current_initiative_id = move_up or move_down
71 local current_grade = tempvotings[current_initiative_id] or 0
72 local is_alone = true
73 if current_grade == 0 then
74 is_alone = false
75 else
76 for initiative_id, grade in pairs(tempvotings) do
77 if current_initiative_id ~= initiative_id and grade == current_grade then
78 is_alone = false
79 break
80 end
81 end
82 end
84 if move_up and current_grade >= 0 and is_alone then
85 for initiative_id, grade in pairs(tempvotings) do
86 if grade > current_grade then
87 tempvotings[initiative_id] = grade - 1
88 end
89 end
91 elseif move_up and current_grade >= 0 and not 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
97 tempvotings[current_initiative_id] = current_grade + 1
99 elseif move_up and current_grade < 0 and is_alone then
100 tempvotings[current_initiative_id] = current_grade + 1
101 for initiative_id, grade in pairs(tempvotings) do
102 if grade < current_grade then
103 tempvotings[initiative_id] = grade + 1
104 end
105 end
107 elseif move_up and current_grade < 0 and not is_alone then
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
113 tempvotings[current_initiative_id] = current_grade
115 elseif move_down and current_grade <= 0 and is_alone then
116 for initiative_id, grade in pairs(tempvotings) do
117 if grade < current_grade then
118 tempvotings[initiative_id] = grade + 1
119 end
120 end
122 elseif move_down and current_grade <= 0 and not 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
128 tempvotings[current_initiative_id] = current_grade - 1
130 elseif move_down and current_grade > 0 and is_alone then
131 tempvotings[current_initiative_id] = current_grade - 1
132 for initiative_id, grade in pairs(tempvotings) do
133 if grade > current_grade then
134 tempvotings[initiative_id] = grade - 1
135 end
136 end
138 elseif move_down and current_grade > 0 and not is_alone then
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
144 tempvotings[current_initiative_id] = current_grade
146 end
148 local tempvotings_list = {}
149 for key, val in pairs(tempvotings) do
150 tempvotings_list[#tempvotings_list+1] = tostring(key) .. ":" .. tostring(val)
151 end
153 tempvoting_string = table.concat(tempvotings_list, ";")
155 request.redirect{
156 module = "vote",
157 view = "list",
158 params = {
159 issue_id = issue.id,
160 scoring = tempvoting_string
161 }
162 }
164 end