liquid_feedback_frontend
view app/main/vote/_action/update.lua @ 1807:dd966ebdfa04
Fixed layout of checkbox label in survey
| author | bsw | 
|---|---|
| date | Thu Nov 18 14:54:49 2021 +0100 (2021-11-18) | 
| parents | 32cc544d5a5b | 
| children | 
 line source
     1 local cancel = param.get("cancel") and true or false
     2 if cancel then return end
     4 local issue = Issue:new_selector():add_where{ "id = ?", param.get("issue_id", atom.integer) }:for_share():single_object_mode():exec()
     6 -- TODO patch for project voting
     7 if config.alternative_voting and config.alternative_voting[tostring(issue.policy.id)] then
     8   return false
     9 end
    12 local preview = (param.get("preview") or param.get("edit")) and true or false
    14 if not app.session.member:has_voting_right_for_unit_id(issue.area.unit_id) then
    15   return execute.view { module = "index", view = "403" }
    16 end
    18 local update_comment = param.get("update_comment") and true or false
    20 if issue.state ~= "voting" and not issue.closed then
    21   slot.put_into("error", _"Voting has not started yet.")
    22   return false
    23 end
    25 if (issue.phase_finished or issue.closed) and preview then
    26   return
    27 end
    29 if issue.phase_finished or issue.closed and not update_comment then
    30   slot.put_into("error", _"This issue is already closed.")
    31   return false
    32 end
    34 local direct_voter = DirectVoter:by_pk(issue.id, app.session.member_id)
    36 if param.get("discard", atom.boolean) then
    37   if direct_voter then
    38     direct_voter:destroy()
    39   end
    40   slot.put_into("notice", _"Your vote has been discarded. Delegation rules apply if set.")
    41   return
    42 end
    44 local move_up 
    45 local move_down
    47 local tempvoting_string = param.get("scoring")
    49 local tempvotings = {}
    50 if not update_comment then
    51   for match in tempvoting_string:gmatch("([^;]+)") do
    52     for initiative_id, grade in match:gmatch("([^:;]+):([^:;]+)") do
    53       tempvotings[tonumber(initiative_id)] = tonumber(grade)
    54       if param.get("move_up_" .. initiative_id .. ".x", atom.integer) then
    55         move_up = tonumber(initiative_id)
    56       elseif param.get("move_down_" .. initiative_id .. ".x", atom.integer) then
    57         move_down = tonumber(initiative_id)
    58       end
    59     end
    60   end
    61 end
    63 if not move_down and not move_up then
    64   if not preview then
    65     if not direct_voter then
    66       if issue.closed then
    67         slot.put_into("error", _"This issue is already closed.")
    68         return false
    69       else 
    70         direct_voter = DirectVoter:new()
    71         direct_voter.issue_id = issue.id
    72         direct_voter.member_id = app.session.member_id
    73         direct_voter:save()
    75         direct_voter = DirectVoter:by_pk(issue.id, app.session.member_id)
    76       end
    77     end
    79     local formatting_engine
    80     if config.enforce_formatting_engine then
    81       formatting_engine = config.enforce_formatting_engine
    82     else
    83       formatting_engine = param.get("formatting_engine")
    84       local formatting_engine_valid = false
    85       for i, fe in ipairs(config.formatting_engines) do
    86         if formatting_engine == fe.id then
    87           formatting_engine_valid = true
    88         end
    89       end
    90       if not formatting_engine_valid then
    91         slot.put_into("error", "invalid formatting engine!")
    92         return false
    93       end
    94     end
    96     local comment = param.get("comment")
    98     if comment ~= direct_voter.comment then
    99       if #comment > 0 then
   100         direct_voter.formatting_engine = formatting_engine
   101         direct_voter.comment = comment
   102         if issue.closed then
   103           direct_voter.comment_changed = 'now'
   104         end
   105         direct_voter:render_content(true)
   106       else
   107         direct_voter.formatting_engine = null
   108         direct_voter.comment = null
   109         if issue.closed then
   110           direct_voter.comment_changed = 'now'
   111         end
   112       end
   113     end
   114     direct_voter:save()
   116   end
   118   if not update_comment then
   119     local scoring = param.get("scoring")
   121     for initiative_id, grade in scoring:gmatch("([^:;]+):([^:;]+)") do
   122       local initiative_id = tonumber(initiative_id)
   123       local grade = tonumber(grade)
   124       local initiative = Initiative:by_id(initiative_id)
   125       if initiative.issue.id ~= issue.id then
   126         return execute.view { module = "index", view = "403" }
   127       end
   128       if not preview and not issue.closed then
   129         local vote = Vote:by_pk(initiative_id, app.session.member.id)
   130         if not vote then
   131           vote = Vote:new()
   132           vote.issue_id = issue.id
   133           vote.initiative_id = initiative.id
   134           vote.member_id = app.session.member.id
   135         end
   136         vote.grade = grade
   137         vote:save()
   138       end
   139     end
   140   end
   142   if not preview and not cancel then
   143     request.redirect{
   144       module = "issue",
   145       view = "show",
   146       id = issue.id
   147     }
   148   end
   150 else
   152   local current_initiative_id = move_up or move_down
   154   local current_grade = tempvotings[current_initiative_id] or 0
   155   local is_alone = true
   156   if current_grade == 0 then
   157     is_alone = false
   158   else
   159     for initiative_id, grade in pairs(tempvotings) do
   160       if current_initiative_id ~= initiative_id and grade == current_grade then
   161         is_alone = false
   162         break
   163       end
   164     end
   165   end
   167   if     move_up   and current_grade >= 0 and     is_alone then
   168     for initiative_id, grade in pairs(tempvotings) do
   169       if grade > current_grade then
   170         tempvotings[initiative_id] = grade - 1
   171       end
   172     end
   174   elseif move_up   and current_grade >= 0 and not is_alone then
   175     for initiative_id, grade in pairs(tempvotings) do
   176       if grade > current_grade then
   177         tempvotings[initiative_id] = grade + 1
   178       end
   179     end
   180     tempvotings[current_initiative_id] = current_grade + 1
   182   elseif move_up   and current_grade  < 0 and     is_alone then
   183     tempvotings[current_initiative_id] = current_grade + 1
   184     for initiative_id, grade in pairs(tempvotings) do
   185       if grade < current_grade then
   186         tempvotings[initiative_id] = grade + 1
   187       end
   188     end
   190   elseif move_up   and current_grade  < 0 and not is_alone then
   191     for initiative_id, grade in pairs(tempvotings) do
   192       if grade <= current_grade then
   193         tempvotings[initiative_id] = grade - 1
   194       end
   195     end
   196     tempvotings[current_initiative_id] = current_grade
   198   elseif move_down and current_grade <= 0 and     is_alone then
   199     for initiative_id, grade in pairs(tempvotings) do
   200       if grade < current_grade then
   201         tempvotings[initiative_id] = grade + 1
   202       end
   203     end
   205   elseif move_down and current_grade <= 0 and not is_alone then
   206     for initiative_id, grade in pairs(tempvotings) do
   207       if grade < current_grade then
   208         tempvotings[initiative_id] = grade - 1
   209       end
   210     end
   211     tempvotings[current_initiative_id] = current_grade - 1
   213   elseif move_down and current_grade  > 0 and     is_alone then
   214     tempvotings[current_initiative_id] = current_grade - 1
   215     for initiative_id, grade in pairs(tempvotings) do
   216       if grade > current_grade then
   217         tempvotings[initiative_id] = grade - 1
   218       end
   219     end
   221   elseif move_down and current_grade  > 0 and not is_alone then
   222     for initiative_id, grade in pairs(tempvotings) do
   223       if grade >= current_grade then
   224         tempvotings[initiative_id] = grade + 1
   225       end
   226     end
   227     tempvotings[current_initiative_id] = current_grade
   229   end
   231   local tempvotings_list = {}
   232   for key, val in pairs(tempvotings) do
   233     tempvotings_list[#tempvotings_list+1] = tostring(key) .. ":" .. tostring(val)
   234   end
   236   tempvoting_string = table.concat(tempvotings_list, ";")
   238   request.redirect{
   239     module = "vote",
   240     view = "list",
   241     params = {
   242       issue_id = issue.id,
   243       scoring = tempvoting_string
   244     }
   245   }
   247 end
