Module:PollArchive

This is the current revision of this page, as edited by Alex (talk | contribs) at 00:34, 16 October 2024 (Created page with "local p = {} local params = require('Module:Paramtest') local commas = require('Module:Addcommas')._add local math = math function p.main(frame) local args = frame:getParent().args local note = args.Note local q = args.Question local e = args.Error local totalVotes = findTotalVotes(args) local answers = answerList(args) local wrapper = mw.html.create('div'):addClass('pollbox'):addClass(e and "archivepollq e" or "archivepollq"):done() local tbl = mw.html..."). The present address (URL) is a permanent link to this version.

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Module documentation
This documentation is transcluded from Template:No documentation/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:PollArchive/doc. [edit]
Module:PollArchive's function main is invoked by Template:PollArchive.
Module:PollArchive requires Module:Addcommas.
Module:PollArchive requires Module:Paramtest.

local p = {}

local params = require('Module:Paramtest')
local commas = require('Module:Addcommas')._add
local math = math

function p.main(frame)
	local args = frame:getParent().args
	local note = args.Note
    local q = args.Question
    local e = args.Error
	local totalVotes = findTotalVotes(args)
	local answers = answerList(args)
	
	local wrapper = mw.html.create('div'):addClass('pollbox'):addClass(e and "archivepollq e" or "archivepollq"):done()
	local tbl = mw.html.create('table'):done()
	tbl:tag('caption'):wikitext(q or e):done()

	if #answers ~= 0 then
		for i, v in ipairs(answers) do
			local count
			local calculation = v.count * 100 / totalVotes
			local tr = mw.html.create('tr')
			local td = mw.html.create('td'):addClass('archivepollpercent'):done()
			local data = mw.html.create('data'):attr('value', calculation):css({ ['width'] = string.format('%s%%', calculation) }):done()
			tbl:node(mw.html.create('tr'):tag('th'):wikitext(v.text):done())
			tr:node(td:node(data))
			tr:tag('td'):wikitext(string.format('(%s votes %s%%)', commas(v.count), calculatePercentage(v.count, calculation))):done()
			tbl:node(tr)
		end
	end
	
	if note ~= nil then
		local tr = mw.html.create('tr'):addClass('archivepollnote'):done()
		tr:tag('td'):attr('colspan', 3):wikitext(string.format("<i><b>Note:</b> %s</i>", note)):done()
		tbl:node(tr)
	end
	
	wrapper:node(tbl)
	
	return tostring(wrapper)
end

function answerList(args)
	local answers = {}
	for i = 1, 20, 1 do
		local answer = args['Answer'..i]
		if answer and params.has_content(answer) then
			local text = answer
			local count = args['Votes'..i]
			table.insert(answers, {
				text = text,
				count = count
			})
		end
	end
	return answers
end

function findTotalVotes(args)
	local total = 0
	for i = 1, 20, 1 do
		local votes = args['Votes'..i]
		if votes and params.has_content(votes) then
			local answer = args['Answer'..i]
			total = total + tonumber(votes)
		end
	end
	
	return total
end

function calculatePercentage(votes, calculation)
	return math.ceil((round(calculation, 1) * 10)) / 10
end

function round(num, numDecimalPlaces)
  local mult = 10 ^ (numDecimalPlaces or 0)
  return math.floor(num * mult + 0.5) / mult
end

return p