Module:CEInsights

From Meta, a Wikimedia project coordination wiki
Module documentation
local p = {}

function p.audience(frame)
	-- Builds an audience-level view
	--
	-- Takes one parameter: string of the audience name to filter against
	--
	-- Returns wikitext
	
	audiencecontent = ''
	
	for _, audience in ipairs(frame.args) do
		-- In principle there should only be one argument but we still need to
		-- iterate like this.
		category_tables = {
			{name = 'Contact and Questions', content = {}},
			{name = 'Skills Development', content = {}},
			{name = 'Grants Support', content = {}},
			{name = 'Programs Support', content = {}},
			{name = 'Software Basics', content = {}},
			{name = 'Software Development', content = {}},
			{name = 'Technical Infrastructure', content = {}},
			{name = 'Global Reach Partnerships', content = {}},
			{name = 'Legal', content = {}},
			{name = 'Communications', content = {}},
		}
		
		-- `content` is a table of tables. Lua doesn't have arrays but if it did you
		-- could think of it like an array of tables. Or an array of objects. An
		-- array of associative arrays?
		--
		-- This first pass re-organizes the `content` table from a flat table to one
		-- divided into the different categories
		for _, entry in ipairs(content) do
			allow_this_one = false
			if audience == 'all' then
				allow_this_one = true
			else
				for __, entry_audience in ipairs(entry.audiences) do
					if entry_audience == audience then
						allow_this_one = true
					end
				end
			end

			if allow_this_one == true then
				entry_is_assigned = false
				for index, blob in ipairs(category_tables) do
					if blob.name == entry.category then
						entry_is_assigned = true
						table.insert(category_tables[index]['content'], entry)
						break
					end
				end
				if entry_is_assigned == false then
					table.insert(category_tables, {name = entry.category, content = {entry}})
				end
			end
		end

		for index, blob in ipairs(category_tables) do
			if #blob.content > 0 then
				msgname = 'category-' .. string.gsub(string.lower(blob.name), ' ', '-')
				if msg[msgname] ~= nil then
					relevant_msg = msg[msgname]
				else
					relevant_msg = 'error'
				end
				audiencecontent = audiencecontent .. '<h2 style="font-size:2em; text-align:' .. get_directionality() .. '">' .. relevant_msg .. '</h2>'
				audiencecontent = audiencecontent .. build_category(frame, blob.content, blob.name)
			end
		end
	end

	return audiencecontent
end

return p