bsw/jbe@0: Opinion = mondelefant.new_class() bsw/jbe@0: Opinion.table = 'opinion' bsw/jbe@0: Opinion.primary_key = { "member_id", "suggestion_id" } bsw/jbe@0: bsw/jbe@0: Opinion:add_reference{ bsw/jbe@0: mode = 'm1', bsw/jbe@0: to = "Initiative", bsw/jbe@0: this_key = 'initiative_id', bsw/jbe@0: that_key = 'id', bsw/jbe@0: ref = 'initiative', bsw/jbe@0: } bsw/jbe@0: bsw/jbe@0: Opinion:add_reference{ bsw/jbe@0: mode = 'm1', bsw/jbe@0: to = "Suggestion", bsw/jbe@0: this_key = 'suggestion_id', bsw/jbe@0: that_key = 'id', bsw/jbe@0: ref = 'suggestion', bsw/jbe@0: } bsw/jbe@0: bsw/jbe@0: Opinion:add_reference{ bsw/jbe@0: mode = 'm1', bsw/jbe@0: to = "Member", bsw/jbe@0: this_key = 'member_id', bsw/jbe@0: that_key = 'id', bsw/jbe@0: ref = 'member', bsw/jbe@0: } bsw/jbe@0: bsw/jbe@0: function Opinion:by_pk(member_id, suggestion_id) bsw/jbe@0: return self:new_selector() bsw/jbe@0: :add_where{ "member_id = ?", member_id } bsw/jbe@0: :add_where{ "suggestion_id = ?", suggestion_id } bsw/jbe@0: :optional_object_mode() bsw/jbe@0: :exec() bsw/jbe@0: end bsw@1649: bsw@1649: bsw@1649: function Opinion:update(suggestion_id, member_id, degree, fulfilled) bsw@1649: bsw@1649: local opinion = Opinion:by_pk(member_id, suggestion_id) bsw@1649: local suggestion = Suggestion:by_id(suggestion_id) bsw@1649: bsw@1649: if not suggestion then bsw@1649: slot.put_into("error", _"This suggestion has been meanwhile deleted") bsw@1649: return false bsw@1649: end bsw@1649: bsw@1649: -- TODO important m1 selectors returning result _SET_! bsw@1649: local issue = suggestion.initiative:get_reference_selector("issue"):for_share():single_object_mode():exec() bsw@1649: bsw@1649: if issue.closed then bsw@1649: slot.put_into("error", _"This issue is already closed.") bsw@1649: return false bsw@1649: elseif issue.fully_frozen then bsw@1649: slot.put_into("error", _"Voting for this issue has already begun.") bsw@1649: return false bsw@1649: elseif bsw@1649: (issue.half_frozen and issue.phase_finished) or bsw@1649: (not issue.accepted and issue.phase_finished) bsw@1649: then bsw@1649: slot.put_into("error", _"Current phase is already closed.") bsw@1649: return false bsw@1649: end bsw@1649: bsw@1649: if degree == 0 then bsw@1649: if opinion then bsw@1649: opinion:destroy() bsw@1649: end bsw@1649: return true bsw@1649: end bsw@1649: bsw@1649: if degree ~= 0 and not app.session.member:has_voting_right_for_unit_id(suggestion.initiative.issue.area.unit_id) then bsw@1649: return execute.view { module = "index", view = "403" } bsw@1649: end bsw@1649: bsw@1649: if not opinion then bsw@1649: opinion = Opinion:new() bsw@1649: opinion.member_id = member_id bsw@1649: opinion.suggestion_id = suggestion_id bsw@1649: opinion.fulfilled = false bsw@1649: end bsw@1649: bsw@1649: bsw@1649: if degree ~= nil then bsw@1649: opinion.degree = degree bsw@1649: end bsw@1649: bsw@1649: if fulfilled ~= nil then bsw@1649: opinion.fulfilled = fulfilled bsw@1649: end bsw@1649: bsw@1649: opinion:save() bsw@1649: return true bsw@1649: bsw@1649: end