liquid_feedback_frontend

annotate model/opinion.lua @ 1742:9f96b7e6e5f2

Changed layout of survey
author bsw
date Mon Oct 11 10:39:23 2021 +0200 (2021-10-11)
parents 4188405c2425
children
rev   line source
bsw/jbe@0 1 Opinion = mondelefant.new_class()
bsw/jbe@0 2 Opinion.table = 'opinion'
bsw/jbe@0 3 Opinion.primary_key = { "member_id", "suggestion_id" }
bsw/jbe@0 4
bsw/jbe@0 5 Opinion:add_reference{
bsw/jbe@0 6 mode = 'm1',
bsw/jbe@0 7 to = "Initiative",
bsw/jbe@0 8 this_key = 'initiative_id',
bsw/jbe@0 9 that_key = 'id',
bsw/jbe@0 10 ref = 'initiative',
bsw/jbe@0 11 }
bsw/jbe@0 12
bsw/jbe@0 13 Opinion:add_reference{
bsw/jbe@0 14 mode = 'm1',
bsw/jbe@0 15 to = "Suggestion",
bsw/jbe@0 16 this_key = 'suggestion_id',
bsw/jbe@0 17 that_key = 'id',
bsw/jbe@0 18 ref = 'suggestion',
bsw/jbe@0 19 }
bsw/jbe@0 20
bsw/jbe@0 21 Opinion:add_reference{
bsw/jbe@0 22 mode = 'm1',
bsw/jbe@0 23 to = "Member",
bsw/jbe@0 24 this_key = 'member_id',
bsw/jbe@0 25 that_key = 'id',
bsw/jbe@0 26 ref = 'member',
bsw/jbe@0 27 }
bsw/jbe@0 28
bsw/jbe@0 29 function Opinion:by_pk(member_id, suggestion_id)
bsw/jbe@0 30 return self:new_selector()
bsw/jbe@0 31 :add_where{ "member_id = ?", member_id }
bsw/jbe@0 32 :add_where{ "suggestion_id = ?", suggestion_id }
bsw/jbe@0 33 :optional_object_mode()
bsw/jbe@0 34 :exec()
bsw/jbe@0 35 end
bsw@1649 36
bsw@1649 37
bsw@1649 38 function Opinion:update(suggestion_id, member_id, degree, fulfilled)
bsw@1649 39
bsw@1649 40 local opinion = Opinion:by_pk(member_id, suggestion_id)
bsw@1649 41 local suggestion = Suggestion:by_id(suggestion_id)
bsw@1649 42
bsw@1649 43 if not suggestion then
bsw@1649 44 slot.put_into("error", _"This suggestion has been meanwhile deleted")
bsw@1649 45 return false
bsw@1649 46 end
bsw@1649 47
bsw@1649 48 -- TODO important m1 selectors returning result _SET_!
bsw@1649 49 local issue = suggestion.initiative:get_reference_selector("issue"):for_share():single_object_mode():exec()
bsw@1649 50
bsw@1649 51 if issue.closed then
bsw@1649 52 slot.put_into("error", _"This issue is already closed.")
bsw@1649 53 return false
bsw@1649 54 elseif issue.fully_frozen then
bsw@1649 55 slot.put_into("error", _"Voting for this issue has already begun.")
bsw@1649 56 return false
bsw@1649 57 elseif
bsw@1649 58 (issue.half_frozen and issue.phase_finished) or
bsw@1649 59 (not issue.accepted and issue.phase_finished)
bsw@1649 60 then
bsw@1649 61 slot.put_into("error", _"Current phase is already closed.")
bsw@1649 62 return false
bsw@1649 63 end
bsw@1649 64
bsw@1649 65 if degree == 0 then
bsw@1649 66 if opinion then
bsw@1649 67 opinion:destroy()
bsw@1649 68 end
bsw@1649 69 return true
bsw@1649 70 end
bsw@1649 71
bsw@1649 72 if degree ~= 0 and not app.session.member:has_voting_right_for_unit_id(suggestion.initiative.issue.area.unit_id) then
bsw@1649 73 return execute.view { module = "index", view = "403" }
bsw@1649 74 end
bsw@1649 75
bsw@1649 76 if not opinion then
bsw@1649 77 opinion = Opinion:new()
bsw@1649 78 opinion.member_id = member_id
bsw@1649 79 opinion.suggestion_id = suggestion_id
bsw@1649 80 opinion.fulfilled = false
bsw@1649 81 end
bsw@1649 82
bsw@1649 83
bsw@1649 84 if degree ~= nil then
bsw@1649 85 opinion.degree = degree
bsw@1649 86 end
bsw@1649 87
bsw@1649 88 if fulfilled ~= nil then
bsw@1649 89 opinion.fulfilled = fulfilled
bsw@1649 90 end
bsw@1649 91
bsw@1649 92 opinion:save()
bsw@1649 93 return true
bsw@1649 94
bsw@1649 95 end

Impressum / About Us