| rev | 
   line source | 
| 
bsw/jbe@0
 | 
     1 local issue = param.get("issue", "table")
 | 
| 
bsw/jbe@0
 | 
     2 
 | 
| 
bsw/jbe@19
 | 
     3 local initiatives_selector = param.get("initiatives_selector", "table")
 | 
| 
bsw@51
 | 
     4 
 | 
| 
bsw@274
 | 
     5 local highlight_initiative = param.get("highlight_initiative", "table")
 | 
| 
bsw@274
 | 
     6 
 | 
| 
bsw@285
 | 
     7 local for_member = param.get("for_member", "table") or app.session.member
 | 
| 
bsw@285
 | 
     8 
 | 
| 
bsw@11
 | 
     9 initiatives_selector
 | 
| 
bsw/jbe@19
 | 
    10   :join("issue", nil, "issue.id = initiative.issue_id")
 | 
| 
bsw@11
 | 
    11 
 | 
| 
bsw@51
 | 
    12 if app.session.member_id then
 | 
| 
bsw@51
 | 
    13   initiatives_selector
 | 
| 
bsw@285
 | 
    14     :left_join("initiator", "_initiator", { "_initiator.initiative_id = initiative.id AND _initiator.member_id = ? AND _initiator.accepted", for_member.id } )
 | 
| 
bsw@285
 | 
    15     :left_join("supporter", "_supporter", { "_supporter.initiative_id = initiative.id AND _supporter.member_id = ?", for_member.id} )
 | 
| 
bsw@285
 | 
    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} )
 | 
| 
bsw@285
 | 
    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")
 | 
| 
bsw@285
 | 
    18 
 | 
| 
bsw@51
 | 
    19     :add_field("(_initiator.member_id NOTNULL)", "is_initiator")
 | 
| 
bsw@285
 | 
    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")
 | 
| 
bsw@285
 | 
    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")
 | 
| 
bsw@285
 | 
    22 
 | 
| 
bsw@285
 | 
    23     :add_field("_direct_supporter_snapshot.member_id NOTNULL", "is_supporter_via_delegation")
 | 
| 
bsw@51
 | 
    24 end
 | 
| 
bsw@11
 | 
    25 
 | 
| 
bsw/jbe@19
 | 
    26 local initiatives_count = initiatives_selector:count()
 | 
| 
bsw@11
 | 
    27 
 | 
| 
bsw/jbe@19
 | 
    28 local limit = param.get("limit", atom.number)
 | 
| 
bsw/jbe@19
 | 
    29 local no_sort = param.get("no_sort", atom.boolean)
 | 
| 
bsw/jbe@19
 | 
    30 
 | 
| 
bsw/jbe@19
 | 
    31 local show_for_issue = param.get("show_for_issue", atom.boolean)
 | 
| 
bsw/jbe@19
 | 
    32 
 | 
| 
bsw/jbe@19
 | 
    33 local show_for_initiative
 | 
| 
bsw/jbe@19
 | 
    34 
 | 
| 
bsw/jbe@19
 | 
    35 local show_for_initiative_id = param.get("for_initiative_id", atom.number)
 | 
| 
bsw/jbe@19
 | 
    36 
 | 
| 
bsw/jbe@19
 | 
    37 if show_for_initiative_id then
 | 
| 
bsw/jbe@19
 | 
    38   show_for_initiative = Initiative:by_id(show_for_initiative_id)
 | 
| 
bsw/jbe@19
 | 
    39 
 | 
| 
bsw/jbe@19
 | 
    40 elseif not show_for_initiative_id and show_for_issue and issue and issue.ranks_available then
 | 
| 
bsw/jbe@19
 | 
    41   winning_initiative = Initiative:new_selector()
 | 
| 
bsw/jbe@19
 | 
    42     :add_where{ "issue_id = ?", issue.id }
 | 
| 
bsw/jbe@19
 | 
    43     :add_where("rank = 1")
 | 
| 
bsw@24
 | 
    44     :optional_object_mode()
 | 
| 
bsw/jbe@19
 | 
    45     :exec()
 | 
| 
bsw/jbe@19
 | 
    46   if winning_initiative then
 | 
| 
bsw/jbe@19
 | 
    47     show_for_initiative = winning_initiative
 | 
| 
bsw/jbe@19
 | 
    48     ui.container{
 | 
| 
bsw/jbe@19
 | 
    49       attr = { class = "admitted_info" },
 | 
| 
bsw/jbe@19
 | 
    50       content = _"This issue has been finished with the following winning initiative:"
 | 
| 
bsw/jbe@19
 | 
    51     }
 | 
| 
bsw/jbe@19
 | 
    52   else
 | 
