| 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@925
 | 
    70     local comment = 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@880
 | 
    76         if issue.closed then
 | 
| 
bsw@880
 | 
    77           direct_voter.comment_changed = 'now'
 | 
| 
bsw@880
 | 
    78         end
 | 
| 
bsw@879
 | 
    79         direct_voter:render_content(true)
 | 
| 
bsw@879
 | 
    80       else
 | 
| 
bsw@879
 | 
    81         direct_voter.formatting_engine = null
 | 
| 
bsw@879
 | 
    82         direct_voter.comment = null
 | 
| 
bsw@880
 | 
    83         if issue.closed then
 | 
| 
bsw@880
 | 
    84           direct_voter.comment_changed = 'now'
 | 
| 
bsw@880
 | 
    85         end
 | 
| 
bsw@879
 | 
    86       end
 | 
| 
bsw@879
 | 
    87     end
 | 
| 
bsw@879
 | 
    88     direct_voter:save()
 | 
| 
bsw@879
 | 
    89     
 | 
| 
bsw/jbe@19
 | 
    90   end
 | 
| 
bsw/jbe@19
 | 
    91 
 | 
| 
bsw@879
 | 
    92   if not update_comment then
 | 
| 
bsw@879
 | 
    93     local scoring = param.get("scoring")
 | 
| 
bsw/jbe@5
 | 
    94 
 | 
| 
bsw@879
 | 
    95     for initiative_id, grade in scoring:gmatch("([^:;]+):([^:;]+)") do
 | 
| 
bsw@879
 | 
    96       local initiative_id = tonumber(initiative_id)
 | 
| 
bsw@879
 | 
    97       local grade = tonumber(grade)
 | 
| 
bsw@879
 | 
    98       local initiative = Initiative:by_id(initiative_id)
 | 
| 
bsw@879
 | 
    99       if initiative.issue.id ~= issue.id then
 | 
| 
bsw@879
 | 
   100         error("initiative from wrong issue")
 | 
| 
bsw@879
 | 
   101       end
 | 
| 
bsw@879
 | 
   102       if not preview and not issue.closed then
 | 
| 
bsw@879
 | 
   103         local vote = Vote:by_pk(initiative_id, app.session.member.id)
 | 
| 
bsw@879
 | 
   104         if not vote then
 | 
| 
bsw@879
 | 
   105           vote = Vote:new()
 | 
| 
bsw@879
 | 
   106           vote.issue_id = issue.id
 | 
| 
bsw@879
 | 
   107           vote.initiative_id = initiative.id
 | 
| 
bsw@879
 | 
   108           vote.member_id = app.session.member.id
 | 
| 
bsw@879
 | 
   109         end
 | 
| 
bsw@879
 | 
   110         vote.grade = grade
 | 
| 
bsw@879
 | 
   111         vote:save()
 | 
| 
bsw@879
 | 
   112       end
 | 
| 
bsw/jbe@19
 | 
   113     end
 | 
| 
bsw@879
 | 
   114   end
 | 
| 
bsw@879
 | 
   115 
 | 
| 
bsw@879
 | 
   116   if not preview and not cancel then
 | 
| 
bsw@879
 | 
   117     request.redirect{
 | 
| 
bsw@879
 | 
   118       module = "issue",
 | 
| 
bsw@879
 | 
   119       view = "show",
 | 
| 
bsw@879
 | 
   120       id = issue.id
 | 
| 
bsw@879
 | 
   121     }
 | 
| 
bsw/jbe@19
 | 
   122   end
 | 
| 
bsw/jbe@19
 | 
   123 
 | 
| 
bsw/jbe@19
 | 
   124 else
 | 
| 
bsw/jbe@19
 | 
   125 
 | 
| 
bsw/jbe@19
 | 
   126   local current_initiative_id = move_up or move_down
 | 
| 
bsw/jbe@5
 | 
   127 
 | 
| 
bsw/jbe@19
 | 
   128   local current_grade = tempvotings[current_initiative_id] or 0
 | 
| 
bsw/jbe@19
 | 
   129   local is_alone = true
 | 
