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