Kairosoft Wiki
No edit summary
No edit summary
Line 34: Line 34:
 
 
 
local table_string = mw.html.create('')
 
local table_string = mw.html.create('')
:tag('h3'):wikitext(title):done()
+
:tag('h3'):wikitext(title):css({ ["border-bottom"]="1px solid #B2AF9C", ["text-align"]="center" }):done()
 
:tag('div'):addClass('ajax-poll')
 
:tag('div'):addClass('ajax-poll')
 
:tag('div'):addClass('header'):wikitext(title):done()
 
:tag('div'):addClass('header'):wikitext(title):done()

Revision as of 02:43, 14 December 2014

Documentation for this module may be created at Module:PreviousPoll/doc

local p = {}

function p.display_results( frame )
	local parameters = frame:getParent().args
	
	local title = parameters['title'] or 'TITLE'
	local startDate = parameters['start'] or 'START DATE'
	local endDate = parameters['end'] or 'END DATE'
	-- local totalvotes = tonumber(parameters['totalvotes']) or 1
	
	local container = mw.html.create('div'):done()
	
	local MAX_ROWS = 20
	
	local totalvotes = 0
	local topVote = 0
	for i=1,MAX_ROWS,1 do
		if parameters['option'..i] then
			local votes = tonumber(parameters['votes'..i])
			totalvotes = totalvotes + votes
			if(votes > topVote) then topVote = votes end
		end
	end
		
	for i=1,MAX_ROWS,1 do
		local optionText = parameters['option'..i]
		local votes = tonumber(parameters['votes'..i])
		-- local winner = parameters['winner'..i]
		
		if optionText then
			container:node(p._makeRow(optionText,votes,totalvotes,votes >= topVote))
		end
	end
	
	local table_string = mw.html.create('')
		:tag('h3'):wikitext(title):css({ ["border-bottom"]="1px solid #B2AF9C", ["text-align"]="center" }):done()
		:tag('div'):addClass('ajax-poll')
			:tag('div'):addClass('header'):wikitext(title):done()
			:node(container)
			:tag('div'):wikitext("<br />'''Start Date''': "..startDate..
				"<br />'''End Date''': "..endDate..
				"<br />'''Total Votes''': "..totalvotes):done()
		:done()
	:done()
	
	return tostring(table_string)
end

function p._makeRow(pText, pVotes, pTotal, pWon)
	local function cleanDecimal(pNum) return math.floor(pNum * 100) / 100 end
	
	local percent = cleanDecimal(pVotes / pTotal * 100)
	local percentText = ''
	if percent ~= 0 then percentText = ' - '..percent..'% of all votes' end
	if pWon then pText = '<span style="color:brown;">☆</span> '..pText end

	local total_row = mw.html.create('div'):addClass('pollAnswer')
		:tag('div'):addClass('pollAnswerName'):wikitext(pText):done()
		:tag('div'):addClass('pollAnswerVotes')
			:tag('span'):wikitext(pVotes..percentText):done()
			:tag('div'):css({ ['width'] = percent..'%' }):done()
		:done()
	:done()
	return total_row
end

return p