123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- modpol.modules = modpol.modules or {}
- modpol.modules.consent = {
- type = "consent",
- }
- -- sets consent to its own callback
- modpol.modules.consent.__index = modpol.modules.consent
- function temp_consent_process()
- return {
- org_id = nil,
- request_id = nil,
- total_votes = 0,
- majority_to_pass = 0.51,
- votes_yes = {},
- votes_no = {}
- }
- end
- -- ===============================================
- -- function to create a new consent process to resolve a pending process
- function modpol.modules.consent:new_process(request_id, org_id)
- local process = temp_consent_process()
- process.request_id = request_id
- process.org_id = org_id
- setmetatable(process, modpol.modules.consent)
- modpol.ocutil.log('Created new process for request id #' .. request_id)
- return process
- end
- -- ======================================================
- -- function for users to vote on a pending request
- function modpol.modules.consent:approve(user, decision)
- if not modpol.orgs.get_org(self.org_id):has_member(user) then
- modpol.ocutil.log('Error in consent:approve -> user not a member of the org')
- return
- end
- if decision then
- table.insert(self.votes_yes, user)
- modpol.ocutil.log('User ' .. user .. ' voted yes on request #' .. self.request_id)
- else
- table.insert(self.votes_no, user)
- modpol.ocutil.log('User ' .. user .. ' voted no on request #' .. self.request_id)
- end
- self.total_votes = self.total_votes + 1
- self:update_status()
- end
- -- ===================================================
- -- determines whether process has finished and resolves request if it has (unfinished)
- function modpol.modules.consent:update_status()
- local process_org = modpol.orgs.get_org(self.org_id)
- local eligible_voters = process_org:get_member_count()
- local votes_needed = math.ceil(self.majority_to_pass * eligible_voters)
-
- if #self.votes_yes >= votes_needed then
- modpol.ocutil.log('Request #' .. self.request_id .. ' passes')
- process_org:resolve_request(self.request_id, true)
- elseif #self.votes_no >= votes_needed then
- modpol.ocutil.log('Request #' .. self.request_id .. ' fails to pass')
- process_org:resolve_request(self.request_id, false)
- else
- modpol.ocutil.log('Waiting for more votes...')
- end
- end
|