liquid_feedback_frontend

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

Impressum / About Us