| 
bsw/jbe@19
 | 
   130   if current_grade == 0 then
 | 
| 
bsw/jbe@19
 | 
   131     is_alone = false
 | 
| 
bsw/jbe@19
 | 
   132   else
 | 
| 
bsw/jbe@19
 | 
   133     for initiative_id, grade in pairs(tempvotings) do
 | 
| 
bsw/jbe@19
 | 
   134       if current_initiative_id ~= initiative_id and grade == current_grade then
 | 
| 
bsw/jbe@19
 | 
   135         is_alone = false
 | 
| 
bsw/jbe@19
 | 
   136         break
 | 
| 
bsw/jbe@19
 | 
   137       end
 | 
| 
bsw/jbe@19
 | 
   138     end
 | 
| 
bsw/jbe@19
 | 
   139   end
 | 
| 
bsw/jbe@5
 | 
   140 
 | 
| 
bsw/jbe@19
 | 
   141   if     move_up   and current_grade >= 0 and     is_alone then
 | 
| 
bsw/jbe@19
 | 
   142     for initiative_id, grade in pairs(tempvotings) do
 | 
| 
bsw/jbe@19
 | 
   143       if grade > current_grade then
 | 
| 
bsw/jbe@19
 | 
   144         tempvotings[initiative_id] = grade - 1
 | 
| 
bsw/jbe@19
 | 
   145       end
 | 
| 
bsw/jbe@19
 | 
   146     end
 | 
| 
bsw/jbe@5
 | 
   147 
 | 
| 
bsw/jbe@19
 | 
   148   elseif move_up   and current_grade >= 0 and not is_alone then
 | 
| 
bsw/jbe@19
 | 
   149     for initiative_id, grade in pairs(tempvotings) do
 | 
| 
bsw/jbe@19
 | 
   150       if grade > current_grade then
 | 
| 
bsw/jbe@19
 | 
   151         tempvotings[initiative_id] = grade + 1
 | 
| 
bsw/jbe@19
 | 
   152       end
 | 
| 
bsw/jbe@19
 | 
   153     end
 | 
| 
bsw/jbe@19
 | 
   154     tempvotings[current_initiative_id] = current_grade + 1
 | 
| 
bsw/jbe@19
 | 
   155 
 | 
| 
bsw/jbe@19
 | 
   156   elseif move_up   and current_grade  < 0 and     is_alone then
 | 
| 
bsw/jbe@19
 | 
   157     tempvotings[current_initiative_id] = current_grade + 1
 | 
| 
bsw/jbe@19
 | 
   158     for initiative_id, grade in pairs(tempvotings) do
 | 
| 
bsw/jbe@19
 | 
   159       if grade < current_grade then
 | 
| 
bsw/jbe@19
 | 
   160         tempvotings[initiative_id] = grade + 1
 | 
| 
bsw/jbe@19
 | 
   161       end
 | 
| 
bsw/jbe@19
 | 
   162     end
 | 
| 
bsw/jbe@19
 | 
   163 
 | 
| 
bsw/jbe@19
 | 
   164   elseif move_up   and current_grade  < 0 and not is_alone then
 | 
| 
bsw/jbe@19
 | 
   165     for initiative_id, grade in pairs(tempvotings) do
 | 
| 
bsw/jbe@19
 | 
   166       if grade <= current_grade then
 | 
| 
bsw/jbe@19
 | 
   167         tempvotings[initiative_id] = grade - 1
 | 
| 
bsw/jbe@19
 | 
   168       end
 | 
| 
bsw/jbe@19
 | 
   169     end
 | 
| 
bsw/jbe@19
 | 
   170     tempvotings[current_initiative_id] = current_grade
 | 
| 
bsw/jbe@19
 | 
   171 
 | 
| 
bsw/jbe@19
 | 
   172   elseif move_down and current_grade <= 0 and     is_alone then
 | 
| 
bsw/jbe@19
 | 
   173     for initiative_id, grade in pairs(tempvotings) do
 | 
| 
bsw/jbe@19
 | 
   174       if grade < current_grade then
 | 
