m (Fewfre moved page Template:Poll/results/Module:PreviousPoll to Module:PreviousPoll without leaving a redirect) |
No edit summary |
||
(8 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
local p = {} |
local p = {} |
||
+ | |||
⚫ | |||
+ | local paramtest = require('Module:Paramtest') |
||
+ | local default_to = paramtest.default_to |
||
+ | local has_content = paramtest.has_content |
||
+ | |||
function p.display_results( frame ) |
function p.display_results( frame ) |
||
local parameters = frame:getParent().args |
local parameters = frame:getParent().args |
||
− | + | ||
local title = parameters['title'] or 'TITLE' |
local title = parameters['title'] or 'TITLE' |
||
local startDate = parameters['start'] or 'START DATE' |
local startDate = parameters['start'] or 'START DATE' |
||
local endDate = parameters['end'] or 'END DATE' |
local endDate = parameters['end'] or 'END DATE' |
||
⚫ | |||
⚫ | |||
⚫ | |||
− | + | ||
⚫ | |||
+ | local MAX_ROWS = 20 |
||
− | + | ||
⚫ | |||
+ | local totalvotes = 0 |
||
⚫ | |||
+ | local topVote = 0 |
||
⚫ | |||
+ | for i=1,MAX_ROWS,1 do |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | totalvotes = totalvotes + votes |
||
⚫ | |||
+ | if(votes > topVote) then topVote = votes end |
||
⚫ | |||
+ | end |
||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end |
end |
||
+ | |||
− | |||
− | for i=1, |
+ | for i=1,MAX_ROWS,1 do |
local optionText = parameters['option'..i] |
local optionText = parameters['option'..i] |
||
⚫ | |||
⚫ | |||
− | local |
+ | local votes = tonumber(default_to(parameters['votes'..i], '0')) |
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
end |
end |
||
end |
end |
||
− | + | ||
⚫ | |||
+ | :tag('h3'):wikitext(title):css({ ["border-bottom"]="1px solid #B2AF9C", ["text-align"]="center" }):done() |
||
+ | :tag('div'):addClass('ajax-poll') |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
return tostring(table_string) |
return tostring(table_string) |
||
end |
end |
||
+ | |||
− | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | :done() |
||
⚫ | |||
+ | end |
||
+ | |||
+ | function p._empty(value) |
||
+ | return not (value == nil or value == "") |
||
+ | end |
||
+ | |||
return p |
return p |
Latest revision as of 03:24, 14 December 2014
Documentation for this module may be created at Module:PreviousPoll/doc
local p = {}
local paramtest = require('Module:Paramtest')
local default_to = paramtest.default_to
local has_content = paramtest.has_content
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 votesContainer = mw.html.create('div'):done()
local MAX_ROWS = 20
local totalvotes = 0
local topVote = 0
for i=1,MAX_ROWS,1 do
if has_content(parameters['option'..i]) then
local votes = tonumber(default_to(parameters['votes'..i], '0'))
totalvotes = totalvotes + votes
if(votes > topVote) then topVote = votes end
end
end
for i=1,MAX_ROWS,1 do
local optionText = parameters['option'..i]
if has_content(optionText) then
local votes = tonumber(default_to(parameters['votes'..i], '0'))
votesContainer:node(p._make_row(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(votesContainer)
: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._make_row(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
function p._empty(value)
return not (value == nil or value == "")
end
return p