Module:Reports

From Meta, a Wikimedia project coordination wiki
Jump to navigation Jump to search
Module documentation[view] [edit] [history] [purge]

Usage[edit]

Render organizations information[edit]

{{#invoke:Reports|render_org_infos_table}}

This loops through all the organizational information in the Lua table onto WAD Portal.

Render affiliate reports[edit]

{{#invoke:Reports|render_arp_table}}

This loops through all the affiliate reports information in the Lua table onto WAD Portal.

Render derecognized affiliates[edit]

{{#invoke:Reports|render_derecognized_affiliates}}

This loops through all the derecognized affiliates information in the Lua table onto WAD Portal.

Translation[edit]

The strings for this module are contained in Template:i18n/Reports.

-- Lua Scripts to display / render information on [[m:Reports_v2.0_test]] page.

local p = {}

-- To edit the list of ARs information, go to the modules below;
org_infos = require( 'Module:Organizational_Informations' )
activities_reports = require( 'Module:Activities_Reports' )
financial_reports = require( 'Module:Financial_Reports' )
grant_reports = require( 'Module:Grant_Reports' )

-- Month number to name Map
months = {
	'January',
	'February',
	'March',
	'April',
	'May',
	'June',
	'July',
	'August',
	'September',
	'October',
	'November',
	'December',
}

-- Short months
short_months = {
	'Jan',
	'Feb',
	'Mar',
	'Apr',
	'May',
	'Jun',
	'Jul',
	'Aug',
	'Sept',
	'Oct',
	'Nov',
	'Dec',
}

-- Utility and helper modules
is_rtl = require( 'Module:Is rtl' )
lang = mw.getCurrentFrame():preprocess('{{int:lang}}')
ModuleMsg = require( 'Module:ModuleMsg' )
msg = ModuleMsg:get_msgs('Template:I18n/Reports', lang)

function get_translation(entry)
	-- Get translated version of a list entry.
	--
	-- Usage:
	--   entry: table containing the entry to translate
	--
	-- Return table: translated entry (with English as fallback)
	
	if entry.unique_id == nil or lang == 'en' then
		-- This system assumes that a unique id is assigned.
		return entry
	end

	for k, v in pairs(entry) do
		if msg[k] ~= nil then
			entry[k] = msg[k]
		end
	end

	return entry
end

function get_directionality()
	-- Should something be left-aligned or right-aligned?
	if is_rtl[lang] == true then
		return 'right'
	end
	return 'left'
end

function is_current_report(ts)
	-- Check if the report year matches the current year
	--
	-- Usage:
	--   ts: time stamp of the report
	--
	-- Return boolean: If report is current or not
	
	c_year = os.date("%Y");
	ts_year = mw.text.split(ts, "-")[1]
		
	if ts_year == c_year then
		return true
	end
	
	return false
end

function iter(gr, fr, ar)
	-- Iterate over multiple Lua tables at the same time
	--
	-- Usage:
	--   gr: Grant report Lua table
	--   fr: Financial reports Lua table
	--   ar: Activities reports Lua table
	--
	-- Return table: Entries at index i of the tables
	
	local i = 0
	return function()
		i = i + 1
		return gr[i], fr[i], ar[i]
	end
end

function get_affiliate_latest_reports(org_info, grant_reports, financial_reports, activities_reports)
	-- Function to fetch the latest report for an affiliate in the
	-- different reports lua tables. In the case of multiple reports submitted
	-- for the same day, month or year, this function will use the timestamp
	-- to get the latest report.
	--
	-- Usage:
	--   org_info: organizational information used for creating the relationship
	--   grant_reports: grant reports Lua table.
	--   financial_reports: financial reports Lua table.
	--   activities_reports: activities reports Lua table.
	--
	-- Return table: entry for the latest grant report, financial_report, activities_report
	
	tmp_gr = {
		dos_stamp = '1800-01-01T00:00:00Z'
	}
	
	tmp_fr = {
		dos_stamp = '1800-01-01T00:00:00Z'
	}
	
	tmp_ar = {
		dos_stamp = '1800-01-01:T00:00:00Z'
	}
	
	for grant_report, financial_report, activities_report in iter(grant_reports, financial_reports, activities_reports) do
		if grant_report ~= nil and org_info.group_name == grant_report.group_name then
			if grant_report.dos_stamp > tmp_gr.dos_stamp then
				tmp_gr = grant_report	
			end
		end
		
		if financial_report ~= nil and org_info.group_name == financial_report.group_name then
			if financial_report.dos_stamp > tmp_fr.dos_stamp then
				tmp_fr = financial_report	
			end
		end
		
		if activities_report ~= nil and org_info.group_name == activities_report.group_name then
			if activities_report.dos_stamp > tmp_ar.dos_stamp then
				tmp_ar = activities_report	
			end
		end
	end
	
	return tmp_gr, tmp_fr, tmp_ar
end

function build_arp_template(frame, org_info, grant_report, financial_report, activities_report)
	-- Builds entries for the ARP template.
	--
	-- Usage:
	--   frame: The frame object
	--   org_info: Organization information
	--   grant_report: Grant report for that organization for the current year
	--   financial_report: Financial report for that organization in the current year
	--   activities_report: Activities report for that organization in the current year
	--
	-- Return string: wikitext
	
	org_info = get_translation(org_info)
	template_args = {}
	
	if org_info.affiliate_code ~= nil then
		template_args.affiliate_code = org_info.affiliate_code	
	end
	
	if org_info.group_name ~= nil then
		template_args.affiliate_name = '[[' .. org_info.group_name .. ']]'
	end
	
	if grant_report.report_link ~= nil then
		template_args.grant_report = '[' .. grant_report.report_link .. ' ' .. grant_report.report_type .. ']'
		if is_current_report(grant_report.dos_stamp) then
			template_args.uptodate_reporting = frame:expandTemplate{title = 'Tick'}
		else
			template_args.uptodate_reporting = frame:expandTemplate{title = 'Cross'}
		end
	end
	
	if financial_report.report_link ~= nil then
		year = mw.text.split(financial_report.dos_stamp, "-")[1]
		template_args.financial_report = '[' .. financial_report.report_link .. ' ' .. year .. ']'
	end
	
	if activities_report.report_link ~= nil then
		year = mw.text.split(activities_report.dos_stamp, "-")[1]
		month_number = mw.text.split(activities_report.end_date, "/")[2]
		month_number = tonumber(month_number)
		if activities_report.report_type == 'Annual Activities Report' then
			template_args.activities_report = '[' .. activities_report.report_link .. ' ' .. year .. ']'
			template_args.fiscal_year = short_months[month_number] .. ' - ' .. short_months[month_number-1]
		else
			template_args.activities_report = '[' .. activities_report.report_link .. ' ' .. months[month_number] .. '-' .. year .. ']'
			template_args.fiscal_year = 'Monthly reporting'
		end
	end
	
	if org_info.legal_entity ~= nil then
		if ( is_current_report(financial_report.dos_stamp) and is_current_report(activities_report.dos_stamp) ) then
			template_args.uptodate_reporting = frame:expandTemplate{title = 'Tick'}
		elseif org_info.legal_entity == 'No' then
			if is_current_report(activities_report.dos_stamp) then
				template_args.uptodate_reporting = frame:expandTemplate{title = 'Tick'}
			end
		else
			template_args.uptodate_reporting = frame:expandTemplate{title = 'Cross'}
		end
	end
	
	if org_info.notes_on_reporting == '' then
		template_args.notes_on_reporting = 'Awaiting M&E staff\'s remark...'
	else
		template_args.notes_on_reporting = "'''''" .. org_info.notes_on_reporting .. "'''''"
	end
	
	if org_info.uptodate_reporting ~= nil then
		template_args.uptodate_reporting = frame:expandTemplate{title = org_info.uptodate_reporting}
		template_args.notes_on_reporting = 'Report(s) consists of drafts/incomplete. Please recheck...'
	end
	
	affiliate_record = frame:expandTemplate{
		title = 'Reports record s1',
		args = template_args
	}
	
	return affiliate_record
end

function build_org_infos_template(frame, entry)
	-- Builds an entries for the organizational information template.
	--
	-- Usage:
	--   frame: The frame object
	--   entry: An org infos entry with relevant data
	--
	-- Return string: wikitext
	
	entry = get_translation(entry)
	social_media = ''
	
	template_args = {
		type = entry.org_type,
	}
	
	if entry.unique_id ~= nil then
		template_args.unique_id = entry.unique_id	
	end
	
	if entry.affiliate_code ~= nil then
		template_args.affiliate_code = entry.affiliate_code	
	end
	
	if entry.group_name ~= nil then
		template_args.name = '[[' .. entry.group_name .. ']]'
	end
	
	if entry.other ~= nil then
		template_args.blog_or_news = '[' .. entry.other	.. ' ' .. entry.affiliate_code .. '\'s news or blog]'
	end
	
	if entry.agreement_date ~= nil then
		template_args.agreement_date = entry.agreement_date
	end
	
	if entry.twitter ~= nil then
		social_media = social_media .. '[[File:Twitter_Logo.png|26px|link=' .. entry.twitter .. ']]'
	end
	
	if entry.facebook ~= nil then
		social_media = social_media .. '   [[File:Facebook_logo.png|26px|link=' .. entry.facebook .. ']]'
	end
	
	template_args.social_media = social_media

	entrycontent = frame:expandTemplate{
		title = 'Reports record s2',
		args = template_args
	}
	
	return entrycontent
end

function build_derecog_template(frame, entry)
	-- Builds an entries for derecognized affiliates template.
	--
	-- Usage:
	--   frame: The frame object
	--   entry: An org infos entry with relevant data
	--
	-- Return string: wikitext
	
	entry = get_translation(entry)

	template_args = {
		type = entry.org_type,
	}
	
	if entry.affiliate_code ~= nil then
		template_args.affiliate_code = entry.affiliate_code	
	end
	
	if entry.group_name ~= nil then
		template_args.name = '[[' .. entry.group_name .. ']]'
	end
	
	for _, activities_report in ipairs(activities_reports) do
		if ( entry.group_name == activities_report.group_name ) then
			-- Let's keep this logic if we intend to show other reports in the future
			latest_gr, latest_fr, latest_ar = get_affiliate_latest_reports(
				entry, grant_reports, financial_reports, activities_reports
			)
			year = mw.text.split(latest_ar.dos_stamp, "-")[1]
			if ( latest_ar ~= nil ) then
				template_args.activities_report =  '[' .. latest_ar.report_link .. ' ' .. year .. ']'
			end
			break
		end
	end
	
	if entry.agreement_date ~= nil then
		template_args.agreement_date = entry.agreement_date
	end
	
	if entry.derecognition_date ~= nil then
		template_args.derecognition_date = entry.derecognition_date
	end

	entrycontent = frame:expandTemplate{
		title = 'Reports derecognized affiliates',
		args = template_args
	}
	
	return entrycontent
end

function p.render_arp_table(frame)
	-- Function to render the AR (Affiliates Report) table
	-- using the 'Reports record s2' template.
	--
	-- Usage:
	--   frame: The frame object
	--
	-- Return string: wikitext
	
	reports = ''

	for _, org_info in ipairs(org_infos) do
		-- TODO: Decide whether to hide the reports for derecognized groups as well.
		for grant_report, financial_report, activities_report in iter(grant_reports, financial_reports, activities_reports) do
			if grant_report == nil then
				grant_report = {}
			end
			
			if financial_report == nil then
				financial_report = {}
			end
			
			if activities_report == nil then
				activities_report = {}	
			end
			
			if (
				org_info.group_name == grant_report.group_name
				or org_info.group_name == financial_report.group_name
				or org_info.group_name == activities_report.group_name
			) then
				latest_gr, latest_fr, latest_ar = get_affiliate_latest_reports(
					org_info, grant_reports, financial_reports, activities_reports
				)
				reports = reports .. "\n" .. build_arp_template(frame, org_info, latest_gr, latest_fr, latest_ar)
				break
			end
		end
	end
	
	return reports
end

function p.render_org_infos_table(frame)
	-- Function for rendering or displaying organizational
	-- information table using the 'Reports record s2' template.
	-- 
	-- Usage:
	--   frame: The frame object
	--
	-- Return string: wikitext
	
	orgInfoTable = ''
	
	for _, org_info in ipairs(org_infos) do
		if org_info.status == 'derecognized' or org_info.status == 'derecognised' then
			-- Ignore!
		else
			orgInfoTable = orgInfoTable .. "\n" .. build_org_infos_template(frame, org_info)
		end
	end
	
	return orgInfoTable
end

function p.render_derecognized_affiliates(frame)
	-- Function for rendering or displaying derecognized affiliates.
	-- 
	-- Usage:
	--   frame: The frame object
	--
	-- Return string: wikitext
	
	derecogTable = ''
	aa_report = ''
	
	for _, org_info in ipairs(org_infos) do
		if org_info.status == 'derecognized' or org_info.status == 'derecognised' then
			derecogTable = derecogTable .. "\n" .. build_derecog_template(frame, org_info)
		end
	end
	
	return derecogTable
end

return p