bsw@879: local cancel = param.get("cancel") and true or false bsw@879: if cancel then return end bsw@879: bsw/jbe@5: local issue = Issue:new_selector():add_where{ "id = ?", param.get("issue_id", atom.integer) }:for_share():single_object_mode():exec() bsw/jbe@5: bsw/jbe@1309: -- TODO patch for project voting bsw/jbe@1309: if config.alternative_voting and config.alternative_voting[tostring(issue.policy.id)] then bsw/jbe@1309: return false bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: bsw@1045: local preview = (param.get("preview") or param.get("edit")) and true or false bsw@879: bsw@281: if not app.session.member:has_voting_right_for_unit_id(issue.area.unit_id) then bsw/jbe@1309: return execute.view { module = "index", view = "403" } bsw@281: end bsw@281: bsw@1045: local update_comment = param.get("update_comment") and true or false bsw@879: bsw@964: if issue.state ~= "voting" and not issue.closed then bsw@964: slot.put_into("error", _"Voting has not started yet.") bsw/jbe@5: return false bsw/jbe@5: end bsw/jbe@5: bsw@1045: if (issue.phase_finished or issue.closed) and preview then bsw@1045: return bsw@1045: end bsw@1045: bsw@964: if issue.phase_finished or issue.closed and not update_comment then bsw@964: slot.put_into("error", _"This issue is already closed.") bsw/jbe@5: return false bsw/jbe@5: end bsw/jbe@5: bsw@521: local direct_voter = DirectVoter:by_pk(issue.id, app.session.member_id) bsw@521: bsw@521: if param.get("discard", atom.boolean) then bsw@521: if direct_voter then bsw@521: direct_voter:destroy() bsw@521: end bsw@521: slot.put_into("notice", _"Your vote has been discarded. Delegation rules apply if set.") bsw@521: return bsw@521: end bsw@26: bsw@519: local move_up bsw@519: local move_down bsw@26: bsw@519: local tempvoting_string = param.get("scoring") bsw@519: bsw@519: local tempvotings = {} bsw@879: if not update_comment then bsw@879: for match in tempvoting_string:gmatch("([^;]+)") do bsw@879: for initiative_id, grade in match:gmatch("([^:;]+):([^:;]+)") do bsw@879: tempvotings[tonumber(initiative_id)] = tonumber(grade) bsw@879: if param.get("move_up_" .. initiative_id .. ".x", atom.integer) then bsw@879: move_up = tonumber(initiative_id) bsw@879: elseif param.get("move_down_" .. initiative_id .. ".x", atom.integer) then bsw@879: move_down = tonumber(initiative_id) bsw@879: end bsw@519: end bsw@519: end bsw@519: end bsw/jbe@19: bsw/jbe@19: if not move_down and not move_up then bsw@879: if not preview then bsw@879: if not direct_voter then bsw@879: if issue.closed then bsw@879: slot.put_into("error", _"This issue is already closed.") bsw@879: return false bsw@879: else bsw@879: direct_voter = DirectVoter:new() bsw@879: direct_voter.issue_id = issue.id bsw@879: direct_voter.member_id = app.session.member_id bsw@879: direct_voter:save() bsw@879: bsw@879: direct_voter = DirectVoter:by_pk(issue.id, app.session.member_id) bsw@879: end bsw@879: end bsw@879: bsw@1045: local formatting_engine bsw@1045: if config.enforce_formatting_engine then bsw@1045: formatting_engine = config.enforce_formatting_engine bsw@1045: else bsw@1045: formatting_engine = param.get("formatting_engine") bsw@1045: local formatting_engine_valid = false bsw@1045: for i, fe in ipairs(config.formatting_engines) do bsw@1045: if formatting_engine == fe.id then bsw@1045: formatting_engine_valid = true bsw@1045: end bsw@1045: end bsw@1045: if not formatting_engine_valid then bsw/jbe@1309: slot.put_into("error", "invalid formatting engine!") bsw/jbe@1309: return false bsw@1045: end bsw@1045: end bsw@1045: bsw@925: local comment = param.get("comment") bsw@879: bsw@879: if comment ~= direct_voter.comment then bsw@879: if #comment > 0 then bsw@879: direct_voter.formatting_engine = formatting_engine bsw@879: direct_voter.comment = comment bsw@880: if issue.closed then bsw@880: direct_voter.comment_changed = 'now' bsw@880: end bsw@879: direct_voter:render_content(true) bsw@879: else bsw@879: direct_voter.formatting_engine = null bsw@879: direct_voter.comment = null bsw@880: if issue.closed then bsw@880: direct_voter.comment_changed = 'now' bsw@880: end bsw@879: end bsw@879: end bsw@879: direct_voter:save() bsw@879: bsw/jbe@19: end bsw/jbe@19: bsw@879: if not update_comment then bsw@879: local scoring = param.get("scoring") bsw/jbe@5: bsw@879: for initiative_id, grade in scoring:gmatch("([^:;]+):([^:;]+)") do bsw@879: local initiative_id = tonumber(initiative_id) bsw@879: local grade = tonumber(grade) bsw@879: local initiative = Initiative:by_id(initiative_id) bsw@879: if initiative.issue.id ~= issue.id then bsw/jbe@1309: return execute.view { module = "index", view = "403" } bsw@879: end bsw@879: if not preview and not issue.closed then bsw@879: local vote = Vote:by_pk(initiative_id, app.session.member.id) bsw@879: if not vote then bsw@879: vote = Vote:new() bsw@879: vote.issue_id = issue.id bsw@879: vote.initiative_id = initiative.id bsw@879: vote.member_id = app.session.member.id bsw@879: end bsw@879: vote.grade = grade bsw@879: vote:save() bsw@879: end bsw/jbe@19: end bsw@879: end bsw@879: bsw@879: if not preview and not cancel then bsw@879: request.redirect{ bsw@879: module = "issue", bsw@879: view = "show", bsw@879: id = issue.id bsw@879: } bsw/jbe@19: end bsw/jbe@19: bsw/jbe@19: else bsw/jbe@19: bsw/jbe@19: local current_initiative_id = move_up or move_down bsw/jbe@5: bsw/jbe@19: local current_grade = tempvotings[current_initiative_id] or 0 bsw/jbe@19: local is_alone = true bsw/jbe@19: if current_grade == 0 then bsw/jbe@19: is_alone = false bsw/jbe@19: else bsw/jbe@19: for initiative_id, grade in pairs(tempvotings) do bsw/jbe@19: if current_initiative_id ~= initiative_id and grade == current_grade then bsw/jbe@19: is_alone = false bsw/jbe@19: break bsw/jbe@19: end bsw/jbe@19: end bsw/jbe@19: end bsw/jbe@5: bsw/jbe@19: if move_up and current_grade >= 0 and is_alone then bsw/jbe@19: for initiative_id, grade in pairs(tempvotings) do bsw/jbe@19: if grade > current_grade then bsw/jbe@19: tempvotings[initiative_id] = grade - 1 bsw/jbe@19: end bsw/jbe@19: end bsw/jbe@5: bsw/jbe@19: elseif move_up and current_grade >= 0 and not is_alone then bsw/jbe@19: for initiative_id, grade in pairs(tempvotings) do bsw/jbe@19: if grade > current_grade then bsw/jbe@19: tempvotings[initiative_id] = grade + 1 bsw/jbe@19: end bsw/jbe@19: end bsw/jbe@19: tempvotings[current_initiative_id] = current_grade + 1 bsw/jbe@19: bsw/jbe@19: elseif move_up and current_grade < 0 and is_alone then bsw/jbe@19: tempvotings[current_initiative_id] = current_grade + 1 bsw/jbe@19: for initiative_id, grade in pairs(tempvotings) do bsw/jbe@19: if grade < current_grade then bsw/jbe@19: tempvotings[initiative_id] = grade + 1 bsw/jbe@19: end bsw/jbe@19: end bsw/jbe@19: bsw/jbe@19: elseif move_up and current_grade < 0 and not is_alone then bsw/jbe@19: for initiative_id, grade in pairs(tempvotings) do bsw/jbe@19: if grade <= current_grade then bsw/jbe@19: tempvotings[initiative_id] = grade - 1 bsw/jbe@19: end bsw/jbe@19: end bsw/jbe@19: tempvotings[current_initiative_id] = current_grade bsw/jbe@19: bsw/jbe@19: elseif move_down and current_grade <= 0 and is_alone then bsw/jbe@19: for initiative_id, grade in pairs(tempvotings) do bsw/jbe@19: if grade < current_grade then bsw/jbe@19: tempvotings[initiative_id] = grade + 1 bsw/jbe@19: end bsw/jbe@19: end bsw/jbe@19: bsw/jbe@19: elseif move_down and current_grade <= 0 and not is_alone then bsw/jbe@19: for initiative_id, grade in pairs(tempvotings) do bsw/jbe@19: if grade < current_grade then bsw/jbe@19: tempvotings[initiative_id] = grade - 1 bsw/jbe@19: end bsw/jbe@19: end bsw/jbe@19: tempvotings[current_initiative_id] = current_grade - 1 bsw/jbe@19: bsw/jbe@19: elseif move_down and current_grade > 0 and is_alone then bsw/jbe@19: tempvotings[current_initiative_id] = current_grade - 1 bsw/jbe@19: for initiative_id, grade in pairs(tempvotings) do bsw/jbe@19: if grade > current_grade then bsw/jbe@19: tempvotings[initiative_id] = grade - 1 bsw/jbe@19: end bsw/jbe@19: end bsw/jbe@19: bsw/jbe@19: elseif move_down and current_grade > 0 and not is_alone then bsw/jbe@19: for initiative_id, grade in pairs(tempvotings) do bsw/jbe@19: if grade >= current_grade then bsw/jbe@19: tempvotings[initiative_id] = grade + 1 bsw/jbe@19: end bsw/jbe@19: end bsw/jbe@19: tempvotings[current_initiative_id] = current_grade bsw/jbe@19: bsw/jbe@5: end bsw/jbe@19: bsw/jbe@19: local tempvotings_list = {} bsw/jbe@19: for key, val in pairs(tempvotings) do bsw/jbe@19: tempvotings_list[#tempvotings_list+1] = tostring(key) .. ":" .. tostring(val) bsw/jbe@5: end bsw/jbe@19: bsw/jbe@19: tempvoting_string = table.concat(tempvotings_list, ";") bsw/jbe@19: bsw/jbe@19: request.redirect{ bsw/jbe@19: module = "vote", bsw/jbe@19: view = "list", bsw/jbe@19: params = { bsw/jbe@19: issue_id = issue.id, bsw/jbe@19: scoring = tempvoting_string bsw/jbe@19: } bsw/jbe@19: } bsw/jbe@19: bsw/jbe@5: end