bsw/jbe@0: local issue = param.get("issue", "table")
bsw/jbe@0:
bsw/jbe@19: local initiatives_selector = param.get("initiatives_selector", "table")
bsw@51:
bsw@274: local highlight_initiative = param.get("highlight_initiative", "table")
bsw@274:
bsw@285: local for_member = param.get("for_member", "table") or app.session.member
bsw@285:
bsw@11: initiatives_selector
bsw/jbe@19: :join("issue", nil, "issue.id = initiative.issue_id")
bsw@11:
bsw@51: if app.session.member_id then
bsw@51: initiatives_selector
bsw@285: :left_join("initiator", "_initiator", { "_initiator.initiative_id = initiative.id AND _initiator.member_id = ? AND _initiator.accepted", for_member.id } )
bsw@285: :left_join("supporter", "_supporter", { "_supporter.initiative_id = initiative.id AND _supporter.member_id = ?", for_member.id} )
bsw@285: :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} )
bsw@285: :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")
bsw@285:
bsw@51: :add_field("(_initiator.member_id NOTNULL)", "is_initiator")
bsw@285: :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")
bsw@285: :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")
bsw@285:
bsw@285: :add_field("_direct_supporter_snapshot.member_id NOTNULL", "is_supporter_via_delegation")
bsw@51: end
bsw@11:
bsw/jbe@19: local initiatives_count = initiatives_selector:count()
bsw@11:
bsw/jbe@19: local limit = param.get("limit", atom.number)
bsw/jbe@19: local no_sort = param.get("no_sort", atom.boolean)
bsw/jbe@19:
bsw/jbe@19: local show_for_issue = param.get("show_for_issue", atom.boolean)
bsw/jbe@19:
bsw/jbe@19: local show_for_initiative
bsw/jbe@19:
bsw/jbe@19: local show_for_initiative_id = param.get("for_initiative_id", atom.number)
bsw/jbe@19:
bsw/jbe@19: if show_for_initiative_id then
bsw/jbe@19: show_for_initiative = Initiative:by_id(show_for_initiative_id)
bsw/jbe@19:
bsw/jbe@19: elseif not show_for_initiative_id and show_for_issue and issue and issue.ranks_available then
bsw/jbe@19: winning_initiative = Initiative:new_selector()
bsw/jbe@19: :add_where{ "issue_id = ?", issue.id }
bsw/jbe@19: :add_where("rank = 1")
bsw@24: :optional_object_mode()
bsw/jbe@19: :exec()
bsw/jbe@19: if winning_initiative then
bsw/jbe@19: show_for_initiative = winning_initiative
bsw/jbe@19: ui.container{
bsw/jbe@19: attr = { class = "admitted_info" },
bsw/jbe@19: content = _"This issue has been finished with the following winning initiative:"
bsw/jbe@19: }
bsw/jbe@19: else
bsw/jbe@19: ui.container{
bsw/jbe@19: attr = { class = "not_admitted_info" },
bsw/jbe@19: content = _"This issue has been finished without any winning initiative."
bsw/jbe@19: }
bsw/jbe@19: end
bsw/jbe@19: end
bsw/jbe@19:
bsw/jbe@19:
bsw/jbe@19: if show_for_initiative then
bsw/jbe@19: initiatives_selector:add_where{ "initiative.id != ?", show_for_initiative.id }
bsw/jbe@19:
bsw/jbe@19: execute.view{
bsw/jbe@19: module = "initiative",
bsw/jbe@19: view = "_list_element",
bsw/jbe@19: params = {
bsw/jbe@19: initiative = show_for_initiative,
bsw/jbe@19: }
bsw/jbe@19: }
bsw/jbe@19: if show_for_issue then
bsw/jbe@19: slot.put("
")
bsw/jbe@19: ui.container{
bsw/jbe@19: attr = { style = "font-weight: bold;" },
bsw/jbe@0: content = function()
bsw/jbe@19: slot.put(_"Alternative initiatives")
bsw/jbe@0: end
bsw/jbe@0: }
bsw/jbe@0: end
bsw/jbe@19: elseif show_for_issue then
bsw/jbe@19: ui.container{
bsw/jbe@19: attr = { style = "font-weight: bold;" },
bsw/jbe@19: content = function()
bsw/jbe@19: slot.put(_"Alternative initiatives")
bsw/jbe@19: end
bsw@11: }
bsw@11: end
bsw/jbe@19:
bsw/jbe@19: if not show_for_initiative or initiatives_count > 1 then
bsw/jbe@19:
bsw/jbe@19:
bsw/jbe@19: local more_initiatives_count
bsw/jbe@19: if limit then
bsw/jbe@19: limit = limit - (show_for_initiative and 1 or 0)
bsw/jbe@19: if initiatives_count > limit then
bsw/jbe@19: more_initiatives_count = initiatives_count - limit
bsw/jbe@19: end
bsw/jbe@19: initiatives_selector:limit(limit)
bsw/jbe@19: end
bsw/jbe@19:
bsw/jbe@19: local issue = param.get("issue", "table")
bsw/jbe@19:
bsw/jbe@19: local name = "initiative_list"
bsw/jbe@19: if issue then
bsw/jbe@19: name = "issue_" .. tostring(issue.id) .. "_initiative_list"
bsw/jbe@19: end
bsw/jbe@19:
bsw/jbe@19: ui.add_partial_param_names{ name }
bsw/jbe@19:
bsw/jbe@19: local order_filter = {
bsw/jbe@19: name = name,
bsw/jbe@19: label = _"Order by"
bsw/jbe@19: }
bsw/jbe@19:
bsw/jbe@19: if issue and issue.ranks_available then
bsw/jbe@19: order_filter[#order_filter+1] = {
bsw/jbe@19: name = "rank",
bsw/jbe@19: label = _"Rank",
bsw/jbe@19: 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
bsw/jbe@19: }
bsw/jbe@19: end
bsw/jbe@19:
bsw/jbe@19: order_filter[#order_filter+1] = {
bsw/jbe@19: name = "potential_support",
bsw/jbe@19: label = _"Potential support",
bsw@270: 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
bsw/jbe@19: }
bsw/jbe@19:
bsw/jbe@19: order_filter[#order_filter+1] = {
bsw/jbe@19: name = "support",
bsw/jbe@19: label = _"Support",
bsw/jbe@19: selector_modifier = function(selector) selector:add_order_by("initiative.satisfied_supporter_count::float / issue.population::float DESC, initiative.id") end
bsw/jbe@19: }
bsw/jbe@19:
bsw/jbe@19: order_filter[#order_filter+1] = {
bsw/jbe@19: name = "newest",
bsw/jbe@19: label = _"Newest",
bsw/jbe@19: selector_modifier = function(selector) selector:add_order_by("initiative.created DESC, initiative.id") end
bsw/jbe@19: }
bsw/jbe@19:
bsw/jbe@19: order_filter[#order_filter+1] = {
bsw/jbe@19: name = "oldest",
bsw/jbe@19: label = _"Oldest",
bsw/jbe@19: selector_modifier = function(selector) selector:add_order_by("initiative.created, initiative.id") end
bsw/jbe@19: }
bsw/jbe@19:
bsw/jbe@19: ui_filters = ui.filters
bsw/jbe@19:
bsw/jbe@19: if no_sort then
bsw/jbe@19: ui_filters = function(args) args.content() end
bsw/jbe@19: if issue.ranks_available then
bsw/jbe@19: initiatives_selector:add_order_by("initiative.rank, initiative.admitted DESC, vote_ratio(initiative.positive_votes, initiative.negative_votes) DESC, initiative.id")
bsw/jbe@19: else
bsw@274: 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")
bsw/jbe@19: end
bsw/jbe@19: end
bsw/jbe@19:
bsw/jbe@19: ui_filters{
bsw/jbe@19: label = _"Change order",
bsw/jbe@19: order_filter,
bsw/jbe@19: selector = initiatives_selector,
bsw/jbe@19: content = function()
bsw/jbe@19: ui.paginate{
bsw/jbe@19: name = issue and "issue_" .. tostring(issue.id) .. "_page" or nil,
bsw/jbe@19: selector = initiatives_selector,
bsw@274: per_page = param.get("per_page", atom.number) or limit,
bsw/jbe@19: content = function()
bsw/jbe@19: local initiatives = initiatives_selector:exec()
bsw@274: if highlight_initiative then
bsw@274: local highlight_initiative_found
bsw@274: for i, initiative in ipairs(initiatives) do
bsw@274: if initiative.id == highlight_initiative.id then
bsw@274: highhighlight_initiative_found = true
bsw@274: end
bsw@274: end
bsw@274: if not highhighlight_initiative_found then
bsw@274: initiatives[#initiatives+1] = highlight_initiative
bsw@274: if more_initiatives_count then
bsw@274: more_initiatives_count = more_initiatives_count - 1
bsw@274: end
bsw@274: end
bsw@274: end
bsw/jbe@19: for i, initiative in ipairs(initiatives) do
bsw/jbe@19: execute.view{
bsw/jbe@19: module = "initiative",
bsw/jbe@19: view = "_list_element",
bsw/jbe@19: params = {
bsw/jbe@19: initiative = initiative,
bsw@274: selected = highlight_initiative and highlight_initiative.id == initiative.id or nil,
bsw/jbe@19: }
bsw/jbe@19: }
bsw/jbe@19: end
bsw/jbe@19: end
bsw/jbe@19: }
bsw/jbe@19: end
bsw/jbe@19: }
bsw/jbe@19:
bsw@288: if more_initiatives_count and more_initiatives_count > 0 then
bsw@274: local text
bsw@274: if more_initiatives_count == 1 then
bsw@274: text = _("and one more initiative")
bsw@274: else
bsw@274: text = _("and #{count} more initiatives", { count = more_initiatives_count })
bsw@274: end
bsw/jbe@19: ui.link{
bsw@274: attr = { class = "more_initiatives_link" },
bsw@274: content = text,
bsw/jbe@19: module = "issue",
bsw/jbe@19: view = "show",
bsw/jbe@19: id = issue.id,
bsw/jbe@19: }
bsw/jbe@19: end
bsw/jbe@19:
bsw/jbe@19: end
bsw/jbe@19:
bsw/jbe@19: if show_for_issue then
bsw/jbe@19: slot.put("
")
bsw/jbe@19:
bsw/jbe@19: if issue and initiatives_count == 1 then
bsw/jbe@19: ui.container{
bsw/jbe@19: content = function()
bsw/jbe@19: if issue.fully_frozen or issue.closed then
bsw/jbe@19: slot.put(_"There were no more alternative initiatives.")
bsw/jbe@19: else
bsw/jbe@19: slot.put(_"There are no more alternative initiatives currently.")
bsw/jbe@19: end
bsw/jbe@19: end
bsw/jbe@19: }
bsw/jbe@19: end
bsw/jbe@19:
bsw/jbe@19: end