| 
bsw/jbe@19
 | 
    53     ui.container{
 | 
| 
bsw/jbe@19
 | 
    54       attr = { class = "not_admitted_info" },
 | 
| 
bsw/jbe@19
 | 
    55       content = _"This issue has been finished without any winning initiative."
 | 
| 
bsw/jbe@19
 | 
    56     }
 | 
| 
bsw/jbe@19
 | 
    57   end
 | 
| 
bsw/jbe@19
 | 
    58 end
 | 
| 
bsw/jbe@19
 | 
    59 
 | 
| 
bsw/jbe@19
 | 
    60 
 | 
| 
bsw/jbe@19
 | 
    61 if show_for_initiative then
 | 
| 
bsw/jbe@19
 | 
    62   initiatives_selector:add_where{ "initiative.id != ?", show_for_initiative.id }
 | 
| 
bsw/jbe@19
 | 
    63 
 | 
| 
bsw/jbe@19
 | 
    64   execute.view{
 | 
| 
bsw/jbe@19
 | 
    65     module = "initiative",
 | 
| 
bsw/jbe@19
 | 
    66     view = "_list_element",
 | 
| 
bsw/jbe@19
 | 
    67     params = {
 | 
| 
bsw/jbe@19
 | 
    68       initiative = show_for_initiative,
 | 
| 
bsw/jbe@19
 | 
    69     }
 | 
| 
bsw/jbe@19
 | 
    70   }
 | 
| 
bsw/jbe@19
 | 
    71   if show_for_issue then
 | 
| 
bsw/jbe@19
 | 
    72     slot.put("<br />")
 | 
| 
bsw/jbe@19
 | 
    73     ui.container{
 | 
| 
bsw/jbe@19
 | 
    74       attr = { style = "font-weight: bold;" },
 | 
| 
bsw/jbe@0
 | 
    75       content = function()
 | 
| 
bsw/jbe@19
 | 
    76         slot.put(_"Alternative initiatives")
 | 
| 
bsw/jbe@0
 | 
    77       end
 | 
| 
bsw/jbe@0
 | 
    78     }
 | 
| 
bsw/jbe@0
 | 
    79   end
 | 
| 
bsw/jbe@19
 | 
    80 elseif show_for_issue then
 | 
| 
bsw/jbe@19
 | 
    81   ui.container{
 | 
| 
bsw/jbe@19
 | 
    82     attr = { style = "font-weight: bold;" },
 | 
| 
bsw/jbe@19
 | 
    83     content = function()
 | 
| 
bsw/jbe@19
 | 
    84       slot.put(_"Alternative initiatives")
 | 
| 
bsw/jbe@19
 | 
    85     end
 | 
| 
bsw@11
 | 
    86   }
 | 
| 
bsw@11
 | 
    87 end
 | 
| 
bsw/jbe@19
 | 
    88 
 | 
| 
bsw/jbe@19
 | 
    89 if not show_for_initiative or initiatives_count > 1 then
 | 
| 
bsw/jbe@19
 | 
    90 
 | 
| 
bsw/jbe@19
 | 
    91 
 | 
| 
bsw/jbe@19
 | 
    92   local more_initiatives_count
 | 
| 
bsw/jbe@19
 | 
    93   if limit then
 | 
| 
bsw/jbe@19
 | 
    94     limit = limit - (show_for_initiative and 1 or 0)
 | 
| 
bsw/jbe@19
 | 
    95     if initiatives_count > limit then
 | 
| 
bsw/jbe@19
 | 
    96       more_initiatives_count = initiatives_count - limit
 | 
| 
bsw/jbe@19
 | 
    97     end
 | 
| 
bsw/jbe@19
 | 
    98     initiatives_selector:limit(limit)
 | 
| 
bsw/jbe@19
 | 
    99   end
 | 
| 
bsw/jbe@19
 | 
   100 
 | 
| 
bsw/jbe@19
 | 
   101   local issue = param.get("issue", "table")
 | 
| 
bsw/jbe@19
 | 
   102 
 | 
| 
bsw/jbe@19
 | 
   103   local name = "initiative_list"
 | 
| 
bsw/jbe@19
 | 
   104   if issue then
 | 
| 
bsw/jbe@19
 | 
   105     name = "issue_" .. tostring(issue.id) ..  "_initiative_list"
 | 
| 
bsw/jbe@19
 | 
   106   end
 | 
| 
bsw/jbe@19
 | 
   107 
 | 
| 
bsw/jbe@19
 | 
   108   ui.add_partial_param_names{ name }
 | 
| 
bsw/jbe@19
 | 
   109 
 | 
| 
bsw/jbe@19
 | 
   110   local order_filter = {
 | 
| 
bsw/jbe@19
 | 
   111     name = name,
 | 
| 
bsw/jbe@19
 | 
   112     label = _"Order by"
 | 
| 
bsw/jbe@19
 | 
   113   }
 | 
