liquid_feedback_frontend

view app/main/initiative/_action/create.lua @ 901:f3d6d08b0125

Added support for creating free timed issues in poll mode
author bsw
date Tue Aug 21 01:16:03 2012 +0200 (2012-08-21)
parents ae9ab3edff89
children a176129ce282
line source
1 local tmp = db:query({ "SELECT text_entries_left, initiatives_left FROM member_contingent_left WHERE member_id = ?", app.session.member.id }, "opt_object")
2 if tmp then
3 if tmp.initiatives_left and tmp.initiatives_left < 1 then
4 slot.put_into("error", _"Sorry, your contingent for creating initiatives has been used up. Please try again later.")
5 return false
6 end
7 if tmp.text_entries_left and tmp.text_entries_left < 1 then
8 slot.put_into("error", _"Sorry, you have reached your personal flood limit. Please be slower...")
9 return false
10 end
11 end
13 local issue
14 local area
16 local issue_id = param.get("issue_id", atom.integer)
17 if issue_id then
18 issue = Issue:new_selector():add_where{"id=?",issue_id}:single_object_mode():exec()
19 if issue.closed then
20 slot.put_into("error", _"This issue is already closed.")
21 return false
22 elseif issue.fully_frozen then
23 slot.put_into("error", _"Voting for this issue has already begun.")
24 return false
25 end
26 area = issue.area
27 else
28 local area_id = param.get("area_id", atom.integer)
29 area = Area:new_selector():add_where{"id=?",area_id}:single_object_mode():exec()
30 if not area.active then
31 slot.put_into("error", "Invalid area.")
32 return false
33 end
34 end
36 if not app.session.member:has_voting_right_for_unit_id(area.unit_id) then
37 error("access denied")
38 end
40 local name = param.get("name")
42 local name = util.trim(name)
44 if #name < 3 then
45 slot.put_into("error", _"This name is really too short!")
46 return false
47 end
49 local formatting_engine = param.get("formatting_engine")
51 local formatting_engine_valid = false
52 for fe, dummy in pairs(config.formatting_engine_executeables) do
53 if formatting_engine == fe then
54 formatting_engine_valid = true
55 end
56 end
57 if not formatting_engine_valid then
58 error("invalid formatting engine!")
59 end
61 if param.get("preview") then
62 return
63 end
66 local initiative = Initiative:new()
68 if not issue then
69 local policy_id = param.get("policy_id", atom.integer)
70 if policy_id == -1 then
71 slot.put_into("error", _"Please choose a policy")
72 return false
73 end
74 local policy = Policy:by_id(policy_id)
75 if not policy.active then
76 slot.put_into("error", "Invalid policy.")
77 return false
78 end
79 if policy.polling and not app.session.member:has_polling_right_for_unit_id(area.unit_id) then
80 error("no polling right for this unit")
81 end
83 if not area:get_reference_selector("allowed_policies")
84 :add_where{ "policy.id = ?", policy_id }
85 :optional_object_mode()
86 :exec()
87 then
88 error("policy not allowed")
89 end
90 issue = Issue:new()
91 issue.area_id = area.id
92 issue.policy_id = policy_id
94 if policy.polling then
95 issue.accepted = 'now'
96 issue.state = 'discussion'
97 initiative.polling = true
99 if policy.free_timeable then
100 local free_timing_string = util.trim(param.get("free_timing"))
101 local available_timings
102 if config.free_timing and config.free_timing.available_func then
103 available_timings = config.free_timing.available_func(policy)
104 if available_timings == false then
105 error("error in free timing config")
106 end
107 end
108 if available_timings then
109 local timing_available = false
110 for i, available_timing in ipairs(available_timings) do
111 if available_timing.id == free_timing_string then
112 timing_available = true
113 end
114 end
115 if not timing_available then
116 error('Invalid timing')
117 end
118 end
119 local timing = config.free_timing.calculate_func(policy, free_timing_string)
120 if not timing then
121 error("error in free timing config")
122 end
123 issue.discussion_time = timing.discussion
124 issue.verification_time = timing.verification
125 issue.voting_time = timing.voting
126 end
128 end
130 issue:save()
132 if config.etherpad then
133 local result = net.curl(
134 config.etherpad.api_base
135 .. "api/1/createGroupPad?apikey=" .. config.etherpad.api_key
136 .. "&groupID=" .. config.etherpad.group_id
137 .. "&padName=Issue" .. tostring(issue.id)
138 .. "&text=" .. request.get_absolute_baseurl() .. "issue/show/" .. tostring(issue.id) .. ".html"
139 )
140 end
141 end
143 if param.get("polling", atom.boolean) and app.session.member:has_polling_right_for_unit_id(area.unit_id) then
144 initiative.polling = true
145 end
146 initiative.issue_id = issue.id
147 initiative.name = name
148 param.update(initiative, "discussion_url")
149 initiative:save()
151 local draft = Draft:new()
152 draft.initiative_id = initiative.id
153 draft.formatting_engine = formatting_engine
154 draft.content = param.get("draft")
155 draft.author_id = app.session.member.id
156 draft:save()
158 local initiator = Initiator:new()
159 initiator.initiative_id = initiative.id
160 initiator.member_id = app.session.member.id
161 initiator.accepted = true
162 initiator:save()
164 local supporter = Supporter:new()
165 supporter.initiative_id = initiative.id
166 supporter.member_id = app.session.member.id
167 supporter.draft_id = draft.id
168 supporter:save()
170 slot.put_into("notice", _"Initiative successfully created")
172 request.redirect{
173 module = "initiative",
174 view = "show",
175 id = initiative.id
176 }

Impressum / About Us