Module:Service award progress

Documentation for this module may be created at Module:Service award progress/doc

local navbar = require('Module:Navbar')._navbar
local mm = require('Module:Math')
local p = {}

local function stripToNil(text)
	-- If text is a string, return its trimmed content, or nil if empty.
	-- Otherwise return text (which may, for example, be nil).
	if type(text) == 'string' then
		text = text:match('(%S.-)%s*$')
	end
	return text
end
local function progressBar(current, total)
	local percentDone = mm._round(current / total * 100, 1)
	local leftBar = ''
	local rightBar = ''
	if percentDone >= 100 then
		leftBar = 100
		rightBar = 0
	else
		leftBar = current / total * 100
		rightBar = (100-current) / total * 100
	end
	local progBarText = '<div style="width:75%; margin:auto; text-align:center;">' ..
			'<p><span style="font-size: 120%;"><b>' .. percentDone .. '%</b></span> completed</p>' ..
			'<p style="border:1px solid #c8ccd1; padding:1px; overflow:hidden;">' ..
			'<span style="width: ' .. leftBar .. '%; height: 2px; background:#00af32; float:left;">&nbsp;</span>' ..
			'<span style="width: ' .. rightBar .. '%; height: 2px; background:#eaecf0; float:left;">&nbsp;</span>' ..
			'</p></div>'
	
	return progBarText
end
local levelNames = {
	'[[Wikipedia:Service awards#Ultimate Editor|Ultimate Editor]]',
	'[[Wikipedia:Service awards#Grand Master Editor|Grand Master Editor]]',
	'[[Wikipedia:Service awards#Master Editor III|Master Editor III]]',
	'[[Wikipedia:Service awards#Master Editor II|Master Editor II]]',
	'[[Wikipedia:Service awards#Master Editor I|Master Editor I]]',
	'[[Wikipedia:Service awards#Senior Editor III|Senior Editor III]]',
	'[[Wikipedia:Service awards#Senior Editor II|Senior Editor II]]',
	'[[Wikipedia:Service awards#Senior Editor I|Senior Editor I]]',
	'[[Wikipedia:Service_awards#Experienced_Editor|Experienced Editor]]',
	'[[Wikipedia:Service_awards#Yeoman_Editor|Yeoman Editor]]',
	'[[Wikipedia:Service_awards#Journeyman_Editor|Journeyman Editor]]',
	'[[Wikipedia:Service_awards#Apprentice_Editor|Apprentice Editor]]',
	'[[Wikipedia:Service_awards#Novice_Editor|Novice Editor]]',
	'Wikipedian',
}
local levelTime = {
	2555, 1826.25, 1643.6, 1460, 1278.38, 1095, 913.1, 730.5, 547.8, 365.25, 182.5, 91.25, 30.4, 0,
}
local levelEdits = { 
	50000, 40000, 30000, 20000, 10000, 7500, 6250, 5000, 3750, 2500, 1200, 600, 200, 0,
}
function p.serviceLevel(args)
	--Inputs
	local editorTime = tonumber(stripToNil(args.editorTime))
	local edits = tonumber(stripToNil(args.edits))
	local styleoverride = stripToNil(args.styleoverride)
	local genderoverride = stripToNil(args.genderoverride)
	local url = stripToNil(args.url)
	
	-- Other Variables
	local timeLevel = '' -- Level associated with time
	local editLevel = '' -- Level associated with edits
	local userLevel = '' -- Full name of current user level
	local lowEdits = '' -- Minimum number of edits for current user level
	local lowTime = '' -- Minimum amount of time for current user level
	local highEdits = '' -- Max number of edits for current user level
	local highTime = '' -- Max amount of time for current user level
	local nextLevel = '' -- Full name of next user level
	local fullMessage = '<div style="' -- Output seed
	local topLevel = false
	local needsEdits = false
	local needsTime = false
	local needsBoth = false
	local displayall = false
	if stripToNil(args.displayall) == 'yes' then
		displayall = true
	end
	
	-- Find "level" of editor based on time
	for x = 1, 38, 1 do
		if editorTime >= levelTime[x] then
			timeLevel = x
			break
		end
	end
	-- Fine "level" of editor based on edits
	for y = 1, 38, 1 do
		if edits >= levelEdits[y] then
			editLevel = y
			break
		end
	end
	
	-- Set user level and the next level up
	if timeLevel == editLevel then
		if timeLevel == 1 then
			userLevel = levelNames[1]
			topLevel = true
		else
			userLevel = levelNames[editLevel]
			nextLevel = levelNames[editLevel-1]
			lowEdits = levelEdits[editLevel]
			highEdits = levelEdits[editLevel-1]
			lowTime = levelTime[editLevel]
			highTime = levelTime[editLevel-1]
			needsBoth = true
		end
	elseif timeLevel < editLevel then
		userLevel = levelNames[editLevel]
		nextLevel = levelNames[editLevel-1]
		lowEdits = levelEdits[editLevel]
		highEdits = levelEdits[editLevel-1]
		lowTime = levelTime[editLevel]
		highTime = levelTime[editLevel-1]
		needsEdits = true
	else
		userLevel = levelNames[timeLevel]
		nextLevel = levelNames[timeLevel-1]
		lowEdits = levelEdits[timeLevel]
		highEdits = levelEdits[timeLevel-1]
		lowTime = levelTime[timeLevel]
		highTime = levelTime[timeLevel-1]
		needsTime = true
	end
	
	-- Build notice
	-- First paragraph
	fullMessage = fullMessage .. styleoverride .. '">' ..
					navbar({"Template:Service award progress",style="float:right",mini=1}) ..
					'Currently, this editor has earned the <b>' .. userLevel .. '</b> service award.<br><br>'
	if topLevel then
		fullMessage = fullMessage .. '<div>This is the highest level achievable by an editor!</div>'
	else
		fullMessage = fullMessage .. '<div>To get to the next level, ' .. nextLevel .. ', ' .. genderoverride .. ' to meet the '
		if needsBoth then
			fullMessage = fullMessage .. '<b>editing</b> and the <b>time</b> requirement.</div>'
		elseif needsEdits then
			fullMessage = fullMessage .. '<b>editing</b> requirement.</div>'
		else
			fullMessage = fullMessage .. '<b>time</b> requirement.</div>'
		end
	
		-- Second paragra / progress bars
		if displayall or needsBoth or needsEdits then
			fullMessage = fullMessage .. 'Progress towards the next level (by edits): [&nbsp;' ..
							'<span class="plainlinks">[' .. url .. ' ' ..
							(edits - lowEdits) .. ']</span>&nbsp;/&nbsp;' ..
							(highEdits - lowEdits) .. '&nbsp;]'
			fullMessage = fullMessage .. progressBar(edits - lowEdits, highEdits - lowEdits)
		end
		if displayall or needsBoth or needsTime then
			fullMessage = fullMessage .. 'Progress towards the next level (by time): [&nbsp;' ..
							(editorTime - lowTime) .. '&nbsp;days&nbsp;/&nbsp;' ..
							(highTime - lowTime) .. '&nbsp;days&nbsp;]'
			fullMessage = fullMessage .. progressBar(editorTime - lowTime, highTime - lowTime)
		end
		fullMessage = fullMessage .. '</div>'
	end
	return fullMessage
end

function p.main(frame)
	return p.serviceLevel(frame.args)
end

return p