| 
bsw/jbe@19
 | 
   114 
 | 
| 
bsw/jbe@19
 | 
   115   if issue and issue.ranks_available then
 | 
| 
bsw/jbe@19
 | 
   116     order_filter[#order_filter+1] = {
 | 
| 
bsw/jbe@19
 | 
   117       name = "rank",
 | 
| 
bsw/jbe@19
 | 
   118       label = _"Rank",
 | 
| 
bsw/jbe@19
 | 
   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
 | 
| 
bsw/jbe@19
 | 
   120     }
 | 
| 
bsw/jbe@19
 | 
   121   end
 | 
| 
bsw/jbe@19
 | 
   122 
 | 
| 
bsw/jbe@19
 | 
   123   order_filter[#order_filter+1] = {
 | 
| 
bsw/jbe@19
 | 
   124     name = "potential_support",
 | 
| 
bsw/jbe@19
 | 
   125     label = _"Potential support",
 | 
| 
bsw@270
 | 
   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
 | 
| 
bsw/jbe@19
 | 
   127   }
 | 
| 
bsw/jbe@19
 | 
   128 
 | 
| 
bsw/jbe@19
 | 
   129   order_filter[#order_filter+1] = {
 | 
| 
bsw/jbe@19
 | 
   130     name = "support",
 | 
| 
bsw/jbe@19
 | 
   131     label = _"Support",
 | 
| 
bsw/jbe@19
 | 
   132     selector_modifier = function(selector) selector:add_order_by("initiative.satisfied_supporter_count::float / issue.population::float DESC, initiative.id") end
 | 
| 
bsw/jbe@19
 | 
   133   }
 | 
| 
bsw/jbe@19
 | 
   134 
 | 
| 
bsw/jbe@19
 | 
   135   order_filter[#order_filter+1] = {
 | 
| 
bsw/jbe@19
 | 
   136     name = "newest",
 | 
| 
bsw/jbe@19
 | 
   137     label = _"Newest",
 | 
| 
bsw/jbe@19
 | 
   138     selector_modifier = function(selector) selector:add_order_by("initiative.created DESC, initiative.id") end
 | 
| 
bsw/jbe@19
 | 
   139   }
 | 
| 
bsw/jbe@19
 | 
   140 
 | 
| 
bsw/jbe@19
 | 
   141   order_filter[#order_filter+1] = {
 | 
| 
bsw/jbe@19
 | 
   142     name = "oldest",
 | 
| 
bsw/jbe@19
 | 
   143     label = _"Oldest",
 | 
| 
bsw/jbe@19
 | 
   144     selector_modifier = function(selector) selector:add_order_by("initiative.created, initiative.id") end
 | 
| 
bsw/jbe@19
 | 
   145   }
 | 
| 
bsw/jbe@19
 | 
   146 
 | 
| 
bsw/jbe@19
 | 
   147   ui_filters = ui.filters
 | 
| 
bsw/jbe@19
 | 
   148 
 | 
| 
bsw/jbe@19
 | 
   149   if no_sort then
 | 
| 
bsw/jbe@19
 | 
   150     ui_filters = function(args) args.content() end
 | 
| 
bsw/jbe@19
 | 
   151     if issue.ranks_available then
 | 
| 
bsw/jbe@19
 | 
   152       initiatives_selector:add_order_by("initiative.rank, initiative.admitted DESC, vote_ratio(initiative.positive_votes, initiative.negative_votes) DESC, initiative.id")
 | 
| 
bsw/jbe@19
 | 
   153     else
 | 
| 
bsw@274
 | 
   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")
 | 
| 
bsw/jbe@19
 | 
   155     end
 | 
| 
bsw/jbe@19
 | 
   156   end
 | 
| 
bsw/jbe@19
 | 
   157 
 | 
