liquid_feedback_frontend

view app/main/initiative/_list.lua @ 285:6c88b4bfb56c

Apply interest/support filter for member at member page
author bsw
date Fri Feb 17 15:16:02 2012 +0100 (2012-02-17)
parents fecd4c13054a
children ad5d7a4eb13d
line source
1 local issue = param.get("issue", "table")
3 local initiatives_selector = param.get("initiatives_selector", "table")
5 local highlight_initiative = param.get("highlight_initiative", "table")
7 local for_member = param.get("for_member", "table") or app.session.member
9 initiatives_selector
10 :join("issue", nil, "issue.id = initiative.issue_id")
12 if app.session.member_id then
13 initiatives_selector
14 :left_join("initiator", "_initiator", { "_initiator.initiative_id = initiative.id AND _initiator.member_id = ? AND _initiator.accepted", for_member.id } )
15 :left_join("supporter", "_supporter", { "_supporter.initiative_id = initiative.id AND _supporter.member_id = ?", for_member.id} )
16 :left_join("delegating_interest_snapshot", "_delegating_interest_snapshot", { "_delegating_interest_snapshot.issue_id = initiative.issue_id AND _delegating_interest_snapshot.member_id = ? AND _delegating_interest_snapshot.event = issue.latest_snapshot_event", for_member.id} )
17 :left_join("direct_supporter_snapshot", "_direct_supporter_snapshot", "_direct_supporter_snapshot.initiative_id = initiative.id AND _direct_supporter_snapshot.member_id = _delegating_interest_snapshot.delegate_member_ids[array_upper(_delegating_interest_snapshot.delegate_member_ids, 1)] AND _direct_supporter_snapshot.event = issue.latest_snapshot_event")
19 :add_field("(_initiator.member_id NOTNULL)", "is_initiator")
20 :add_field({"(_supporter.member_id NOTNULL) AND NOT EXISTS(SELECT 1 FROM opinion WHERE opinion.initiative_id = initiative.id AND opinion.member_id = ? AND ((opinion.degree = 2 AND NOT fulfilled) OR (opinion.degree = -2 AND fulfilled)))", for_member.id }, "is_supporter")
21 :add_field({"EXISTS(SELECT 1 FROM opinion WHERE opinion.initiative_id = initiative.id AND opinion.member_id = ? AND ((opinion.degree = 2 AND NOT fulfilled) OR (opinion.degree = -2 AND fulfilled)))", for_member.id }, "is_potential_supporter")
23 :add_field("_direct_supporter_snapshot.member_id NOTNULL", "is_supporter_via_delegation")
24 end
26 local initiatives_count = initiatives_selector:count()
28 local limit = param.get("limit", atom.number)
29 local no_sort = param.get("no_sort", atom.boolean)
31 local show_for_issue = param.get("show_for_issue", atom.boolean)
33 local show_for_initiative
35 local show_for_initiative_id = param.get("for_initiative_id", atom.number)
37 if show_for_initiative_id then
38 show_for_initiative = Initiative:by_id(show_for_initiative_id)
40 elseif not show_for_initiative_id and show_for_issue and issue and issue.ranks_available then
41 winning_initiative = Initiative:new_selector()
42 :add_where{ "issue_id = ?", issue.id }
43 :add_where("rank = 1")
44 :optional_object_mode()
45 :exec()
46 if winning_initiative then
47 show_for_initiative = winning_initiative
48 ui.container{
49 attr = { class = "admitted_info" },
50 content = _"This issue has been finished with the following winning initiative:"
51 }
52 else
53 ui.container{
54 attr = { class = "not_admitted_info" },
55 content = _"This issue has been finished without any winning initiative."
56 }
57 end
58 end
61 if show_for_initiative then
62 initiatives_selector:add_where{ "initiative.id != ?", show_for_initiative.id }
64 execute.view{
65 module = "initiative",
66 view = "_list_element",
67 params = {
68 initiative = show_for_initiative,
69 }
70 }
71 if show_for_issue then
72 slot.put("<br />")
73 ui.container{
74 attr = { style = "font-weight: bold;" },
75 content = function()
76 slot.put(_"Alternative initiatives")
77 end
78 }
79 end
80 elseif show_for_issue then
81 ui.container{
82 attr = { style = "font-weight: bold;" },
83 content = function()
84 slot.put(_"Alternative initiatives")
85 end
86 }
87 end
89 if not show_for_initiative or initiatives_count > 1 then
92 local more_initiatives_count
93 if limit then
94 limit = limit - (show_for_initiative and 1 or 0)
95 if initiatives_count > limit then
96 more_initiatives_count = initiatives_count - limit
97 end
98 initiatives_selector:limit(limit)
99 end
101 local issue = param.get("issue", "table")
103 local name = "initiative_list"
104 if issue then
105 name = "issue_" .. tostring(issue.id) .. "_initiative_list"
106 end
108 ui.add_partial_param_names{ name }
110 local order_filter = {
111 name = name,
112 label = _"Order by"
113 }
115 if issue and issue.ranks_available then
116 order_filter[#order_filter+1] = {
117 name = "rank",
118 label = _"Rank",
119 selector_modifier = function(selector) selector:add_order_by("initiative.rank, initiative.admitted DESC, vote_ratio(initiative.positive_votes, initiative.negative_votes) DESC, initiative.id") end
120 }
121 end
123 order_filter[#order_filter+1] = {
124 name = "potential_support",
125 label = _"Potential support",
126 selector_modifier = function(selector) selector:add_order_by("CASE WHEN issue.population = 0 THEN 0 ELSE initiative.supporter_count::float / issue.population::float END DESC, initiative.id") end
127 }
129 order_filter[#order_filter+1] = {
130 name = "support",
131 label = _"Support",
132 selector_modifier = function(selector) selector:add_order_by("initiative.satisfied_supporter_count::float / issue.population::float DESC, initiative.id") end
133 }
135 order_filter[#order_filter+1] = {
136 name = "newest",
137 label = _"Newest",
138 selector_modifier = function(selector) selector:add_order_by("initiative.created DESC, initiative.id") end
139 }
141 order_filter[#order_filter+1] = {
142 name = "oldest",
143 label = _"Oldest",
144 selector_modifier = function(selector) selector:add_order_by("initiative.created, initiative.id") end
145 }
147 ui_filters = ui.filters
149 if no_sort then
150 ui_filters = function(args) args.content() end
151 if issue.ranks_available then
152 initiatives_selector:add_order_by("initiative.rank, initiative.admitted DESC, vote_ratio(initiative.positive_votes, initiative.negative_votes) DESC, initiative.id")
153 else
154 initiatives_selector:add_order_by("CASE WHEN issue.population = 0 OR initiative.supporter_count = 0 OR initiative.supporter_count ISNULL THEN 0 ELSE initiative.supporter_count::float / issue.population::float END DESC, initiative.id")
155 end
156 end
158 ui_filters{
159 label = _"Change order",
160 order_filter,
161 selector = initiatives_selector,
162 content = function()
163 ui.paginate{
164 name = issue and "issue_" .. tostring(issue.id) .. "_page" or nil,
165 selector = initiatives_selector,
166 per_page = param.get("per_page", atom.number) or limit,
167 content = function()
168 local initiatives = initiatives_selector:exec()
169 if highlight_initiative then
170 local highlight_initiative_found
171 for i, initiative in ipairs(initiatives) do
172 if initiative.id == highlight_initiative.id then
173 highhighlight_initiative_found = true
174 end
175 end
176 if not highhighlight_initiative_found then
177 initiatives[#initiatives+1] = highlight_initiative
178 if more_initiatives_count then
179 more_initiatives_count = more_initiatives_count - 1
180 end
181 end
182 end
183 for i, initiative in ipairs(initiatives) do
184 execute.view{
185 module = "initiative",
186 view = "_list_element",
187 params = {
188 initiative = initiative,
189 selected = highlight_initiative and highlight_initiative.id == initiative.id or nil,
190 }
191 }
192 end
193 end
194 }
195 end
196 }
198 if more_initiatives_count then
199 local text
200 if more_initiatives_count == 1 then
201 text = _("and one more initiative")
202 else
203 text = _("and #{count} more initiatives", { count = more_initiatives_count })
204 end
205 ui.link{
206 attr = { class = "more_initiatives_link" },
207 content = text,
208 module = "issue",
209 view = "show",
210 id = issue.id,
211 }
212 end
214 end
216 if show_for_issue then
217 slot.put("<br />")
219 if issue and initiatives_count == 1 then
220 ui.container{
221 content = function()
222 if issue.fully_frozen or issue.closed then
223 slot.put(_"There were no more alternative initiatives.")
224 else
225 slot.put(_"There are no more alternative initiatives currently.")
226 end
227 end
228 }
229 end
231 end

Impressum / About Us