bsw/jbe@0: Issue = mondelefant.new_class() bsw/jbe@0: Issue.table = 'issue' bsw/jbe@0: bsw/jbe@0: Issue:add_reference{ bsw/jbe@0: mode = 'm1', bsw/jbe@0: to = "Area", bsw/jbe@0: this_key = 'area_id', bsw/jbe@0: that_key = 'id', bsw/jbe@0: ref = 'area', bsw/jbe@0: } bsw/jbe@0: bsw/jbe@0: Issue:add_reference{ bsw/jbe@0: mode = 'm1', bsw/jbe@0: to = "Policy", bsw/jbe@0: this_key = 'policy_id', bsw/jbe@0: that_key = 'id', bsw/jbe@0: ref = 'policy', bsw/jbe@0: } bsw/jbe@0: bsw/jbe@0: Issue:add_reference{ bsw/jbe@0: mode = '1m', bsw/jbe@0: to = "Initiative", bsw/jbe@0: this_key = 'id', bsw/jbe@0: that_key = 'issue_id', bsw/jbe@0: ref = 'initiatives', bsw/jbe@0: back_ref = 'issue' bsw/jbe@0: } bsw/jbe@0: bsw/jbe@0: Issue:add_reference{ bsw/jbe@0: mode = '1m', bsw/jbe@0: to = "Interest", bsw/jbe@0: this_key = 'id', bsw/jbe@0: that_key = 'issue_id', bsw/jbe@0: ref = 'interests', bsw/jbe@0: back_ref = 'issue', bsw/jbe@0: default_order = '"id"' bsw/jbe@0: } bsw/jbe@0: bsw/jbe@0: Issue:add_reference{ bsw/jbe@0: mode = '1m', bsw/jbe@0: to = "Supporter", bsw/jbe@0: this_key = 'id', bsw/jbe@0: that_key = 'issue_id', bsw/jbe@0: ref = 'supporters', bsw/jbe@0: back_ref = 'issue', bsw/jbe@0: default_order = '"id"' bsw/jbe@0: } bsw/jbe@0: bsw/jbe@0: Issue:add_reference{ bsw/jbe@0: mode = '1m', bsw/jbe@0: to = "DirectVoter", bsw/jbe@0: this_key = 'id', bsw/jbe@0: that_key = 'issue_id', bsw/jbe@0: ref = 'direct_voters', bsw/jbe@0: back_ref = 'issue', bsw/jbe@0: default_order = '"member_id"' bsw/jbe@0: } bsw/jbe@0: bsw/jbe@0: Issue:add_reference{ bsw/jbe@0: mode = '1m', bsw/jbe@0: to = "Vote", bsw/jbe@0: this_key = 'id', bsw/jbe@0: that_key = 'issue_id', bsw/jbe@0: ref = 'votes', bsw/jbe@0: back_ref = 'issue', bsw/jbe@0: default_order = '"member_id", "initiative_id"' bsw/jbe@0: } bsw/jbe@0: bsw/jbe@0: Issue:add_reference{ bsw@2: mode = '1m', bsw@2: to = "Delegation", bsw@2: this_key = 'id', bsw@2: that_key = 'issue_id', bsw@2: ref = 'delegations', bsw@2: back_ref = 'issue' bsw@2: } bsw@2: bsw@2: Issue:add_reference{ bsw/jbe@0: mode = 'mm', bsw/jbe@0: to = "Member", bsw/jbe@0: this_key = 'id', bsw/jbe@0: that_key = 'id', bsw/jbe@0: connected_by_table = 'interest', bsw/jbe@0: connected_by_this_key = 'issue_id', bsw/jbe@0: connected_by_that_key = 'member_id', bsw/jbe@0: ref = 'members' bsw/jbe@0: } bsw/jbe@0: bsw@3: Issue:add_reference{ bsw@3: mode = 'mm', bsw@3: to = "Member", bsw@3: this_key = 'id', bsw@3: that_key = 'id', bsw@3: connected_by_table = 'direct_interest_snapshot', bsw@3: connected_by_this_key = 'issue_id', bsw@3: connected_by_that_key = 'member_id', bsw@3: ref = 'interested_members_snapshot' bsw@3: } bsw@3: bsw/jbe@0: function Issue:get_state_name_for_state(value) bsw@2: local state_name_table = { bsw@2: new = _"New", bsw@3: accepted = _"Discussion", bsw@2: frozen = _"Frozen", bsw@2: voting = _"Voting", bsw@2: finished = _"Finished", bsw@2: cancelled = _"Cancelled" bsw@2: } bsw/jbe@0: return state_name_table[value] or value bsw/jbe@0: end bsw/jbe@0: bsw@2: function Issue:get_search_selector(search_string) bsw/jbe@0: return self:new_selector() bsw/jbe@0: :join('"initiative"', nil, '"initiative"."issue_id" = "issue"."id"') bsw@2: :add_where{ '"initiative"."text_search_data" @@ "text_search_query"(?)', search_string } bsw/jbe@0: :set_distinct() bsw/jbe@0: end bsw/jbe@0: bsw/jbe@0: function Issue.object_get:state() bsw/jbe@0: if self.accepted then bsw@3: if self.closed then bsw/jbe@5: return "finished" bsw@3: elseif self.fully_frozen then bsw@3: return "voting" bsw@3: elseif self.half_frozen then bsw@3: return "frozen" bsw/jbe@0: else bsw/jbe@0: return "accepted" bsw/jbe@0: end bsw/jbe@0: else bsw/jbe@0: if self.closed then bsw@2: return "cancelled" bsw/jbe@0: else bsw/jbe@0: return "new" bsw/jbe@0: end bsw/jbe@0: end bsw@2: end bsw@2: bsw@2: function Issue.object_get:state_name() bsw@2: return Issue:get_state_name_for_state(self.state) bsw@2: end bsw@2: bsw@2: function Issue.object_get:state_time_left() bsw@2: local state = self.state bsw@2: local last_event_time bsw@2: local duration bsw@2: if state == "new" then bsw@2: last_event_time = self.created bsw@2: duration = self.policy.admission_time bsw@2: elseif state == "accepted" then bsw@2: last_event_time = self.accepted bsw@2: duration = self.policy.discussion_time bsw@2: elseif state == "frozen" then bsw@2: last_event_time = self.half_frozen bsw@2: duration = self.policy.verification_time bsw@2: elseif state == "voting" then bsw@2: last_event_time = self.fully_frozen bsw@2: duration = self.policy.voting_time bsw@2: end bsw@2: return db:query{ "SELECT ?::timestamp + ?::interval - now() as time_left", last_event_time, duration }[1].time_left bsw@2: end bsw@2: bsw@2: function Issue.object_get:next_states() bsw@2: local state = self.state bsw@2: local next_states bsw@2: if state == "new" then bsw@2: next_states = { "accepted", "cancelled" } bsw@2: elseif state == "accepted" then bsw@2: next_states = { "frozen" } bsw@2: elseif state == "frozen" then bsw@2: next_states = { "voting" } bsw@2: elseif state == "voting" then bsw@2: next_states = { "finished" } bsw@2: end bsw@2: return next_states bsw@2: end bsw@2: bsw@2: function Issue.object_get:next_states_names() bsw@2: local next_states = self.next_states bsw@2: if not next_states then bsw@2: return bsw@2: end bsw@2: local state_names = {} bsw@2: for i, state in ipairs(self.next_states) do bsw@2: state_names[#state_names+1] = Issue:get_state_name_for_state(state) bsw@2: end bsw@2: return table.concat(state_names, ", ") bsw/jbe@0: end