liquid_feedback_frontend

view model/issue.lua @ 492:00f6935c2c32

Added help text for notification settings
author bsw
date Thu Mar 15 21:03:57 2012 +0100 (2012-03-15)
parents 76027e762c76
children 63d6549cc00b
line source
1 Issue = mondelefant.new_class()
2 Issue.table = 'issue'
4 Issue:add_reference{
5 mode = 'm1',
6 to = "Area",
7 this_key = 'area_id',
8 that_key = 'id',
9 ref = 'area',
10 }
12 Issue:add_reference{
13 mode = 'm1',
14 to = "Policy",
15 this_key = 'policy_id',
16 that_key = 'id',
17 ref = 'policy',
18 }
20 Issue:add_reference{
21 mode = '1m',
22 to = "Initiative",
23 this_key = 'id',
24 that_key = 'issue_id',
25 ref = 'initiatives',
26 back_ref = 'issue'
27 }
29 Issue:add_reference{
30 mode = '1m',
31 to = "Interest",
32 this_key = 'id',
33 that_key = 'issue_id',
34 ref = 'interests',
35 back_ref = 'issue',
36 default_order = '"id"'
37 }
39 Issue:add_reference{
40 mode = '1m',
41 to = "Supporter",
42 this_key = 'id',
43 that_key = 'issue_id',
44 ref = 'supporters',
45 back_ref = 'issue',
46 default_order = '"id"'
47 }
49 Issue:add_reference{
50 mode = '1m',
51 to = "DirectVoter",
52 this_key = 'id',
53 that_key = 'issue_id',
54 ref = 'direct_voters',
55 back_ref = 'issue',
56 default_order = '"member_id"'
57 }
59 Issue:add_reference{
60 mode = '1m',
61 to = "Vote",
62 this_key = 'id',
63 that_key = 'issue_id',
64 ref = 'votes',
65 back_ref = 'issue',
66 default_order = '"member_id", "initiative_id"'
67 }
69 Issue:add_reference{
70 mode = '1m',
71 to = "Delegation",
72 this_key = 'id',
73 that_key = 'issue_id',
74 ref = 'delegations',
75 back_ref = 'issue'
76 }
78 Issue:add_reference{
79 mode = 'mm',
80 to = "Member",
81 this_key = 'id',
82 that_key = 'id',
83 connected_by_table = 'interest',
84 connected_by_this_key = 'issue_id',
85 connected_by_that_key = 'member_id',
86 ref = 'members'
87 }
89 Issue:add_reference{
90 mode = 'mm',
91 to = "Member",
92 this_key = 'id',
93 that_key = 'id',
94 connected_by_table = 'direct_interest_snapshot',
95 connected_by_this_key = 'issue_id',
96 connected_by_that_key = 'member_id',
97 ref = 'interested_members_snapshot'
98 }
100 Issue:add_reference{
101 mode = 'mm',
102 to = "Member",
103 this_key = 'id',
104 that_key = 'id',
105 connected_by_table = 'direct_voter',
106 connected_by_this_key = 'issue_id',
107 connected_by_that_key = 'member_id',
108 ref = 'direct_voters'
109 }
113 function Issue:get_state_name_for_state(value)
114 local state_name_table = {
115 new = _"New",
116 accepted = _"Discussion",
117 frozen = _"Frozen",
118 voting = _"Voting",
119 finished = _"Finished",
120 cancelled = _"Cancelled"
121 }
122 return state_name_table[value] or value or ''
123 end
125 function Issue:get_search_selector(search_string)
126 return self:new_selector()
127 :join('"initiative"', nil, '"initiative"."issue_id" = "issue"."id"')
128 :join('"draft"', nil, '"draft"."initiative_id" = "initiative"."id"')
129 :add_where{ '"initiative"."text_search_data" @@ "text_search_query"(?) OR "draft"."text_search_data" @@ "text_search_query"(?)', search_string, search_string }
130 :add_group_by('"issue"."id"')
131 :add_group_by('"issue"."state"')
132 :add_group_by('"issue"."area_id"')
133 :add_group_by('"issue"."policy_id"')
134 :add_group_by('"issue"."created"')
135 :add_group_by('"issue"."accepted"')
136 :add_group_by('"issue"."half_frozen"')
137 :add_group_by('"issue"."fully_frozen"')
138 :add_group_by('"issue"."closed"')
139 :add_group_by('"issue"."ranks_available"')
140 :add_group_by('"issue"."status_quo_schulze_rank"')
141 :add_group_by('"issue"."cleaned"')
142 :add_group_by('"issue"."snapshot"')
143 :add_group_by('"issue"."latest_snapshot_event"')
144 :add_group_by('"issue"."population"')
145 :add_group_by('"issue"."voter_count"')
146 :add_group_by('"issue"."admission_time"')
147 :add_group_by('"issue"."discussion_time"')
148 :add_group_by('"issue"."verification_time"')
149 :add_group_by('"issue"."voting_time"')
150 :add_group_by('"_interest"."member_id"')
151 :add_group_by("_delegating_interest.delegate_member_ids")
152 --:set_distinct()
153 end
155 function Issue:modify_selector_for_state(initiatives_selector, state)
156 if state == "new" then
157 initiatives_selector:add_where("issue.accepted ISNULL AND issue.closed ISNULL")
158 elseif state == "accepted" then
159 initiatives_selector:add_where("issue.accepted NOTNULL AND issue.half_frozen ISNULL AND issue.closed ISNULL")
160 elseif state == "frozen" then
161 initiatives_selector:add_where("issue.half_frozen NOTNULL AND issue.fully_frozen ISNULL AND issue.closed ISNULL")
162 elseif state == "voting" then
163 initiatives_selector:add_where("issue.fully_frozen NOTNULL AND issue.closed ISNULL")
164 elseif state == "finished" then
165 initiatives_selector:add_where("issue.fully_frozen NOTNULL AND issue.closed NOTNULL")
166 elseif state == "cancelled" then
167 initiatives_selector:add_where("issue.fully_frozen ISNULL AND issue.closed NOTNULL")
168 else
169 error("Invalid state")
170 end
171 end
173 function Issue.object_get:state()
174 if self.closed then
175 if self.fully_frozen then
176 return "finished"
177 else
178 return "cancelled"
179 end
180 elseif self.fully_frozen then
181 return "voting"
182 elseif self.half_frozen then
183 return "frozen"
184 elseif self.accepted then
185 return "accepted"
186 else
187 return "new"
188 end
190 end
192 function Issue.object_get:state_name()
193 return Issue:get_state_name_for_state(self.state)
194 end
196 function Issue.object_get:state_time_left()
197 local state = self.state
198 local last_event_time
199 local duration
200 if state == "new" then
201 last_event_time = self.created
202 duration = self.admission_time
203 elseif state == "accepted" then
204 last_event_time = self.accepted
205 duration = self.discussion_time
206 elseif state == "frozen" then
207 last_event_time = self.half_frozen
208 duration = self.verification_time
209 elseif state == "voting" then
210 last_event_time = self.fully_frozen
211 duration = self.voting_time
212 end
213 return db:query{ "SELECT ?::timestamp + ?::interval - CURRENT_TIMESTAMP(0) as time_left", last_event_time, duration }[1].time_left
214 end
216 function Issue.object_get:next_states()
217 local state = self.state
218 local next_states
219 if state == "new" then
220 next_states = { "accepted", "cancelled" }
221 elseif state == "accepted" then
222 next_states = { "frozen" }
223 elseif state == "frozen" then
224 next_states = { "voting" }
225 elseif state == "voting" then
226 next_states = { "finished" }
227 end
228 return next_states
229 end
231 function Issue.object_get:next_states_names()
232 local next_states = self.next_states
233 if not next_states then
234 return
235 end
236 local state_names = {}
237 for i, state in ipairs(self.next_states) do
238 state_names[#state_names+1] = Issue:get_state_name_for_state(state)
239 end
240 return table.concat(state_names, ", ")
241 end

Impressum / About Us