liquid_feedback_frontend

annotate 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
rev   line source
bsw/jbe@5 1 local issue = Issue:new_selector():add_where{ "id = ?", param.get("issue_id", atom.integer) }:for_share():single_object_mode():exec()
bsw/jbe@5 2
bsw@281 3 if not app.session.member:has_voting_right_for_unit_id(issue.area.unit_id) then
bsw@281 4 error("access denied")
bsw@281 5 end
bsw@281 6
bsw/jbe@5 7 if issue.closed then
bsw/jbe@5 8 slot.put_into("error", _"This issue is already closed.")
bsw/jbe@5 9 return false
bsw/jbe@5 10 end
bsw/jbe@5 11
bsw/jbe@5 12 if issue.state ~= "voting" then
bsw/jbe@5 13 slot.put_into("error", _"Voting has not started yet.")
bsw/jbe@5 14 return false
bsw/jbe@5 15 end
bsw/jbe@5 16
bsw@26 17
bsw@519 18 local move_up
bsw@519 19 local move_down
bsw@26 20
bsw@519 21 local tempvoting_string = param.get("scoring")
bsw@519 22
bsw@519 23 local tempvotings = {}
bsw@519 24 for match in tempvoting_string:gmatch("([^;]+)") do
bsw@519 25 for initiative_id, grade in match:gmatch("([^:;]+):([^:;]+)") do
bsw@519 26 tempvotings[tonumber(initiative_id)] = tonumber(grade)
bsw@519 27 if param.get("move_up_" .. initiative_id .. ".x", atom.integer) then
bsw@519 28 move_up = tonumber(initiative_id)
bsw@519 29 elseif param.get("move_down_" .. initiative_id .. ".x", atom.integer) then
bsw@519 30 move_down = tonumber(initiative_id)
bsw@519 31 end
bsw@519 32 end
bsw@519 33 end
bsw/jbe@19 34
bsw/jbe@19 35 if not move_down and not move_up then
bsw/jbe@19 36 local direct_voter = DirectVoter:by_pk(issue.id, app.session.member_id)
bsw/jbe@19 37
bsw@26 38 if param.get("discard", atom.boolean) then
bsw@26 39 if direct_voter then
bsw@26 40 direct_voter:destroy()
bsw@26 41 end
bsw@26 42 slot.put_into("notice", _"Your vote has been discarded. Delegation rules apply if set.")
bsw@26 43 return
bsw@26 44 end
bsw@26 45
bsw/jbe@19 46 if not direct_voter then
bsw/jbe@19 47 direct_voter = DirectVoter:new()
bsw/jbe@19 48 direct_voter.issue_id = issue.id
bsw/jbe@19 49 direct_voter.member_id = app.session.member_id
bsw/jbe@19 50 end
bsw/jbe@19 51
bsw/jbe@19 52 direct_voter:save()
bsw/jbe@19 53
bsw/jbe@19 54 local scoring = param.get("scoring")
bsw/jbe@5 55
bsw/jbe@19 56 for initiative_id, grade in scoring:gmatch("([^:;]+):([^:;]+)") do
bsw/jbe@19 57 local initiative_id = tonumber(initiative_id)
bsw/jbe@19 58 local grade = tonumber(grade)
bsw/jbe@19 59 local initiative = Initiative:by_id(initiative_id)
bsw/jbe@19 60 if initiative.issue.id ~= issue.id then
bsw/jbe@19 61 error("initiative from wrong issue")
bsw/jbe@19 62 end
bsw/jbe@19 63 local vote = Vote:by_pk(initiative_id, app.session.member.id)
bsw/jbe@19 64 if not vote then
bsw/jbe@19 65 vote = Vote:new()
bsw/jbe@19 66 vote.issue_id = issue.id
bsw/jbe@19 67 vote.initiative_id = initiative.id
bsw/jbe@19 68 vote.member_id = app.session.member.id
bsw/jbe@19 69 end
bsw/jbe@19 70 vote.grade = grade
bsw/jbe@19 71 vote:save()
bsw/jbe@19 72 end
bsw/jbe@19 73
bsw/jbe@19 74 else
bsw/jbe@19 75
bsw/jbe@19 76 local current_initiative_id = move_up or move_down
bsw/jbe@5 77
bsw/jbe@19 78 local current_grade = tempvotings[current_initiative_id] or 0
bsw/jbe@19 79 local is_alone = true
bsw/jbe@19 80 if current_grade == 0 then
bsw/jbe@19 81 is_alone = false
bsw/jbe@19 82 else
bsw/jbe@19 83 for initiative_id, grade in pairs(tempvotings) do
bsw/jbe@19 84 if current_initiative_id ~= initiative_id and grade == current_grade then
bsw/jbe@19 85 is_alone = false
bsw/jbe@19 86 break
bsw/jbe@19 87 end
bsw/jbe@19 88 end
bsw/jbe@19 89 end
bsw/jbe@5 90
bsw/jbe@19 91 if move_up and current_grade >= 0 and is_alone then
bsw/jbe@19 92 for initiative_id, grade in pairs(tempvotings) do
bsw/jbe@19 93 if grade > current_grade then
bsw/jbe@19 94 tempvotings[initiative_id] = grade - 1
bsw/jbe@19 95 end
bsw/jbe@19 96 end
bsw/jbe@5 97
bsw/jbe@19 98 elseif move_up and current_grade >= 0 and not is_alone then
bsw/jbe@19 99 for initiative_id, grade in pairs(tempvotings) do
bsw/jbe@19 100 if grade > current_grade then
bsw/jbe@19 101 tempvotings[initiative_id] = grade + 1
bsw/jbe@19 102 end
bsw/jbe@19 103 end
bsw/jbe@19 104 tempvotings[current_initiative_id] = current_grade + 1
bsw/jbe@19 105
bsw/jbe@19 106 elseif move_up and current_grade < 0 and is_alone then
bsw/jbe@19 107 tempvotings[current_initiative_id] = current_grade + 1
bsw/jbe@19 108 for initiative_id, grade in pairs(tempvotings) do
bsw/jbe@19 109 if grade < current_grade then
bsw/jbe@19 110 tempvotings[initiative_id] = grade + 1
bsw/jbe@19 111 end
bsw/jbe@19 112 end
bsw/jbe@19 113
bsw/jbe@19 114 elseif move_up and current_grade < 0 and not is_alone then
bsw/jbe@19 115 for initiative_id, grade in pairs(tempvotings) do
bsw/jbe@19 116 if grade <= current_grade then
bsw/jbe@19 117 tempvotings[initiative_id] = grade - 1
bsw/jbe@19 118 end
bsw/jbe@19 119 end
bsw/jbe@19 120 tempvotings[current_initiative_id] = current_grade
bsw/jbe@19 121
bsw/jbe@19 122 elseif move_down and current_grade <= 0 and is_alone then
bsw/jbe@19 123 for initiative_id, grade in pairs(tempvotings) do
bsw/jbe@19 124 if grade < current_grade then
bsw/jbe@19 125 tempvotings[initiative_id] = grade + 1
bsw/jbe@19 126 end
bsw/jbe@19 127 end
bsw/jbe@19 128
bsw/jbe@19 129 elseif move_down and current_grade <= 0 and not is_alone then
bsw/jbe@19 130 for initiative_id, grade in pairs(tempvotings) do
bsw/jbe@19 131 if grade < current_grade then
bsw/jbe@19 132 tempvotings[initiative_id] = grade - 1
bsw/jbe@19 133 end
bsw/jbe@19 134 end
bsw/jbe@19 135 tempvotings[current_initiative_id] = current_grade - 1
bsw/jbe@19 136
bsw/jbe@19 137 elseif move_down and current_grade > 0 and is_alone then
bsw/jbe@19 138 tempvotings[current_initiative_id] = current_grade - 1
bsw/jbe@19 139 for initiative_id, grade in pairs(tempvotings) do
bsw/jbe@19 140 if grade > current_grade then
bsw/jbe@19 141 tempvotings[initiative_id] = grade - 1
bsw/jbe@19 142 end
bsw/jbe@19 143 end
bsw/jbe@19 144
bsw/jbe@19 145 elseif move_down and current_grade > 0 and not is_alone then
bsw/jbe@19 146 for initiative_id, grade in pairs(tempvotings) do
bsw/jbe@19 147 if grade >= current_grade then
bsw/jbe@19 148 tempvotings[initiative_id] = grade + 1
bsw/jbe@19 149 end
bsw/jbe@19 150 end
bsw/jbe@19 151 tempvotings[current_initiative_id] = current_grade
bsw/jbe@19 152
bsw/jbe@5 153 end
bsw/jbe@19 154
bsw/jbe@19 155 local tempvotings_list = {}
bsw/jbe@19 156 for key, val in pairs(tempvotings) do
bsw/jbe@19 157 tempvotings_list[#tempvotings_list+1] = tostring(key) .. ":" .. tostring(val)
bsw/jbe@5 158 end
bsw/jbe@19 159
bsw/jbe@19 160 tempvoting_string = table.concat(tempvotings_list, ";")
bsw/jbe@19 161
bsw/jbe@19 162 request.redirect{
bsw/jbe@19 163 module = "vote",
bsw/jbe@19 164 view = "list",
bsw/jbe@19 165 params = {
bsw/jbe@19 166 issue_id = issue.id,
bsw/jbe@19 167 scoring = tempvoting_string
bsw/jbe@19 168 }
bsw/jbe@19 169 }
bsw/jbe@19 170
bsw/jbe@5 171 end

Impressum / About Us