liquid_feedback_frontend

view app/main/vote/list.lua @ 1558:5e7d76a32227

Hide discard vote button after voting is closed
author bsw
date Tue Nov 10 20:23:37 2020 +0100 (2020-11-10)
parents 09423cf96e9d
children 7eda69e1d14f
line source
1 local issue = Issue:by_id(param.get("issue_id"), atom.integer)
3 -- TODO patch for project voting
4 if not issue.closed and config.alternative_voting and config.alternative_voting[tostring(issue.policy.id)] then
5 local voting_config = config.alternative_voting[tostring(issue.policy.id)]
7 local url = encode.url {
8 module = voting_config.module,
9 view = voting_config.view,
10 params = { issue_id = issue.id }
11 }
13 return request.redirect{ external = url }
14 end
16 if not issue then
17 execute.view { module = "index", view = "404" }
18 return
19 end
21 local member_id = param.get("member_id", atom.integer)
22 local member
23 local readonly = false
25 local preview = param.get("preview") and true or false
27 if member_id then
28 if not issue.closed then
29 execute.view{ module = "index", view = "403" }
30 return
31 end
32 member = Member:by_id(member_id)
33 readonly = true
34 end
36 if issue.closed then
37 if not member then
38 member = app.session.member
39 end
40 readonly = true
41 end
43 if preview then
44 readonly = true
45 end
47 local submit_button_text = _"Finish voting"
48 local edit_button_text = _"Edit again"
50 if issue.closed then
51 submit_button_text = _"Save voting comment"
52 edit_button_text = _"Edit voting comment"
53 end
55 execute.view {
56 module = "issue", view = "_head", params = { issue = issue }
57 }
59 local direct_voter
61 if member then
62 direct_voter = DirectVoter:by_pk(issue.id, member.id)
63 else
64 member = app.session.member
65 direct_voter = DirectVoter:by_pk(issue.id, member.id)
66 end
70 local tempvoting_string = param.get("scoring")
72 local tempvotings = {}
73 if tempvoting_string then
74 for match in tempvoting_string:gmatch("([^;]+)") do
75 for initiative_id, grade in match:gmatch("([^:;]+):([^:;]+)") do
76 tempvotings[tonumber(initiative_id)] = tonumber(grade)
77 end
78 end
79 end
81 local initiatives = issue:get_reference_selector("initiatives"):add_where("initiative.admitted"):add_order_by("initiative.satisfied_supporter_count DESC"):exec()
83 local min_grade = -1;
84 local max_grade = 1;
86 for i, initiative in ipairs(initiatives) do
87 -- TODO performance
88 initiative.vote = Vote:by_pk(initiative.id, member.id)
89 if tempvotings[initiative.id] then
90 initiative.vote = {}
91 initiative.vote.grade = tempvotings[initiative.id]
92 end
93 if initiative.vote then
94 if initiative.vote.grade > max_grade then
95 max_grade = initiative.vote.grade
96 end
97 if initiative.vote.grade < min_grade then
98 min_grade = initiative.vote.grade
99 end
100 end
101 end
103 local sections = {}
104 for i = min_grade, max_grade do
105 sections[i] = {}
106 for j, initiative in ipairs(initiatives) do
107 if (initiative.vote and initiative.vote.grade == i) or (not initiative.vote and i == 0) then
108 sections[i][#(sections[i])+1] = initiative
109 end
110 end
111 end
113 local approval_count, disapproval_count = 0, 0
114 for i = min_grade, -1 do
115 if #sections[i] > 0 then
116 disapproval_count = disapproval_count + 1
117 end
118 end
119 local approval_count = 0
120 for i = 1, max_grade do
121 if #sections[i] > 0 then
122 approval_count = approval_count + 1
123 end
124 end
126 if not readonly then
127 slot.put('<script src="' .. request.get_relative_baseurl() .. 'static/js/dragdrop.js"></script>')
128 slot.put('<script src="' .. request.get_relative_baseurl() .. 'static/js/voting.js"></script>')
129 end
131 ui.script{
132 script = function()
133 slot.put(
134 "voting_text_approval_single = ", encode.json(_"Approval [single entry]"), ";\n",
135 "voting_text_approval_multi = ", encode.json(_"Approval [many entries]"), ";\n",
136 "voting_text_first_preference_single = ", encode.json(_"Approval (first preference) [single entry]"), ";\n",
137 "voting_text_first_preference_multi = ", encode.json(_"Approval (first preference) [many entries]"), ";\n",
138 "voting_text_second_preference_single = ", encode.json(_"Approval (second preference) [single entry]"), ";\n",
139 "voting_text_second_preference_multi = ", encode.json(_"Approval (second preference) [many entries]"), ";\n",
140 "voting_text_third_preference_single = ", encode.json(_"Approval (third preference) [single entry]"), ";\n",
141 "voting_text_third_preference_multi = ", encode.json(_"Approval (third preference) [many entries]"), ";\n",
142 "voting_text_numeric_preference_single = ", encode.json(_"Approval (#th preference) [single entry]"), ";\n",
143 "voting_text_numeric_preference_multi = ", encode.json(_"Approval (#th preference) [many entries]"), ";\n",
144 "voting_text_abstention_single = ", encode.json(_"Abstention [single entry]"), ";\n",
145 "voting_text_abstention_multi = ", encode.json(_"Abstention [many entries]"), ";\n",
146 "voting_text_disapproval_above_one_single = ", encode.json(_"Disapproval (prefer to lower block) [single entry]"), ";\n",
147 "voting_text_disapproval_above_one_multi = ", encode.json(_"Disapproval (prefer to lower block) [many entries]"), ";\n",
148 "voting_text_disapproval_above_many_single = ", encode.json(_"Disapproval (prefer to lower blocks) [single entry]"), ";\n",
149 "voting_text_disapproval_above_many_multi = ", encode.json(_"Disapproval (prefer to lower blocks) [many entries]"), ";\n",
150 "voting_text_disapproval_above_last_single = ", encode.json(_"Disapproval (prefer to last block) [single entry]"), ";\n",
151 "voting_text_disapproval_above_last_multi = ", encode.json(_"Disapproval (prefer to last block) [many entries]"), ";\n",
152 "voting_text_disapproval_single = ", encode.json(_"Disapproval [single entry]"), ";\n",
153 "voting_text_disapproval_multi = ", encode.json(_"Disapproval [many entries]"), ";\n"
154 )
155 end
156 }
158 if issue.state == "finished_with_winner"
159 or issue.state == "finished_without_winner"
160 then
162 local members_selector = Member:new_selector()
163 :join("delegating_voter", nil, "delegating_voter.member_id = member.id")
164 :add_where{ "delegating_voter.issue_id = ?", issue.id }
165 :add_where{ "delegating_voter.delegate_member_ids[1] = ?", member.id }
166 :add_field("delegating_voter.weight", "voter_weight")
167 :add_field("delegating_voter.ownweight", "ownweight")
168 :join("issue", nil, "issue.id = delegating_voter.issue_id")
170 ui.sidebar( "tab-members", function()
171 ui.sidebarHead(function()
172 ui.heading{ level = 4, content = _"Incoming delegations" }
173 end)
174 execute.view{
175 module = "member",
176 view = "_list",
177 params = {
178 members_selector = members_selector,
179 trustee = member,
180 issue = issue,
181 initiative = initiative,
182 for_votes = true, no_filter = true,
183 member_class = "sidebarRow sidebarRowNarrow",
184 }
185 }
186 end)
187 end
189 ui.container{ attr = { class = "mdl-grid" }, content = function()
190 ui.container{ attr = { class = "mdl-cell mdl-cell--12-col" }, content = function()
192 ui.container{ attr = { class = "mdl-card mdl-card__fullwidth mdl-shadow--2dp" }, content = function()
193 ui.container{ attr = { class = "mdl-card__title mdl-card--border" }, content = function()
194 ui.heading { attr = { class = "mdl-card__title-text" }, level = 2, content = function()
195 if preview then
196 ui.tag{ content = _"Preview of voting ballot" }
197 elseif readonly then
198 local str = _("Ballot of '#{member_name}'", { member_name = string.format(
199 '<a href="%s">%s</a>',
200 encode.url{ module = "member", view = "show", id = member.id },
201 encode.html(member.name)
202 ) })
203 ui.tag{ content = function () slot.put ( str ) end }
204 else
205 ui.tag{ content = _"Voting" }
206 end
207 end }
208 end }
210 ui.container{ attr = { class = "mdl-card__content" }, content = function()
212 ui.form{
213 record = direct_voter,
214 attr = {
215 id = "voting_form",
216 class = readonly and "voting_form_readonly" or "voting_form_active"
217 },
218 module = "vote",
219 action = "update",
220 params = { issue_id = issue.id },
221 content = function()
222 if not readonly or preview then
223 local scoring = param.get("scoring")
224 if not scoring then
225 for i, initiative in ipairs(initiatives) do
226 local vote = initiative.vote
227 if vote then
228 tempvotings[initiative.id] = vote.grade
229 else
230 tempvotings[initiative.id] = 0
231 end
232 end
233 local tempvotings_list = {}
234 for key, val in pairs(tempvotings) do
235 tempvotings_list[#tempvotings_list+1] = tostring(key) .. ":" .. tostring(val)
236 end
237 if #tempvotings_list > 0 then
238 scoring = table.concat(tempvotings_list, ";")
239 else
240 scoring = ""
241 end
242 end
243 slot.put('<input type="hidden" name="scoring" value="' .. scoring .. '"/>')
244 end
245 if preview then
246 ui.container{ content = _"Your choice" }
247 elseif not readonly then
248 ui.container{ content = _"Make your choice by placing the initiatives" }
249 end
251 ui.container{
252 attr = { id = "voting" },
253 content = function()
254 local approval_index, disapproval_index = 0, 0
255 local approval_used, disapproval_used
256 for grade = max_grade, min_grade, -1 do
257 local entries = sections[grade]
258 local class
259 if grade > 0 then
260 class = "approval"
261 elseif grade < 0 then
262 class = "disapproval"
263 else
264 class = "abstention"
265 end
266 if
267 #entries > 0 or
268 (grade == 1 and not approval_used) or
269 (grade == -1 and not disapproval_used) or
270 grade == 0
271 then
272 ui.container{
273 attr = { class = class },
274 content = function()
275 local heading
276 if class == "approval" then
277 approval_used = true
278 approval_index = approval_index + 1
279 if approval_count > 1 then
280 if approval_index == 1 then
281 if #entries == 1 then
282 heading = _"Approval (first preference) [single entry]"
283 else
284 heading = _"Approval (first preference) [many entries]"
285 end
286 elseif approval_index == 2 then
287 if #entries == 1 then
288 heading = _"Approval (second preference) [single entry]"
289 else
290 heading = _"Approval (second preference) [many entries]"
291 end
292 elseif approval_index == 3 then
293 if #entries == 1 then
294 heading = _"Approval (third preference) [single entry]"
295 else
296 heading = _"Approval (third preference) [many entries]"
297 end
298 else
299 if #entries == 1 then
300 heading = _"Approval (#th preference) [single entry]"
301 else
302 heading = _"Approval (#th preference) [many entries]"
303 end
304 end
305 else
306 if #entries == 1 then
307 heading = _"Approval [single entry]"
308 else
309 heading = _"Approval [many entries]"
310 end
311 end
312 elseif class == "abstention" then
313 if #entries == 1 then
314 heading = _"Abstention [single entry]"
315 else
316 heading = _"Abstention [many entries]"
317 end
318 elseif class == "disapproval" then
319 disapproval_used = true
320 disapproval_index = disapproval_index + 1
321 if disapproval_count > disapproval_index + 1 then
322 if #entries == 1 then
323 heading = _"Disapproval (prefer to lower blocks) [single entry]"
324 else
325 heading = _"Disapproval (prefer to lower blocks) [many entries]"
326 end
327 elseif disapproval_count == 2 and disapproval_index == 1 then
328 if #entries == 1 then
329 heading = _"Disapproval (prefer to lower block) [single entry]"
330 else
331 heading = _"Disapproval (prefer to lower block) [many entries]"
332 end
333 elseif disapproval_index == disapproval_count - 1 then
334 if #entries == 1 then
335 heading = _"Disapproval (prefer to last block) [single entry]"
336 else
337 heading = _"Disapproval (prefer to last block) [many entries]"
338 end
339 else
340 if #entries == 1 then
341 heading = _"Disapproval [single entry]"
342 else
343 heading = _"Disapproval [many entries]"
344 end
345 end
346 end
347 ui.tag {
348 tag = "div",
349 attr = { class = "cathead " },
350 content = heading
351 }
352 for i, initiative in ipairs(entries) do
353 ui.container{
354 attr = {
355 class = "movable",
356 id = "entry_" .. tostring(initiative.id)
357 },
358 content = function()
359 local initiators_selector = initiative:get_reference_selector("initiating_members")
360 :add_where("accepted")
361 local initiators = initiators_selector:exec()
362 local initiator_names = {}
363 for i, initiator in ipairs(initiators) do
364 initiator_names[#initiator_names+1] = initiator.name
365 end
366 local initiator_names_string = table.concat(initiator_names, ", ")
367 ui.container{
368 attr = { style = "float: right; position: relative;" },
369 content = function()
370 ui.link{
371 attr = { class = "clickable" },
372 content = _"Show",
373 module = "initiative",
374 view = "show",
375 id = initiative.id
376 }
377 slot.put(" ")
378 ui.link{
379 attr = { class = "clickable", target = "_blank" },
380 content = _"(new window)",
381 module = "initiative",
382 view = "show",
383 id = initiative.id
384 }
385 if not readonly then
386 slot.put(" ")
387 ui.image{ attr = { class = "grabber" }, static = "icons/grabber.png" }
388 end
389 end
390 }
391 if not readonly then
392 ui.container{
393 attr = { style = "float: left; position: relative;" },
394 content = function()
395 ui.tag{
396 tag = "button",
397 attr = {
398 onclick = "if (jsFail) return true; voting_moveUp(this.parentNode.parentNode); return(false);",
399 name = "move_up_" .. tostring(initiative.id),
400 class = "clickable mdl-button mdl-js-button mdl-button--icon",
401 alt = _"Move up",
402 },
403 content = function()
404 ui.tag{ tag = "i", attr = { class = "material-icons" }, content = "arrow_upward" }
405 end
406 }
407 ui.tag{
408 tag = "button",
409 attr = {
410 onclick = "if (jsFail) return true; voting_moveDown(this.parentNode.parentNode); return(false);",
411 name = "move_down_" .. tostring(initiative.id),
412 class = "clickable mdl-button mdl-js-button mdl-button--icon",
413 alt = _"Move down"
414 },
415 content = function()
416 ui.tag{ tag = "i", attr = { class = "material-icons" }, content = "arrow_downward" }
417 end
418 }
419 slot.put("&nbsp;")
420 end
421 }
422 end
423 ui.container{
424 content = function()
425 ui.tag{ attr = { class = "initiative_name" }, content = function()
426 ui.tag{ content = "i" .. initiative.id .. ": " }
427 ui.tag{ content = initiative.shortened_name }
428 end }
429 slot.put("<br />")
430 for i, initiator in ipairs(initiators) do
431 ui.link{
432 attr = { class = "clickable" },
433 content = function ()
434 execute.view{
435 module = "member_image",
436 view = "_show",
437 params = {
438 member = initiator,
439 image_type = "avatar",
440 show_dummy = true,
441 class = "micro_avatar",
442 popup_text = text
443 }
444 }
445 end,
446 module = "member", view = "show", id = initiator.id
447 }
448 slot.put(" ")
449 ui.tag{ content = initiator.name }
450 slot.put(" ")
451 end
452 end
453 }
454 end
455 }
456 end
457 end
458 }
459 end
460 end
461 end
462 }
463 if app.session.member_id and preview then
464 local formatting_engine = param.get("formatting_engine") or config.enforce_formatting_engine
465 local comment = param.get("comment")
466 if comment and #comment > 0 then
467 local rendered_comment = format.wiki_text(comment, formatting_engine)
468 ui.container{ content = _"Voting comment" }
469 ui.container { attr = { class = "member_statement" }, content = function()
470 slot.put(rendered_comment)
471 end }
472 slot.put("<br />")
473 end
474 end
475 if (readonly or direct_voter and direct_voter.comment) and not preview and not (app.session.member_id == member.id) then
476 local text
477 if direct_voter and direct_voter.comment_changed then
478 text = _("Voting comment (last updated: #{timestamp})", { timestamp = format.timestamp(direct_voter.comment_changed) })
479 elseif direct_voter and direct_voter.comment then
480 text = _"Voting comment"
481 end
482 if text then
483 ui.container{ content = text }
484 end
485 if direct_voter and direct_voter.comment then
486 local rendered_comment = direct_voter:get_content('html')
487 ui.container { attr = { class = "member_statement" }, content = function()
488 slot.put(rendered_comment)
489 end }
490 slot.put("<br />")
491 end
492 end
493 if app.session.member_id and app.session.member_id == member.id then
494 if (not readonly or direct_voter) and not preview then
495 ui.container{ content = function()
496 if not config.enforce_formatting_engine then
497 ui.field.select{
498 label = _"Wiki engine for statement",
499 name = "formatting_engine",
500 foreign_records = config.formatting_engines,
501 attr = {id = "formatting_engine"},
502 foreign_id = "id",
503 foreign_name = "name",
504 value = param.get("formatting_engine") or direct_voter and direct_voter.formatting_engine
505 }
506 end
507 ui.container{ content = _"Voting comment (optional)" }
508 ui.field.text{
509 name = "comment",
510 multiline = true,
511 value = param.get("comment") or direct_voter and direct_voter.comment,
512 attr = { style = "height: 10ex; width: 100%;" },
513 }
514 end }
515 end
517 if preview then
518 if not config.enforce_formatting_engine then
519 ui.field.hidden{ name = "formatting_engine", value = param.get("formatting_engine") }
520 end
521 ui.field.hidden{ name = "comment", value = param.get("comment") or direct_voter and direct_voter.comment }
522 end
524 if not readonly or direct_voter or preview then
525 if preview then
526 slot.put(" ")
527 ui.tag{
528 tag = "input",
529 attr = {
530 type = "submit",
531 class = "mdl-button mdl-js-button mdl-button--raised mdl-button--colored",
532 name = issue.closed and "update_comment" or nil,
533 value = submit_button_text -- finish voting / update comment
534 }
535 }
536 end
537 if not preview then
538 ui.tag{
539 tag = "input",
540 attr = {
541 type = "submit",
542 name = "preview",
543 class = "mdl-button mdl-js-button mdl-button--raised mdl-button--colored",
544 value = _"Preview",
545 }
546 }
547 else
548 slot.put(" &nbsp; ")
549 ui.tag{
550 tag = "input",
551 attr = {
552 type = "submit",
553 name = "edit",
554 class = "mdl-button mdl-js-button mdl-button--raised",
555 value = edit_button_text,
556 }
557 }
558 end
559 end
560 end
561 end
562 }
563 slot.put("<br />")
564 ui.link{
565 attr = { class = "mdl-button mdl-js-button mdl-button--raised" },
566 text = _"Cancel",
567 module = "issue",
568 view = "show",
569 id = issue.id
570 }
571 if direct_voter and not issue.closed then
572 slot.put(" &nbsp; ")
573 ui.link {
574 attr = { class = "mdl-button mdl-js-button mdl-button--raised" },
575 module = "vote", action = "update",
576 params = {
577 issue_id = issue.id,
578 discard = true
579 },
580 routing = {
581 default = {
582 mode = "redirect",
583 module = "issue",
584 view = "show",
585 id = issue.id
586 }
587 },
588 text = _"Discard my vote"
589 }
590 end
592 end }
593 end }
594 end }
595 end }

Impressum / About Us