| 
bsw/jbe@19
 | 
   175         tempvotings[initiative_id] = grade + 1
 | 
| 
bsw/jbe@19
 | 
   176       end
 | 
| 
bsw/jbe@19
 | 
   177     end
 | 
| 
bsw/jbe@19
 | 
   178 
 | 
| 
bsw/jbe@19
 | 
   179   elseif move_down and current_grade <= 0 and not is_alone then
 | 
| 
bsw/jbe@19
 | 
   180     for initiative_id, grade in pairs(tempvotings) do
 | 
| 
bsw/jbe@19
 | 
   181       if grade < current_grade then
 | 
| 
bsw/jbe@19
 | 
   182         tempvotings[initiative_id] = grade - 1
 | 
| 
bsw/jbe@19
 | 
   183       end
 | 
| 
bsw/jbe@19
 | 
   184     end
 | 
| 
bsw/jbe@19
 | 
   185     tempvotings[current_initiative_id] = current_grade - 1
 | 
| 
bsw/jbe@19
 | 
   186 
 | 
| 
bsw/jbe@19
 | 
   187   elseif move_down and current_grade  > 0 and     is_alone then
 | 
| 
bsw/jbe@19
 | 
   188     tempvotings[current_initiative_id] = current_grade - 1
 | 
| 
bsw/jbe@19
 | 
   189     for initiative_id, grade in pairs(tempvotings) do
 | 
| 
bsw/jbe@19
 | 
   190       if grade > current_grade then
 | 
| 
bsw/jbe@19
 | 
   191         tempvotings[initiative_id] = grade - 1
 | 
| 
bsw/jbe@19
 | 
   192       end
 | 
| 
bsw/jbe@19
 | 
   193     end
 | 
| 
bsw/jbe@19
 | 
   194 
 | 
| 
bsw/jbe@19
 | 
   195   elseif move_down and current_grade  > 0 and not is_alone then
 | 
| 
bsw/jbe@19
 | 
   196     for initiative_id, grade in pairs(tempvotings) do
 | 
| 
bsw/jbe@19
 | 
   197       if grade >= current_grade then
 | 
| 
bsw/jbe@19
 | 
   198         tempvotings[initiative_id] = grade + 1
 | 
| 
bsw/jbe@19
 | 
   199       end
 | 
| 
bsw/jbe@19
 | 
   200     end
 | 
| 
bsw/jbe@19
 | 
   201     tempvotings[current_initiative_id] = current_grade
 | 
| 
bsw/jbe@19
 | 
   202 
 | 
| 
bsw/jbe@5
 | 
   203   end
 | 
| 
bsw/jbe@19
 | 
   204 
 | 
| 
bsw/jbe@19
 | 
   205   local tempvotings_list = {}
 | 
| 
bsw/jbe@19
 | 
   206   for key, val in pairs(tempvotings) do
 | 
| 
bsw/jbe@19
 | 
   207     tempvotings_list[#tempvotings_list+1] = tostring(key) .. ":" .. tostring(val)
 | 
| 
bsw/jbe@5
 | 
   208   end
 | 
| 
bsw/jbe@19
 | 
   209 
 | 
| 
bsw/jbe@19
 | 
   210   tempvoting_string = table.concat(tempvotings_list, ";")
 | 
| 
bsw/jbe@19
 | 
   211 
 | 
| 
bsw/jbe@19
 | 
   212   request.redirect{
 | 
| 
bsw/jbe@19
 | 
   213     module = "vote",
 | 
| 
bsw/jbe@19
 | 
   214     view = "list",
 | 
| 
bsw/jbe@19
 | 
   215     params = {
 | 
| 
bsw/jbe@19
 | 
   216       issue_id = issue.id,
 | 
| 
bsw/jbe@19
 | 
   217       scoring = tempvoting_string
 | 
| 
bsw/jbe@19
 | 
   218     }
 | 
| 
bsw/jbe@19
 | 
   219   }
 | 
| 
bsw/jbe@19
 | 
   220 
 | 
| 
bsw/jbe@5
 | 
   221 end
 |