| 
bsw/jbe@19
 | 
   158   ui_filters{
 | 
| 
bsw/jbe@19
 | 
   159     label = _"Change order",
 | 
| 
bsw/jbe@19
 | 
   160     order_filter,
 | 
| 
bsw/jbe@19
 | 
   161     selector = initiatives_selector,
 | 
| 
bsw/jbe@19
 | 
   162     content = function()
 | 
| 
bsw/jbe@19
 | 
   163       ui.paginate{
 | 
| 
bsw/jbe@19
 | 
   164         name = issue and "issue_" .. tostring(issue.id) .. "_page" or nil,
 | 
| 
bsw/jbe@19
 | 
   165         selector = initiatives_selector,
 | 
| 
bsw@274
 | 
   166         per_page = param.get("per_page", atom.number) or limit,
 | 
| 
bsw/jbe@19
 | 
   167         content = function()
 | 
| 
bsw/jbe@19
 | 
   168           local initiatives = initiatives_selector:exec()
 | 
| 
bsw@274
 | 
   169           if highlight_initiative then
 | 
| 
bsw@274
 | 
   170             local highlight_initiative_found
 | 
| 
bsw@274
 | 
   171             for i, initiative in ipairs(initiatives) do
 | 
| 
bsw@274
 | 
   172               if initiative.id == highlight_initiative.id then
 | 
| 
bsw@274
 | 
   173                 highhighlight_initiative_found = true
 | 
| 
bsw@274
 | 
   174               end
 | 
| 
bsw@274
 | 
   175             end
 | 
| 
bsw@274
 | 
   176             if not highhighlight_initiative_found then
 | 
| 
bsw@274
 | 
   177               initiatives[#initiatives+1] = highlight_initiative
 | 
| 
bsw@274
 | 
   178               if more_initiatives_count then
 | 
| 
bsw@274
 | 
   179                 more_initiatives_count = more_initiatives_count - 1
 | 
| 
bsw@274
 | 
   180               end
 | 
| 
bsw@274
 | 
   181             end
 | 
| 
bsw@274
 | 
   182           end
 | 
| 
bsw/jbe@19
 | 
   183           for i, initiative in ipairs(initiatives) do
 | 
| 
bsw/jbe@19
 | 
   184             execute.view{
 | 
| 
bsw/jbe@19
 | 
   185               module = "initiative",
 | 
| 
bsw/jbe@19
 | 
   186               view = "_list_element",
 | 
| 
bsw/jbe@19
 | 
   187               params = {
 | 
| 
bsw/jbe@19
 | 
   188                 initiative = initiative,
 | 
| 
bsw@274
 | 
   189                 selected = highlight_initiative and highlight_initiative.id == initiative.id or nil,
 | 
| 
bsw/jbe@19
 | 
   190               }
 | 
| 
bsw/jbe@19
 | 
   191             }
 | 
| 
bsw/jbe@19
 | 
   192           end
 | 
| 
bsw/jbe@19
 | 
   193         end
 | 
| 
bsw/jbe@19
 | 
   194       }
 | 
| 
bsw/jbe@19
 | 
   195     end
 | 
| 
bsw/jbe@19
 | 
   196   }
 | 
| 
bsw/jbe@19
 | 
   197 
 | 
| 
bsw@288
 | 
   198   if more_initiatives_count and more_initiatives_count > 0 then
 | 
| 
bsw@274
 | 
   199     local text
 | 
| 
bsw@274
 | 
   200     if more_initiatives_count == 1 then
 | 
| 
bsw@274
 | 
   201       text = _("and one more initiative")
 | 
| 
bsw@274
 | 
   202     else
 | 
| 
bsw@274
 | 
   203       text = _("and #{count} more initiatives", { count = more_initiatives_count })
 | 
| 
bsw@274
 | 
   204     end
 | 
| 
bsw/jbe@19
 | 
   205     ui.link{
 | 
| 
bsw@274
 | 
   206       attr = { class = "more_initiatives_link" },
 | 
| 
bsw@274
 | 
   207       content = text,
 | 
| 
bsw/jbe@19
 | 
   208       module = "issue",
 | 
| 
bsw/jbe@19
 | 
   209       view = "show",
 | 
| 
bsw/jbe@19
 | 
   210       id = issue.id,
 | 
| 
bsw/jbe@19
 | 
   211     }
 | 
| 
bsw/jbe@19
 | 
   212   end
 | 
| 
bsw/jbe@19
 | 
   213 
 | 
| 
bsw/jbe@19
 | 
   214 end
 | 
| 
bsw/jbe@19
 | 
   215 
 | 
| 
bsw/jbe@19
 | 
   216 if show_for_issue then
 | 
| 
bsw/jbe@19
 | 
   217   slot.put("<br />")
 | 
| 
bsw/jbe@19
 | 
   218   
 | 
| 
bsw/jbe@19
 | 
   219   if issue and initiatives_count == 1 then
 | 
| 
bsw/jbe@19
 | 
   220     ui.container{
 | 
| 
bsw/jbe@19
 | 
   221       content = function()
 | 
| 
bsw/jbe@19
 | 
   222         if issue.fully_frozen or issue.closed then
 | 
| 
bsw/jbe@19
 | 
   223           slot.put(_"There were no more alternative initiatives.")
 | 
| 
bsw/jbe@19
 | 
   224         else
 | 
| 
bsw/jbe@19
 | 
   225           slot.put(_"There are no more alternative initiatives currently.")
 | 
| 
bsw/jbe@19
 | 
   226         end
 | 
| 
bsw/jbe@19
 | 
   227       end
 | 
| 
bsw/jbe@19
 | 
   228     }
 | 
| 
bsw/jbe@19
 | 
   229   end
 | 
| 
bsw/jbe@19
 | 
   230 
 | 
| 
bsw/jbe@19
 | 
   231 end
 |